Skip to content

Commit

Permalink
Remove deprecated support to apply some methods in the Connection Han…
Browse files Browse the repository at this point in the history
…dle to the connections pools for the current role when the `role` arguments isn't provided
  • Loading branch information
rafaelfranca committed Feb 20, 2024
1 parent 9489c14 commit 64cbcd7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 60 deletions.
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
* Remove deprecated support to apply `#connection_pool_list`, `#active_connections?`, `#clear_active_connections!`,
`#clear_reloadable_connections!`, `#clear_all_connections!` and `#flush_idle_connections!` to the connections pools
for the current role when the `role` argument isn't provided.

*Rafael Mendonça França*

* Remove deprecated `#all_connection_pools`.

*Rafael Mendonça França*
Expand Down
Expand Up @@ -88,14 +88,10 @@ def connection_pool_names # :nodoc:
connection_name_to_pool_manager.keys
end

# Returns the pools for a connection handler and given role. If +:all+ is passed,
# Returns the pools for a connection handler and given role. If +:all+ is passed,
# all pools belonging to the connection handler will be returned.
def connection_pool_list(role = nil)
if role.nil?
deprecation_for_pool_handling(__method__)
role = ActiveRecord::Base.current_role
connection_name_to_pool_manager.values.flat_map { |m| m.pool_configs(role).map(&:pool) }
elsif role == :all
if role.nil? || role == :all
connection_name_to_pool_manager.values.flat_map { |m| m.pool_configs.map(&:pool) }
else
connection_name_to_pool_manager.values.flat_map { |m| m.pool_configs(role).map(&:pool) }
Expand Down Expand Up @@ -157,23 +153,13 @@ def establish_connection(config, owner_name: Base, role: Base.current_role, shar
# Returns true if there are any active connections among the connection
# pools that the ConnectionHandler is managing.
def active_connections?(role = nil)
if role.nil?
deprecation_for_pool_handling(__method__)
role = ActiveRecord::Base.current_role
end

each_connection_pool(role).any?(&:active_connection?)
end

# Returns any connections in use by the current thread back to the pool,
# and also returns connections to the pool cached by threads that are no
# longer alive.
def clear_active_connections!(role = nil)
if role.nil?
deprecation_for_pool_handling(__method__)
role = ActiveRecord::Base.current_role
end

each_connection_pool(role).each do |pool|
pool.release_connection
pool.disable_query_cache!
Expand All @@ -184,32 +170,17 @@ def clear_active_connections!(role = nil)
#
# See ConnectionPool#clear_reloadable_connections! for details.
def clear_reloadable_connections!(role = nil)
if role.nil?
deprecation_for_pool_handling(__method__)
role = ActiveRecord::Base.current_role
end

each_connection_pool(role).each(&:clear_reloadable_connections!)
end

def clear_all_connections!(role = nil)
if role.nil?
deprecation_for_pool_handling(__method__)
role = ActiveRecord::Base.current_role
end

each_connection_pool(role).each(&:disconnect!)
end

# Disconnects all currently idle connections.
#
# See ConnectionPool#flush! for details.
def flush_idle_connections!(role = nil)
if role.nil?
deprecation_for_pool_handling(__method__)
role = ActiveRecord::Base.current_role
end

each_connection_pool(role).each(&:flush!)
end

Expand Down Expand Up @@ -273,23 +244,6 @@ def pool_managers
connection_name_to_pool_manager.values
end

def deprecation_for_pool_handling(method)
roles = []
pool_managers.each do |pool_manager|
roles << pool_manager.role_names
end

if roles.flatten.uniq.count > 1
ActiveRecord.deprecator.warn(<<-MSG.squish)
`#{method}` currently only applies to connection pools in the current
role (`#{ActiveRecord::Base.current_role}`). In Rails 7.2, this method
will apply to all known pools, regardless of role. To affect only those
connections belonging to a specific role, pass the role name as an
argument. To switch to the new behavior, pass `:all` as the role name.
MSG
end
end

def disconnect_pool_from_pool_manager(pool_manager, role, shard)
pool_config = pool_manager.remove_pool_config(role, shard)

Expand Down
Expand Up @@ -323,9 +323,7 @@ def test_connection_pool_list
assert_equal([@rw_pool], @handler.connection_pool_list(:writing))
assert_equal([@ro_pool], @handler.connection_pool_list(:reading))

assert_deprecated(ActiveRecord.deprecator) do
@handler.connection_pool_list
end
assert_equal([@rw_pool, @ro_pool], @handler.connection_pool_list)
end

def test_retrieve_connection
Expand All @@ -334,22 +332,20 @@ def test_retrieve_connection
end

def test_active_connections?
assert_deprecated(ActiveRecord.deprecator) do
assert_not_predicate @handler, :active_connections?
end
assert_not_predicate @handler, :active_connections?

assert @handler.retrieve_connection(@connection_name)
assert @handler.retrieve_connection(@connection_name, role: :reading)

assert_deprecated(ActiveRecord.deprecator) do
assert_predicate @handler, :active_connections?
end
assert_predicate @handler, :active_connections?

@handler.clear_active_connections!(:writing)

assert_predicate @handler, :active_connections?

@handler.clear_active_connections!(:all)

assert_deprecated(ActiveRecord.deprecator) do
assert_not_predicate @handler, :active_connections?
end
assert_not_predicate @handler, :active_connections?
end

def test_retrieve_connection_pool
Expand Down
4 changes: 4 additions & 0 deletions guides/source/7_2_release_notes.md
Expand Up @@ -150,6 +150,10 @@ Please refer to the [Changelog][active-record] for detailed changes.

* Remove deprecated `#all_connection_pools`.

* Remove deprecated support to apply `#connection_pool_list`, `#active_connections?`, `#clear_active_connections!`,
`#clear_reloadable_connections!`, `#clear_all_connections!` and `#flush_idle_connections!` to the connections pools
for the current role when the `role` argument isn't provided.

### Deprecations

* Deprecate `Rails.application.config.active_record.allow_deprecated_singular_associations_name`
Expand Down

0 comments on commit 64cbcd7

Please sign in to comment.