From e09fc6977889568a584d246f23e07d18c738342e Mon Sep 17 00:00:00 2001
From: pbair <pbair@gitlab.com>
Date: Wed, 4 Aug 2021 12:32:52 -0400
Subject: [PATCH] Prepare indexes for bigint column conversions

Add migrations to create indexes asynchronously for tables that need to
have columns converted from int to bigint. Creating the indexes
asynchronously will preventing the column swap migrations from running
for an excessive length of time.

Changelog: other
---
 ...s_for_ci_job_artifact_bigint_conversion.rb | 34 +++++++++++++++++++
 ...e_indexes_for_tagging_bigint_conversion.rb | 29 ++++++++++++++++
 ..._indexes_for_ci_stage_bigint_conversion.rb | 19 +++++++++++
 db/schema_migrations/20210804151444           |  1 +
 db/schema_migrations/20210804153307           |  1 +
 db/schema_migrations/20210804154407           |  1 +
 6 files changed, 85 insertions(+)
 create mode 100644 db/post_migrate/20210804151444_prepare_indexes_for_ci_job_artifact_bigint_conversion.rb
 create mode 100644 db/post_migrate/20210804153307_prepare_indexes_for_tagging_bigint_conversion.rb
 create mode 100644 db/post_migrate/20210804154407_prepare_indexes_for_ci_stage_bigint_conversion.rb
 create mode 100644 db/schema_migrations/20210804151444
 create mode 100644 db/schema_migrations/20210804153307
 create mode 100644 db/schema_migrations/20210804154407

