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 0000000000000000000000000000000000000000..630483ee1f801947e97d98f6100c00892def6e98 --- /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 0000000000000000000000000000000000000000..17ae2ad1325c2e5cf8fa3f8444631ca872166b3c --- /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 0000000000000000000000000000000000000000..ab216ad946cdf2fe0df56d429edce540140d6428 --- /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 0000000000000000000000000000000000000000..e2b8672fb3f79e0ae8ef7e1a5376ad9399cdb316 --- /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 19171a90734ef471a3bc9f7475940bea0a584899..31af30c3c5fcba77f13bcc6ce081eca9ffcc275e 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 756cb23b7a4ef84d9fb5858e1d14f0fbd7f6b293..32097b003d9409fc2f77a6b4944d3089eacdb289 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],