Commit 67591afb authored by Thong Kuah's avatar Thong Kuah Committed by Kamil Trzciński

Add feature flag

parent 4b3ddbd5
......@@ -9,15 +9,27 @@ module CrossDatabaseModification
class_methods do
def transaction(**options, &block)
super(**options) do
if connection.current_transaction.respond_to?(:add_gitlab_schema) && gitlab_schema
connection.current_transaction.add_gitlab_schema(gitlab_schema)
end
if track_gitlab_schema_in_current_transaction?
super(**options) do
if connection.current_transaction.respond_to?(:add_gitlab_schema) && gitlab_schema
connection.current_transaction.add_gitlab_schema(gitlab_schema)
end
yield
yield
end
else
super(**options, &block)
end
end
def track_gitlab_schema_in_current_transaction?
return false unless Feature::FlipperFeature.table_exists?
Feature.enabled?(:track_gitlab_schema_in_current_transaction, default_enabled: :yaml)
rescue ActiveRecord::NoDatabaseError, PG::ConnectionBad
false
end
def gitlab_schema
case self.name
when 'ActiveRecord::Base', 'ApplicationRecord'
......
---
name: track_gitlab_schema_in_current_transaction
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/76717
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/349944
milestone: '14.7'
type: development
group: group::sharding
default_enabled: false
......@@ -4,6 +4,56 @@ require 'spec_helper'
RSpec.describe CrossDatabaseModification do
describe '.transaction' do
def assert_no_gitlab_schema_comment(log)
include_arg = [/gitlab_schema/] * log.size
expect(log).not_to include(*include_arg)
end
context 'feature flag disabled' do
before do
stub_feature_flags(track_gitlab_schema_in_current_transaction: false)
end
it 'does not add gitlab_schema comment' do
recorder = ActiveRecord::QueryRecorder.new do
ApplicationRecord.transaction do
Project.first
end
end
expect(recorder.log).to include(
/SAVEPOINT/,
/SELECT.*FROM "projects"/,
/RELEASE SAVEPOINT/
)
assert_no_gitlab_schema_comment(recorder.log)
end
end
context 'feature flag is not yet setup' do
before do
allow(Feature::FlipperFeature).to receive(:table_exists?).and_raise(ActiveRecord::NoDatabaseError)
end
it 'does not add gitlab_schema comment' do
recorder = ActiveRecord::QueryRecorder.new do
ApplicationRecord.transaction do
Project.first
end
end
expect(recorder.log).to include(
/SAVEPOINT/,
/SELECT.*FROM "projects"/,
/RELEASE SAVEPOINT/
)
assert_no_gitlab_schema_comment(recorder.log)
end
end
it 'adds gitlab_schema to the current transaction', :aggregate_failures do
recorder = ActiveRecord::QueryRecorder.new do
ApplicationRecord.transaction do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment