Update MultipleDatabase cop to allow some methods
What does this MR do and why?
Related issue: #350191 (closed)
Update the Database/MultipleDatabases
cop to have an allowlist of methods that are safe to call on ActiveRecord::Base
. For now there is only one method added: no_touching
, which is safe to call even with multiple databases configured.
Also remove offending files from the rubocop todo list based on their usage of no_touching
being considered safe.
How to set up and validate locally
We can verify that ActiveRecord::Base.no_touching
is safe to use with multiple databases.
- Setup multiple databases according to https://docs.gitlab.com/ee/development/database/multiple_databases.html#development-setup
- Start a rails console with separate connections for
main
andci
:GITLAB_USE_MODEL_LOAD_BALANCING=true rails c
- First, we can try the behavior without
no_touching
. If you run the below, you'll see update queries for each object to set theupdated_at
:Project.first.touch; Ci::Pipeline.first.touch
- Now try with
no_touching
, and it will load each object but no longer run the update queries:ActiveRecord::Base.no_touching { Project.first.touch; Ci::Pipeline.first.touch }
- We can also try with
no_touching
only on a specific base model. The following will only applyno_touching
to the pipeline model:Ci::ApplicationRecord.no_touching { Project.first.touch; Ci::Pipeline.first.touch }
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
-
I have evaluated the MR acceptance checklist for this MR.
Edited by Patrick Bair