From 590abfed9e41fbbba776f8427e2c3858f6757e65 Mon Sep 17 00:00:00 2001 From: Aidan Date: Sun, 13 Oct 2024 22:57:51 -0400 Subject: [PATCH] add `with_connection_proxy` method `ActiveRecord::Base` now has the `with_connection` method, which is like the old `connection` method on `ActiveRecord::Base` but it yields the connection to a block instead of returning it: https://github.com/rails/rails/commit/22f41a1b40eb3ff5b94c8db4495ec5c93b513685 This new method is now used for transactions, so in order for transactions to work we need to implement an equivalent in Replica Pools. --- lib/replica_pools/active_record_extensions.rb | 4 ++++ lib/replica_pools/hijack.rb | 1 + 2 files changed, 5 insertions(+) diff --git a/lib/replica_pools/active_record_extensions.rb b/lib/replica_pools/active_record_extensions.rb index ebd58bb..1513628 100644 --- a/lib/replica_pools/active_record_extensions.rb +++ b/lib/replica_pools/active_record_extensions.rb @@ -17,6 +17,10 @@ def connection_proxy ReplicaPools.proxy end + def with_connection_proxy + yield ReplicaPools.proxy + end + # Make sure transactions run on leader # Even if they're initiated from ActiveRecord::Base # (which doesn't have our hijack). diff --git a/lib/replica_pools/hijack.rb b/lib/replica_pools/hijack.rb index 8bfe9de..1d92f04 100644 --- a/lib/replica_pools/hijack.rb +++ b/lib/replica_pools/hijack.rb @@ -18,6 +18,7 @@ def inherited(child) def hijack_connection class << self alias_method :connection, :connection_proxy + alias_method :with_connection, :with_connection_proxy end end end