Resolve "Fix `Gitlab::Database::SchemaCacheWithRenamedTable` does not work with many databases"
What does this MR do and why?
Fix Gitlab::Database::SchemaCacheWithRenamedTable to work with many databases
Instead of getting the connection from ActiveRecord::Base
, we use the connection from ActiveRecord::ConnectionAdapters::SchemaCache
. Schema cache will return the proper connection based on the database context.
Screenshots or screen recordings
These are strongly recommended to assist reviewers and reduce the time to merge your change.
How to set up and validate locally
Numbered steps to set up and validate the change are strongly suggested.
-
Add to
TABLES_TO_BE_RENAMED
. A good one is:TABLES_TO_BE_RENAMED = { 'services' => 'integrations' }.freeze
-
Setup multiple databases
-
Only in
gitlabhq_development_ci
, create the services view withCREATE VIEW services AS SELECT * FROM integrations;
-
Check the
:renamed_tables_cache
is distinct -
Cleanup. Drop the services view in the
gitlabhq_development_ci
database withDROP VIEW services;
Before:
[1] pry(main)> Ci::ApplicationRecord.retrieve_connection.schema_cache.send :renamed_tables_cache
=> {}
[2] pry(main)> ApplicationRecord.retrieve_connection.schema_cache.send :renamed_tables_cache
=> {}
After:
[1] pry(main)> ApplicationRecord.retrieve_connection.schema_cache.send :renamed_tables_cache
=> {}
[2] pry(main)> Ci::ApplicationRecord.retrieve_connection.schema_cache.send :renamed_tables_cache
=> {"services"=>"integrations"}
[4] pry(main)> Ci::ApplicationRecord.retrieve_connection.schema_cache.send :underlying_table, 'services'
=> "integrations"
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.
Related to #336899 (closed)