7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/switchman/active_record/test_fixtures.rb', line 7
def setup_fixtures(config = ::ActiveRecord::Base)
super
return unless run_in_transaction?
::ActiveSupport::Notifications.unsubscribe(@connection_subscriber)
@connection_subscriber =
::ActiveSupport::Notifications.subscribe("!connection.active_record") do |_, _, _, _, payload|
spec_name = if ::Rails.version < "7.1"
payload[:spec_name] if payload.key?(:spec_name)
elsif payload.key?(:connection_name)
payload[:connection_name]
end
shard = payload[:shard] if payload.key?(:shard)
if spec_name && !FORBIDDEN_DB_ENVS.include?(shard)
begin
connection = ::ActiveRecord::Base.connection_handler.retrieve_connection(spec_name, shard: shard)
connection.connect! if ::Rails.version >= "7.1" rescue ::ActiveRecord::ConnectionNotEstablished, ::ActiveRecord::NoDatabaseError
connection = nil
end
if connection
setup_shared_connection_pool
unless @fixture_connections.include?(connection)
connection.begin_transaction joinable: false, _lazy: false
connection.pool.lock_thread = true if lock_threads
@fixture_connections << connection
end
end
end
end
end
|