Skip to content

Commit

Permalink
add with_connection_proxy method
Browse files Browse the repository at this point in the history
`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:
rails/rails@22f41a1

This new method is now used for transactions, so in order for
transactions to work we need to implement an equivalent in Replica
Pools.
  • Loading branch information
a-lavis committed Oct 14, 2024
1 parent 1d82421 commit 590abfe
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/replica_pools/active_record_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
1 change: 1 addition & 0 deletions lib/replica_pools/hijack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 590abfe

Please sign in to comment.