diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2016-07-17 04:19:23 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-17 04:19:23 -0300 |
commit | 9fcfb86c42d116cd4145b702dd5c73ce1493b5b9 (patch) | |
tree | 6589938e9c2ce9ed901947b3cfb1d1453a2843b8 /activerecord/lib/active_record | |
parent | bad3a120f1690f393d8f6204b3ceee60f0ce707b (diff) | |
parent | 31a8588a01cf5aadb864f609efad6ee24b5d4055 (diff) | |
download | rails-9fcfb86c42d116cd4145b702dd5c73ce1493b5b9.tar.gz rails-9fcfb86c42d116cd4145b702dd5c73ce1493b5b9.tar.bz2 rails-9fcfb86c42d116cd4145b702dd5c73ce1493b5b9.zip |
Merge pull request #20818 from jeremywadsack/use_transactional_fixtures_all_databases
Use notification to ensure that lazy-loaded model classes have transactions
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb | 15 | ||||
-rw-r--r-- | activerecord/lib/active_record/fixtures.rb | 28 |
2 files changed, 43 insertions, 0 deletions
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 diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index 51bf12d0bf..6d142285cd 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -968,6 +968,7 @@ module ActiveRecord @fixture_cache = {} @fixture_connections = [] @@already_loaded_fixtures ||= {} + @connection_subscriber = nil # Load fixtures once and begin transaction. if run_in_transaction? @@ -977,10 +978,36 @@ module ActiveRecord @loaded_fixtures = load_fixtures(config) @@already_loaded_fixtures[self.class] = @loaded_fixtures end + + # Begin transactions for connections already established @fixture_connections = enlist_fixture_connections @fixture_connections.each do |connection| connection.begin_transaction joinable: false end + + # When connections are established in the future, begin a transaction too + @connection_subscriber = ActiveSupport::Notifications.subscribe('!connection.active_record') do |_, _, _, _, payload| + model_class = nil + begin + model_class = payload[:class_name].constantize if payload[:class_name] + rescue NameError + model_class = nil + end + + if model_class + begin + connection = ActiveRecord::Base.connection_handler.retrieve_connection(model_class) + rescue ConnectionNotEstablished + connection = nil + end + + if connection && !@fixture_connections.include?(connection) + connection.begin_transaction joinable: false + @fixture_connections << connection + end + end + end + # Load fixtures for every test. else ActiveRecord::FixtureSet.reset_cache @@ -995,6 +1022,7 @@ module ActiveRecord def teardown_fixtures # Rollback changes if a transaction is active. if run_in_transaction? + ActiveSupport::Notifications.unsubscribe(@connection_subscriber) if @connection_subscriber @fixture_connections.each do |connection| connection.rollback_transaction if connection.transaction_open? end |