Use lease_connection on Rails 7.2+ in ActiveRecord5Adapter#894
Open
benaitcheson wants to merge 1 commit into
Open
Use lease_connection on Rails 7.2+ in ActiveRecord5Adapter#894benaitcheson wants to merge 1 commit into
benaitcheson wants to merge 1 commit into
Conversation
Rails 7.2 soft-deprecated ActiveRecord::Base.connection in favour of lease_connection, which better describes that the caller is acquiring a connection from the pool rather than holding a permanent one (see rails/rails#51230). Prefer lease_connection when available, and fall back to connection on Rails < 7.2 where it is not defined. Refs CanCanCommunity#893
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #893.
Why
Rails 7.2 soft-deprecated
ActiveRecord::Base.connectionin favour oflease_connection, which better reflects that the caller is acquiring a connection from the pool rather than holding a permanent one (see rails/rails#51230).connectionstill works on 7.2+, but apps that opt into stricter connection-pool semantics (ActiveRecord.permanent_connection_checkout = :deprecatedor:disallowed) see warnings or errors fromActiveRecord5Adapter#visit_nodes.What
Prefer
@model_class.lease_connectionwhen defined; fall back to@model_class.connectionon Rails < 7.2. The conditional usesrespond_to?(:lease_connection)rather than a Rails-version check so the same code path works against ActiveRecord main without further changes. This is the pattern used by other gems spanning the 7.2 boundary, e.g.good_jobandpg_ha_migrations.Backward compatibility
No behavioural change on Rails 5.2 / 6.x / 7.0 / 7.1 —
lease_connectionis undefined there and the fallback path keeps callingconnection. The existing CI matrix should cover the fallback.