diff options
Diffstat (limited to 'spec/support/database/multiple_databases.rb')
-rw-r--r-- | spec/support/database/multiple_databases.rb | 37 |
1 files changed, 26 insertions, 11 deletions
diff --git a/spec/support/database/multiple_databases.rb b/spec/support/database/multiple_databases.rb index 9e72ea589e3..94857b47127 100644 --- a/spec/support/database/multiple_databases.rb +++ b/spec/support/database/multiple_databases.rb @@ -6,6 +6,10 @@ module Database skip 'Skipping because multiple databases not set up' unless Gitlab::Database.has_config?(:ci) 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) db_config = (config_model || model).connection_db_config @@ -46,6 +50,26 @@ module Database new_handler&.clear_all_connections! end # 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 module ActiveRecordBaseEstablishConnection @@ -69,18 +93,9 @@ RSpec.configure do |config| end end - config.around(:each, :mocked_ci_connection) do |example| - with_reestablished_active_record_base(reconnect: true) do - reconfigure_db_connection( - name: :ci, - model: Ci::ApplicationRecord, - config_model: ActiveRecord::Base - ) - + config.around(:each, :add_ci_connection) do |example| + with_added_ci_connection do example.run - - # Cleanup connection_specification_name for Ci::ApplicationRecord - Ci::ApplicationRecord.remove_connection end end end |