From b1edb2fc3ba2a3d7a1ac4f1b4b572a941fb1876e Mon Sep 17 00:00:00 2001
From: Shane Maglangit <smaglangit@gitlab.com>
Date: Tue, 4 Mar 2025 10:07:57 +0000
Subject: [PATCH] Add index on approvals.project_id

Changelog: other
---
 ...50224074639_add_approvals_project_id_index.rb | 16 ++++++++++++++++
 ...20250224074659_add_approvals_project_id_fk.rb | 16 ++++++++++++++++
 db/schema_migrations/20250224074639              |  1 +
 db/schema_migrations/20250224074659              |  1 +
 db/structure.sql                                 |  5 +++++
 5 files changed, 39 insertions(+)
 create mode 100644 db/post_migrate/20250224074639_add_approvals_project_id_index.rb
 create mode 100644 db/post_migrate/20250224074659_add_approvals_project_id_fk.rb
 create mode 100644 db/schema_migrations/20250224074639
 create mode 100644 db/schema_migrations/20250224074659

diff --git a/db/post_migrate/20250224074639_add_approvals_project_id_index.rb b/db/post_migrate/20250224074639_add_approvals_project_id_index.rb
new file mode 100644
index 0000000000000..afde48c08c4c5
--- /dev/null
+++ b/db/post_migrate/20250224074639_add_approvals_project_id_index.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+class AddApprovalsProjectIdIndex < Gitlab::Database::Migration[2.2]
+  disable_ddl_transaction!
+  milestone '17.10'
+
+  INDEX_NAME = 'index_approvals_on_project_id'
+
+  def up
+    add_concurrent_index :approvals, :project_id, name: INDEX_NAME
+  end
+
+  def down
+    remove_concurrent_index_by_name :approvals, INDEX_NAME
+  end
+end
diff --git a/db/post_migrate/20250224074659_add_approvals_project_id_fk.rb b/db/post_migrate/20250224074659_add_approvals_project_id_fk.rb
new file mode 100644
index 0000000000000..0c35e3273b306
--- /dev/null
+++ b/db/post_migrate/20250224074659_add_approvals_project_id_fk.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+class AddApprovalsProjectIdFk < Gitlab::Database::Migration[2.2]
+  disable_ddl_transaction!
+  milestone '17.10'
+
+  def up
+    add_concurrent_foreign_key :approvals, :projects, column: :project_id, on_delete: :cascade
+  end
+
+  def down
+    with_lock_retries do
+      remove_foreign_key :approvals, column: :project_id
+    end
+  end
+end
diff --git a/db/schema_migrations/20250224074639 b/db/schema_migrations/20250224074639
new file mode 100644
index 0000000000000..9e09a394ef7c8
--- /dev/null
+++ b/db/schema_migrations/20250224074639
@@ -0,0 +1 @@
+2631c6b29c8bc28fab1ad8665c076b55938ae7f168bd820bfe8df0327e5f39e5
\ No newline at end of file
diff --git a/db/schema_migrations/20250224074659 b/db/schema_migrations/20250224074659
new file mode 100644
index 0000000000000..cccd206686639
--- /dev/null
+++ b/db/schema_migrations/20250224074659
@@ -0,0 +1 @@
+2e50706103444c53aa8d1ade1664ac267253b0edbc3669cb1ce110045d335250
\ No newline at end of file
diff --git a/db/structure.sql b/db/structure.sql
index 5b6ce385a0027..7aa9e1f8da0e4 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -31672,6 +31672,8 @@ CREATE INDEX index_approval_rules_code_owners_rule_type ON approval_merge_reques
 
 CREATE INDEX index_approvals_on_merge_request_id_and_created_at ON approvals USING btree (merge_request_id, created_at);
 
+CREATE INDEX index_approvals_on_project_id ON approvals USING btree (project_id);
+
 CREATE UNIQUE INDEX index_approvals_on_user_id_and_merge_request_id ON approvals USING btree (user_id, merge_request_id);
 
 CREATE INDEX index_approver_groups_on_group_id ON approver_groups USING btree (group_id);
@@ -40268,6 +40270,9 @@ ALTER TABLE ONLY user_member_roles
 ALTER TABLE ONLY boards_epic_board_labels
     ADD CONSTRAINT fk_cb8ded70e2 FOREIGN KEY (group_id) REFERENCES namespaces(id) ON DELETE CASCADE;
 
+ALTER TABLE ONLY approvals
+    ADD CONSTRAINT fk_cbce403122 FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE;
+
 ALTER TABLE ONLY slack_integrations
     ADD CONSTRAINT fk_cbe270434e FOREIGN KEY (integration_id) REFERENCES integrations(id) ON DELETE CASCADE;
 
-- 
GitLab