Add options to reduce the number of migrated rows

What does this MR do and why?

This MR adds options to reduce the number of migrated rows during the CI builds metadata migration for self-managed instances.

Migration behavior:

  • Default behavior: Migrates everything if no customization is provided
  • DATABASE_CI_JOBS_PROCESSING_DATA_CUTOFF: Controls when to stop creating ci_job_definitions. Use this if you only care about job definitions for jobs that might need to run at some point (manual play or retries). Timeout data, exit_code, exposed artifacts, and environment data will still be migrated for all jobs.
  • DATABASE_CI_JOBS_MIGRATION_CUTOFF: Controls which jobs you care about migrating overall. Non-migrated jobs will not have timeout data, exit_code used, exposed artifacts will not appear in the MR for those old jobs, and environment data will be missing.

Migration order:

Migrates from the newest partition to the oldest so that the most recent records are moved first.

Test and validate

  1. Create a new file at db/post_migrate/20251202101206_finalize_builds_metadata_migration.rb with the following content:
# frozen_string_literal: true

class FinalizeBuildsMetadataMigration < Gitlab::Database::Migration[2.3]
  disable_ddl_transaction!
  milestone '18.7'
  restrict_gitlab_migration gitlab_schema: :gitlab_ci

  MIGRATION = 'MoveCiBuildsMetadata'
  BATCH_SIZE = 1000
  SUB_BATCH_SIZE = 100
  TABLE_NAME = :p_ci_builds

  def up
    each_partition do |partition, ids|
      next unless ids.include?(104)

      finalize_batched_background_migration(
        job_class_name: MIGRATION,
        table_name: partition.identifier,
        column_name: :id,
        job_arguments: [:partition_id, ids]
      )
    end
  end

  def down
  end

  def each_partition
    Gitlab::Database::PostgresPartitionedTable.each_partition(TABLE_NAME) do |partition|
      ids = partition.condition.scan(/\d+/).map(&:to_i)
      yield(partition, ids)
    end
  end
end
  1. Execute this command to start the migration on a production thin clone:
DISABLE_POSTGRES_PARTITION_CREATION_ON_STARTUP=1 RAILS_ENV=test VERSION=20251202101206 pgai use -o ci  bin/rails db:migrate:up:ci

References

Related to #579807
Docs update: !214376

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 Marius Bobin

Merge request reports

Loading