Commit 714f274c authored by Dylan Griffith's avatar Dylan Griffith

Merge branch 'pb-fixed-mocked-ci-connection-2' into 'master'

Only mock ci connections in single db mode

See merge request gitlab-org/gitlab!74846
parents bbf25373 de7337f7
...@@ -487,25 +487,9 @@ RSpec.describe Gitlab::Database::LoadBalancing::LoadBalancer, :request_store do ...@@ -487,25 +487,9 @@ RSpec.describe Gitlab::Database::LoadBalancing::LoadBalancer, :request_store do
end end
end end
describe 'primary connection re-use', :reestablished_active_record_base do describe 'primary connection re-use', :reestablished_active_record_base, :add_ci_connection do
let(:model) { Ci::ApplicationRecord } let(:model) { Ci::ApplicationRecord }
around do |example|
if Gitlab::Database.has_config?(:ci)
example.run
else
# fake additional Database
model.establish_connection(
ActiveRecord::DatabaseConfigurations::HashConfig.new(Rails.env, 'ci', ActiveRecord::Base.connection_db_config.configuration_hash)
)
example.run
# Cleanup connection_specification_name for Ci::ApplicationRecord
model.remove_connection
end
end
describe '#read' do describe '#read' do
it 'returns ci replica connection' do it 'returns ci replica connection' do
expect { |b| lb.read(&b) }.to yield_with_args do |args| expect { |b| lb.read(&b) }.to yield_with_args do |args|
......
...@@ -17,7 +17,7 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasMetrics, query_ana ...@@ -17,7 +17,7 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasMetrics, query_ana
process_sql(ActiveRecord::Base, "SELECT 1 FROM projects") process_sql(ActiveRecord::Base, "SELECT 1 FROM projects")
end end
context 'properly observes all queries', :mocked_ci_connection do context 'properly observes all queries', :add_ci_connection do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
where do where do
......
...@@ -6,6 +6,10 @@ module Database ...@@ -6,6 +6,10 @@ module Database
skip 'Skipping because multiple databases not set up' unless Gitlab::Database.has_config?(:ci) skip 'Skipping because multiple databases not set up' unless Gitlab::Database.has_config?(:ci)
end end
def skip_if_multiple_databases_are_setup
skip 'Skipping because multiple databases are set up' if Gitlab::Database.has_config?(:ci)
end
def reconfigure_db_connection(name: nil, config_hash: {}, model: ActiveRecord::Base, config_model: nil) def reconfigure_db_connection(name: nil, config_hash: {}, model: ActiveRecord::Base, config_model: nil)
db_config = (config_model || model).connection_db_config db_config = (config_model || model).connection_db_config
...@@ -46,6 +50,26 @@ module Database ...@@ -46,6 +50,26 @@ module Database
new_handler&.clear_all_connections! new_handler&.clear_all_connections!
end end
# rubocop:enable Database/MultipleDatabases # rubocop:enable Database/MultipleDatabases
def with_added_ci_connection
if Gitlab::Database.has_config?(:ci)
# No need to add a ci: connection if we already have one
yield
else
with_reestablished_active_record_base(reconnect: true) do
reconfigure_db_connection(
name: :ci,
model: Ci::ApplicationRecord,
config_model: ActiveRecord::Base
)
yield
# Cleanup connection_specification_name for Ci::ApplicationRecord
Ci::ApplicationRecord.remove_connection
end
end
end
end end
module ActiveRecordBaseEstablishConnection module ActiveRecordBaseEstablishConnection
...@@ -69,18 +93,9 @@ RSpec.configure do |config| ...@@ -69,18 +93,9 @@ RSpec.configure do |config|
end end
end end
config.around(:each, :mocked_ci_connection) do |example| config.around(:each, :add_ci_connection) do |example|
with_reestablished_active_record_base(reconnect: true) do with_added_ci_connection do
reconfigure_db_connection(
name: :ci,
model: Ci::ApplicationRecord,
config_model: ActiveRecord::Base
)
example.run example.run
# Cleanup connection_specification_name for Ci::ApplicationRecord
Ci::ApplicationRecord.remove_connection
end end
end end
end end
......
...@@ -56,4 +56,43 @@ RSpec.describe 'Database::MultipleDatabases' do ...@@ -56,4 +56,43 @@ RSpec.describe 'Database::MultipleDatabases' do
end end
end end
end end
describe '.with_added_ci_connection' do
context 'when only a single database is setup' do
before do
skip_if_multiple_databases_are_setup
end
it 'connects Ci::ApplicationRecord to the main database for the duration of the block', :aggregate_failures do
main_database = current_database(ActiveRecord::Base)
original_database = current_database(Ci::ApplicationRecord)
with_added_ci_connection do
expect(current_database(Ci::ApplicationRecord)).to eq(main_database)
end
expect(current_database(Ci::ApplicationRecord)).to eq(original_database)
end
end
context 'when multiple databases are setup' do
before do
skip_if_multiple_databases_not_setup
end
it 'does not mock the original Ci::ApplicationRecord connection', :aggregate_failures do
original_database = current_database(Ci::ApplicationRecord)
with_added_ci_connection do
expect(current_database(Ci::ApplicationRecord)).to eq(original_database)
end
expect(current_database(Ci::ApplicationRecord)).to eq(original_database)
end
end
def current_database(connection_class)
connection_class.retrieve_connection.execute('select current_database()').first
end
end
end end
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