From b735b7e47d30ad897687ca181e9cf5bcaed2e346 Mon Sep 17 00:00:00 2001
From: Michael Becker <11881043-wandering_person@users.noreply.gitlab.com>
Date: Fri, 21 Jul 2023 07:58:54 +0000
Subject: [PATCH] Synchronous creation:
 index_vuln_findings_on_uuid_including_vuln_id

This index was originally added in an async migration in the following
two MRs:

1. [Index vulnerability findings on uuid including vuln id][0]
2. [Recreate async index `index_vuln_findings_on_uuid_including_vuln_id`][1]

This commit is a followup to add the index synchronously as the
final step in the [Asynchronous Index Creation][2] process

[0]:https://gitlab.com/gitlab-org/gitlab/-/merge_requests/115175
[1]:https://gitlab.com/gitlab-org/gitlab/-/merge_requests/125646
[2]:https://docs.gitlab.com/ee/development/database/adding_database_indexes.html#create-indexes-asynchronously

Changelog: performance
---
 ...dex_vulnerability_findings_on_uuid_sync.rb | 21 +++++++++++++++++++
 db/schema_migrations/20230713234121           |  1 +
 db/structure.sql                              |  2 ++
 .../models/vulnerabilities/finding_spec.rb    |  2 +-
 4 files changed, 25 insertions(+), 1 deletion(-)
 create mode 100644 db/post_migrate/20230713234121_create_index_vulnerability_findings_on_uuid_sync.rb
 create mode 100644 db/schema_migrations/20230713234121

diff --git a/db/post_migrate/20230713234121_create_index_vulnerability_findings_on_uuid_sync.rb b/db/post_migrate/20230713234121_create_index_vulnerability_findings_on_uuid_sync.rb
new file mode 100644
index 0000000000000..abbf2390b0d6e
--- /dev/null
+++ b/db/post_migrate/20230713234121_create_index_vulnerability_findings_on_uuid_sync.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+class CreateIndexVulnerabilityFindingsOnUuidSync < Gitlab::Database::Migration[2.1]
+  INDEX_NAME = 'index_vuln_findings_on_uuid_including_vuln_id'
+
+  disable_ddl_transaction!
+
+  def up
+    return if index_exists_by_name?(:vulnerability_occurrences, INDEX_NAME)
+
+    disable_statement_timeout do
+      execute <<~SQL
+        CREATE UNIQUE INDEX CONCURRENTLY #{INDEX_NAME} ON vulnerability_occurrences (uuid) include (vulnerability_id);
+      SQL
+    end
+  end
+
+  def down
+    remove_concurrent_index_by_name(:vulnerability_occurrences, INDEX_NAME)
+  end
+end
diff --git a/db/schema_migrations/20230713234121 b/db/schema_migrations/20230713234121
new file mode 100644
index 0000000000000..84382d7cf7311
--- /dev/null
+++ b/db/schema_migrations/20230713234121
@@ -0,0 +1 @@
+8dd7e4a28417629324f904b20d16e67a0832a6a731c3e7095097dc632d93ca9d
\ No newline at end of file
diff --git a/db/structure.sql b/db/structure.sql
index 340b8fdec7994..be6b9f355c53b 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -33304,6 +33304,8 @@ CREATE UNIQUE INDEX index_verification_codes_on_phone_and_visitor_id_code ON ONL
 
 COMMENT ON INDEX index_verification_codes_on_phone_and_visitor_id_code IS 'JiHu-specific index';
 
+CREATE UNIQUE INDEX index_vuln_findings_on_uuid_including_vuln_id ON vulnerability_occurrences USING btree (uuid) INCLUDE (vulnerability_id);
+
 CREATE UNIQUE INDEX index_vuln_historical_statistics_on_project_id_and_date ON vulnerability_historical_statistics USING btree (project_id, date);
 
 CREATE INDEX index_vuln_reads_common_query_on_resolved_on_default_branch ON vulnerability_reads USING btree (project_id, state, report_type, vulnerability_id DESC) WHERE (resolved_on_default_branch IS TRUE);
diff --git a/ee/spec/models/vulnerabilities/finding_spec.rb b/ee/spec/models/vulnerabilities/finding_spec.rb
index bb21a93723f2f..a26c0ef8cc726 100644
--- a/ee/spec/models/vulnerabilities/finding_spec.rb
+++ b/ee/spec/models/vulnerabilities/finding_spec.rb
@@ -7,7 +7,7 @@
   it { is_expected.to define_enum_for(:severity) }
   it { is_expected.to define_enum_for(:detection_method) }
 
-  it { is_expected.to have_locked_schema('b10c77716e35e1b63499af167373cfa2f43d9a0f919c934fe974b9c4ad492a15').reference('https://gitlab.com/gitlab-org/gitlab/-/issues/349315') }
+  it { is_expected.to have_locked_schema('0865cedff019ba2d3430a1e4354506034acc179fcdb2632efd645bb51c132eb8').reference('https://gitlab.com/gitlab-org/gitlab/-/issues/349315') }
 
   where(vulnerability_finding_signatures: [true, false])
   with_them do
-- 
GitLab