From 9a32f8f4471fff4316d0f08415a7d70d98840244 Mon Sep 17 00:00:00 2001 From: Marius Bobin Date: Mon, 6 Feb 2023 12:33:37 +0200 Subject: [PATCH] Validate and replace FK for ci_job_variables and ci_builds Changelog: added --- ...i_job_variables_partition_id_and_job_id.rb | 15 ++++++++ ...to_ci_builds_ci_job_variables_on_job_id.rb | 35 +++++++++++++++++++ db/schema_migrations/20230227153231 | 1 + db/schema_migrations/20230227153232 | 1 + db/structure.sql | 5 +-- spec/db/schema_spec.rb | 2 +- 6 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 db/post_migrate/20230227153231_validate_fk_on_ci_job_variables_partition_id_and_job_id.rb create mode 100644 db/post_migrate/20230227153232_remove_fk_to_ci_builds_ci_job_variables_on_job_id.rb create mode 100644 db/schema_migrations/20230227153231 create mode 100644 db/schema_migrations/20230227153232 diff --git a/db/post_migrate/20230227153231_validate_fk_on_ci_job_variables_partition_id_and_job_id.rb b/db/post_migrate/20230227153231_validate_fk_on_ci_job_variables_partition_id_and_job_id.rb new file mode 100644 index 00000000000000..035d26dbe94123 --- /dev/null +++ b/db/post_migrate/20230227153231_validate_fk_on_ci_job_variables_partition_id_and_job_id.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class ValidateFkOnCiJobVariablesPartitionIdAndJobId < Gitlab::Database::Migration[2.1] + TABLE_NAME = :ci_job_variables + FK_NAME = :fk_rails_fbf3b34792_p + COLUMNS = [:partition_id, :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/20230227153232_remove_fk_to_ci_builds_ci_job_variables_on_job_id.rb b/db/post_migrate/20230227153232_remove_fk_to_ci_builds_ci_job_variables_on_job_id.rb new file mode 100644 index 00000000000000..f5dae8b57e017c --- /dev/null +++ b/db/post_migrate/20230227153232_remove_fk_to_ci_builds_ci_job_variables_on_job_id.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +class RemoveFkToCiBuildsCiJobVariablesOnJobId < Gitlab::Database::Migration[2.1] + disable_ddl_transaction! + + SOURCE_TABLE_NAME = :ci_job_variables + TARGET_TABLE_NAME = :ci_builds + COLUMN = :job_id + TARGET_COLUMN = :id + FK_NAME = :fk_rails_fbf3b34792 + + 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/20230227153231 b/db/schema_migrations/20230227153231 new file mode 100644 index 00000000000000..18e685b52b09aa --- /dev/null +++ b/db/schema_migrations/20230227153231 @@ -0,0 +1 @@ +a4ae1dd4a14b977302c61d0731e9d350fbdc089a909ca3151fb4b9699b0efbb8 \ No newline at end of file diff --git a/db/schema_migrations/20230227153232 b/db/schema_migrations/20230227153232 new file mode 100644 index 00000000000000..c605a0f57542ee --- /dev/null +++ b/db/schema_migrations/20230227153232 @@ -0,0 +1 @@ +a9082aa6b8cb86be9534fe41db5790b6cca876e6fcc284daf8e5b3163b92bd06 \ No newline at end of file diff --git a/db/structure.sql b/db/structure.sql index 19171a90734ef4..ea5ed451f13469 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -36618,10 +36618,7 @@ ALTER TABLE ONLY serverless_domain_cluster ADD CONSTRAINT fk_rails_fbdba67eb1 FOREIGN KEY (creator_id) REFERENCES users(id) ON DELETE SET NULL; ALTER TABLE ONLY ci_job_variables - ADD CONSTRAINT fk_rails_fbf3b34792 FOREIGN KEY (job_id) REFERENCES ci_builds(id) ON DELETE CASCADE; - -ALTER TABLE ONLY ci_job_variables - ADD CONSTRAINT fk_rails_fbf3b34792_p FOREIGN KEY (partition_id, job_id) REFERENCES ci_builds(partition_id, id) ON UPDATE CASCADE ON DELETE CASCADE NOT VALID; + ADD CONSTRAINT fk_rails_fbf3b34792_p FOREIGN KEY (partition_id, job_id) REFERENCES ci_builds(partition_id, id) ON UPDATE CASCADE ON DELETE CASCADE; ALTER TABLE ONLY packages_nuget_metadata ADD CONSTRAINT fk_rails_fc0c19f5b4 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..300a6a2ca6eca2 100644 --- a/spec/db/schema_spec.rb +++ b/spec/db/schema_spec.rb @@ -41,7 +41,7 @@ ci_builds_runner_session: %w[partition_id build_id], p_ci_builds_metadata: %w[partition_id], ci_job_artifacts: %w[partition_id job_id], - ci_job_variables: %w[partition_id], + ci_job_variables: %w[partition_id job_id], ci_namespace_monthly_usages: %w[namespace_id], ci_pending_builds: %w[partition_id build_id], ci_pipeline_variables: %w[partition_id], -- GitLab