From 01dd38a88def947ad5a8b2e3f82939a5cd5c80ff Mon Sep 17 00:00:00 2001
From: Tianwen Chen <tchen@gitlab.com>
Date: Fri, 21 Jul 2023 15:09:19 +0000
Subject: [PATCH] Initialize the conversion for ci_stages.pipeline_id

Changelog: changed
---
 app/models/ci/stage.rb                        |  3 +++
 ...ize_conversion_of_ci_stages_pipeline_id.rb | 16 +++++++++++++++
 ...ckfill_ci_stages_pipeline_id_conversion.rb | 20 +++++++++++++++++++
 db/schema_migrations/20230717062425           |  1 +
 db/schema_migrations/20230717062445           |  1 +
 db/structure.sql                              | 12 +++++++++++
 6 files changed, 53 insertions(+)
 create mode 100644 db/migrate/20230717062425_initialize_conversion_of_ci_stages_pipeline_id.rb
 create mode 100644 db/post_migrate/20230717062445_backfill_ci_stages_pipeline_id_conversion.rb
 create mode 100644 db/schema_migrations/20230717062425
 create mode 100644 db/schema_migrations/20230717062445

diff --git a/app/models/ci/stage.rb b/app/models/ci/stage.rb
index 4f9a2e4456272..5cd85fcf5eead 100644
--- a/app/models/ci/stage.rb
+++ b/app/models/ci/stage.rb
@@ -8,9 +8,12 @@ class Stage < Ci::ApplicationRecord
     include Gitlab::OptimisticLocking
     include Presentable
     include SafelyChangeColumnDefault
+    include IgnorableColumns
 
     columns_changing_default :partition_id
 
+    ignore_column :pipeline_id_convert_to_bigint, remove_with: '16.6', remove_after: '2023-10-22'
+
     partitionable scope: :pipeline
 
     enum status: Ci::HasStatus::STATUSES_ENUM
diff --git a/db/migrate/20230717062425_initialize_conversion_of_ci_stages_pipeline_id.rb b/db/migrate/20230717062425_initialize_conversion_of_ci_stages_pipeline_id.rb
new file mode 100644
index 0000000000000..70d6406bb6753
--- /dev/null
+++ b/db/migrate/20230717062425_initialize_conversion_of_ci_stages_pipeline_id.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+class InitializeConversionOfCiStagesPipelineId < Gitlab::Database::Migration[2.1]
+  disable_ddl_transaction!
+
+  TABLE = :ci_stages
+  COLUMNS = %i[pipeline_id]
+
+  def up
+    initialize_conversion_of_integer_to_bigint(TABLE, COLUMNS)
+  end
+
+  def down
+    revert_initialize_conversion_of_integer_to_bigint(TABLE, COLUMNS)
+  end
+end
diff --git a/db/post_migrate/20230717062445_backfill_ci_stages_pipeline_id_conversion.rb b/db/post_migrate/20230717062445_backfill_ci_stages_pipeline_id_conversion.rb
new file mode 100644
index 0000000000000..d93517fb49b01
--- /dev/null
+++ b/db/post_migrate/20230717062445_backfill_ci_stages_pipeline_id_conversion.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+# See https://docs.gitlab.com/ee/development/migration_style_guide.html
+# for more information on how to write migrations for GitLab.
+
+class BackfillCiStagesPipelineIdConversion < Gitlab::Database::Migration[2.1]
+  restrict_gitlab_migration gitlab_schema: :gitlab_ci
+
+  TABLE = :ci_stages
+  COLUMNS = %i[pipeline_id]
+  SUB_BATCH_SIZE = 250
+
+  def up
+    backfill_conversion_of_integer_to_bigint(TABLE, COLUMNS, sub_batch_size: SUB_BATCH_SIZE)
+  end
+
+  def down
+    revert_backfill_conversion_of_integer_to_bigint(TABLE, COLUMNS)
+  end
+end
diff --git a/db/schema_migrations/20230717062425 b/db/schema_migrations/20230717062425
new file mode 100644
index 0000000000000..735c10874e0a7
--- /dev/null
+++ b/db/schema_migrations/20230717062425
@@ -0,0 +1 @@
+5303be6b0da16a7b1fa64c1c99ef3809ee2f5c131daaf25e6073ffc4122fe8ec
\ No newline at end of file
diff --git a/db/schema_migrations/20230717062445 b/db/schema_migrations/20230717062445
new file mode 100644
index 0000000000000..20b05fded6528
--- /dev/null
+++ b/db/schema_migrations/20230717062445
@@ -0,0 +1 @@
+cb600935d177a342f082bf896ffbc23f638fae2d124a89f7604b84c1e0ca9a2a
\ No newline at end of file
diff --git a/db/structure.sql b/db/structure.sql
index 408d9d851aac7..d6689c5669cb9 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -249,6 +249,15 @@ 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_1a857e8db6cd() RETURNS trigger
     LANGUAGE plpgsql
     AS $$
@@ -14078,6 +14087,7 @@ CREATE TABLE ci_stages (
     "position" integer,
     id bigint NOT NULL,
     partition_id bigint NOT NULL,
+    pipeline_id_convert_to_bigint bigint,
     CONSTRAINT check_81b431e49b CHECK ((lock_version IS NOT NULL))
 );
 
@@ -35312,6 +35322,8 @@ 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_1a857e8db6cd BEFORE INSERT OR UPDATE ON vulnerability_occurrences FOR EACH ROW EXECUTE FUNCTION trigger_1a857e8db6cd();
 
 CREATE TRIGGER trigger_7f3d66a7d7f5 BEFORE INSERT OR UPDATE ON ci_pipeline_variables FOR EACH ROW EXECUTE FUNCTION trigger_7f3d66a7d7f5();
-- 
GitLab