.ai/principles/distilled/database-fundamentals.md
db:gitlabcom-database-testing pipeline?"db/structure.sql is updated and relevant version files under db/schema_migrations were added or removed.db:check-migrations and db:check-schema pipeline jobs ran successfully.gitlab-org namespace ID 9970, project IDs 13083/278964, or user ID 1614863); DO NOT accept plans returning 0 records unless the query is an UPDATE.~data-deletion label and a description of how deleted data could be recovered.change method or explicit down method; data migrations must include a rollback procedure or explanation of why it is non-reversible.disable_ddl_transaction!.CREATE INDEX CONCURRENTLY tested in Database Lab with execution time noted in the MR description.#f_upcoming_release.WHERE, ORDER BY, GROUP BY, and JOIN clauses.db/fixtures/development/.index(A, B, C) makes index(A, B) and index(A) redundant).dependent: ... associations that become unnecessary after adding a foreign key migration.gitlab-com-database-testing comment against query performance guidelines.update, upsert, delete, update_all, upsert_all, delete_all, destroy_all), require raw SQL and query plan in the MR description and a database review.main, ci, sec, or geo database schemas.preload instead of includes when includes would generate a cross-database join.disable_joins: true on has_one/has_many through: associations that span databases, and verify intermediate result sets are bounded (have LIMIT 1 or a unique-column WHERE clause).pluck to map unbounded cross-database ID sets; use bounded alternatives.::Gitlab::Database.allow_cross_joins_across_databases(url: ...) pointing to a tracking issue; DO NOT override ActiveRecord associations to allow cross-joins.dependent: :nullify or dependent: :destroy across databases; use dependent: :restrict_with_error or loose foreign keys instead.gitlab_schema (ApplicationRecord for gitlab_main_org, Ci::ApplicationRecord for gitlab_ci, Geo::TrackingBase for gitlab_geo, etc.).no_cross_db_foreign_keys_spec.rb and plan conversion to a loose foreign key.skip_if_shared_database, skip_if_database_exists, skip_if_multiple_databases_are_setup, or skip_if_multiple_databases_not_setup helpers in specs that must run only in specific database modes.When a diff modifies or replaces an existing structure, always verify the current state from an authoritative source before flagging a discrepancy. Never infer the pre-change state solely from diff context — check the actual source of truth. For example:
down methods: verify the down schema against the actual pre-migration schema by querying the local PostgreSQL database (\d tablename) or, if unavailable, reading the schema from the base branch (git show master:db/structure.sql).For the full picture, see: