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

Reschedule artifact expiry backfill

parent 1cd9ee31
---
title: Reschedule artifact expiry backfill
merge_request: 55093
author:
type: changed
# frozen_string_literal: true
class RescheduleArtifactExpiryBackfill < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
MIGRATION = 'BackfillArtifactExpiryDate'.freeze
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
cc9f56a872cf5e9084e863adc599545754594fb03f30f18433923e0429986e39
\ No newline at end of file
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20210224150506_reschedule_artifact_expiry_backfill.rb')
RSpec.describe RescheduleArtifactExpiryBackfill, :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