From a6d179e39c630e2a5449e409b2adf284a4c27bc8 Mon Sep 17 00:00:00 2001
From: moaz-khalifa <mkhalifa@gitlab.com>
Date: Fri, 5 Jan 2024 20:34:58 +0100
Subject: [PATCH] Remove package_registry duplicated indexes

 Remove package_registry duplicated indexes that already covered by other indexes.

 Changelog: changed
---
 ...ove_package_registry_duplicated_indexes.rb | 36 +++++++++++++++++++
 db/schema_migrations/20240108181808           |  1 +
 db/structure.sql                              |  6 ----
 .../helpers/database/duplicate_indexes.yml    | 13 -------
 4 files changed, 37 insertions(+), 19 deletions(-)
 create mode 100644 db/post_migrate/20240108181808_remove_package_registry_duplicated_indexes.rb
 create mode 100644 db/schema_migrations/20240108181808

diff --git a/db/post_migrate/20240108181808_remove_package_registry_duplicated_indexes.rb b/db/post_migrate/20240108181808_remove_package_registry_duplicated_indexes.rb
new file mode 100644
index 0000000000000..96380b94d5069
--- /dev/null
+++ b/db/post_migrate/20240108181808_remove_package_registry_duplicated_indexes.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+class RemovePackageRegistryDuplicatedIndexes < Gitlab::Database::Migration[2.2]
+  disable_ddl_transaction!
+  milestone '16.8'
+
+  DUPLICATED_INDEXES = [
+    {
+      name: :index_packages_debian_group_distributions_on_group_id,
+      table: :packages_debian_group_distributions,
+      column: :group_id
+    },
+    {
+      name: :index_packages_debian_project_distributions_on_project_id,
+      table: :packages_debian_project_distributions,
+      column: :project_id
+    },
+    {
+      name: :index_packages_tags_on_package_id,
+      table: :packages_tags,
+      column: :package_id
+    }
+  ]
+
+  def up
+    DUPLICATED_INDEXES.each do |index|
+      remove_concurrent_index_by_name(index[:table], index[:name])
+    end
+  end
+
+  def down
+    DUPLICATED_INDEXES.each do |index|
+      add_concurrent_index(index[:table], index[:column], name: index[:name])
+    end
+  end
+end
diff --git a/db/schema_migrations/20240108181808 b/db/schema_migrations/20240108181808
new file mode 100644
index 0000000000000..542a576fdf1b9
--- /dev/null
+++ b/db/schema_migrations/20240108181808
@@ -0,0 +1 @@
+688603d4b31b17d1eeb133e293876aae77ab68b80f656fb9b902ca38d4c7beed
\ No newline at end of file
diff --git a/db/structure.sql b/db/structure.sql
index 950792e9d5207..7218399453c71 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -34001,14 +34001,10 @@ CREATE INDEX index_packages_debian_group_component_files_on_component_id ON pack
 
 CREATE INDEX index_packages_debian_group_distributions_on_creator_id ON packages_debian_group_distributions USING btree (creator_id);
 
-CREATE INDEX index_packages_debian_group_distributions_on_group_id ON packages_debian_group_distributions USING btree (group_id);
-
 CREATE INDEX index_packages_debian_project_component_files_on_component_id ON packages_debian_project_component_files USING btree (component_id);
 
 CREATE INDEX index_packages_debian_project_distributions_on_creator_id ON packages_debian_project_distributions USING btree (creator_id);
 
-CREATE INDEX index_packages_debian_project_distributions_on_project_id ON packages_debian_project_distributions USING btree (project_id);
-
 CREATE INDEX index_packages_debian_publications_on_distribution_id ON packages_debian_publications USING btree (distribution_id);
 
 CREATE UNIQUE INDEX index_packages_debian_publications_on_package_id ON packages_debian_publications USING btree (package_id);
@@ -34085,8 +34081,6 @@ CREATE INDEX index_packages_rpm_metadata_on_package_id ON packages_rpm_metadata
 
 CREATE INDEX index_packages_rpm_repository_files_on_project_id_and_file_name ON packages_rpm_repository_files USING btree (project_id, file_name);
 
-CREATE INDEX index_packages_tags_on_package_id ON packages_tags USING btree (package_id);
-
 CREATE INDEX index_packages_tags_on_package_id_and_updated_at ON packages_tags USING btree (package_id, updated_at DESC);
 
 CREATE INDEX index_packages_tags_on_project_id ON packages_tags USING btree (project_id);
diff --git a/spec/support/helpers/database/duplicate_indexes.yml b/spec/support/helpers/database/duplicate_indexes.yml
index 87a1e0c2c50ab..acfda313020a7 100644
--- a/spec/support/helpers/database/duplicate_indexes.yml
+++ b/spec/support/helpers/database/duplicate_indexes.yml
@@ -105,19 +105,6 @@ ml_models:
 p_ci_runner_machine_builds:
   index_p_ci_runner_machine_builds_on_runner_machine_id:
     - index_ci_runner_machine_builds_on_runner_machine_id
-packages_debian_group_distributions:
-  uniq_pkgs_debian_group_distributions_group_id_and_codename:
-    - index_packages_debian_group_distributions_on_group_id
-  uniq_pkgs_debian_group_distributions_group_id_and_suite:
-    - index_packages_debian_group_distributions_on_group_id
-packages_debian_project_distributions:
-  uniq_pkgs_debian_project_distributions_project_id_and_codename:
-    - index_packages_debian_project_distributions_on_project_id
-  uniq_pkgs_debian_project_distributions_project_id_and_suite:
-    - index_packages_debian_project_distributions_on_project_id
-packages_tags:
-  index_packages_tags_on_package_id_and_updated_at:
-    - index_packages_tags_on_package_id
 pages_domains:
   index_pages_domains_on_project_id_and_enabled_until:
     - index_pages_domains_on_project_id
-- 
GitLab