Share connection pools for non-default shards

Unless `legacy_connection_handling` was set to `false`, only the default
shard's connection pools were shared during transactional tests.
This commit is contained in:
Eugene Kenny 2020-10-29 22:52:23 +00:00
parent 03d5fad310
commit 348d6c624c
3 changed files with 41 additions and 7 deletions

View File

@ -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

View File

@ -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

View File

@ -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