Add the gitlab_mr_diff schema and mr_diff connection

What does this MR do and why?

Adds the gitlab_mr_diff schema, the mr_diff database connection and MrDiffApplicationRecord. This is the first step in moving the merge request diff tables to their own database.

It changes nothing at runtime:

  • The mr_diff connection uses fallback_database: main, and MrDiffApplicationRecord only calls connects_to when mr_diff is configured. Without it, the model uses main's connection pool.
  • No tables use gitlab_mr_diff yet. The tables move in a later merge request.

Not included yet:

  • The config/database.yml template for mr_diff and the CI variable that turns it on in CI. https://gitlab.com/gitlab-org/gitlab/-/work_items/635395 tracks them.
  • The hardcoded database lists (the gitlab:db:migration_testing tasks, gitlab:db:truncate_legacy_tables:mr_diff, TablesTruncate, schema_load_fix, spec/support/multiple_databases.rb and loose foreign key turbo mode with its ops flag). They only run once CI configures mr_diff, so they go in the same merge request as the template.
  • The background migration and background operation workers, the Sidekiq duration limit, and the Prometheus health indicator case. They only matter once mr_diff has its own connection, and the workers' cron entries and the Sidekiq limit would add work on every installation before then.

References

How to set up and validate locally

  1. With no mr_diff entry in config/database.yml, check the fallback in a Rails console:

    Gitlab::Database.has_config?(:mr_diff)            # => false
    MrDiffApplicationRecord.connection_db_config.name # => "main"
    Gitlab::Database.gitlab_schemas_for_connection(ApplicationRecord.connection).include?(:gitlab_mr_diff) # => true
  2. Run the specs that check connection and schema definitions:

    bin/rspec spec/lib/gitlab/database_spec.rb spec/lib/gitlab/database/gitlab_schema_spec.rb spec/lib/gitlab/database/database_connection_info_spec.rb

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Patrick Bajao

Merge request reports

Loading
Loading