From 2fbe793eebafcf073dc55fede860dd44d05ac53e Mon Sep 17 00:00:00 2001
From: Manoj M J <mmj@gitlab.com>
Date: Wed, 13 Sep 2023 15:16:34 +0200
Subject: [PATCH] Add sync index on `projects.organization_id`

Changelog: other
---
 .../20230913130629_index_org_id_on_projects.rb    | 15 +++++++++++++++
 db/schema_migrations/20230913130629               |  1 +
 db/structure.sql                                  |  2 ++
 spec/db/schema_spec.rb                            |  3 +--
 4 files changed, 19 insertions(+), 2 deletions(-)
 create mode 100644 db/post_migrate/20230913130629_index_org_id_on_projects.rb
 create mode 100644 db/schema_migrations/20230913130629

diff --git a/db/post_migrate/20230913130629_index_org_id_on_projects.rb b/db/post_migrate/20230913130629_index_org_id_on_projects.rb
new file mode 100644
index 0000000000000..45186b900c608
--- /dev/null
+++ b/db/post_migrate/20230913130629_index_org_id_on_projects.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+class IndexOrgIdOnProjects < Gitlab::Database::Migration[2.1]
+  disable_ddl_transaction!
+
+  INDEX_NAME = 'index_projects_on_organization_id'
+
+  def up
+    add_concurrent_index :projects, :organization_id, name: INDEX_NAME
+  end
+
+  def down
+    remove_concurrent_index_by_name :projects, INDEX_NAME
+  end
+end
diff --git a/db/schema_migrations/20230913130629 b/db/schema_migrations/20230913130629
new file mode 100644
index 0000000000000..267592178577e
--- /dev/null
+++ b/db/schema_migrations/20230913130629
@@ -0,0 +1 @@
+4d5c78a1ebe2699831e1490c4df8ccbfbd1528af711ad67fc03232f34499e89e
\ No newline at end of file
diff --git a/db/structure.sql b/db/structure.sql
index d89580e0a432e..9b0f43aa497e8 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -33360,6 +33360,8 @@ CREATE INDEX index_projects_on_namespace_id_and_id ON projects USING btree (name
 
 CREATE INDEX index_projects_on_namespace_id_and_repository_size_limit ON projects USING btree (namespace_id, repository_size_limit);
 
+CREATE INDEX index_projects_on_organization_id ON projects USING btree (organization_id);
+
 CREATE INDEX index_projects_on_path_and_id ON projects USING btree (path, id);
 
 CREATE INDEX index_projects_on_path_trigram ON projects USING gin (path gin_trgm_ops);
diff --git a/spec/db/schema_spec.rb b/spec/db/schema_spec.rb
index fea3c5b77a130..04a83a1f6ab7d 100644
--- a/spec/db/schema_spec.rb
+++ b/spec/db/schema_spec.rb
@@ -15,8 +15,7 @@
     search_namespace_index_assignments: [%w[search_index_id index_type]],
     slack_integrations_scopes: [%w[slack_api_scope_id]],
     notes: %w[namespace_id], # this index is added in an async manner, hence it needs to be ignored in the first phase.
-    vulnerabilities: [%w[finding_id]], # index will be created in https://gitlab.com/gitlab-org/gitlab/-/issues/423541
-    projects: %w[organization_id] # this index is added in an async manner, hence it needs to be ignored in the first phase.
+    vulnerabilities: [%w[finding_id]] # index will be created in https://gitlab.com/gitlab-org/gitlab/-/issues/423541
   }.with_indifferent_access.freeze
 
   TABLE_PARTITIONS = %w[ci_builds_metadata].freeze
-- 
GitLab