From dd28c491d972b2392b166421066ca17c48a06fc4 Mon Sep 17 00:00:00 2001 From: Marius Bobin Date: Fri, 27 Jan 2023 14:50:35 +0200 Subject: [PATCH] Validate and replace FK for ci_sources_pipelines and ci_builds Changelog: added --- ...s_source_partition_id_and_source_job_id.rb | 15 ++++++++ ...s_ci_sources_pipelines_on_source_job_id.rb | 35 +++++++++++++++++++ db/schema_migrations/20230227123949 | 1 + db/schema_migrations/20230227123950 | 1 + db/structure.sql | 5 +-- spec/db/schema_spec.rb | 2 +- 6 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 db/post_migrate/20230227123949_validate_fk_on_ci_sources_pipelines_source_partition_id_and_source_job_id.rb create mode 100644 db/post_migrate/20230227123950_remove_fk_to_ci_builds_ci_sources_pipelines_on_source_job_id.rb create mode 100644 db/schema_migrations/20230227123949 create mode 100644 db/schema_migrations/20230227123950 diff --git a/db/post_migrate/20230227123949_validate_fk_on_ci_sources_pipelines_source_partition_id_and_source_job_id.rb b/db/post_migrate/20230227123949_validate_fk_on_ci_sources_pipelines_source_partition_id_and_source_job_id.rb new file mode 100644 index 00000000000000..630483ee1f8019 --- /dev/null +++ b/db/post_migrate/20230227123949_validate_fk_on_ci_sources_pipelines_source_partition_id_and_source_job_id.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class ValidateFkOnCiSourcesPipelinesSourcePartitionIdAndSourceJobId < Gitlab::Database::Migration[2.1] + TABLE_NAME = :ci_sources_pipelines + FK_NAME = :fk_be5624bf37_p + COLUMNS = [:source_partition_id, :source_job_id] + + def up + validate_foreign_key(TABLE_NAME, COLUMNS, name: FK_NAME) + end + + def down + # no-op + end +end diff --git a/db/post_migrate/20230227123950_remove_fk_to_ci_builds_ci_sources_pipelines_on_source_job_id.rb b/db/post_migrate/20230227123950_remove_fk_to_ci_builds_ci_sources_pipelines_on_source_job_id.rb new file mode 100644 index 00000000000000..17ae2ad1325c2e --- /dev/null +++ b/db/post_migrate/20230227123950_remove_fk_to_ci_builds_ci_sources_pipelines_on_source_job_id.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +class RemoveFkToCiBuildsCiSourcesPipelinesOnSourceJobId < Gitlab::Database::Migration[2.1] + disable_ddl_transaction! + + SOURCE_TABLE_NAME = :ci_sources_pipelines + TARGET_TABLE_NAME = :ci_builds + COLUMN = :source_job_id + TARGET_COLUMN = :id + FK_NAME = :fk_be5624bf37 + + def up + with_lock_retries do + remove_foreign_key_if_exists( + SOURCE_TABLE_NAME, + TARGET_TABLE_NAME, + name: FK_NAME, + reverse_lock_order: true + ) + end + end + + def down + add_concurrent_foreign_key( + SOURCE_TABLE_NAME, + TARGET_TABLE_NAME, + column: COLUMN, + target_column: TARGET_COLUMN, + validate: true, + reverse_lock_order: true, + on_delete: :cascade, + name: FK_NAME + ) + end +end diff --git a/db/schema_migrations/20230227123949 b/db/schema_migrations/20230227123949 new file mode 100644 index 00000000000000..ab216ad946cdf2 --- /dev/null +++ b/db/schema_migrations/20230227123949 @@ -0,0 +1 @@ +cf72b9c6cd86bf0fbb0599f16bfcfd360567a8cdf30275ba59c1aeaba8317f2a \ No newline at end of file diff --git a/db/schema_migrations/20230227123950 b/db/schema_migrations/20230227123950 new file mode 100644 index 00000000000000..e2b8672fb3f79e --- /dev/null +++ b/db/schema_migrations/20230227123950 @@ -0,0 +1 @@ +d7f195e2cb4ab9f7f4637ba7667605eea02e66ea417b4ae496a1acae9931be84 \ No newline at end of file diff --git a/db/structure.sql b/db/structure.sql index 19171a90734ef4..31af30c3c5fcba 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -34650,10 +34650,7 @@ ALTER TABLE ONLY snippets ADD CONSTRAINT fk_be41fd4bb7 FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE; ALTER TABLE ONLY ci_sources_pipelines - ADD CONSTRAINT fk_be5624bf37 FOREIGN KEY (source_job_id) REFERENCES ci_builds(id) ON DELETE CASCADE; - -ALTER TABLE ONLY ci_sources_pipelines - ADD CONSTRAINT fk_be5624bf37_p FOREIGN KEY (source_partition_id, source_job_id) REFERENCES ci_builds(partition_id, id) ON UPDATE CASCADE ON DELETE CASCADE NOT VALID; + ADD CONSTRAINT fk_be5624bf37_p FOREIGN KEY (source_partition_id, source_job_id) REFERENCES ci_builds(partition_id, id) ON UPDATE CASCADE ON DELETE CASCADE; ALTER TABLE ONLY packages_maven_metadata ADD CONSTRAINT fk_be88aed360 FOREIGN KEY (package_id) REFERENCES packages_packages(id) ON DELETE CASCADE; diff --git a/spec/db/schema_spec.rb b/spec/db/schema_spec.rb index 756cb23b7a4ef8..32097b003d9409 100644 --- a/spec/db/schema_spec.rb +++ b/spec/db/schema_spec.rb @@ -49,7 +49,7 @@ ci_resources: %w[partition_id build_id], ci_runner_projects: %w[runner_id], ci_running_builds: %w[partition_id build_id], - ci_sources_pipelines: %w[partition_id source_partition_id], + ci_sources_pipelines: %w[partition_id source_partition_id source_job_id], ci_stages: %w[partition_id], ci_trigger_requests: %w[commit_id], ci_unit_test_failures: %w[partition_id build_id], -- GitLab