From 8856885ffa6934c4db995845d4d540d80174f8e7 Mon Sep 17 00:00:00 2001
From: Fabien Catteau <fcatteau@gitlab.com>
Date: Wed, 25 Sep 2024 09:03:13 +0000
Subject: [PATCH] Remove index from SBOM tables

Remove unused uniques index from
sbom_source_packages and sbom_components tables.
These have been replaced with new unique indexes
that now include organization_id (sharding key).

Changelog: changed
---
 ...om_source_packages_on_name_and_purl_type.rb | 18 ++++++++++++++++++
 ...nts_on_component_type_name_and_purl_type.rb | 18 ++++++++++++++++++
 db/schema_migrations/20240923154747            |  1 +
 db/schema_migrations/20240923154851            |  1 +
 db/structure.sql                               |  4 ----
 5 files changed, 38 insertions(+), 4 deletions(-)
 create mode 100644 db/post_migrate/20240923154747_drop_idx_sbom_source_packages_on_name_and_purl_type.rb
 create mode 100644 db/post_migrate/20240923154851_drop_index_sbom_components_on_component_type_name_and_purl_type.rb
 create mode 100644 db/schema_migrations/20240923154747
 create mode 100644 db/schema_migrations/20240923154851

diff --git a/db/post_migrate/20240923154747_drop_idx_sbom_source_packages_on_name_and_purl_type.rb b/db/post_migrate/20240923154747_drop_idx_sbom_source_packages_on_name_and_purl_type.rb
new file mode 100644
index 000000000000..bf95d1655615
--- /dev/null
+++ b/db/post_migrate/20240923154747_drop_idx_sbom_source_packages_on_name_and_purl_type.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+class DropIdxSbomSourcePackagesOnNameAndPurlType < Gitlab::Database::Migration[2.2]
+  disable_ddl_transaction!
+
+  milestone '17.5'
+
+  INDEX_NAME = 'idx_sbom_source_packages_on_name_and_purl_type'
+
+  def up
+    remove_concurrent_index_by_name :sbom_source_packages, INDEX_NAME
+  end
+
+  def down
+    # no-op, we don't want to re-introduce this index as it is redundant with
+    # index_sbom_source_packages_on_name_and_purl_type_and_org_id
+  end
+end
diff --git a/db/post_migrate/20240923154851_drop_index_sbom_components_on_component_type_name_and_purl_type.rb b/db/post_migrate/20240923154851_drop_index_sbom_components_on_component_type_name_and_purl_type.rb
new file mode 100644
index 000000000000..7d37ba1933e2
--- /dev/null
+++ b/db/post_migrate/20240923154851_drop_index_sbom_components_on_component_type_name_and_purl_type.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+class DropIndexSbomComponentsOnComponentTypeNameAndPurlType < Gitlab::Database::Migration[2.2]
+  disable_ddl_transaction!
+
+  milestone '17.5'
+
+  INDEX_NAME = 'index_sbom_components_on_component_type_name_and_purl_type'
+
+  def up
+    remove_concurrent_index_by_name :sbom_components, INDEX_NAME
+  end
+
+  def down
+    # no-op, we don't want to re-introduce this index as it is redundant with
+    # idx_sbom_components_on_name_purl_type_component_type_and_org_id
+  end
+end
diff --git a/db/schema_migrations/20240923154747 b/db/schema_migrations/20240923154747
new file mode 100644
index 000000000000..3c76b62f4854
--- /dev/null
+++ b/db/schema_migrations/20240923154747
@@ -0,0 +1 @@
+ac46537f3d38e51cedf49c337c53f4cefacc5eb894cb4f7e925a264b72ad67b8
\ No newline at end of file
diff --git a/db/schema_migrations/20240923154851 b/db/schema_migrations/20240923154851
new file mode 100644
index 000000000000..8c485481b747
--- /dev/null
+++ b/db/schema_migrations/20240923154851
@@ -0,0 +1 @@
+394ce1f1e331201db052632b58a9d95173bc4cabd7fd895fe003f5fdfcefc37c
\ No newline at end of file
diff --git a/db/structure.sql b/db/structure.sql
index 01083691504a..4bb204a5bcad 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -27104,8 +27104,6 @@ CREATE INDEX idx_sbom_occurr_on_project_component_version_input_file_path ON sbo
 
 CREATE INDEX idx_sbom_occurrences_on_project_id_and_source_id ON sbom_occurrences USING btree (project_id, source_id);
 
-CREATE UNIQUE INDEX idx_sbom_source_packages_on_name_and_purl_type ON sbom_source_packages USING btree (name, purl_type);
-
 CREATE INDEX idx_scan_result_policies_on_configuration_id_id_updated_at ON scan_result_policies USING btree (security_orchestration_policy_configuration_id, id, updated_at);
 
 CREATE INDEX idx_scan_result_policy_violations_on_policy_id_and_id ON scan_result_policy_violations USING btree (scan_result_policy_id, id);
@@ -30368,8 +30366,6 @@ CREATE UNIQUE INDEX index_sbom_component_versions_on_component_id_and_version ON
 
 CREATE INDEX index_sbom_component_versions_on_organization_id ON sbom_component_versions USING btree (organization_id);
 
-CREATE UNIQUE INDEX index_sbom_components_on_component_type_name_and_purl_type ON sbom_components USING btree (name, purl_type, component_type);
-
 CREATE INDEX index_sbom_components_on_organization_id ON sbom_components USING btree (organization_id);
 
 CREATE INDEX index_sbom_occurr_on_project_id_and_component_version_id_and_id ON sbom_occurrences USING btree (project_id, component_version_id, id);
-- 
GitLab