未验证 提交 ba2f38e4 编写于 作者: E eileencodes

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: NJohn Crepezzi <john.crepezzi@gmail.com>
上级 c93f3f02
......@@ -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"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册