From 26de14b015a040e3e446710f82fa2304cc1e8674 Mon Sep 17 00:00:00 2001
From: Tianwen Chen <tchen@gitlab.com>
Date: Tue, 14 Nov 2023 16:00:28 +0000
Subject: [PATCH] Cleanup for converted ci_pipeline_variables.pipeline_id

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
---
 ...i_pipeline_variables_pipeline_id_bigint.rb | 32 +++++++++++++++++++
 db/schema_migrations/20231024073401           |  1 +
 db/structure.sql                              | 17 ----------
 3 files changed, 33 insertions(+), 17 deletions(-)
 create mode 100644 db/post_migrate/20231024073401_cleanup_ci_pipeline_variables_pipeline_id_bigint.rb
 create mode 100644 db/schema_migrations/20231024073401

diff --git a/db/post_migrate/20231024073401_cleanup_ci_pipeline_variables_pipeline_id_bigint.rb b/db/post_migrate/20231024073401_cleanup_ci_pipeline_variables_pipeline_id_bigint.rb
new file mode 100644
index 0000000000000..bbc2abadcec45
--- /dev/null
+++ b/db/post_migrate/20231024073401_cleanup_ci_pipeline_variables_pipeline_id_bigint.rb
@@ -0,0 +1,32 @@
+# frozen_string_literal: true
+
+class CleanupCiPipelineVariablesPipelineIdBigint < Gitlab::Database::Migration[2.1]
+  disable_ddl_transaction!
+
+  TABLE = :ci_pipeline_variables
+  REFERENCING_TABLE = :ci_pipelines
+  COLUMNS = [:pipeline_id]
+  INDEX_NAME = :index_ci_pipeline_variables_on_pipeline_id_bigint_and_key
+  FK_NAME = :temp_fk_rails_8d3b04e3e1
+
+  def up
+    with_lock_retries(raise_on_exhaustion: true) do
+      lock_tables(:ci_pipelines, TABLE)
+      cleanup_conversion_of_integer_to_bigint(TABLE, COLUMNS)
+    end
+  end
+
+  def down
+    restore_conversion_of_integer_to_bigint(TABLE, COLUMNS)
+
+    add_concurrent_index(
+      TABLE, [:pipeline_id_convert_to_bigint, :key],
+      name: INDEX_NAME, unique: true
+    )
+    add_concurrent_foreign_key(
+      TABLE, REFERENCING_TABLE,
+      column: :pipeline_id_convert_to_bigint, name: FK_NAME,
+      on_delete: :cascade, validate: true, reverse_lock_order: true
+    )
+  end
+end
diff --git a/db/schema_migrations/20231024073401 b/db/schema_migrations/20231024073401
new file mode 100644
index 0000000000000..c4879144126f2
--- /dev/null
+++ b/db/schema_migrations/20231024073401
@@ -0,0 +1 @@
+842fcd7c485ec4757810444172f2a0b7ec69ea0eda14662e0418ee3befdcaadc
\ No newline at end of file
diff --git a/db/structure.sql b/db/structure.sql
index 59e62cfcb3bc4..dd001675a2188 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -565,15 +565,6 @@ BEGIN
 END;
 $$;
 
-CREATE FUNCTION trigger_7f3d66a7d7f5() RETURNS trigger
-    LANGUAGE plpgsql
-    AS $$
-BEGIN
-  NEW."pipeline_id_convert_to_bigint" := NEW."pipeline_id";
-  RETURN NEW;
-END;
-$$;
-
 CREATE FUNCTION trigger_b2d852e1e2cb() RETURNS trigger
     LANGUAGE plpgsql
     AS $$
@@ -14242,7 +14233,6 @@ CREATE TABLE ci_pipeline_variables (
     encrypted_value text,
     encrypted_value_salt character varying,
     encrypted_value_iv character varying,
-    pipeline_id_convert_to_bigint integer DEFAULT 0 NOT NULL,
     variable_type smallint DEFAULT 1 NOT NULL,
     partition_id bigint NOT NULL,
     raw boolean DEFAULT false NOT NULL,
@@ -32039,8 +32029,6 @@ CREATE INDEX index_ci_pipeline_schedules_on_project_id ON ci_pipeline_schedules
 
 CREATE UNIQUE INDEX index_ci_pipeline_variables_on_pipeline_id_and_key ON ci_pipeline_variables USING btree (pipeline_id, key);
 
-CREATE UNIQUE INDEX index_ci_pipeline_variables_on_pipeline_id_bigint_and_key ON ci_pipeline_variables USING btree (pipeline_id_convert_to_bigint, key);
-
 CREATE INDEX index_ci_pipelines_config_on_pipeline_id ON ci_pipelines_config USING btree (pipeline_id);
 
 CREATE INDEX index_ci_pipelines_for_ondemand_dast_scans ON ci_pipelines USING btree (id) WHERE (source = 13);
@@ -36931,8 +36919,6 @@ CREATE TRIGGER trigger_10ee1357e825 BEFORE INSERT OR UPDATE ON p_ci_builds FOR E
 
 CREATE TRIGGER trigger_1bd97da9c1a4 BEFORE INSERT OR UPDATE ON ci_pipelines FOR EACH ROW EXECUTE FUNCTION trigger_1bd97da9c1a4();
 
-CREATE TRIGGER trigger_7f3d66a7d7f5 BEFORE INSERT OR UPDATE ON ci_pipeline_variables FOR EACH ROW EXECUTE FUNCTION trigger_7f3d66a7d7f5();
-
 CREATE TRIGGER trigger_b2d852e1e2cb BEFORE INSERT OR UPDATE ON ci_pipelines FOR EACH ROW EXECUTE FUNCTION trigger_b2d852e1e2cb();
 
 CREATE TRIGGER trigger_delete_project_namespace_on_project_delete AFTER DELETE ON projects FOR EACH ROW WHEN ((old.project_namespace_id IS NOT NULL)) EXECUTE FUNCTION delete_associated_project_namespace();
@@ -39952,9 +39938,6 @@ ALTER TABLE issue_search_data
 ALTER TABLE product_analytics_events_experimental
     ADD CONSTRAINT product_analytics_events_experimental_project_id_fkey FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE;
 
-ALTER TABLE ONLY ci_pipeline_variables
-    ADD CONSTRAINT temp_fk_rails_8d3b04e3e1 FOREIGN KEY (pipeline_id_convert_to_bigint) REFERENCES ci_pipelines(id) ON DELETE CASCADE;
-
 ALTER TABLE ONLY user_follow_users
     ADD CONSTRAINT user_follow_users_followee_id_fkey FOREIGN KEY (followee_id) REFERENCES users(id) ON DELETE CASCADE;
 
-- 
GitLab