diff options
Diffstat (limited to 'spec/lib/gitlab/background_migration')
5 files changed, 204 insertions, 11 deletions
diff --git a/spec/lib/gitlab/background_migration/backfill_projects_with_coverage_spec.rb b/spec/lib/gitlab/background_migration/backfill_projects_with_coverage_spec.rb new file mode 100644 index 00000000000..49056154744 --- /dev/null +++ b/spec/lib/gitlab/background_migration/backfill_projects_with_coverage_spec.rb @@ -0,0 +1,94 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::BackgroundMigration::BackfillProjectsWithCoverage, schema: 20210818185845 do + let(:projects) { table(:projects) } + let(:project_ci_feature_usages) { table(:project_ci_feature_usages) } + let(:ci_pipelines) { table(:ci_pipelines) } + let(:ci_daily_build_group_report_results) { table(:ci_daily_build_group_report_results) } + let(:group) { table(:namespaces).create!(name: 'user', path: 'user') } + let(:project_1) { projects.create!(namespace_id: group.id) } + let(:project_2) { projects.create!(namespace_id: group.id) } + let(:pipeline_1) { ci_pipelines.create!(project_id: project_1.id, source: 13) } + let(:pipeline_2) { ci_pipelines.create!(project_id: project_1.id, source: 13) } + let(:pipeline_3) { ci_pipelines.create!(project_id: project_2.id, source: 13) } + let(:pipeline_4) { ci_pipelines.create!(project_id: project_2.id, source: 13) } + + subject { described_class.new } + + describe '#perform' do + before do + ci_daily_build_group_report_results.create!( + id: 1, + project_id: project_1.id, + date: 4.days.ago, + last_pipeline_id: pipeline_1.id, + ref_path: 'main', + group_name: 'rspec', + data: { coverage: 95.0 }, + default_branch: true, + group_id: group.id + ) + + ci_daily_build_group_report_results.create!( + id: 2, + project_id: project_1.id, + date: 3.days.ago, + last_pipeline_id: pipeline_2.id, + ref_path: 'main', + group_name: 'rspec', + data: { coverage: 95.0 }, + default_branch: true, + group_id: group.id + ) + + ci_daily_build_group_report_results.create!( + id: 3, + project_id: project_2.id, + date: 2.days.ago, + last_pipeline_id: pipeline_3.id, + ref_path: 'main', + group_name: 'rspec', + data: { coverage: 95.0 }, + default_branch: true, + group_id: group.id + ) + + ci_daily_build_group_report_results.create!( + id: 4, + project_id: project_2.id, + date: 1.day.ago, + last_pipeline_id: pipeline_4.id, + ref_path: 'test_branch', + group_name: 'rspec', + data: { coverage: 95.0 }, + default_branch: false, + group_id: group.id + ) + + stub_const("#{described_class}::INSERT_DELAY_SECONDS", 0) + end + + it 'creates entries per project and default_branch combination in the given range', :aggregate_failures do + subject.perform(1, 4, 2) + + entries = project_ci_feature_usages.order('project_id ASC, default_branch DESC') + + expect(entries.count).to eq(3) + expect(entries[0]).to have_attributes(project_id: project_1.id, feature: 1, default_branch: true) + expect(entries[1]).to have_attributes(project_id: project_2.id, feature: 1, default_branch: true) + expect(entries[2]).to have_attributes(project_id: project_2.id, feature: 1, default_branch: false) + end + + context 'when an entry for the project and default branch combination already exists' do + before do + subject.perform(1, 4, 2) + end + + it 'does not create a new entry' do + expect { subject.perform(1, 4, 2) }.not_to change { project_ci_feature_usages.count } + end + end + end +end diff --git a/spec/lib/gitlab/background_migration/extract_project_topics_into_separate_table_spec.rb b/spec/lib/gitlab/background_migration/extract_project_topics_into_separate_table_spec.rb new file mode 100644 index 00000000000..a111007a984 --- /dev/null +++ b/spec/lib/gitlab/background_migration/extract_project_topics_into_separate_table_spec.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::BackgroundMigration::ExtractProjectTopicsIntoSeparateTable, schema: 20210730104800 do + it 'correctly extracts project topics into separate table' do + namespaces = table(:namespaces) + projects = table(:projects) + taggings = table(:taggings) + tags = table(:tags) + project_topics = table(:project_topics) + topics = table(:topics) + + namespace = namespaces.create!(name: 'foo', path: 'foo') + project = projects.create!(namespace_id: namespace.id) + tag_1 = tags.create!(name: 'Topic1') + tag_2 = tags.create!(name: 'Topic2') + tag_3 = tags.create!(name: 'Topic3') + topic_3 = topics.create!(name: 'Topic3') + tagging_1 = taggings.create!(taggable_type: 'Project', taggable_id: project.id, context: 'topics', tag_id: tag_1.id) + tagging_2 = taggings.create!(taggable_type: 'Project', taggable_id: project.id, context: 'topics', tag_id: tag_2.id) + other_tagging = taggings.create!(taggable_type: 'Other', taggable_id: project.id, context: 'topics', tag_id: tag_1.id) + tagging_3 = taggings.create!(taggable_type: 'Project', taggable_id: project.id, context: 'topics', tag_id: tag_3.id) + tagging_4 = taggings.create!(taggable_type: 'Project', taggable_id: -1, context: 'topics', tag_id: tag_1.id) + tagging_5 = taggings.create!(taggable_type: 'Project', taggable_id: project.id, context: 'topics', tag_id: -1) + + subject.perform(tagging_1.id, tagging_5.id) + + # Tagging records + expect { tagging_1.reload }.to raise_error(ActiveRecord::RecordNotFound) + expect { tagging_2.reload }.to raise_error(ActiveRecord::RecordNotFound) + expect { other_tagging.reload }.not_to raise_error(ActiveRecord::RecordNotFound) + expect { tagging_3.reload }.to raise_error(ActiveRecord::RecordNotFound) + expect { tagging_4.reload }.to raise_error(ActiveRecord::RecordNotFound) + expect { tagging_5.reload }.to raise_error(ActiveRecord::RecordNotFound) + + # Topic records + topic_1 = topics.find_by(name: 'Topic1') + topic_2 = topics.find_by(name: 'Topic2') + expect(topics.all).to contain_exactly(topic_1, topic_2, topic_3) + + # ProjectTopic records + expect(project_topics.all.map(&:topic_id)).to contain_exactly(topic_1.id, topic_2.id, topic_3.id) + end +end diff --git a/spec/lib/gitlab/background_migration/migrate_merge_request_diff_commit_users_spec.rb b/spec/lib/gitlab/background_migration/migrate_merge_request_diff_commit_users_spec.rb index 496ce151032..91e8dcdf880 100644 --- a/spec/lib/gitlab/background_migration/migrate_merge_request_diff_commit_users_spec.rb +++ b/spec/lib/gitlab/background_migration/migrate_merge_request_diff_commit_users_spec.rb @@ -91,6 +91,18 @@ RSpec.describe Gitlab::BackgroundMigration::MigrateMergeRequestDiffCommitUsers d end describe '#perform' do + it 'skips jobs that have already been completed' do + Gitlab::Database::BackgroundMigrationJob.create!( + class_name: 'MigrateMergeRequestDiffCommitUsers', + arguments: [1, 10], + status: :succeeded + ) + + expect(migration).not_to receive(:get_data_to_update) + + migration.perform(1, 10) + end + it 'migrates the data in the range' do commits.create!( merge_request_diff_id: diff.id, diff --git a/spec/lib/gitlab/background_migration/migrate_pages_metadata_spec.rb b/spec/lib/gitlab/background_migration/migrate_pages_metadata_spec.rb index 906a6a747c9..815dc2e73e5 100644 --- a/spec/lib/gitlab/background_migration/migrate_pages_metadata_spec.rb +++ b/spec/lib/gitlab/background_migration/migrate_pages_metadata_spec.rb @@ -7,7 +7,7 @@ RSpec.describe Gitlab::BackgroundMigration::MigratePagesMetadata, schema: 201909 subject(:migrate_pages_metadata) { described_class.new } - describe '#perform_on_relation' do + describe '#perform' do let(:namespaces) { table(:namespaces) } let(:builds) { table(:ci_builds) } let(:pages_metadata) { table(:project_pages_metadata) } @@ -23,9 +23,9 @@ RSpec.describe Gitlab::BackgroundMigration::MigratePagesMetadata, schema: 201909 not_migrated_no_pages = projects.create!(namespace_id: namespace.id, name: 'Not Migrated No Pages') project_not_in_relation_scope = projects.create!(namespace_id: namespace.id, name: 'Other') - projects_relation = projects.where(id: [not_migrated_with_pages, not_migrated_no_pages, migrated]) + ids = [not_migrated_no_pages.id, not_migrated_with_pages.id, migrated.id] - migrate_pages_metadata.perform_on_relation(projects_relation) + migrate_pages_metadata.perform(ids.min, ids.max) expect(pages_metadata.find_by_project_id(not_migrated_with_pages.id).deployed).to eq(true) expect(pages_metadata.find_by_project_id(not_migrated_no_pages.id).deployed).to eq(false) @@ -33,12 +33,4 @@ RSpec.describe Gitlab::BackgroundMigration::MigratePagesMetadata, schema: 201909 expect(pages_metadata.find_by_project_id(project_not_in_relation_scope.id)).to be_nil end end - - describe '#perform' do - it 'creates relation and delegates to #perform_on_relation' do - expect(migrate_pages_metadata).to receive(:perform_on_relation).with(projects.where(id: 3..5)) - - migrate_pages_metadata.perform(3, 5) - end - end end diff --git a/spec/lib/gitlab/background_migration/steal_migrate_merge_request_diff_commit_users_spec.rb b/spec/lib/gitlab/background_migration/steal_migrate_merge_request_diff_commit_users_spec.rb new file mode 100644 index 00000000000..f2fb2ab6b6e --- /dev/null +++ b/spec/lib/gitlab/background_migration/steal_migrate_merge_request_diff_commit_users_spec.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::BackgroundMigration::StealMigrateMergeRequestDiffCommitUsers do + let(:migration) { described_class.new } + + describe '#perform' do + it 'processes the background migration' do + spy = instance_spy( + Gitlab::BackgroundMigration::MigrateMergeRequestDiffCommitUsers + ) + + allow(Gitlab::BackgroundMigration::MigrateMergeRequestDiffCommitUsers) + .to receive(:new) + .and_return(spy) + + expect(spy).to receive(:perform).with(1, 4) + expect(migration).to receive(:schedule_next_job) + + migration.perform(1, 4) + end + end + + describe '#schedule_next_job' do + it 'schedules the next job in ascending order' do + Gitlab::Database::BackgroundMigrationJob.create!( + class_name: 'MigrateMergeRequestDiffCommitUsers', + arguments: [10, 20] + ) + + Gitlab::Database::BackgroundMigrationJob.create!( + class_name: 'MigrateMergeRequestDiffCommitUsers', + arguments: [40, 50] + ) + + expect(BackgroundMigrationWorker) + .to receive(:perform_in) + .with(5.minutes, 'StealMigrateMergeRequestDiffCommitUsers', [10, 20]) + + migration.schedule_next_job + end + + it 'does not schedule any new jobs when there are none' do + expect(BackgroundMigrationWorker).not_to receive(:perform_in) + + migration.schedule_next_job + end + end +end |