Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix preventing_writes for granular swapping
When we implemented granular connection swapping we treated `prevent_writes` similarly to `role` and `shard`. However, it's a little different. `prevent_writes` is a feature of a conneciton whereas `role` and `shared` are used to lookup a connection. A writing connection that wants to prevent writes doesn't get stored in the pool as such, `prevent_writes` is added as a way to change behavior of a connection. Because of this when `preventing_writes?` calls `ActiveRecord::Base.current_preventing_writes` the lookup is on the wrong class if we're using granular swapping. Instead we need to lookup the `prevent_writes` from the class that called `connected_to` rather than `ActiveRecord::Base`. To do that I've done the following: 1) Added access to the `connection_specification_name` as `owner_name` on the pool so the connection can access it. 2) Call `safe_constantize` on the `owner_name` since the `connected_to_stack` stores the class as a class and not a string. 3) Now we can call `safe_constantize` on the klass that we got out of the pool to be able to look up the correct `prevent_writes` from the stack. 4) NullPool's get a nil connection specification name because if they're not connected they can't prevent writes. This required changing some tests but we don't support multi-db for a case where you're establishing a connection directly from the adapter. I benchmarked this and the two versions were pretty close, although this is a little bit slower. I tried a few other ways of fixing this. I first tried adding `prevent_writes` to the connection in `retrieve_connection` based on the `current_preventing_writes` but then that behavior only worked correctly for ActiveRecord connections. If we already had a connection and `retrieve_connection` isn't called then the tests testing that behavior would fail. This was the best way to not introduce confusing differences between global and granular connections. Fixes #40559
- Loading branch information
1 parent
86a9c89
commit b92dc5b
Showing
4 changed files
with
90 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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