debian-mirror-gitlab/spec/lib/gitlab/database/background_migration_job_spec.rb

137 lines
4.9 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Database::BackgroundMigrationJob do
it_behaves_like 'having unique enum values'
2022-03-02 08:16:31 +05:30
it { is_expected.to be_a Gitlab::Database::SharedModel }
2020-07-28 23:09:34 +05:30
describe '.for_migration_execution' do
let!(:job1) { create(:background_migration_job) }
let!(:job2) { create(:background_migration_job, arguments: ['hi', 2]) }
let!(:job3) { create(:background_migration_job, class_name: 'OtherJob', arguments: ['hi', 2]) }
it 'returns jobs matching class_name and arguments' do
relation = described_class.for_migration_execution('TestJob', ['hi', 2])
expect(relation.count).to eq(1)
expect(relation.first).to have_attributes(class_name: 'TestJob', arguments: ['hi', 2])
end
it 'normalizes class names by removing leading ::' do
relation = described_class.for_migration_execution('::TestJob', ['hi', 2])
expect(relation.count).to eq(1)
expect(relation.first).to have_attributes(class_name: 'TestJob', arguments: ['hi', 2])
end
end
describe '.for_partitioning_migration' do
let!(:job1) { create(:background_migration_job, arguments: [1, 100, 'other_table']) }
let!(:job2) { create(:background_migration_job, arguments: [1, 100, 'audit_events']) }
let!(:job3) { create(:background_migration_job, class_name: 'OtherJob', arguments: [1, 100, 'audit_events']) }
it 'returns jobs matching class_name and the table_name job argument' do
relation = described_class.for_partitioning_migration('TestJob', 'audit_events')
expect(relation.count).to eq(1)
expect(relation.first).to have_attributes(class_name: 'TestJob', arguments: [1, 100, 'audit_events'])
end
it 'normalizes class names by removing leading ::' do
relation = described_class.for_partitioning_migration('::TestJob', 'audit_events')
expect(relation.count).to eq(1)
expect(relation.first).to have_attributes(class_name: 'TestJob', arguments: [1, 100, 'audit_events'])
end
end
describe '.mark_all_as_succeeded' do
let!(:job1) { create(:background_migration_job, arguments: [1, 100]) }
let!(:job2) { create(:background_migration_job, arguments: [1, 100]) }
let!(:job3) { create(:background_migration_job, arguments: [101, 200]) }
let!(:job4) { create(:background_migration_job, class_name: 'OtherJob', arguments: [1, 100]) }
it 'marks all matching jobs as succeeded' do
expect { described_class.mark_all_as_succeeded('TestJob', [1, 100]) }
.to change { described_class.succeeded.count }.from(0).to(2)
expect(job1.reload).to be_succeeded
expect(job2.reload).to be_succeeded
expect(job3.reload).to be_pending
expect(job4.reload).to be_pending
end
it 'normalizes class_names by removing leading ::' do
expect { described_class.mark_all_as_succeeded('::TestJob', [1, 100]) }
.to change { described_class.succeeded.count }.from(0).to(2)
expect(job1.reload).to be_succeeded
expect(job2.reload).to be_succeeded
expect(job3.reload).to be_pending
expect(job4.reload).to be_pending
end
2020-11-24 15:15:51 +05:30
it 'returns the number of jobs updated' do
expect(described_class.succeeded.count).to eq(0)
jobs_updated = described_class.mark_all_as_succeeded('::TestJob', [1, 100])
expect(jobs_updated).to eq(2)
expect(described_class.succeeded.count).to eq(2)
end
2020-07-28 23:09:34 +05:30
context 'when previous matching jobs have already succeeded' do
let(:initial_time) { Time.now.round }
let!(:job1) { create(:background_migration_job, :succeeded, created_at: initial_time, updated_at: initial_time) }
it 'does not update non-pending jobs' do
2021-01-03 14:25:43 +05:30
travel_to(initial_time + 1.day) do
2020-07-28 23:09:34 +05:30
expect { described_class.mark_all_as_succeeded('TestJob', [1, 100]) }
.to change { described_class.succeeded.count }.from(1).to(2)
end
expect(job1.reload.updated_at).to eq(initial_time)
expect(job2.reload).to be_succeeded
expect(job3.reload).to be_pending
expect(job4.reload).to be_pending
end
end
end
describe '#class_name=' do
context 'when the class_name is given without the leading ::' do
it 'sets the class_name to the given value' do
job = described_class.new(class_name: 'TestJob')
expect(job.class_name).to eq('TestJob')
end
end
context 'when the class_name is given with the leading ::' do
it 'removes the leading :: when setting the class_name' do
job = described_class.new(class_name: '::TestJob')
expect(job.class_name).to eq('TestJob')
end
end
context 'when the value is nil' do
it 'sets the class_name to nil' do
job = described_class.new(class_name: nil)
expect(job.class_name).to be_nil
end
end
context 'when the values is blank' do
it 'sets the class_name to the given value' do
job = described_class.new(class_name: '')
expect(job.class_name).to eq('')
end
end
end
end