Commit 951c7bfd authored by Matija Čupić's avatar Matija Čupić Committed by Yannis Roussos

Schedule artifact expiry backfill again

The previous background migration left around 400.000 artifacts without
an expiry date. This schedules the background migration again to
backfill those entries as well.
parent a8d7e020
---
title: Schedule artifact expiry backfill again.
merge_request: 59270
author:
type: changed
# frozen_string_literal: true
class RescheduleArtifactExpiryBackfillAgain < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
MIGRATION = 'BackfillArtifactExpiryDate'
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
407806cc168ef9859c9a4f1bd4db7a56aee01367e784ea0767889863b9ace35d
\ No newline at end of file
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20210413132500_reschedule_artifact_expiry_backfill_again.rb')
RSpec.describe RescheduleArtifactExpiryBackfillAgain, :migration do
let(:migration_class) { Gitlab::BackgroundMigration::BackfillArtifactExpiryDate }
let(:migration_name) { migration_class.to_s.demodulize }
before do
table(:namespaces).create!(id: 123, name: 'test_namespace', path: 'test_namespace')
table(:projects).create!(id: 123, name: 'sample_project', path: 'sample_project', namespace_id: 123)
end
it 'correctly schedules background migrations' do
first_artifact = create_artifact(job_id: 0, expire_at: nil, created_at: Date.new(2020, 06, 21))
second_artifact = create_artifact(job_id: 1, expire_at: nil, created_at: Date.new(2020, 06, 21))
create_artifact(job_id: 2, expire_at: Date.yesterday, created_at: Date.new(2020, 06, 21))
create_artifact(job_id: 3, expire_at: nil, created_at: Date.new(2020, 06, 23))
Sidekiq::Testing.fake! do
freeze_time do
migrate!
expect(BackgroundMigrationWorker.jobs.size).to eq(1)
expect(migration_name).to be_scheduled_migration_with_multiple_args(first_artifact.id, second_artifact.id)
end
end
end
private
def create_artifact(params)
table(:ci_builds).create!(id: params[:job_id], project_id: 123)
table(:ci_job_artifacts).create!(project_id: 123, file_type: 1, **params)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment