Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move advisory locks to own connection handler.
Removes the use of `ActiveRecord::AdvisoryLockBase` since it inherits from `ActiveRecord::Base` and hence share module attributes that are defined in `ActiveRecord::Base`. This is problematic because establishing connections through `ActiveRecord::AdvisoryLockBase` can end up changing state of the default connection handler of `ActiveRecord::Base` leading to unexpected behaviors in a Rails application. In the case of rails#39157, Running migrations with `rails db:migrate:primary_shard_one` was not working as the application itself defined the following ``` class ApplicationRecord < ActiveRecord::Base self.abstract_class = true connects_to shards: { default: { writing: :primary }, shard_one: { writing: :primary_shard_one } } end ``` In the database migrate rake task, the default connection was established with the database config of `primary_shard_one`. However, the default connection was altered to that of `primary` because `ActiveRecord::AdvisoryLockBase.establish_connection` ended up loading `ApplicationRecord` which calls `connects_to shards:`. Since all we really need here is just a normal database connection, we can avoid accidentally altering the default connection handler state during the migration by creating a custom connection handler used for retrieving a connection.
- Loading branch information