Fix incorrect removal of current_shard in establish_connection

If we enter a `connected_to` block and call `establish_connection` like
the test added here we need to ensure that `shard: current_shard` is passed
to the handler, otherwise the connection will be established on
`default` not on `shard_one`.

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
This commit is contained in:
eileencodes 2020-08-14 11:05:05 -04:00
parent c93f3f0226
commit ba2f38e498
No known key found for this signature in database
GPG Key ID: BA5C575120BBE8DF
2 changed files with 11 additions and 1 deletions

@ -49,7 +49,7 @@ module ConnectionHandling
def establish_connection(config_or_env = nil)
config_or_env ||= DEFAULT_ENV.call.to_sym
db_config, owner_name = resolve_config_for_connection(config_or_env)
connection_handler.establish_connection(db_config, owner_name: owner_name)
connection_handler.establish_connection(db_config, owner_name: owner_name, shard: current_shard)
end
# Connects a model to the databases specified. The +database+ keyword

@ -25,6 +25,16 @@ def teardown
end
unless in_memory_db?
def test_establishing_a_connection_in_connected_to_block_uses_current_role_and_shard
ActiveRecord::Base.connected_to(shard: :shard_one) do
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
ActiveRecord::Base.establish_connection(db_config)
assert_nothing_raised { Person.first }
assert_equal [:default, :shard_one], ActiveRecord::Base.connection_handlers[:writing].send(:owner_to_pool_manager).fetch("ActiveRecord::Base").instance_variable_get(:@name_to_pool_config).keys
end
end
def test_establish_connection_using_3_levels_config
previous_env, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], "default_env"