aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/middleware/database_selector/resolver.rb
diff options
context:
space:
mode:
authoreileencodes <eileencodes@gmail.com>2019-06-12 14:08:43 -0400
committereileencodes <eileencodes@gmail.com>2019-06-14 16:11:36 -0400
commitcd881ab169b60b125e274548778c86252094ac7a (patch)
treea51f398e170caaf2eb03a1698ecb02c6f44892d3 /activerecord/lib/active_record/middleware/database_selector/resolver.rb
parentc78d5243700a1a8ac2fc0408e8cc2e7660ead72f (diff)
downloadrails-cd881ab169b60b125e274548778c86252094ac7a.tar.gz
rails-cd881ab169b60b125e274548778c86252094ac7a.tar.bz2
rails-cd881ab169b60b125e274548778c86252094ac7a.zip
Move while_preventing_writes from conn to handler
If we put the `while_preventing_writes` on the connection then the middleware that sends reads to the primary and ensures they can't write will not work. The `while_preventing_writes` will only be applied to the connection which it's called on - which in the case of the middleware is Ar::Base. This worked fine if you called it directly like `OtherDbConn.connection.while_preventing_writes` but Rails didn't have a way of knowing you wanted to call it on all the connections. The change here moves the `while_preventing_writes` method from the connection to the handler so that it can block writes to all queries for that handler. This will apply to all the connections associated with that handler.
Diffstat (limited to 'activerecord/lib/active_record/middleware/database_selector/resolver.rb')
-rw-r--r--activerecord/lib/active_record/middleware/database_selector/resolver.rb4
1 files changed, 2 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/middleware/database_selector/resolver.rb b/activerecord/lib/active_record/middleware/database_selector/resolver.rb
index 5b89c8f668..3eb1039c50 100644
--- a/activerecord/lib/active_record/middleware/database_selector/resolver.rb
+++ b/activerecord/lib/active_record/middleware/database_selector/resolver.rb
@@ -45,8 +45,8 @@ module ActiveRecord
private
def read_from_primary(&blk)
- ActiveRecord::Base.connection.while_preventing_writes do
- ActiveRecord::Base.connected_to(role: ActiveRecord::Base.writing_role) do
+ ActiveRecord::Base.connected_to(role: ActiveRecord::Base.writing_role) do
+ ActiveRecord::Base.connection_handler.while_preventing_writes do
instrumenter.instrument("database_selector.active_record.read_from_primary") do
yield
end