From e4384360a16dd9a19d4d2d25d0ef1f2b862ed2a6 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Wed, 19 Jul 2023 14:16:28 +0000 Subject: Add latest changes from gitlab-org/gitlab@16-2-stable-ee --- ...0523101514_finalize_user_type_migration_spec.rb | 9 +- ..._finalize_backfill_resource_link_events_spec.rb | 68 ++++++++++ ...int_backfill_is_finished_for_self_hosts_spec.rb | 25 ++++ ...i_build_needs_to_big_int_for_self_hosts_spec.rb | 146 +++++++++++++++++++++ ...npm_packages_on_project_id_name_version_spec.rb | 20 +++ ..._in_application_settings_for_gitlab_com_spec.rb | 44 +++++++ ..._in_application_settings_for_gitlab_com_spec.rb | 44 +++++++ ...3_queue_backfill_missing_ci_cd_settings_spec.rb | 26 ++++ ...ion_column_in_vulnerability_occurrences_spec.rb | 26 ++++ ...031_cleanup_project_pipeline_status_key_spec.rb | 12 ++ ...or_merge_request_metrics_for_self_hosts_spec.rb | 107 +++++++++++++++ ...deduplicate_inactive_alert_integrations_spec.rb | 71 ++++++++++ ...backfill_is_finished_for_gitlab_dot_com_spec.rb | 35 +++++ 13 files changed, 632 insertions(+), 1 deletion(-) create mode 100644 spec/migrations/20230530012406_finalize_backfill_resource_link_events_spec.rb create mode 100644 spec/migrations/20230613192703_ensure_ci_build_needs_big_int_backfill_is_finished_for_self_hosts_spec.rb create mode 100644 spec/migrations/20230613192703_swap_ci_build_needs_to_big_int_for_self_hosts_spec.rb create mode 100644 spec/migrations/20230616082958_add_unique_index_for_npm_packages_on_project_id_name_version_spec.rb create mode 100644 spec/migrations/20230621070810_update_requeue_workers_in_application_settings_for_gitlab_com_spec.rb create mode 100644 spec/migrations/20230621074611_update_elasticsearch_number_of_shards_in_application_settings_for_gitlab_com_spec.rb create mode 100644 spec/migrations/20230628023103_queue_backfill_missing_ci_cd_settings_spec.rb create mode 100644 spec/migrations/20230629095819_queue_backfill_uuid_conversion_column_in_vulnerability_occurrences_spec.rb create mode 100644 spec/migrations/20230703024031_cleanup_project_pipeline_status_key_spec.rb create mode 100644 spec/migrations/cleanup_bigint_conversion_for_merge_request_metrics_for_self_hosts_spec.rb create mode 100644 spec/migrations/deduplicate_inactive_alert_integrations_spec.rb create mode 100644 spec/migrations/ensure_events_bigint_backfill_is_finished_for_gitlab_dot_com_spec.rb (limited to 'spec/migrations') diff --git a/spec/migrations/20230523101514_finalize_user_type_migration_spec.rb b/spec/migrations/20230523101514_finalize_user_type_migration_spec.rb index abf3a506748..01c05c38098 100644 --- a/spec/migrations/20230523101514_finalize_user_type_migration_spec.rb +++ b/spec/migrations/20230523101514_finalize_user_type_migration_spec.rb @@ -5,7 +5,14 @@ require_migration! RSpec.describe FinalizeUserTypeMigration, feature_category: :devops_reports do it 'finalizes MigrateHumanUserType migration' do - expect(described_class).to be_finalize_background_migration_of('MigrateHumanUserType') + expect_next_instance_of(described_class) do |instance| + expect(instance).to receive(:ensure_batched_background_migration_is_finished).with( + job_class_name: 'MigrateHumanUserType', + table_name: :users, + column_name: :id, + job_arguments: [] + ) + end migrate! end diff --git a/spec/migrations/20230530012406_finalize_backfill_resource_link_events_spec.rb b/spec/migrations/20230530012406_finalize_backfill_resource_link_events_spec.rb new file mode 100644 index 00000000000..0298b470ac8 --- /dev/null +++ b/spec/migrations/20230530012406_finalize_backfill_resource_link_events_spec.rb @@ -0,0 +1,68 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_migration! + +RSpec.describe FinalizeBackfillResourceLinkEvents, feature_category: :team_planning do + let(:batched_migrations) { table(:batched_background_migrations) } + + context 'when migration is missing' do + before do + batched_migrations.where(job_class_name: described_class::MIGRATION).delete_all + end + + it 'warns migration not found' do + expect(Gitlab::AppLogger) + .to receive(:warn).with(/Could not find batched background migration for the given configuration:/) + .once + + migrate! + end + end + + context 'with migration present' do + let!(:batched_migration) do + batched_migrations.create!( + job_class_name: described_class::MIGRATION, + table_name: :system_note_metadata, + column_name: :id, + interval: 2.minutes, + min_value: 1, + max_value: 5, + batch_size: 5, + sub_batch_size: 5, + gitlab_schema: :gitlab_main, + status: status + ) + end + + context 'when migrations have finished' do + let(:status) { 3 } # finished enum value + + it 'does not raise an error' do + expect { migrate! }.not_to raise_error + end + end + + context 'with different migration statuses' do + using RSpec::Parameterized::TableSyntax + + where(:status, :description) do + 0 | 'paused' + 1 | 'active' + 4 | 'failed' + 5 | 'finalizing' + end + + with_them do + it 'finalizes the migration' do + expect do + migrate! + + batched_migration.reload + end.to change { batched_migration.status }.from(status).to(3) + end + end + end + end +end diff --git a/spec/migrations/20230613192703_ensure_ci_build_needs_big_int_backfill_is_finished_for_self_hosts_spec.rb b/spec/migrations/20230613192703_ensure_ci_build_needs_big_int_backfill_is_finished_for_self_hosts_spec.rb new file mode 100644 index 00000000000..0fe9cecb729 --- /dev/null +++ b/spec/migrations/20230613192703_ensure_ci_build_needs_big_int_backfill_is_finished_for_self_hosts_spec.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_migration! + +RSpec.describe EnsureCiBuildNeedsBigIntBackfillIsFinishedForSelfHosts, migration: :gitlab_ci, feature_category: :continuous_integration do + describe '#up' do + let(:migration_arguments) do + { + job_class_name: 'CopyColumnUsingBackgroundMigrationJob', + table_name: 'ci_build_needs', + column_name: 'id', + job_arguments: [['id'], ['id_convert_to_bigint']] + } + end + + it 'ensures the migration is completed' do + expect_next_instance_of(described_class) do |instance| + expect(instance).to receive(:ensure_batched_background_migration_is_finished).with(migration_arguments) + end + + migrate! + end + end +end diff --git a/spec/migrations/20230613192703_swap_ci_build_needs_to_big_int_for_self_hosts_spec.rb b/spec/migrations/20230613192703_swap_ci_build_needs_to_big_int_for_self_hosts_spec.rb new file mode 100644 index 00000000000..3db5d3b3c16 --- /dev/null +++ b/spec/migrations/20230613192703_swap_ci_build_needs_to_big_int_for_self_hosts_spec.rb @@ -0,0 +1,146 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_migration! + +RSpec.describe SwapCiBuildNeedsToBigIntForSelfHosts, feature_category: :continuous_integration do + after do + connection = described_class.new.connection + connection.execute('ALTER TABLE ci_build_needs DROP COLUMN IF EXISTS id_convert_to_bigint') + end + + describe '#up' do + context 'when on GitLab.com, dev, or test' do + before do + connection = described_class.new.connection + connection.execute('ALTER TABLE ci_build_needs ALTER COLUMN id TYPE bigint') + connection.execute('ALTER TABLE ci_build_needs DROP COLUMN IF EXISTS id_convert_to_bigint') + end + + it 'does not swap the columns' do + # rubocop: disable RSpec/AnyInstanceOf + allow_any_instance_of(described_class).to receive(:com_or_dev_or_test_but_not_jh?).and_return(true) + # rubocop: enable RSpec/AnyInstanceOf + + ci_build_needs = table(:ci_build_needs) + + disable_migrations_output do + reversible_migration do |migration| + migration.before -> { + ci_build_needs.reset_column_information + + expect(ci_build_needs.columns.find { |c| c.name == 'id' }.sql_type).to eq('bigint') + expect(ci_build_needs.columns.find { |c| c.name == 'id_convert_to_bigint' }).to be nil + } + + migration.after -> { + ci_build_needs.reset_column_information + + expect(ci_build_needs.columns.find { |c| c.name == 'id' }.sql_type).to eq('bigint') + expect(ci_build_needs.columns.find { |c| c.name == 'id_convert_to_bigint' }).to be nil + } + end + end + end + end + + context 'when a self-hosted installation has already completed the swap' do + before do + connection = described_class.new.connection + connection.execute('ALTER TABLE ci_build_needs ALTER COLUMN id TYPE bigint') + connection.execute('ALTER TABLE ci_build_needs ADD COLUMN IF NOT EXISTS id_convert_to_bigint integer') + end + + it 'does not swap the columns' do + # rubocop: disable RSpec/AnyInstanceOf + allow_any_instance_of(described_class).to receive(:com_or_dev_or_test_but_not_jh?).and_return(false) + # rubocop: enable RSpec/AnyInstanceOf + + ci_build_needs = table(:ci_build_needs) + + migrate! + + expect(ci_build_needs.columns.find { |c| c.name == 'id' }.sql_type).to eq('bigint') + expect(ci_build_needs.columns.find do |c| + c.name == 'id_convert_to_bigint' + end.sql_type).to eq('integer') + end + end + + context 'when a self-hosted installation has the `id_convert_to_bigint` column already dropped' do + before do + connection = described_class.new.connection + connection.execute('ALTER TABLE ci_build_needs ALTER COLUMN id TYPE bigint') + connection.execute('ALTER TABLE ci_build_needs DROP COLUMN IF EXISTS id_convert_to_bigint') + end + + it 'does not swap the columns' do + # rubocop: disable RSpec/AnyInstanceOf + allow_any_instance_of(described_class).to receive(:com_or_dev_or_test_but_not_jh?).and_return(false) + # rubocop: enable RSpec/AnyInstanceOf + + ci_build_needs = table(:ci_build_needs) + + disable_migrations_output do + reversible_migration do |migration| + migration.before -> { + ci_build_needs.reset_column_information + + expect(ci_build_needs.columns.find { |c| c.name == 'id' }.sql_type).to eq('bigint') + expect(ci_build_needs.columns.find { |c| c.name == 'id_convert_to_bigint' }).to be nil + } + + migration.after -> { + ci_build_needs.reset_column_information + + expect(ci_build_needs.columns.find { |c| c.name == 'id' }.sql_type).to eq('bigint') + expect(ci_build_needs.columns.find { |c| c.name == 'id_convert_to_bigint' }).to be nil + } + end + end + end + end + + context 'when an installation is self-hosted' do + before do + connection = described_class.new.connection + connection.execute('ALTER TABLE ci_build_needs ALTER COLUMN id TYPE integer') + connection.execute('ALTER TABLE ci_build_needs ADD COLUMN IF NOT EXISTS id_convert_to_bigint bigint') + connection.execute('ALTER TABLE ci_build_needs ALTER COLUMN id_convert_to_bigint TYPE bigint') + connection.execute('DROP INDEX IF EXISTS index_ci_build_needs_on_id_convert_to_bigint') + connection.execute('CREATE OR REPLACE FUNCTION trigger_3207b8d0d6f3() RETURNS trigger LANGUAGE plpgsql AS $$ + BEGIN NEW."id_convert_to_bigint" := NEW."id"; RETURN NEW; END; $$;') + end + + it 'swaps the columns' do + # rubocop: disable RSpec/AnyInstanceOf + allow_any_instance_of(described_class).to receive(:com_or_dev_or_test_but_not_jh?).and_return(false) + # rubocop: enable RSpec/AnyInstanceOf + + ci_build_needs = table(:ci_build_needs) + + disable_migrations_output do + reversible_migration do |migration| + migration.before -> { + ci_build_needs.reset_column_information + + expect(ci_build_needs.columns.find { |c| c.name == 'id' }.sql_type).to eq('integer') + expect(ci_build_needs.columns.find do |c| + c.name == 'id_convert_to_bigint' + end.sql_type).to eq('bigint') + } + + migration.after -> { + ci_build_needs.reset_column_information + + expect(ci_build_needs.columns.find { |c| c.name == 'id' }.sql_type).to eq('bigint') + expect(ci_build_needs.columns.find do |c| + c.name == 'id_convert_to_bigint' + end.sql_type).to eq('integer') + } + end + end + end + end + end +end diff --git a/spec/migrations/20230616082958_add_unique_index_for_npm_packages_on_project_id_name_version_spec.rb b/spec/migrations/20230616082958_add_unique_index_for_npm_packages_on_project_id_name_version_spec.rb new file mode 100644 index 00000000000..c7c97b16f97 --- /dev/null +++ b/spec/migrations/20230616082958_add_unique_index_for_npm_packages_on_project_id_name_version_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_migration! + +RSpec.describe AddUniqueIndexForNpmPackagesOnProjectIdNameVersion, feature_category: :package_registry do + it 'schedules an index creation' do + reversible_migration do |migration| + migration.before -> { + expect(ActiveRecord::Base.connection.indexes('packages_packages').map(&:name)) + .not_to include('idx_packages_on_project_id_name_version_unique_when_npm') + } + + migration.after -> { + expect(ActiveRecord::Base.connection.indexes('packages_packages').map(&:name)) + .to include('idx_packages_on_project_id_name_version_unique_when_npm') + } + end + end +end diff --git a/spec/migrations/20230621070810_update_requeue_workers_in_application_settings_for_gitlab_com_spec.rb b/spec/migrations/20230621070810_update_requeue_workers_in_application_settings_for_gitlab_com_spec.rb new file mode 100644 index 00000000000..763d9ea610c --- /dev/null +++ b/spec/migrations/20230621070810_update_requeue_workers_in_application_settings_for_gitlab_com_spec.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_migration! + +RSpec.describe UpdateRequeueWorkersInApplicationSettingsForGitlabCom, feature_category: :global_search do + let(:settings) { table(:application_settings) } + + describe "#up" do + it 'does nothing' do + record = settings.create! + + expect { migrate! }.not_to change { record.reload.elasticsearch_requeue_workers } + end + + it 'updates elasticsearch_requeue_workers when gitlab.com' do + allow(Gitlab).to receive(:com?).and_return(true) + + record = settings.create! + + expect { migrate! }.to change { record.reload.elasticsearch_requeue_workers }.from(false).to(true) + end + end + + describe "#down" do + it 'does nothing' do + record = settings.create!(elasticsearch_requeue_workers: true) + + migrate! + + expect { schema_migrate_down! }.not_to change { record.reload.elasticsearch_requeue_workers } + end + + it 'updates elasticsearch_requeue_workers when gitlab.com' do + allow(Gitlab).to receive(:com?).and_return(true) + + record = settings.create!(elasticsearch_requeue_workers: true) + + migrate! + + expect { schema_migrate_down! }.to change { record.reload.elasticsearch_requeue_workers }.from(true).to(false) + end + end +end diff --git a/spec/migrations/20230621074611_update_elasticsearch_number_of_shards_in_application_settings_for_gitlab_com_spec.rb b/spec/migrations/20230621074611_update_elasticsearch_number_of_shards_in_application_settings_for_gitlab_com_spec.rb new file mode 100644 index 00000000000..80a1f9f59e2 --- /dev/null +++ b/spec/migrations/20230621074611_update_elasticsearch_number_of_shards_in_application_settings_for_gitlab_com_spec.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_migration! + +RSpec.describe UpdateElasticsearchNumberOfShardsInApplicationSettingsForGitlabCom, feature_category: :global_search do + let(:settings) { table(:application_settings) } + + describe "#up" do + it 'does nothing when not in gitlab.com' do + record = settings.create! + + expect { migrate! }.not_to change { record.reload.elasticsearch_worker_number_of_shards } + end + + it 'updates elasticsearch_worker_number_of_shards when gitlab.com' do + allow(Gitlab).to receive(:com?).and_return(true) + + record = settings.create! + + expect { migrate! }.to change { record.reload.elasticsearch_worker_number_of_shards }.from(2).to(16) + end + end + + describe "#down" do + it 'does nothing when not in gitlab.com' do + record = settings.create!(elasticsearch_worker_number_of_shards: 16) + + migrate! + + expect { schema_migrate_down! }.not_to change { record.reload.elasticsearch_worker_number_of_shards } + end + + it 'updates elasticsearch_worker_number_of_shards when gitlab.com' do + allow(Gitlab).to receive(:com?).and_return(true) + + record = settings.create!(elasticsearch_worker_number_of_shards: 16) + + migrate! + + expect { schema_migrate_down! }.to change { record.reload.elasticsearch_worker_number_of_shards }.from(16).to(2) + end + end +end diff --git a/spec/migrations/20230628023103_queue_backfill_missing_ci_cd_settings_spec.rb b/spec/migrations/20230628023103_queue_backfill_missing_ci_cd_settings_spec.rb new file mode 100644 index 00000000000..f6c470260ff --- /dev/null +++ b/spec/migrations/20230628023103_queue_backfill_missing_ci_cd_settings_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_migration! + +RSpec.describe QueueBackfillMissingCiCdSettings, feature_category: :source_code_management do + let!(:batched_migration) { described_class::MIGRATION } + + it 'schedules a new batched migration' do + reversible_migration do |migration| + migration.before -> { + expect(batched_migration).not_to have_scheduled_batched_migration + } + + migration.after -> { + expect(batched_migration).to have_scheduled_batched_migration( + table_name: :projects, + column_name: :id, + interval: described_class::DELAY_INTERVAL, + batch_size: described_class::BATCH_SIZE, + sub_batch_size: described_class::SUB_BATCH_SIZE + ) + } + end + end +end diff --git a/spec/migrations/20230629095819_queue_backfill_uuid_conversion_column_in_vulnerability_occurrences_spec.rb b/spec/migrations/20230629095819_queue_backfill_uuid_conversion_column_in_vulnerability_occurrences_spec.rb new file mode 100644 index 00000000000..eb9a131008a --- /dev/null +++ b/spec/migrations/20230629095819_queue_backfill_uuid_conversion_column_in_vulnerability_occurrences_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_migration! + +RSpec.describe QueueBackfillUuidConversionColumnInVulnerabilityOccurrences, feature_category: :vulnerability_management do + let!(:batched_migration) { described_class::MIGRATION } + + it 'schedules a new batched migration' do + reversible_migration do |migration| + migration.before -> { + expect(batched_migration).not_to have_scheduled_batched_migration + } + + migration.after -> { + expect(batched_migration).to have_scheduled_batched_migration( + table_name: :vulnerability_occurrences, + column_name: :id, + interval: described_class::DELAY_INTERVAL, + batch_size: described_class::BATCH_SIZE, + sub_batch_size: described_class::SUB_BATCH_SIZE + ) + } + end + end +end diff --git a/spec/migrations/20230703024031_cleanup_project_pipeline_status_key_spec.rb b/spec/migrations/20230703024031_cleanup_project_pipeline_status_key_spec.rb new file mode 100644 index 00000000000..4232162134a --- /dev/null +++ b/spec/migrations/20230703024031_cleanup_project_pipeline_status_key_spec.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_migration! + +RSpec.describe CleanupProjectPipelineStatusKey, feature_category: :redis do + it 'enqueues a RedisMigrationWorker job from cursor 0' do + expect(RedisMigrationWorker).to receive(:perform_async).with('BackfillProjectPipelineStatusTtl', '0') + + migrate! + end +end diff --git a/spec/migrations/cleanup_bigint_conversion_for_merge_request_metrics_for_self_hosts_spec.rb b/spec/migrations/cleanup_bigint_conversion_for_merge_request_metrics_for_self_hosts_spec.rb new file mode 100644 index 00000000000..dc4adae9138 --- /dev/null +++ b/spec/migrations/cleanup_bigint_conversion_for_merge_request_metrics_for_self_hosts_spec.rb @@ -0,0 +1,107 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_migration! + +RSpec.describe CleanupBigintConversionForMergeRequestMetricsForSelfHosts, feature_category: :database do + after do + connection = described_class.new.connection + connection.execute('ALTER TABLE merge_request_metrics DROP COLUMN IF EXISTS id_convert_to_bigint') + end + + describe '#up' do + context 'when is GitLab.com, dev, or test' do + before do + # As we call `schema_migrate_down!` before each example, and for this migration + # `#down` is same as `#up`, we need to ensure we start from the expected state. + connection = described_class.new.connection + connection.execute('ALTER TABLE merge_request_metrics DROP COLUMN IF EXISTS id_convert_to_bigint') + end + + it 'does nothing' do + # rubocop: disable RSpec/AnyInstanceOf + allow_any_instance_of(described_class).to receive(:com_or_dev_or_test_but_not_jh?).and_return(true) + # rubocop: enable RSpec/AnyInstanceOf + + merge_request_metrics = table(:merge_request_metrics) + + disable_migrations_output do + reversible_migration do |migration| + migration.before -> { + merge_request_metrics.reset_column_information + + expect(merge_request_metrics.columns.find { |c| c.name == 'id_convert_to_bigint' }).to be nil + } + + migration.after -> { + merge_request_metrics.reset_column_information + + expect(merge_request_metrics.columns.find { |c| c.name == 'id_convert_to_bigint' }).to be nil + } + end + end + end + end + + context 'when is a self-host customer with the temporary column already dropped' do + before do + # As we call `schema_migrate_down!` before each example, and for this migration + # `#down` is same as `#up`, we need to ensure we start from the expected state. + connection = described_class.new.connection + connection.execute('ALTER TABLE merge_request_metrics ALTER COLUMN id TYPE bigint') + connection.execute('ALTER TABLE merge_request_metrics DROP COLUMN IF EXISTS id_convert_to_bigint') + end + + it 'does nothing' do + # rubocop: disable RSpec/AnyInstanceOf + allow_any_instance_of(described_class).to receive(:com_or_dev_or_test_but_not_jh?).and_return(false) + # rubocop: enable RSpec/AnyInstanceOf + + merge_request_metrics = table(:merge_request_metrics) + + migrate! + + expect(merge_request_metrics.columns.find { |c| c.name == 'id' }.sql_type).to eq('bigint') + expect(merge_request_metrics.columns.find { |c| c.name == 'id_convert_to_bigint' }).to be nil + end + end + + context 'when is a self-host with the temporary columns' do + before do + # As we call `schema_migrate_down!` before each example, and for this migration + # `#down` is same as `#up`, we need to ensure we start from the expected state. + connection = described_class.new.connection + connection.execute('ALTER TABLE merge_request_metrics ALTER COLUMN id TYPE bigint') + connection.execute('ALTER TABLE merge_request_metrics ADD COLUMN IF NOT EXISTS id_convert_to_bigint integer') + end + + it 'drop the temporary columns' do + # rubocop: disable RSpec/AnyInstanceOf + allow_any_instance_of(described_class).to receive(:com_or_dev_or_test_but_not_jh?).and_return(false) + # rubocop: enable RSpec/AnyInstanceOf + + merge_request_metrics = table(:merge_request_metrics) + + disable_migrations_output do + reversible_migration do |migration| + migration.before -> { + merge_request_metrics.reset_column_information + + expect(merge_request_metrics.columns.find { |c| c.name == 'id' }.sql_type).to eq('bigint') + expect(merge_request_metrics.columns.find do |c| + c.name == 'id_convert_to_bigint' + end.sql_type).to eq('integer') + } + + migration.after -> { + merge_request_metrics.reset_column_information + + expect(merge_request_metrics.columns.find { |c| c.name == 'id' }.sql_type).to eq('bigint') + expect(merge_request_metrics.columns.find { |c| c.name == 'id_convert_to_bigint' }).to be nil + } + end + end + end + end + end +end diff --git a/spec/migrations/deduplicate_inactive_alert_integrations_spec.rb b/spec/migrations/deduplicate_inactive_alert_integrations_spec.rb new file mode 100644 index 00000000000..7f963a9bd0a --- /dev/null +++ b/spec/migrations/deduplicate_inactive_alert_integrations_spec.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_migration! + +RSpec.describe DeduplicateInactiveAlertIntegrations, feature_category: :incident_management do + let!(:namespace_class) { table(:namespaces) } + let!(:project_class) { table(:projects) } + let!(:integration_class) { table(:alert_management_http_integrations) } + + let!(:namespace_0) { namespace_class.create!(name: 'namespace1', path: 'namespace1') } + let!(:namespace_1) { namespace_class.create!(name: 'namespace2', path: 'namespace2') } + let!(:namespace_2) { namespace_class.create!(name: 'namespace3', path: 'namespace3') } + + let!(:project_with_inactive_duplicate) { create_project(namespace_0, namespace_0) } + let!(:project_with_multiple_duplicates) { create_project(namespace_0, namespace_1) } + let!(:project_without_duplicates) { create_project(namespace_0, namespace_2) } + + let!(:integrations) do + [ + create_integration(project_with_inactive_duplicate, 'default'), + create_integration(project_with_inactive_duplicate, 'other'), + create_integration(project_with_inactive_duplicate, 'other', active: false), + create_integration(project_with_multiple_duplicates, 'default', active: false), + create_integration(project_with_multiple_duplicates, 'default', active: false), + create_integration(project_with_multiple_duplicates, 'other', active: false), + create_integration(project_with_multiple_duplicates, 'other'), + create_integration(project_without_duplicates, 'default'), + create_integration(project_without_duplicates, 'other', active: false) + ] + end + + describe '#up' do + it 'updates the endpoint identifier of duplicate inactive integrations' do + expect { migrate! } + .to not_change { integrations[0].reload } + .and not_change { integrations[1].reload } + .and not_change { integrations[6].reload } + .and not_change { integrations[7].reload } + .and not_change { integrations[8].reload } + + expect { integrations[2].reload }.to raise_error(ActiveRecord::RecordNotFound) + expect { integrations[3].reload }.to raise_error(ActiveRecord::RecordNotFound) + expect { integrations[4].reload }.to raise_error(ActiveRecord::RecordNotFound) + expect { integrations[5].reload }.to raise_error(ActiveRecord::RecordNotFound) + + endpoints = integration_class.pluck(:endpoint_identifier, :project_id) + expect(endpoints.uniq).to match_array(endpoints) + end + end + + private + + def create_integration(project, endpoint_identifier, active: true) + integration_class.create!( + project_id: project.id, + endpoint_identifier: endpoint_identifier, + active: active, + encrypted_token_iv: 'iv', + encrypted_token: 'token', + name: "HTTP Integration - #{endpoint_identifier}" + ) + end + + def create_project(namespace, project_namespace) + project_class.create!( + namespace_id: namespace.id, + project_namespace_id: project_namespace.id + ) + end +end diff --git a/spec/migrations/ensure_events_bigint_backfill_is_finished_for_gitlab_dot_com_spec.rb b/spec/migrations/ensure_events_bigint_backfill_is_finished_for_gitlab_dot_com_spec.rb new file mode 100644 index 00000000000..6694b690d30 --- /dev/null +++ b/spec/migrations/ensure_events_bigint_backfill_is_finished_for_gitlab_dot_com_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_migration! + +RSpec.describe EnsureEventsBigintBackfillIsFinishedForGitlabDotCom, feature_category: :database do + describe '#up' do + let(:migration_arguments) do + { + job_class_name: 'CopyColumnUsingBackgroundMigrationJob', + table_name: 'events', + column_name: 'id', + job_arguments: [['target_id'], ['target_id_convert_to_bigint']] + } + end + + it 'ensures the migration is completed for GitLab.com, dev, or test' do + expect_next_instance_of(described_class) do |instance| + expect(instance).to receive(:com_or_dev_or_test_but_not_jh?).and_return(true) + expect(instance).to receive(:ensure_batched_background_migration_is_finished).with(migration_arguments) + end + + migrate! + end + + it 'skips the check for other instances' do + expect_next_instance_of(described_class) do |instance| + expect(instance).to receive(:com_or_dev_or_test_but_not_jh?).and_return(false) + expect(instance).not_to receive(:ensure_batched_background_migration_is_finished) + end + + migrate! + end + end +end -- cgit v1.2.3