diff --git a/activerecord/lib/active_record/connection_adapters/legacy_pool_manager.rb b/activerecord/lib/active_record/connection_adapters/legacy_pool_manager.rb index d5c6a20ccc..eaa879e4a7 100644 --- a/activerecord/lib/active_record/connection_adapters/legacy_pool_manager.rb +++ b/activerecord/lib/active_record/connection_adapters/legacy_pool_manager.rb @@ -7,6 +7,10 @@ module ActiveRecord @name_to_pool_config = {} end + def shard_names + @name_to_pool_config.keys + end + def pool_configs(_ = nil) @name_to_pool_config.values end diff --git a/activerecord/lib/active_record/test_fixtures.rb b/activerecord/lib/active_record/test_fixtures.rb index 98bc2584a8..b987dc0ca6 100644 --- a/activerecord/lib/active_record/test_fixtures.rb +++ b/activerecord/lib/active_record/test_fixtures.rb @@ -199,10 +199,11 @@ module ActiveRecord writing_pool_manager = writing_handler.send(:owner_to_pool_manager)[name] return unless writing_pool_manager - writing_pool_config = writing_pool_manager.get_pool_config(nil, :default) - pool_manager = handler.send(:owner_to_pool_manager)[name] - pool_manager.set_pool_config(nil, :default, writing_pool_config) + pool_manager.shard_names.each do |shard_name| + writing_pool_config = writing_pool_manager.get_pool_config(nil, shard_name) + pool_manager.set_pool_config(nil, shard_name, writing_pool_config) + end end end end diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index 19ce2b4273..74dc29ed70 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -1447,8 +1447,22 @@ if current_adapter?(:SQLite3Adapter) && !in_memory_db? end def test_writing_and_reading_connections_are_the_same - rw_conn = ActiveRecord::Base.connection_handler.connection_pool_list(:writing).first.connection - ro_conn = ActiveRecord::Base.connection_handler.connection_pool_list(:reading).first.connection + handler = ActiveRecord::Base.connection_handler + rw_conn = handler.retrieve_connection_pool("ActiveRecord::Base", role: :writing).connection + ro_conn = handler.retrieve_connection_pool("ActiveRecord::Base", role: :reading).connection + + assert_equal rw_conn, ro_conn + end + + def test_writing_and_reading_connections_are_the_same_for_non_default_shards + ActiveRecord::Base.connects_to shards: { + default: { writing: :default, reading: :readonly }, + two: { writing: :default, reading: :readonly } + } + + handler = ActiveRecord::Base.connection_handler + rw_conn = handler.retrieve_connection_pool("ActiveRecord::Base", role: :writing, shard: :two).connection + ro_conn = handler.retrieve_connection_pool("ActiveRecord::Base", role: :reading, shard: :two).connection assert_equal rw_conn, ro_conn end @@ -1525,8 +1539,23 @@ if current_adapter?(:SQLite3Adapter) && !in_memory_db? writing = ActiveRecord::Base.connection_handlers[:writing] reading = ActiveRecord::Base.connection_handlers[:reading] - rw_conn = writing.connection_pool_list.first.connection - ro_conn = reading.connection_pool_list.first.connection + rw_conn = writing.retrieve_connection_pool("ActiveRecord::Base").connection + ro_conn = reading.retrieve_connection_pool("ActiveRecord::Base").connection + + assert_equal rw_conn, ro_conn + end + + def test_writing_and_reading_connections_are_the_same_for_non_default_shards_with_legacy_handling + ActiveRecord::Base.connects_to shards: { + default: { writing: :default, reading: :readonly }, + two: { writing: :default, reading: :readonly } + } + + writing = ActiveRecord::Base.connection_handlers[:writing] + reading = ActiveRecord::Base.connection_handlers[:reading] + + rw_conn = writing.retrieve_connection_pool("ActiveRecord::Base", shard: :two).connection + ro_conn = reading.retrieve_connection_pool("ActiveRecord::Base", shard: :two).connection assert_equal rw_conn, ro_conn end