From 336de522d99820a4e10d7ddf88d7434a8d9369a5 Mon Sep 17 00:00:00 2001 From: Maxime Orefice Date: Thu, 19 Jan 2023 10:04:43 +0100 Subject: [PATCH 1/4] Add FK to ci_running_builds Changelog: added --- ...119085509_add_index_to_ci_running_build.rb | 17 ++++++++++ ...552_add_foreign_key_to_ci_running_build.rb | 31 +++++++++++++++++++ db/schema_migrations/20230119085509 | 1 + db/schema_migrations/20230119085552 | 1 + db/structure.sql | 3 ++ 5 files changed, 53 insertions(+) create mode 100644 db/post_migrate/20230119085509_add_index_to_ci_running_build.rb create mode 100644 db/post_migrate/20230119085552_add_foreign_key_to_ci_running_build.rb create mode 100644 db/schema_migrations/20230119085509 create mode 100644 db/schema_migrations/20230119085552 diff --git a/db/post_migrate/20230119085509_add_index_to_ci_running_build.rb b/db/post_migrate/20230119085509_add_index_to_ci_running_build.rb new file mode 100644 index 00000000000000..cc0f53e723b1ad --- /dev/null +++ b/db/post_migrate/20230119085509_add_index_to_ci_running_build.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class AddIndexToCiRunningBuild < Gitlab::Database::Migration[2.1] + disable_ddl_transaction! + + INDEX_NAME = :index_ci_running_builds_on_partition_id_build_id + TABLE_NAME = :ci_running_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/20230119085552_add_foreign_key_to_ci_running_build.rb b/db/post_migrate/20230119085552_add_foreign_key_to_ci_running_build.rb new file mode 100644 index 00000000000000..94dfdc5b967ea2 --- /dev/null +++ b/db/post_migrate/20230119085552_add_foreign_key_to_ci_running_build.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +class AddForeignKeyToCiRunningBuild < Gitlab::Database::Migration[2.1] + disable_ddl_transaction! + + SOURCE_TABLE_NAME = :ci_running_builds + TARGET_TABLE_NAME = :ci_builds + COLUMN = :build_id + TARGET_COLUMN = :id + FK_NAME = :fk_rails_da45cfa165_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/20230119085509 b/db/schema_migrations/20230119085509 new file mode 100644 index 00000000000000..1eb8297a4b3688 --- /dev/null +++ b/db/schema_migrations/20230119085509 @@ -0,0 +1 @@ +6206e50e14c129aeb1d44fbd82add001e73b338bbe80bdade852ff7ec0bc0f86 \ No newline at end of file diff --git a/db/schema_migrations/20230119085552 b/db/schema_migrations/20230119085552 new file mode 100644 index 00000000000000..d548c864d2f7b8 --- /dev/null +++ b/db/schema_migrations/20230119085552 @@ -0,0 +1 @@ +4bc2f855e1448c3c1b3d6d2b853dc61b049048fa0fee663fe798d86ea88b09a0 \ No newline at end of file diff --git a/db/structure.sql b/db/structure.sql index 5f33b103d72f96..5a3de8d8706fe8 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -35676,6 +35676,9 @@ ALTER TABLE ONLY merge_request_reviewers ALTER TABLE ONLY ci_running_builds ADD CONSTRAINT fk_rails_da45cfa165 FOREIGN KEY (build_id) REFERENCES ci_builds(id) ON DELETE CASCADE; +ALTER TABLE ONLY ci_running_builds + ADD CONSTRAINT fk_rails_da45cfa165_p FOREIGN KEY (partition_id, build_id) REFERENCES ci_builds(partition_id, id) ON UPDATE CASCADE ON DELETE CASCADE; + ALTER TABLE ONLY jira_imports ADD CONSTRAINT fk_rails_da617096ce FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL; -- GitLab From 9baa90fb055f141782d19addd8eb4a3fd926ceac Mon Sep 17 00:00:00 2001 From: Maxime Orefice Date: Thu, 19 Jan 2023 10:50:05 +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..a9506ecea4d769 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_running_builds: %w[partition_id] # We have a composite FK for this }.with_indifferent_access.freeze TABLE_PARTITIONS = %w[ci_builds_metadata].freeze -- GitLab From 87c0961d93d1d4358d49a63a775f682a26e48bff Mon Sep 17 00:00:00 2001 From: Maxime Orefice Date: Thu, 19 Jan 2023 15:05:00 +0100 Subject: [PATCH 3/4] Make index unique --- db/post_migrate/20230119085509_add_index_to_ci_running_build.rb | 2 +- db/structure.sql | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/db/post_migrate/20230119085509_add_index_to_ci_running_build.rb b/db/post_migrate/20230119085509_add_index_to_ci_running_build.rb index cc0f53e723b1ad..f340b78801fb37 100644 --- a/db/post_migrate/20230119085509_add_index_to_ci_running_build.rb +++ b/db/post_migrate/20230119085509_add_index_to_ci_running_build.rb @@ -8,7 +8,7 @@ class AddIndexToCiRunningBuild < 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 5a3de8d8706fe8..8b05ce220eca00 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -29129,6 +29129,8 @@ CREATE INDEX index_ci_runners_on_version ON ci_runners USING btree (version); CREATE UNIQUE INDEX index_ci_running_builds_on_build_id ON ci_running_builds USING btree (build_id); +CREATE UNIQUE INDEX index_ci_running_builds_on_partition_id_build_id ON ci_running_builds USING btree (partition_id, build_id); + CREATE INDEX index_ci_running_builds_on_project_id ON ci_running_builds USING btree (project_id); CREATE INDEX index_ci_running_builds_on_runner_id ON ci_running_builds USING btree (runner_id); -- GitLab From abcd8fc26071b168537b9f32358b2efb0139ad52 Mon Sep 17 00:00:00 2001 From: Maxime Orefice Date: Mon, 23 Jan 2023 10:22:28 +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 a9506ecea4d769..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_running_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