From aa8744356ded404e448a541cfee314f41b99e9b0 Mon Sep 17 00:00:00 2001
From: Tianwen Chen <tchen@gitlab.com>
Date: Tue, 5 Dec 2023 07:14:27 +0000
Subject: [PATCH] Cleanup for converted ci_stages.pipeline_id conversion

This is to step to remove the trigger and old integer column
See https://docs.gitlab.com/ee/development/database/avoiding_downtime_in_migrations.html#remove-the-trigger-and-old-integer-columns-release-n--2

Changelog: changed
---
 ...45_cleanup_ci_stages_pipeline_id_bigint.rb | 53 +++++++++++++++++++
 db/schema_migrations/20231120070345           |  1 +
 db/structure.sql                              | 23 --------
 .../helpers/database/duplicate_indexes.yml    |  4 --
 4 files changed, 54 insertions(+), 27 deletions(-)
 create mode 100644 db/post_migrate/20231120070345_cleanup_ci_stages_pipeline_id_bigint.rb
 create mode 100644 db/schema_migrations/20231120070345

diff --git a/db/post_migrate/20231120070345_cleanup_ci_stages_pipeline_id_bigint.rb b/db/post_migrate/20231120070345_cleanup_ci_stages_pipeline_id_bigint.rb
new file mode 100644
index 0000000000000..c9238eb727200
--- /dev/null
+++ b/db/post_migrate/20231120070345_cleanup_ci_stages_pipeline_id_bigint.rb
@@ -0,0 +1,53 @@
+# frozen_string_literal: true
+
+class CleanupCiStagesPipelineIdBigint < Gitlab::Database::Migration[2.2]
+  include Gitlab::Database::MigrationHelpers::WraparoundAutovacuum
+
+  disable_ddl_transaction!
+  milestone "16.7"
+
+  TABLE = :ci_stages
+  REFERENCING_TABLE = :ci_pipelines
+  COLUMN = :pipeline_id
+  OLD_COLUMN = :pipeline_id_convert_to_bigint
+  INDEXES = {
+    'index_ci_stages_on_pipeline_id_convert_to_bigint_and_name' => [
+      [:pipeline_id_convert_to_bigint, :name], { unique: true }
+    ],
+    'index_ci_stages_on_pipeline_id_convert_to_bigint' => [
+      [:pipeline_id_convert_to_bigint], {}
+    ],
+    'index_ci_stages_on_pipeline_id_convert_to_bigint_and_id' => [
+      [:pipeline_id_convert_to_bigint, :id], { where: 'status = ANY (ARRAY[0, 1, 2, 8, 9, 10])' }
+    ],
+    'index_ci_stages_on_pipeline_id_convert_to_bigint_and_position' => [
+      [:pipeline_id_convert_to_bigint, :position], {}
+    ]
+  }
+  OLD_FK_NAME = :fk_c5ddde695f
+
+  def up
+    return unless can_execute_on?(:ci_pipelines, :ci_stages)
+
+    with_lock_retries(raise_on_exhaustion: true) do
+      lock_tables(REFERENCING_TABLE, TABLE)
+      cleanup_conversion_of_integer_to_bigint(TABLE, [COLUMN])
+    end
+  end
+
+  def down
+    return unless can_execute_on?(:ci_pipelines, :ci_stages)
+
+    restore_conversion_of_integer_to_bigint(TABLE, [COLUMN])
+
+    INDEXES.each do |index_name, (columns, options)|
+      add_concurrent_index(TABLE, columns, name: index_name, **options)
+    end
+
+    add_concurrent_foreign_key(
+      TABLE, REFERENCING_TABLE,
+      column: OLD_COLUMN, name: OLD_FK_NAME,
+      on_delete: :cascade, validate: true, reverse_lock_order: true
+    )
+  end
+end
diff --git a/db/schema_migrations/20231120070345 b/db/schema_migrations/20231120070345
new file mode 100644
index 0000000000000..70ba566885a6f
--- /dev/null
+++ b/db/schema_migrations/20231120070345
@@ -0,0 +1 @@
+7f3abae7002d20e30f9e4a30d580e49c5d72a7728d13ee45a5392fb4396da13b
\ No newline at end of file
diff --git a/db/structure.sql b/db/structure.sql
index fb31618cd7bc9..ec019f72918ca 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -531,15 +531,6 @@ RETURN NULL;
 END
 $$;
 
-CREATE FUNCTION trigger_07bc3c48f407() RETURNS trigger
-    LANGUAGE plpgsql
-    AS $$
-BEGIN
-  NEW."pipeline_id_convert_to_bigint" := NEW."pipeline_id";
-  RETURN NEW;
-END;
-$$;
-
 CREATE FUNCTION trigger_10ee1357e825() RETURNS trigger
     LANGUAGE plpgsql
     AS $$
