Skip to content

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.

https://github.com/rails/rails/blob/main/activerecord/lib/active_record/connection_adapters/schema_cache.rb#L36

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.

  1. Add to TABLES_TO_BE_RENAMED. A good one is:

    TABLES_TO_BE_RENAMED = { 'services' => 'integrations' }.freeze
  2. Setup multiple databases

  3. Only in gitlabhq_development_ci, create the services view with CREATE VIEW services AS SELECT * FROM integrations;

  4. Check the :renamed_tables_cache is distinct

  5. Cleanup. Drop the services view in the gitlabhq_development_ci database with DROP 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.

Related to #336899 (closed)

Edited by Thong Kuah

Merge request reports