Background migrations and migration specs are not isolated from application code

While background migrations must be isolated and can not use application code, many rely on app/models, eg:

  • Gitlab::BackgroundMigration::ArchiveLegacyTraces
  • Gitlab::BackgroundMigration::PopulateMergeRequestMetricsWithEventsData

Or violations in specs:

  • Project.find(project.id).create_repository in spec/migrations/backfill_store_project_full_path_in_repo_spec.rb
  • Project.create and User.create in spec/migrations/clean_up_for_members_spec.rb
  • Project#save in spec/migrations/cleanup_namespaceless_pending_delete_projects_spec.rb
  • there are 85 violations of RSpec/FactoriesInMigrationSpecs in CE

Since migration specs have fixed schema versions, it is impossible to introduce certain changes to the application code when there's dependency between the two. Eg. adding a new column with default value using add_column_with_default is discouraged for large tables but using the default_value_for helper in the model fails in those specs because it cannot set the attribute value (which does not exist because of the fixed schema version).

I came across this when added a new attribute to Project. The failing pipeline: https://gitlab.com/gitlab-org/gitlab-ce/pipelines/49512650