Skip to content

Add finalize_background_migration spec matcher for testing

What does this MR do and why?

Adds a simple be_finalize_background_migration_of spec matcher to validate that finalize_background_migration was called correctly for the expected class.

How to set up and validate locally

  1. Queue a background migration and add the finalize migration:
  • db/post_migrate/20220426190417_schedule_test_background_migration.rb:

    # frozen_string_literal: true
    
    class ScheduleTestBackgroundMigration < Gitlab::Database::Migration[1.0]
      disable_ddl_transaction!
    
      MIGRATION = 'TestBackgroundMigration'
      DELAY_INTERVAL = 2.minutes
    
      def up
        queue_background_migration_jobs_by_range_at_intervals(
          define_batchable_model('users'),
          MIGRATION,
          DELAY_INTERVAL,
          track_jobs: true
        )
      end
    
      def down
        # no-op
      end
    end
  • lib/gitlab/background_migration/test_background_migration.rb:

    # frozen_string_literal: true
    
    class Gitlab::BackgroundMigration::TestBackgroundMigration
      def perform(start_id, end_id)
        mark_job_as_succeeded(start_id, end_id)
      end
    
      private
    
      def mark_job_as_succeeded(*arguments)
        ::Gitlab::Database::BackgroundMigrationJob.mark_all_as_succeeded(
          self.class.name.demodulize,
          arguments
        )
      end
    end
  • db/post_migrate/20220426193321_finalize_test_background_migration.rb:

    # frozen_string_literal: true
    
    class FinalizeTestBackgroundMigration < Gitlab::Database::Migration[1.0]
      disable_ddl_transaction!
    
      MIGRATION = 'TestBackgroundMigration'
    
      def up
        finalize_background_migration(MIGRATION)
      end
    
      def down
        # no-op
      end
    end
  1. Add the spec using the matcher, spec/migrations/20220426193321_finalize_test_background_migration_spec.rb:

    # frozen_string_literal: true
    
    require 'spec_helper'
    require_migration!
    
    RSpec.describe FinalizeTestBackgroundMigration, :migration do
      let_it_be(:migration) { described_class::MIGRATION }
    
      describe '#up' do
        it 'finalizes the background migration' do
          expect(described_class).to be_finalize_background_migration_of(migration)
    
          migrate!
        end
      end
    end
  2. bin/rails db:migrate RAILS_ENV=test and bin/rspec spec/migrations/20220426193321_finalize_test_background_migration_spec.rb, notice it passes

  3. Change the MIGRATION name in the spec to something different, and see it failing, i.e. for expect(described_class).to be_finalize_background_migration_of('invalid'):

     expected: ("invalid")
     got: ("TestBackgroundMigration")
     # ./db/post_migrate/20220426193321_finalize_test_background_migration.rb:9:in `up'
     # ./lib/gitlab/database/migrations/lock_retry_mixin.rb:36:in `ddl_transaction'

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Catalin Irimie

Merge request reports