diff --git a/db/post_migrate/20210804151444_prepare_indexes_for_ci_job_artifact_bigint_conversion.rb b/db/post_migrate/20210804151444_prepare_indexes_for_ci_job_artifact_bigint_conversion.rb
new file mode 100644
index 0000000000000000..8115465e311327f9
--- /dev/null
+++ b/db/post_migrate/20210804151444_prepare_indexes_for_ci_job_artifact_bigint_conversion.rb
@@ -0,0 +1,34 @@
+# frozen_string_literal: true
+
+class PrepareIndexesForCiJobArtifactBigintConversion < ActiveRecord::Migration[6.1]
+  include Gitlab::Database::MigrationHelpers
+
+  def up
+    prepare_async_index :ci_job_artifacts, :id_convert_to_bigint, unique: true,
+      name: :index_ci_job_artifact_on_id_convert_to_bigint
+
+    prepare_async_index :ci_job_artifacts, [:project_id, :id_convert_to_bigint], where: 'file_type = 18',
+      name: :index_ci_job_artifacts_for_terraform_reports_bigint
+
+    prepare_async_index :ci_job_artifacts, :id_convert_to_bigint, where: 'file_type = 18',
+      name: :index_ci_job_artifacts_id_for_terraform_reports_bigint
+
+    prepare_async_index :ci_job_artifacts, [:expire_at, :job_id_convert_to_bigint],
+      name: :index_ci_job_artifacts_on_expire_at_and_job_id_bigint
+
+    prepare_async_index :ci_job_artifacts, [:job_id_convert_to_bigint, :file_type], unique: true,
+      name: :index_ci_job_artifacts_on_job_id_and_file_type_bigint
+  end
+
+  def down
+    unprepare_async_index_by_name :ci_job_artifacts, :index_ci_job_artifacts_on_job_id_and_file_type_bigint
+
+    unprepare_async_index_by_name :ci_job_artifacts, :index_ci_job_artifacts_on_expire_at_and_job_id_bigint
+
+    unprepare_async_index_by_name :ci_job_artifacts, :index_ci_job_artifacts_id_for_terraform_reports_bigint
+
+    unprepare_async_index_by_name :ci_job_artifacts, :index_ci_job_artifacts_for_terraform_reports_bigint
+
+    unprepare_async_index_by_name :ci_job_artifacts, :index_ci_job_artifact_on_id_convert_to_bigint
+  end
+end
diff --git a/db/post_migrate/20210804153307_prepare_indexes_for_tagging_bigint_conversion.rb b/db/post_migrate/20210804153307_prepare_indexes_for_tagging_bigint_conversion.rb
new file mode 100644
index 0000000000000000..98f90bafce3486cb
--- /dev/null
+++ b/db/post_migrate/20210804153307_prepare_indexes_for_tagging_bigint_conversion.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+class PrepareIndexesForTaggingBigintConversion < ActiveRecord::Migration[6.1]
+  include Gitlab::Database::MigrationHelpers
+
+  def up
+    prepare_async_index :taggings, :id_convert_to_bigint, unique: true,
+      name: :index_taggings_on_id_convert_to_bigint
+
+    prepare_async_index :taggings, [:taggable_id_convert_to_bigint, :taggable_type],
+      name: :i_taggings_on_taggable_id_convert_to_bigint_and_taggable_type
+
+    prepare_async_index :taggings, [:taggable_id_convert_to_bigint, :taggable_type, :context],
+      name: :i_taggings_on_taggable_bigint_and_taggable_type_and_context
+
+    prepare_async_index :taggings, [:tag_id, :taggable_id_convert_to_bigint, :taggable_type, :context, :tagger_id, :tagger_type],
+      unique: true, name: :taggings_idx_tmp
+  end
+
+  def down
+    unprepare_async_index_by_name :taggings, :taggings_idx_tmp
+
+    unprepare_async_index_by_name :taggings, :i_taggings_on_taggable_bigint_and_taggable_type_and_context
+
+    unprepare_async_index_by_name :taggings, :i_taggings_on_taggable_id_convert_to_bigint_and_taggable_type
+
+    unprepare_async_index_by_name :taggings, :index_taggings_on_id_convert_to_bigint
+  end
+end
diff --git a/db/post_migrate/20210804154407_prepare_indexes_for_ci_stage_bigint_conversion.rb b/db/post_migrate/20210804154407_prepare_indexes_for_ci_stage_bigint_conversion.rb
new file mode 100644
index 0000000000000000..82af595b2d38b3d7
--- /dev/null
+++ b/db/post_migrate/20210804154407_prepare_indexes_for_ci_stage_bigint_conversion.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+class PrepareIndexesForCiStageBigintConversion < ActiveRecord::Migration[6.1]
+  include Gitlab::Database::MigrationHelpers
+
+  def up
+    prepare_async_index :ci_stages, :id_convert_to_bigint, unique: true,
+      name: :index_ci_stages_on_id_convert_to_bigint
+
+    prepare_async_index :ci_stages, [:pipeline_id, :id_convert_to_bigint], where: 'status in (0, 1, 2, 8, 9, 10)',
+      name: :index_ci_stages_on_pipeline_id_and_id_convert_to_bigint
+  end
+
+  def down
+    unprepare_async_index_by_name :ci_stages, :index_ci_stages_on_pipeline_id_and_id_convert_to_bigint
+
+    unprepare_async_index_by_name :ci_stages, :index_ci_stages_on_id_convert_to_bigint
+  end
+end
diff --git a/db/schema_migrations/20210804151444 b/db/schema_migrations/20210804151444
new file mode 100644
index 0000000000000000..0d633c3da361219e
--- /dev/null
+++ b/db/schema_migrations/20210804151444
@@ -0,0 +1 @@
+4a6676e9185a99070751c91c71a7a9e6a845426d68567abf80a2e414251e5805
\ No newline at end of file
diff --git a/db/schema_migrations/20210804153307 b/db/schema_migrations/20210804153307
new file mode 100644
index 0000000000000000..ae7baaa4cf4f6ad2
--- /dev/null
+++ b/db/schema_migrations/20210804153307
@@ -0,0 +1 @@
+d73756410c7f37662c50bb05c372e6ac32ba81f232c07debcd42d1f679eb74ef
\ No newline at end of file
diff --git a/db/schema_migrations/20210804154407 b/db/schema_migrations/20210804154407
new file mode 100644
index 0000000000000000..dc0e40c711797dea
--- /dev/null
+++ b/db/schema_migrations/20210804154407
@@ -0,0 +1 @@
+ab7ee98704e844de4a3ba3ae14ea64dd46539e63d49c7c7e0d67ed03ebc3bbd4
\ No newline at end of file
-- 
GitLab