@@ -14724,7 +14715,6 @@ ALTER SEQUENCE ci_sources_projects_id_seq OWNED BY ci_sources_projects.id;
 
 CREATE TABLE ci_stages (
     project_id integer,
-    pipeline_id_convert_to_bigint integer,
     created_at timestamp without time zone,
     updated_at timestamp without time zone,
     name character varying,
@@ -32355,14 +32345,6 @@ CREATE UNIQUE INDEX index_ci_stages_on_pipeline_id_and_name ON ci_stages USING b
 
 CREATE INDEX index_ci_stages_on_pipeline_id_and_position ON ci_stages USING btree (pipeline_id, "position");
 
-CREATE INDEX index_ci_stages_on_pipeline_id_convert_to_bigint ON ci_stages USING btree (pipeline_id_convert_to_bigint);
-
-CREATE INDEX index_ci_stages_on_pipeline_id_convert_to_bigint_and_id ON ci_stages USING btree (pipeline_id_convert_to_bigint, id) WHERE (status = ANY (ARRAY[0, 1, 2, 8, 9, 10]));
-
-CREATE UNIQUE INDEX index_ci_stages_on_pipeline_id_convert_to_bigint_and_name ON ci_stages USING btree (pipeline_id_convert_to_bigint, name);
-
-CREATE INDEX index_ci_stages_on_pipeline_id_convert_to_bigint_and_position ON ci_stages USING btree (pipeline_id_convert_to_bigint, "position");
-
 CREATE INDEX index_ci_stages_on_project_id ON ci_stages USING btree (project_id);
 
 CREATE INDEX index_ci_subscriptions_projects_author_id ON ci_subscriptions_projects USING btree (author_id);
@@ -37063,8 +37045,6 @@ CREATE TRIGGER push_rules_loose_fk_trigger AFTER DELETE ON push_rules REFERENCIN
 
 CREATE TRIGGER tags_loose_fk_trigger AFTER DELETE ON tags REFERENCING OLD TABLE AS old_table FOR EACH STATEMENT EXECUTE FUNCTION insert_into_loose_foreign_keys_deleted_records();
 
-CREATE TRIGGER trigger_07bc3c48f407 BEFORE INSERT OR UPDATE ON ci_stages FOR EACH ROW EXECUTE FUNCTION trigger_07bc3c48f407();
-
 CREATE TRIGGER trigger_10ee1357e825 BEFORE INSERT OR UPDATE ON p_ci_builds FOR EACH ROW EXECUTE FUNCTION trigger_10ee1357e825();
 
 CREATE TRIGGER trigger_b2d852e1e2cb BEFORE INSERT OR UPDATE ON ci_pipelines FOR EACH ROW EXECUTE FUNCTION trigger_b2d852e1e2cb();
@@ -37980,9 +37960,6 @@ ALTER TABLE ONLY timelogs
 ALTER TABLE ONLY geo_event_log
     ADD CONSTRAINT fk_c4b1c1f66e FOREIGN KEY (repository_deleted_event_id) REFERENCES geo_repository_deleted_events(id) ON DELETE CASCADE;
 
-ALTER TABLE ONLY ci_stages
-    ADD CONSTRAINT fk_c5ddde695f FOREIGN KEY (pipeline_id_convert_to_bigint) REFERENCES ci_pipelines(id) ON DELETE CASCADE;
-
 ALTER TABLE ONLY issues
     ADD CONSTRAINT fk_c63cbf6c25 FOREIGN KEY (closed_by_id) REFERENCES users(id) ON DELETE SET NULL;
 
diff --git a/spec/support/helpers/database/duplicate_indexes.yml b/spec/support/helpers/database/duplicate_indexes.yml
index 37aa437460803..5ae529ea8ef66 100644
--- a/spec/support/helpers/database/duplicate_indexes.yml
+++ b/spec/support/helpers/database/duplicate_indexes.yml
@@ -42,10 +42,6 @@ ci_stages:
     - index_ci_stages_on_pipeline_id
   index_ci_stages_on_pipeline_id_and_position:
     - index_ci_stages_on_pipeline_id
-  index_ci_stages_on_pipeline_id_convert_to_bigint_and_name:
-    - index_ci_stages_on_pipeline_id_convert_to_bigint
-  index_ci_stages_on_pipeline_id_convert_to_bigint_and_position:
-    - index_ci_stages_on_pipeline_id_convert_to_bigint
 dast_site_tokens:
   index_dast_site_token_on_project_id_and_url:
     - index_dast_site_tokens_on_project_id
-- 
GitLab