From 856e616e652ef4b560bd02edd3912299f0119060 Mon Sep 17 00:00:00 2001 From: Maxime Orefice Date: Thu, 19 Jan 2023 10:17:14 +0100 Subject: [PATCH 1/4] Add FK to ci_pending_builds Changelog: added --- ...119090944_add_index_to_ci_pending_build.rb | 17 ++++++++++ ...027_add_foreign_key_to_ci_pending_build.rb | 31 +++++++++++++++++++ db/schema_migrations/20230119090944 | 1 + db/schema_migrations/20230119091027 | 1 + db/structure.sql | 4 +++ 5 files changed, 54 insertions(+) create mode 100644 db/post_migrate/20230119090944_add_index_to_ci_pending_build.rb create mode 100644 db/post_migrate/20230119091027_add_foreign_key_to_ci_pending_build.rb create mode 100644 db/schema_migrations/20230119090944 create mode 100644 db/schema_migrations/20230119091027 diff --git a/db/post_migrate/20230119090944_add_index_to_ci_pending_build.rb b/db/post_migrate/20230119090944_add_index_to_ci_pending_build.rb new file mode 100644 index 00000000000000..5976dee1eb135d --- /dev/null +++ b/db/post_migrate/20230119090944_add_index_to_ci_pending_build.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class AddIndexToCiPendingBuild < Gitlab::Database::Migration[2.1] + disable_ddl_transaction! + + INDEX_NAME = :index_ci_pending_builds_on_partition_id_build_id + TABLE_NAME = :ci_pending_builds + 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/20230119091027_add_foreign_key_to_ci_pending_build.rb b/db/post_migrate/20230119091027_add_foreign_key_to_ci_pending_build.rb new file mode 100644 index 00000000000000..9f7f807e7383cb --- /dev/null +++ b/db/post_migrate/20230119091027_add_foreign_key_to_ci_pending_build.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +class AddForeignKeyToCiPendingBuild < Gitlab::Database::Migration[2.1] + disable_ddl_transaction! + + SOURCE_TABLE_NAME = :ci_pending_builds + TARGET_TABLE_NAME = :ci_builds + COLUMN = :build_id + TARGET_COLUMN = :id + FK_NAME = :fk_rails_725a2644a3_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: true, + reverse_lock_order: true, + name: FK_NAME, + on_update: :cascade + ) + end + + def down + with_lock_retries do + remove_foreign_key_if_exists(SOURCE_TABLE_NAME, name: FK_NAME) + end + end +end diff --git a/db/schema_migrations/20230119090944 b/db/schema_migrations/20230119090944 new file mode 100644 index 00000000000000..b9efaee5bc04fb --- /dev/null +++ b/db/schema_migrations/20230119090944 @@ -0,0 +1 @@ +6d66200b1707cb469891aa661ed944b133833abe5a0cdd37e4cae1b78b36426f \ No newline at end of file diff --git a/db/schema_migrations/20230119091027 b/db/schema_migrations/20230119091027 new file mode 100644 index 00000000000000..f4a5d3d556aabc --- /dev/null +++ b/db/schema_migrations/20230119091027 @@ -0,0 +1 @@ +0e269f5d643757423c3597f646b298224ab3c2eb60ab92cb030edb6ec8fb3241 \ No newline at end of file diff --git a/db/structure.sql b/db/structure.sql index 5f33b103d72f96..b5e5619daab3a7 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -35013,6 +35013,10 @@ ALTER TABLE ONLY project_custom_attributes ALTER TABLE ONLY ci_pending_builds ADD CONSTRAINT fk_rails_725a2644a3 FOREIGN KEY (build_id) REFERENCES ci_builds(id) ON DELETE CASCADE; +ALTER TABLE ONLY ci_pending_builds + ADD CONSTRAINT fk_rails_725a2644a3_p FOREIGN KEY (partition_id, build_id) REFERENCES ci_builds(partition_id, id) ON UPDATE CASCADE + ON DELETE CASCADE; + ALTER TABLE security_findings ADD CONSTRAINT fk_rails_729b763a54 FOREIGN KEY (scanner_id) REFERENCES vulnerability_scanners(id) ON DELETE CASCADE; -- GitLab From a1de254cd48a35a83f005a50c592e7236ccfec88 Mon Sep 17 00:00:00 2001 From: Maxime Orefice Date: Thu, 19 Jan 2023 10:51:55 +0100 Subject: [PATCH 2/4] Fix specs --- spec/db/schema_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/db/schema_spec.rb b/spec/db/schema_spec.rb index 4c2383292c7267..f725e3cd2c5382 100644 --- a/spec/db/schema_spec.rb +++ b/spec/db/schema_spec.rb @@ -10,7 +10,8 @@ let(:columns_name_with_jsonb) { retrieve_columns_name_with_jsonb } IGNORED_INDEXES_ON_FKS = { - slack_integrations_scopes: %w[slack_api_scope_id] + slack_integrations_scopes: %w[slack_api_scope_id], + ci_pending_builds: %w[partition_id] # We have a composite FK for this }.with_indifferent_access.freeze TABLE_PARTITIONS = %w[ci_builds_metadata].freeze -- GitLab From d6152954e7db63c627d7776f4c8a60bacc1ad758 Mon Sep 17 00:00:00 2001 From: Maxime Orefice Date: Thu, 19 Jan 2023 15:12:30 +0100 Subject: [PATCH 3/4] Make index unique --- .../20230119090944_add_index_to_ci_pending_build.rb | 2 +- db/structure.sql | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/db/post_migrate/20230119090944_add_index_to_ci_pending_build.rb b/db/post_migrate/20230119090944_add_index_to_ci_pending_build.rb index 5976dee1eb135d..de58b6a0ef89fa 100644 --- a/db/post_migrate/20230119090944_add_index_to_ci_pending_build.rb +++ b/db/post_migrate/20230119090944_add_index_to_ci_pending_build.rb @@ -8,7 +8,7 @@ class AddIndexToCiPendingBuild < Gitlab::Database::Migration[2.1] COLUMNS = [:partition_id, :build_id] def up - add_concurrent_index(TABLE_NAME, COLUMNS, name: INDEX_NAME) + add_concurrent_index(TABLE_NAME, COLUMNS, unique: true, name: INDEX_NAME) end def down diff --git a/db/structure.sql b/db/structure.sql index b5e5619daab3a7..0e3dbe781ce7b6 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -28983,6 +28983,8 @@ CREATE UNIQUE INDEX index_ci_pending_builds_on_build_id ON ci_pending_builds USI CREATE INDEX index_ci_pending_builds_on_namespace_id ON ci_pending_builds USING btree (namespace_id); +CREATE UNIQUE INDEX index_ci_pending_builds_on_partition_id_build_id ON ci_pending_builds USING btree (partition_id, build_id); + CREATE INDEX index_ci_pending_builds_on_project_id ON ci_pending_builds USING btree (project_id); CREATE INDEX index_ci_pending_builds_on_tag_ids ON ci_pending_builds USING btree (tag_ids) WHERE (cardinality(tag_ids) > 0); @@ -35014,8 +35016,7 @@ ALTER TABLE ONLY ci_pending_builds ADD CONSTRAINT fk_rails_725a2644a3 FOREIGN KEY (build_id) REFERENCES ci_builds(id) ON DELETE CASCADE; ALTER TABLE ONLY ci_pending_builds - ADD CONSTRAINT fk_rails_725a2644a3_p FOREIGN KEY (partition_id, build_id) REFERENCES ci_builds(partition_id, id) ON UPDATE CASCADE - ON DELETE CASCADE; + ADD CONSTRAINT fk_rails_725a2644a3_p FOREIGN KEY (partition_id, build_id) REFERENCES ci_builds(partition_id, id) ON UPDATE CASCADE ON DELETE CASCADE; ALTER TABLE security_findings ADD CONSTRAINT fk_rails_729b763a54 FOREIGN KEY (scanner_id) REFERENCES vulnerability_scanners(id) ON DELETE CASCADE; -- GitLab From 51dbe28c233cf3834968d59590f61b62ea1d8cd9 Mon Sep 17 00:00:00 2001 From: Maxime Orefice Date: Mon, 23 Jan 2023 09:49:23 +0100 Subject: [PATCH 4/4] Remove ignored index fk spec --- spec/db/schema_spec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/db/schema_spec.rb b/spec/db/schema_spec.rb index f725e3cd2c5382..4c2383292c7267 100644 --- a/spec/db/schema_spec.rb +++ b/spec/db/schema_spec.rb @@ -10,8 +10,7 @@ let(:columns_name_with_jsonb) { retrieve_columns_name_with_jsonb } IGNORED_INDEXES_ON_FKS = { - slack_integrations_scopes: %w[slack_api_scope_id], - ci_pending_builds: %w[partition_id] # We have a composite FK for this + slack_integrations_scopes: %w[slack_api_scope_id] }.with_indifferent_access.freeze TABLE_PARTITIONS = %w[ci_builds_metadata].freeze -- GitLab