20210224150506_reschedule_artifact_expiry_backfill.rb 1013 Bytes
Newer Older
1 2 3 4 5 6
# frozen_string_literal: true

class RescheduleArtifactExpiryBackfill < ActiveRecord::Migration[6.0]
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false
Lee Tickett's avatar
Lee Tickett committed
7
  MIGRATION = 'BackfillArtifactExpiryDate'
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  SWITCH_DATE = Date.new(2020, 06, 22).freeze

  disable_ddl_transaction!

  class JobArtifact < ActiveRecord::Base
    include EachBatch

    self.inheritance_column = :_type_disabled
    self.table_name = 'ci_job_artifacts'

    scope :without_expiry_date, -> { where(expire_at: nil) }
    scope :before_switch, -> { where("date(created_at AT TIME ZONE 'UTC') < ?::date", SWITCH_DATE) }
  end

  def up
    Gitlab::BackgroundMigration.steal(MIGRATION) do |job|
      job.delete

      false
    end

    queue_background_migration_jobs_by_range_at_intervals(
      JobArtifact.without_expiry_date.before_switch,
      MIGRATION,
      2.minutes,
      batch_size: 200_000
    )
  end

  def down
    Gitlab::BackgroundMigration.steal(MIGRATION) do |job|
      job.delete

      false
    end
  end
end