1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Remove database kwarg from connected_to

The `database` kwarg was deprecated in #37874. It shouldn't be used by
apps even before the deprecation because it's kind of dangerous to use
in a request. Also it makes the `connected_to` method really ugly.

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
This commit is contained in:
eileencodes 2020-11-03 16:22:08 -05:00
parent 4640cff2b2
commit ce629d9ff3
No known key found for this signature in database
GPG key ID: BA5C575120BBE8DF
3 changed files with 8 additions and 60 deletions

View file

@ -132,9 +132,7 @@ module ActiveRecord
# ActiveRecord::Base.connected_to(role: :reading, shard: :shard_one_replica) do # ActiveRecord::Base.connected_to(role: :reading, shard: :shard_one_replica) do
# Dog.first # finds first Dog record stored on the shard one replica # Dog.first # finds first Dog record stored on the shard one replica
# end # end
# def connected_to(role: nil, shard: nil, prevent_writes: false, &blk)
# The database kwarg is deprecated and will be removed in 6.2.0 without replacement.
def connected_to(database: nil, role: nil, shard: nil, prevent_writes: false, &blk)
if legacy_connection_handling if legacy_connection_handling
if self != Base if self != Base
raise NotImplementedError, "`connected_to` can only be called on ActiveRecord::Base with legacy connection handling." raise NotImplementedError, "`connected_to` can only be called on ActiveRecord::Base with legacy connection handling."
@ -145,31 +143,15 @@ module ActiveRecord
end end
end end
if database && (role || shard) unless role || shard
raise ArgumentError, "`connected_to` cannot accept a `database` argument with any other arguments." raise ArgumentError, "must provide a `shard` and/or `role`."
elsif database
ActiveSupport::Deprecation.warn("The database key in `connected_to` is deprecated. It will be removed in Rails 6.2.0 without replacement.")
if database.is_a?(Hash)
role, database = database.first
role = role.to_sym
end end
db_config, owner_name = resolve_config_for_connection(database)
handler = lookup_connection_handler(role)
handler.establish_connection(db_config, owner_name: owner_name, role: role)
with_handler(role, &blk)
elsif role || shard
unless role unless role
raise ArgumentError, "`connected_to` cannot accept a `shard` argument without a `role`." raise ArgumentError, "`connected_to` cannot accept a `shard` argument without a `role`."
end end
with_role_and_shard(role, shard, prevent_writes, &blk) with_role_and_shard(role, shard, prevent_writes, &blk)
else
raise ArgumentError, "must provide a `shard` and/or `role`."
end
end end
# Connects a role and/or shard to the provided connection names. Optionally `prevent_writes` # Connects a role and/or shard to the provided connection names. Optionally `prevent_writes`

View file

@ -206,23 +206,6 @@ module ActiveRecord
ENV["RAILS_ENV"] = previous_env ENV["RAILS_ENV"] = previous_env
end end
def test_switching_connections_with_database_and_role_raises
error = assert_raises(ArgumentError) do
assert_deprecated do
ActiveRecord::Base.connected_to(database: :readonly, role: :writing) { }
end
end
assert_equal "`connected_to` cannot accept a `database` argument with any other arguments.", error.message
end
def test_database_argument_is_deprecated
assert_deprecated do
ActiveRecord::Base.connected_to(database: { writing: { adapter: "sqlite3", database: "test/db/primary.sqlite3" } }) { }
end
ensure
ActiveRecord::Base.establish_connection(:arunit)
end
def test_switching_connections_without_database_and_role_raises def test_switching_connections_without_database_and_role_raises
error = assert_raises(ArgumentError) do error = assert_raises(ArgumentError) do
ActiveRecord::Base.connected_to { } ActiveRecord::Base.connected_to { }

View file

@ -224,23 +224,6 @@ module ActiveRecord
ENV["RAILS_ENV"] = previous_env ENV["RAILS_ENV"] = previous_env
end end
def test_switching_connections_with_database_and_role_raises
error = assert_raises(ArgumentError) do
assert_deprecated do
ActiveRecord::Base.connected_to(database: :readonly, role: :writing) { }
end
end
assert_equal "`connected_to` cannot accept a `database` argument with any other arguments.", error.message
end
def test_database_argument_is_deprecated
assert_deprecated do
ActiveRecord::Base.connected_to(database: { writing: { adapter: "sqlite3", database: "test/db/primary.sqlite3" } }) { }
end
ensure
ActiveRecord::Base.establish_connection(:arunit)
end
def test_switching_connections_without_database_and_role_raises def test_switching_connections_without_database_and_role_raises
error = assert_raises(ArgumentError) do error = assert_raises(ArgumentError) do
ActiveRecord::Base.connected_to { } ActiveRecord::Base.connected_to { }