Merge pull request #42579 from eileencodes/fix-nil-pool_config-in-legacy-handling

Fix nil pool_config in legacy connection handling
This commit is contained in:
Eileen M. Uchitelle 2021-06-23 10:53:02 -04:00 committed by GitHub
commit 97b494fefb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 2 deletions

@ -23,8 +23,12 @@ def get_pool_config(_, shard)
@name_to_pool_config[shard]
end
def set_pool_config(_, shard, pool_config)
@name_to_pool_config[shard] = pool_config
def set_pool_config(role, shard, pool_config)
if pool_config
@name_to_pool_config[shard] = pool_config
else
raise ArgumentError, "The `pool_config` for the :#{role} role and :#{shard} shard was `nil`. Please check your configuration. If you want your writing role to be something other than `:writing` set `config.active_record.writing_role` in your application configuration. The same setting should be applied for the `reading_role` if applicable."
end
end
end
end

@ -99,6 +99,40 @@ def test_loading_relations_with_multi_db_connection_handlers
end
unless in_memory_db?
def test_not_setting_writing_role_while_using_another_named_role_raises
old_handler = ActiveRecord::Base.connection_handler
assert_deprecated do
ActiveRecord::Base.connection_handlers = { writing: ConnectionHandler.new }
end
ActiveRecord::Base.connection_handler = ActiveRecord::Base.connection_handlers[:writing]
ActiveRecord::Base.establish_connection :arunit
ActiveRecord::Base.connects_to(shards: { default: { all: :arunit }, one: { all: :arunit } })
assert_raises(ArgumentError) { setup_shared_connection_pool }
ensure
ActiveRecord::Base.connection_handler = old_handler
ActiveRecord::Base.establish_connection :arunit
end
def test_setting_writing_role_while_using_another_named_role_does_not_raise
old_role, ActiveRecord.writing_role = ActiveRecord.writing_role, :all
old_handler = ActiveRecord::Base.connection_handler
assert_deprecated do
ActiveRecord::Base.connection_handlers = { all: ConnectionHandler.new }
end
ActiveRecord::Base.connection_handler = ActiveRecord::Base.connection_handlers[:all]
ActiveRecord::Base.establish_connection :arunit
ActiveRecord::Base.connects_to(shards: { default: { all: :arunit }, one: { all: :arunit } })
assert_nothing_raised { setup_shared_connection_pool }
ensure
ActiveRecord.writing_role = old_role
ActiveRecord::Base.connection_handler = old_handler
ActiveRecord::Base.establish_connection :arunit
end
def test_establish_connection_using_3_levels_config
previous_env, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], "default_env"