From 0213e6e5f5c4543ffed753550efda819748364ea Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Wed, 21 Aug 2024 11:12:57 +0200 Subject: [PATCH] Fix Active Record 7.2 deprecation warnings Fix: https://github.com/Shopify/identity_cache/pull/573 --- lib/identity_cache.rb | 51 +++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/lib/identity_cache.rb b/lib/identity_cache.rb index 15fd653e..af492e56 100644 --- a/lib/identity_cache.rb +++ b/lib/identity_cache.rb @@ -117,27 +117,36 @@ def should_fill_cache? # :nodoc: !readonly end - def should_use_cache? # :nodoc: - ActiveRecord::Base.connection_handler.connection_pool_list(ActiveRecord::Base.current_role).none? do |pool| - pool.active_connection? && - # Rails wraps each of your tests in a transaction, so that any changes - # made to the database during the test can be rolled back afterwards. - # These transactions are flagged as "unjoinable", which tries to make - # your application behave as if they weren't there. In particular: - # - # - Opening another transaction during the test creates a savepoint, - # which can be rolled back independently of the main transaction. - # - When those nested transactions complete, any `after_commit` - # callbacks for records modified during the transaction will run, - # even though the changes haven't actually been committed yet. - # - # By ignoring unjoinable transactions, IdentityCache's behaviour - # during your test suite will more closely match production. - # - # When there are no open transactions, `current_transaction` returns a - # special `NullTransaction` object that is unjoinable, meaning we will - # use the cache. - pool.connection.current_transaction.joinable? + # Rails wraps each of your tests in a transaction, so that any changes + # made to the database during the test can be rolled back afterwards. + # These transactions are flagged as "unjoinable", which tries to make + # your application behave as if they weren't there. In particular: + # + # - Opening another transaction during the test creates a savepoint, + # which can be rolled back independently of the main transaction. + # - When those nested transactions complete, any `after_commit` + # callbacks for records modified during the transaction will run, + # even though the changes haven't actually been committed yet. + # + # By ignoring unjoinable transactions, IdentityCache's behaviour + # during your test suite will more closely match production. + # + # When there are no open transactions, `current_transaction` returns a + # special `NullTransaction` object that is unjoinable, meaning we will + # use the cache. + if ActiveRecord::ConnectionAdapters::ConnectionPool.method_defined?(:active_connection) + def should_use_cache? # :nodoc: + ActiveRecord::Base.connection_handler.connection_pool_list(ActiveRecord::Base.current_role).none? do |pool| + pool.active_connection? && + pool.active_connection.current_transaction.joinable? + end + end + else + def should_use_cache? # :nodoc: + ActiveRecord::Base.connection_handler.connection_pool_list(ActiveRecord::Base.current_role).none? do |pool| + pool.active_connection? && + pool.connection.current_transaction.joinable? + end end end