Commit 2625e7a4 authored by Kamil Trzciński's avatar Kamil Trzciński

Make `DbCleaner` and `BeforeAllAdapter` to support many connections

This makes these classes to look at a top-level connections
classes and always initialise them.

Changelog: fixed
parent 47e8eade
# frozen_string_literal: true
module EE
class BeforeAllAdapter
def self.begin_transaction
TestProf::BeforeAll::Adapters::ActiveRecord.begin_transaction
::Geo::BaseRegistry.connection.begin_transaction
end
def self.rollback_transaction
TestProf::BeforeAll::Adapters::ActiveRecord.rollback_transaction
::Geo::BaseRegistry.connection.rollback_transaction
end
end
end
......@@ -8,15 +8,5 @@ module EE
def deletion_except_tables
super << 'licenses'
end
override :setup_database_cleaner
def setup_database_cleaner
if ::Gitlab::Geo.geo_database_configured?
::DatabaseCleaner[:active_record, { connection: ::Geo::BaseRegistry }]
TestProf::BeforeAll.adapter = ::EE::BeforeAllAdapter
end
super
end
end
end
# frozen_string_literal: true
class BeforeAllAdapter # rubocop:disable Gitlab/NamespacedClass
def self.all_connection_pools
::ActiveRecord::Base.connection_handler.all_connection_pools
end
def self.begin_transaction
self.all_connection_pools.each do |connection_pool|
connection_pool.connection.begin_transaction(joinable: false)
end
end
def self.rollback_transaction
self.all_connection_pools.each do |connection_pool|
if connection_pool.connection.open_transactions.zero?
warn "!!! before_all transaction has been already rollbacked and " \
"could work incorrectly"
next
end
connection_pool.connection.rollback_transaction
end
end
end
TestProf::BeforeAll.adapter = ::BeforeAllAdapter
# frozen_string_literal: true
module DbCleaner
def all_connection_classes
::ActiveRecord::Base.connection_handler.connection_pool_names.map(&:constantize)
end
def delete_from_all_tables!(except: [])
except << 'ar_internal_metadata'
......@@ -12,7 +16,9 @@ module DbCleaner
end
def setup_database_cleaner
DatabaseCleaner[:active_record, { connection: ActiveRecord::Base }]
all_connection_classes.each do |connection_class|
DatabaseCleaner[:active_record, { connection: connection_class }]
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