From 31a8588a01cf5aadb864f609efad6ee24b5d4055 Mon Sep 17 00:00:00 2001 From: Jeremy Wadsack Date: Sun, 28 Jun 2015 15:11:34 -0700 Subject: Create connection.active_record notification and use that to ensure that lazy- loaded model classes have their connections wrapped in transactions. See #17776 In Rails 4 config.eager_load was changed to false in the test environment. This means that model classes that connect to alternate databases with establish_connection are not loaded at start up. If use_transactional_fixtures is enabled, transactions are wrapped around the connections that have been established only at the start of the test suite. So model classes loaded later don't have transactions causing data created in the alternate database not to be removed. This change resolves that by creating a new connection.active_record notification that gets fired whenever a connection is established. I then added a subscriber after we set up transactions in the test environment to listen for additional connections and wrap those in transactions as well. --- .../connection_adapters/abstract/connection_pool.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb index 9b74c3a10f..1d9579f1a8 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -849,6 +849,21 @@ module ActiveRecord remove_connection(spec.name) owner_to_pool[spec.name] = ConnectionAdapters::ConnectionPool.new(spec) + + message_bus = ActiveSupport::Notifications.instrumenter + payload = { + connection_id: object_id + } + if spec + payload[:class_name] = spec.name + payload[:config] = spec.config + end + + message_bus.instrument('!connection.active_record', payload) do + owner_to_pool[spec.name] = ConnectionAdapters::ConnectionPool.new(spec) + end + + owner_to_pool[spec.name] end # Returns true if there are any active connections among the connection -- cgit v1.2.3