Skip to content

Commit

Permalink
Deprecate name argument in remove_connection
Browse files Browse the repository at this point in the history
The `name` argument is not useful as `remove_connection` should be called
on the class that established the connection. Allowing `name` to be
passed here doesn't match how any of the other methods behave on the
connection classes. While this behavior has been around for a long time,
I'm not sure anyone is using this as it's not documented when to use
name, nor are there any tests.
  • Loading branch information
eileencodes committed Jul 6, 2023
1 parent 1cbd88f commit b69fa80
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
* Deprecate `name` argument on `#remove_connection`.

The `name` argument is deprecated on `#remove_connection` without replacement. `#remove_connection` should be called directly on the class that established the connection.

*Eileen M. Uchitelle*

* Fix has_one through singular building with inverse.

Allows building of records from an association with a has_one through a
Expand Down
8 changes: 8 additions & 0 deletions activerecord/lib/active_record/connection_handling.rb
Expand Up @@ -293,6 +293,14 @@ def connected?
end

def remove_connection(name = nil)
if name
ActiveRecord.deprecator.warn(<<-MSG.squish)
The name argument for `#remove_connection` is deprecated without replacement
and will be removed in Rails 7.2. `#remove_connection` should always be called
on the connection class directly, which makes the name argument obsolete.
MSG
end

name ||= @connection_specification_name if defined?(@connection_specification_name)
# if removing a connection that has a pool, we reset the
# connection_specification_name so it will use the parent
Expand Down
Expand Up @@ -279,6 +279,20 @@ def test_a_class_using_custom_pool_and_switching_back_to_primary
assert_same klass2.connection, ActiveRecord::Base.connection
end

def test_remove_connection_with_name_argument_is_deprecated
klass2 = Class.new(Base) { def self.name; "klass2"; end }

assert_same klass2.connection, ActiveRecord::Base.connection

pool = klass2.establish_connection(ActiveRecord::Base.connection_pool.db_config.configuration_hash)
assert_same klass2.connection, pool.connection
assert_not_same klass2.connection, ActiveRecord::Base.connection

assert_deprecated(ActiveRecord.deprecator) do
ActiveRecord::Base.remove_connection("klass2")
end
end

class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
Expand Down

0 comments on commit b69fa80

Please sign in to comment.