.ai/principles/distilled/testing-migrations.md
db/post_migrate) and background migrations (lib/gitlab/background_migration) — these are mandatory.(ee/)spec/migrations/ or spec/lib/(ee/)background_migrations/ — the :migration RSpec tag is applied automatically.:gitlab_main (for example :gitlab_ci), explicitly specify it with an RSpec tag like migration: :gitlab_ci.require_migration! at the top of every migration spec to load the migration file (migration files are not autoloaded by Rails).require_migration!('populate_bar_column').table helper to create test data in migration specs (e.g., table(:projects).create!(...)); DO NOT use FactoryBot in migration specs, as it relies on application code that may change after the migration runs.migrate! helper to run the migration under test; DO NOT call the migration's up method directly, as migrate! also bumps the schema version in schema_migrations (required for the after hook to work correctly).reversible_migration helper to test migrations that implement change or both up and down hooks; it verifies that state after reversal matches state before the migration ran.have_scheduled_batched_migration matcher to verify that a BatchedMigration record was created with the expected class, table, column, job arguments, and attributes (e.g., interval).be_finalize_background_migration_of matcher to verify that a migration calls finalize_background_migration with the expected background migration class.:migration_with_transaction metadata only when testing migrations that alter seeded data in deletion_except_tables, so the test runs within a transaction and data is rolled back to original values.For the full picture, see: