From a69ac48a27f75f96ca608ca7a481aea9bed305f5 Mon Sep 17 00:00:00 2001
From: Max Orefice <morefice@gitlab.com>
Date: Wed, 19 Jun 2024 12:13:21 +0000
Subject: [PATCH] Drop duplicate index on p_ci_stages

---
 ...4828_drop_index_ci_stage_on_pipeline_id.rb | 29 +++++++++++++++++++
 db/schema_migrations/20240610134828           |  1 +
 db/structure.sql                              |  6 ----
 .../helpers/database/duplicate_indexes.yml    | 12 --------
 4 files changed, 30 insertions(+), 18 deletions(-)
 create mode 100644 db/post_migrate/20240610134828_drop_index_ci_stage_on_pipeline_id.rb
 create mode 100644 db/schema_migrations/20240610134828

diff --git a/db/post_migrate/20240610134828_drop_index_ci_stage_on_pipeline_id.rb b/db/post_migrate/20240610134828_drop_index_ci_stage_on_pipeline_id.rb
new file mode 100644
index 0000000000000..946b200a19bcd
--- /dev/null
+++ b/db/post_migrate/20240610134828_drop_index_ci_stage_on_pipeline_id.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+class DropIndexCiStageOnPipelineId < Gitlab::Database::Migration[2.2]
+  include Gitlab::Database::PartitioningMigrationHelpers::IndexHelpers
+
+  milestone '17.2'
+  disable_ddl_transaction!
+
+  TABLE_NAME = :p_ci_stages
+  INDEX_NAME = :p_ci_stages_pipeline_id_idx
+  COLUM_NAME = :pipeline_id
+
+  def up
+    remove_concurrent_partitioned_index_by_name(TABLE_NAME, index_name_for(TABLE_NAME, COLUM_NAME))
+  end
+
+  def down
+    add_concurrent_partitioned_index(TABLE_NAME, COLUM_NAME, name: INDEX_NAME)
+    rename_index(:ci_stages, :index_788357c6f1, :index_ci_stages_on_pipeline_id)
+  end
+
+  private
+
+  def index_name_for(table, column)
+    columns = Array.wrap(column.to_s)
+    index = indexes(table).find { |i| i.columns == columns }
+    index.name
+  end
+end
diff --git a/db/schema_migrations/20240610134828 b/db/schema_migrations/20240610134828
new file mode 100644
index 0000000000000..9f1abb4ee10a1
--- /dev/null
+++ b/db/schema_migrations/20240610134828
@@ -0,0 +1 @@
+b3b178f6ac16b3b2ff6eb53f3e7cf85f251378678af21f2f09eeba57f2ab61b7
\ No newline at end of file
diff --git a/db/structure.sql b/db/structure.sql
index 0f2a400e0f378..89ac473440409 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -26238,10 +26238,6 @@ CREATE INDEX index_ci_sources_projects_on_pipeline_id ON ci_sources_projects USI
 
 CREATE UNIQUE INDEX index_ci_sources_projects_on_source_project_id_and_pipeline_id ON ci_sources_projects USING btree (source_project_id, pipeline_id);
 
-CREATE INDEX p_ci_stages_pipeline_id_idx ON ONLY p_ci_stages USING btree (pipeline_id);
-
-CREATE INDEX index_ci_stages_on_pipeline_id ON ci_stages USING btree (pipeline_id);
-
 CREATE INDEX p_ci_stages_pipeline_id_id_idx ON ONLY p_ci_stages USING btree (pipeline_id, id) WHERE (status = ANY (ARRAY[0, 1, 2, 8, 9, 10]));
 
 CREATE INDEX index_ci_stages_on_pipeline_id_and_id ON ci_stages USING btree (pipeline_id, id) WHERE (status = ANY (ARRAY[0, 1, 2, 8, 9, 10]));
@@ -31036,8 +31032,6 @@ ALTER INDEX p_ci_job_artifacts_project_id_id_idx1 ATTACH PARTITION index_ci_job_
 
 ALTER INDEX p_ci_job_artifacts_project_id_idx1 ATTACH PARTITION index_ci_job_artifacts_on_project_id_for_security_reports;
 
-ALTER INDEX p_ci_stages_pipeline_id_idx ATTACH PARTITION index_ci_stages_on_pipeline_id;
-
 ALTER INDEX p_ci_stages_pipeline_id_id_idx ATTACH PARTITION index_ci_stages_on_pipeline_id_and_id;
 
 ALTER INDEX p_ci_stages_pipeline_id_position_idx ATTACH PARTITION index_ci_stages_on_pipeline_id_and_position;
diff --git a/spec/support/helpers/database/duplicate_indexes.yml b/spec/support/helpers/database/duplicate_indexes.yml
index cc1a7a8364299..5505a8ef54efc 100644
--- a/spec/support/helpers/database/duplicate_indexes.yml
+++ b/spec/support/helpers/database/duplicate_indexes.yml
@@ -1,9 +1,4 @@
 ---
-ci_stages:
-  index_ci_stages_on_pipeline_id_and_position:
-  - index_ci_stages_on_pipeline_id
-  index_ci_stages_on_pipeline_id_name_partition_id_unique:
-  - index_ci_stages_on_pipeline_id
 dast_site_tokens:
   index_dast_site_token_on_project_id_and_url:
   - index_dast_site_tokens_on_project_id
@@ -58,13 +53,6 @@ ml_model_versions:
 ml_models:
   index_ml_models_on_project_id_and_name:
   - index_ml_models_on_project_id
-p_ci_stages:
-  p_ci_stages_pipeline_id_name_partition_id_idx:
-  - p_ci_stages_pipeline_id_idx
-  p_ci_stages_pipeline_id_idx:
-  - p_ci_stages_pipeline_id_idx
-  p_ci_stages_pipeline_id_position_idx:
-  - p_ci_stages_pipeline_id_idx
 pages_domains:
   index_pages_domains_on_project_id_and_enabled_until:
   - index_pages_domains_on_project_id
-- 
GitLab