From 902a5f8dbf39d11847a6791b5dd62d0ccbc5be84 Mon Sep 17 00:00:00 2001 From: Maxime Orefice Date: Wed, 21 Dec 2022 09:45:23 +0100 Subject: [PATCH] Add FK including partition_id to ci_build_needs Changelog: added --- ...uild_needs_on_partition_id_and_build_id.rb | 17 +++++++++ ...uild_needs_on_partition_id_and_build_id.rb | 37 +++++++++++++++++++ db/schema_migrations/20230130103957 | 1 + db/schema_migrations/20230130103958 | 1 + db/structure.sql | 5 +++ 5 files changed, 61 insertions(+) create mode 100644 db/post_migrate/20230130103957_add_fk_index_to_ci_build_needs_on_partition_id_and_build_id.rb create mode 100644 db/post_migrate/20230130103958_add_fk_to_ci_build_needs_on_partition_id_and_build_id.rb create mode 100644 db/schema_migrations/20230130103957 create mode 100644 db/schema_migrations/20230130103958 diff --git a/db/post_migrate/20230130103957_add_fk_index_to_ci_build_needs_on_partition_id_and_build_id.rb b/db/post_migrate/20230130103957_add_fk_index_to_ci_build_needs_on_partition_id_and_build_id.rb new file mode 100644 index 00000000000000..cea4070a8be988 --- /dev/null +++ b/db/post_migrate/20230130103957_add_fk_index_to_ci_build_needs_on_partition_id_and_build_id.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class AddFkIndexToCiBuildNeedsOnPartitionIdAndBuildId < Gitlab::Database::Migration[2.1] + disable_ddl_transaction! + + INDEX_NAME = :index_ci_build_needs_on_partition_id_build_id + TABLE_NAME = :ci_build_needs + COLUMNS = [:partition_id, :build_id] + + def up + add_concurrent_index(TABLE_NAME, COLUMNS, name: INDEX_NAME) + end + + def down + remove_concurrent_index_by_name(TABLE_NAME, INDEX_NAME) + end +end diff --git a/db/post_migrate/20230130103958_add_fk_to_ci_build_needs_on_partition_id_and_build_id.rb b/db/post_migrate/20230130103958_add_fk_to_ci_build_needs_on_partition_id_and_build_id.rb new file mode 100644 index 00000000000000..46985f12ff9bb1 --- /dev/null +++ b/db/post_migrate/20230130103958_add_fk_to_ci_build_needs_on_partition_id_and_build_id.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +class AddFkToCiBuildNeedsOnPartitionIdAndBuildId < Gitlab::Database::Migration[2.1] + disable_ddl_transaction! + + SOURCE_TABLE_NAME = :ci_build_needs + TARGET_TABLE_NAME = :ci_builds + COLUMN = :build_id + TARGET_COLUMN = :id + FK_NAME = :fk_rails_3cf221d4ed_p + PARTITION_COLUMN = :partition_id + + def up + add_concurrent_foreign_key( + SOURCE_TABLE_NAME, + TARGET_TABLE_NAME, + column: [PARTITION_COLUMN, COLUMN], + target_column: [PARTITION_COLUMN, TARGET_COLUMN], + validate: false, + reverse_lock_order: true, + on_update: :cascade, + on_delete: :cascade, + name: FK_NAME + ) + end + + def down + with_lock_retries do + remove_foreign_key_if_exists( + SOURCE_TABLE_NAME, + TARGET_TABLE_NAME, + name: FK_NAME, + reverse_lock_order: true + ) + end + end +end diff --git a/db/schema_migrations/20230130103957 b/db/schema_migrations/20230130103957 new file mode 100644 index 00000000000000..ace3e7f1a8d8ff --- /dev/null +++ b/db/schema_migrations/20230130103957 @@ -0,0 +1 @@ +17e3e3236ba71778e86a4df672ba2b7080a127658792b60669738fdad4fe2f36 \ No newline at end of file diff --git a/db/schema_migrations/20230130103958 b/db/schema_migrations/20230130103958 new file mode 100644 index 00000000000000..ffc3d56dfc7d68 --- /dev/null +++ b/db/schema_migrations/20230130103958 @@ -0,0 +1 @@ +0678dd7f9e8ad7c7e5761b7b624340fdf7785688d41badfb4affc8a2e2181072 \ No newline at end of file diff --git a/db/structure.sql b/db/structure.sql index a7e230d4cf1b38..e7c62867349500 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -28977,6 +28977,8 @@ CREATE UNIQUE INDEX index_chat_teams_on_namespace_id ON chat_teams USING btree ( CREATE UNIQUE INDEX index_ci_build_needs_on_build_id_and_name ON ci_build_needs USING btree (build_id, name); +CREATE INDEX index_ci_build_needs_on_partition_id_build_id ON ci_build_needs USING btree (partition_id, build_id); + CREATE UNIQUE INDEX index_ci_build_pending_states_on_build_id ON ci_build_pending_states USING btree (build_id); CREATE INDEX index_ci_build_pending_states_on_partition_id_build_id ON ci_build_pending_states USING btree (partition_id, build_id); @@ -34822,6 +34824,9 @@ ALTER TABLE ONLY chat_teams ALTER TABLE ONLY ci_build_needs ADD CONSTRAINT fk_rails_3cf221d4ed FOREIGN KEY (build_id) REFERENCES ci_builds(id) ON DELETE CASCADE; +ALTER TABLE ONLY ci_build_needs + ADD CONSTRAINT fk_rails_3cf221d4ed_p FOREIGN KEY (partition_id, build_id) REFERENCES ci_builds(partition_id, id) ON UPDATE CASCADE ON DELETE CASCADE NOT VALID; + ALTER TABLE ONLY cluster_groups ADD CONSTRAINT fk_rails_3d28377556 FOREIGN KEY (group_id) REFERENCES namespaces(id) ON DELETE CASCADE; -- GitLab