From ce629d9ff32216e098ee8b1ead390157a5ad419a Mon Sep 17 00:00:00 2001 From: eileencodes Date: Tue, 3 Nov 2020 16:22:08 -0500 Subject: [PATCH] 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 --- .../lib/active_record/connection_handling.rb | 34 +++++-------------- .../connection_handlers_multi_db_test.rb | 17 ---------- ...egacy_connection_handlers_multi_db_test.rb | 17 ---------- 3 files changed, 8 insertions(+), 60 deletions(-) diff --git a/activerecord/lib/active_record/connection_handling.rb b/activerecord/lib/active_record/connection_handling.rb index 83c887defe..9d8d6877fe 100644 --- a/activerecord/lib/active_record/connection_handling.rb +++ b/activerecord/lib/active_record/connection_handling.rb @@ -132,9 +132,7 @@ module ActiveRecord # ActiveRecord::Base.connected_to(role: :reading, shard: :shard_one_replica) do # Dog.first # finds first Dog record stored on the shard one replica # end - # - # 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) + def connected_to(role: nil, shard: nil, prevent_writes: false, &blk) if legacy_connection_handling if self != Base raise NotImplementedError, "`connected_to` can only be called on ActiveRecord::Base with legacy connection handling." @@ -145,31 +143,15 @@ module ActiveRecord end end - if database && (role || shard) - raise ArgumentError, "`connected_to` cannot accept a `database` argument with any other arguments." - 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 - - 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 - raise ArgumentError, "`connected_to` cannot accept a `shard` argument without a `role`." - end - - with_role_and_shard(role, shard, prevent_writes, &blk) - else + unless role || shard raise ArgumentError, "must provide a `shard` and/or `role`." end + + unless role + raise ArgumentError, "`connected_to` cannot accept a `shard` argument without a `role`." + end + + with_role_and_shard(role, shard, prevent_writes, &blk) end # Connects a role and/or shard to the provided connection names. Optionally `prevent_writes` diff --git a/activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb b/activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb index 5fe39712cd..05f358781c 100644 --- a/activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb +++ b/activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb @@ -206,23 +206,6 @@ module ActiveRecord ENV["RAILS_ENV"] = previous_env 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 error = assert_raises(ArgumentError) do ActiveRecord::Base.connected_to { } diff --git a/activerecord/test/cases/connection_adapters/legacy_connection_handlers_multi_db_test.rb b/activerecord/test/cases/connection_adapters/legacy_connection_handlers_multi_db_test.rb index 9e58474473..fe4baccc70 100644 --- a/activerecord/test/cases/connection_adapters/legacy_connection_handlers_multi_db_test.rb +++ b/activerecord/test/cases/connection_adapters/legacy_connection_handlers_multi_db_test.rb @@ -224,23 +224,6 @@ module ActiveRecord ENV["RAILS_ENV"] = previous_env 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 error = assert_raises(ArgumentError) do ActiveRecord::Base.connected_to { }