Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'db/post_migrate')
-rw-r--r--db/post_migrate/20230317004428_migrate_daily_redis_hll_events_to_weekly_aggregation.rb6
-rw-r--r--db/post_migrate/20230405132104_remove_saml_provider_and_identities_non_root_group.rb37
-rw-r--r--db/post_migrate/20230419094939_swap_merge_request_metrics_id_to_bigint_for_self_hosts.rb4
-rw-r--r--db/post_migrate/20230612232000_queue_backfill_dismissal_reason_in_vulnerability_reads.rb25
-rw-r--r--db/post_migrate/20230701043315_ensure_backfill_for_ci_pipeline_variables_pipeline_id_is_finished.rb18
-rw-r--r--db/post_migrate/20230701053315_ensure_again_backfill_for_ci_pipeline_variables_pipeline_id_is_finished.rb23
-rw-r--r--db/post_migrate/20230702053002_create_async_index_for_ci_pipline_variables_pipeline_id.rb14
-rw-r--r--db/post_migrate/20230710024518_ensure_todos_bigint_backfill_completed_for_self_managed.rb29
-rw-r--r--db/post_migrate/20230710024903_swap_todos_note_id_to_bigint_for_self_managed.rb62
-rw-r--r--db/post_migrate/20230711033030_backfill_ci_pipeline_chat_data_pipeline_id_bigint_conversion.rb17
-rw-r--r--db/post_migrate/20230711182029_schedule_remove_temp_index_vulnerability_occurrences.rb14
-rw-r--r--db/post_migrate/20230712052619_drop_index_deployments_on_project_id_and_status.rb21
-rw-r--r--db/post_migrate/20230712054057_drop_index_deployments_on_project_id_sha.rb21
-rw-r--r--db/post_migrate/20230712055956_drop_index_deployments_on_environment_id_and_iid_and_project_id.rb21
-rw-r--r--db/post_migrate/20230712135428_backfill_ci_pipeline_messages_pipeline_id_bigint_conversion.rb17
-rw-r--r--db/post_migrate/20230712141733_remove_chat_names_integration_id_column.rb11
-rw-r--r--db/post_migrate/20230712145557_queue_backfill_missing_vulnerability_dismissal_details.rb27
-rw-r--r--db/post_migrate/20230713021230_remove_namespaces_users_managing_group_id_fk.rb20
-rw-r--r--db/post_migrate/20230713234121_create_index_vulnerability_findings_on_uuid_sync.rb21
-rw-r--r--db/post_migrate/20230714015909_add_index_for_member_expiring_query.rb18
-rw-r--r--db/post_migrate/20230714095946_schedule_unique_index_project_authorizations_on_unique_project_user.rb19
-rw-r--r--db/post_migrate/20230717055730_backfill_ci_pipelines_auto_canceled_by_id_conversion.rb17
-rw-r--r--db/post_migrate/20230717062445_backfill_ci_stages_pipeline_id_conversion.rb20
-rw-r--r--db/post_migrate/20230717091811_add_prepared_at_index_to_merge_requests_sync.rb15
-rw-r--r--db/post_migrate/20230717144729_drop_ci_job_artifacts_partition_id_default_v2.rb16
-rw-r--r--db/post_migrate/20230717144744_drop_ci_stages_partition_id_default_v2.rb16
-rw-r--r--db/post_migrate/20230717144802_drop_ci_build_trace_metadata_partition_id_default_v2.rb16
-rw-r--r--db/post_migrate/20230717144817_drop_ci_pipeline_variable_partition_id_default_v2.rb16
-rw-r--r--db/post_migrate/20230718020825_swap_events_target_id_to_bigint_for_gitlab_dot_com.rb49
-rw-r--r--db/post_migrate/20230718025027_cleanup_bigint_conversion_for_events_for_gitlab_com.rb13
-rw-r--r--db/post_migrate/20230718094501_prepare_notes_namespace_id_index.rb14
-rw-r--r--db/post_migrate/20230718145613_add_temp_index_for_project_statistics_pipeline_artifacts_size_migration.rb19
-rw-r--r--db/post_migrate/20230719083202_backfill_project_statistics_storage_size_without_pipeline_artifacts_size.rb29
-rw-r--r--db/post_migrate/20230721134004_index_project_id_and_package_manager_for_sbom_occurrences.rb15
-rw-r--r--db/post_migrate/20230721134546_index_project_id_component_id_and_id_for_sbom_occurrences.rb15
-rw-r--r--db/post_migrate/20230721181046_drop_index_issues_on_project_id_and_created_at_issue_type_incident.rb15
-rw-r--r--db/post_migrate/20230721194757_drop_index_issues_on_incident_issue_type.rb15
-rw-r--r--db/post_migrate/20230721200323_drop_index_on_issues_closed_incidents_by_project_id_and_closed_at.rb15
-rw-r--r--db/post_migrate/20230721200810_drop_index_on_issues_health_status_asc_order.rb18
-rw-r--r--db/post_migrate/20230721200849_drop_index_on_issues_health_status_desc_order.rb18
-rw-r--r--db/post_migrate/20230723171006_backfill_ci_sources_pipelines_source_conversion.rb17
-rw-r--r--db/post_migrate/20230724071541_queue_backfill_default_branch_protection_namespace_setting.rb26
-rw-r--r--db/post_migrate/20230724085146_replace_old_fk_p_ci_builds_metadata_to_builds_v3.rb47
-rw-r--r--db/post_migrate/20230724085149_replace_old_fk_p_ci_runner_machine_builds_to_builds_v3.rb47
-rw-r--r--db/post_migrate/20230724123547_cleanup_conversion_big_int_ci_build_needs_self_managed.rb31
-rw-r--r--db/post_migrate/20230724150939_index_projects_on_namespace_id_and_repository_size_limit.rb18
-rw-r--r--db/post_migrate/20230724164745_queue_delete_orphaned_transferred_project_approval_rules.rb25
-rw-r--r--db/post_migrate/20230724212040_add_temporary_indexes_for_orphaned_approval_rules.rb27
-rw-r--r--db/post_migrate/20230725035942_create_sync_index_for_ci_pipline_variables_pipeline_id.rb17
-rw-r--r--db/post_migrate/20230726024322_add_not_valid_foreign_key_for_ci_pipeline_variables_pipeline_id.rb29
-rw-r--r--db/post_migrate/20230726080827_add_issue_search_data_namespace_id_foreign_key.rb24
-rw-r--r--db/post_migrate/20230726201351_remove_issues_issue_type_column.rb13
-rw-r--r--db/post_migrate/20230726231029_remove_plan_limits_ci_active_pipelines_column.rb13
-rw-r--r--db/post_migrate/20230727102936_drop_prepared_at_index.rb17
-rw-r--r--db/post_migrate/20230727103144_add_prepared_at_created_at_index.rb20
-rw-r--r--db/post_migrate/20230727115635_index_vulnerability_reads_on_project_id_and_vulnerability_id.rb15
-rw-r--r--db/post_migrate/20230727132342_prepare_index_on_vulnerability_occurrences_uuid_async.rb21
-rw-r--r--db/post_migrate/20230728122928_prepare_index_on_vulnerability_occurrences_uuid_including_vulnerability_id_async.rb22
-rw-r--r--db/post_migrate/20230728134722_remove_application_settings_ignored_columns.rb55
-rw-r--r--db/post_migrate/20230728151058_add_auditor_index_to_users_table.rb14
-rw-r--r--db/post_migrate/20230728174927_add_epic_work_item_type.rb82
-rw-r--r--db/post_migrate/20230731090319_add_notes_namespace_id_foreign_key.rb19
-rw-r--r--db/post_migrate/20230731100513_add_index_on_vulnerability_reads_for_filtering.rb23
-rw-r--r--db/post_migrate/20230731210422_remove_temp_index_vulnerability_occurrences.rb15
-rw-r--r--db/post_migrate/20230801150214_retry_cleanup_bigint_conversion_for_events_for_gitlab_com.rb34
-rw-r--r--db/post_migrate/20230802085923_queue_fix_allow_descendants_override_disabled_shared_runners.rb26
-rw-r--r--db/post_migrate/20230802090519_drop_schema_inconsistencies_table.rb18
-rw-r--r--db/post_migrate/20230802092922_add_unique_index_project_authorizations_on_unique_project_user.rb19
-rw-r--r--db/post_migrate/20230804053643_add_ticket_work_item_type.rb87
-rw-r--r--db/post_migrate/20230804121704_remove_namespaces_user_details_enterprise_group_id_fk.rb20
-rw-r--r--db/post_migrate/20230804121705_remove_namespaces_user_details_provisioned_by_group_id_fk.rb20
-rw-r--r--db/post_migrate/20230807083334_add_linked_items_work_item_widget.rb58
-rw-r--r--db/post_migrate/20230807085752_ensure_id_uniqueness_for_p_ci_builds.rb49
-rw-r--r--db/post_migrate/20230808123136_cleanup_application_settings_database_apdex_settings_rename.rb21
-rw-r--r--db/post_migrate/20230808170646_remove_free_user_cap_over_limt_notified_at_column.rb9
-rw-r--r--db/post_migrate/20230809090349_ensure_id_uniqueness_for_p_ci_builds_v2.rb50
-rw-r--r--db/post_migrate/20230809104007_ensure_epic_user_mentions_bigint_backfill_is_finished_for_self_hosts.rb19
-rw-r--r--db/post_migrate/20230809104753_swap_epic_user_mentions_note_id_to_bigint_for_self_hosts.rb78
-rw-r--r--db/post_migrate/20230809133249_index_sbom_occurrences_on_project_id_component_id_and_input_file_path.rb15
-rw-r--r--db/post_migrate/20230810103141_ensure_suggestions_note_id_bigint_backfill_is_finished_for_self_hosts.rb21
-rw-r--r--db/post_migrate/20230810103534_swap_suggestions_note_id_to_bigint_for_self_hosts.rb64
-rw-r--r--db/post_migrate/20230810122746_ensure_snippet_user_mentions_bigint_backfill_is_finished_for_self_hosts.rb21
-rw-r--r--db/post_migrate/20230810123044_swap_snippet_user_mentions_note_id_to_bigint_for_self_hosts.rb78
-rw-r--r--db/post_migrate/20230811103654_ensure_vum_bigint_backfill_is_finished_for_self_hosts.rb21
-rw-r--r--db/post_migrate/20230811103941_swap_vulnerability_user_mentions_note_id_to_bigint_for_self_hosts.rb78
-rw-r--r--db/post_migrate/20230811145848_ensure_commit_user_mentions_note_id_bigint_backfill_is_finished_for_self_managed.rb23
-rw-r--r--db/post_migrate/20230811150636_swap_commit_user_mentions_note_id_to_bigint_for_self_managed.rb69
-rw-r--r--db/post_migrate/20230814143925_ensure_timelogs_note_id_bigint_backfill_is_finished_for_self_hosts.rb21
-rw-r--r--db/post_migrate/20230814144045_swap_timelogs_note_id_to_bigint_for_self_hosts.rb58
89 files changed, 2385 insertions, 25 deletions
diff --git a/db/post_migrate/20230317004428_migrate_daily_redis_hll_events_to_weekly_aggregation.rb b/db/post_migrate/20230317004428_migrate_daily_redis_hll_events_to_weekly_aggregation.rb
index 168354cd946..59bff26f964 100644
--- a/db/post_migrate/20230317004428_migrate_daily_redis_hll_events_to_weekly_aggregation.rb
+++ b/db/post_migrate/20230317004428_migrate_daily_redis_hll_events_to_weekly_aggregation.rb
@@ -3,12 +3,6 @@
class MigrateDailyRedisHllEventsToWeeklyAggregation < Gitlab::Database::Migration[2.1]
disable_ddl_transaction!
- # Due to the feature flag `use_metric_definitions_for_events_list`, this makes a `SELECT` call
- # so this migration is now considered a DML (data manipulation) migration.
- # For the time being, we need to specify only `main` database because it is now technically retriving DB data.
- # Should be removed with <issue link>
- restrict_gitlab_migration gitlab_schema: :gitlab_main
-
DAILY_EVENTS =
%w[g_edit_by_web_ide
g_edit_by_sfe
diff --git a/db/post_migrate/20230405132104_remove_saml_provider_and_identities_non_root_group.rb b/db/post_migrate/20230405132104_remove_saml_provider_and_identities_non_root_group.rb
index 55a017464c2..a3bf2cfda17 100644
--- a/db/post_migrate/20230405132104_remove_saml_provider_and_identities_non_root_group.rb
+++ b/db/post_migrate/20230405132104_remove_saml_provider_and_identities_non_root_group.rb
@@ -7,24 +7,27 @@ class RemoveSamlProviderAndIdentitiesNonRootGroup < Gitlab::Database::Migration[
restrict_gitlab_migration gitlab_schema: :gitlab_main
def up
- each_batch_range('saml_providers', scope: ->(table) { table.all }, of: BATCH_SIZE) do |min, max|
- execute <<~SQL
- DELETE FROM identities
- WHERE identities.saml_provider_id
- IN
- (
- SELECT saml_providers.id FROM saml_providers
- INNER JOIN namespaces ON namespaces.id=saml_providers.group_id
- AND namespaces.type='Group' AND namespaces.parent_id IS NOT NULL
- AND saml_providers.id BETWEEN #{min} AND #{max}
- );
+ ::Gitlab::Database
+ .allow_cross_joins_across_databases(url: "https://gitlab.com/gitlab-org/gitlab/-/issues/419931") do
+ each_batch_range('saml_providers', scope: ->(table) { table.all }, of: BATCH_SIZE) do |min, max|
+ execute <<~SQL
+ DELETE FROM identities
+ WHERE identities.saml_provider_id
+ IN
+ (
+ SELECT saml_providers.id FROM saml_providers
+ INNER JOIN namespaces ON namespaces.id=saml_providers.group_id
+ AND namespaces.type='Group' AND namespaces.parent_id IS NOT NULL
+ AND saml_providers.id BETWEEN #{min} AND #{max}
+ );
- DELETE FROM saml_providers
- USING namespaces
- WHERE namespaces.id=saml_providers.group_id
- AND namespaces.type='Group' AND namespaces.parent_id IS NOT NULL
- AND saml_providers.id BETWEEN #{min} AND #{max};
- SQL
+ DELETE FROM saml_providers
+ USING namespaces
+ WHERE namespaces.id=saml_providers.group_id
+ AND namespaces.type='Group' AND namespaces.parent_id IS NOT NULL
+ AND saml_providers.id BETWEEN #{min} AND #{max};
+ SQL
+ end
end
end
diff --git a/db/post_migrate/20230419094939_swap_merge_request_metrics_id_to_bigint_for_self_hosts.rb b/db/post_migrate/20230419094939_swap_merge_request_metrics_id_to_bigint_for_self_hosts.rb
index d249ffb9c1b..f27cc34aef8 100644
--- a/db/post_migrate/20230419094939_swap_merge_request_metrics_id_to_bigint_for_self_hosts.rb
+++ b/db/post_migrate/20230419094939_swap_merge_request_metrics_id_to_bigint_for_self_hosts.rb
@@ -31,8 +31,8 @@ class SwapMergeRequestMetricsIdToBigintForSelfHosts < Gitlab::Database::Migratio
def swap
add_concurrent_index TABLE_NAME, :id_convert_to_bigint, unique: true,
name: 'index_merge_request_metrics_on_id_convert_to_bigint'
- add_concurrent_index TABLE_NAME, 'target_project_id, merged_at DESC NULLS LAST, id_convert_to_bigint DESC',
- name: TMP_INDEX_NAME
+ add_concurrent_index TABLE_NAME, [:target_project_id, :merged_at, :id_convert_to_bigint],
+ name: TMP_INDEX_NAME, order: { merged_at: 'DESC NULLS LAST', id_convert_to_bigint: 'DESC' }
with_lock_retries(raise_on_exhaustion: true) do
execute "LOCK TABLE #{TABLE_NAME} IN ACCESS EXCLUSIVE MODE"
diff --git a/db/post_migrate/20230612232000_queue_backfill_dismissal_reason_in_vulnerability_reads.rb b/db/post_migrate/20230612232000_queue_backfill_dismissal_reason_in_vulnerability_reads.rb
new file mode 100644
index 00000000000..2f7c670795a
--- /dev/null
+++ b/db/post_migrate/20230612232000_queue_backfill_dismissal_reason_in_vulnerability_reads.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+class QueueBackfillDismissalReasonInVulnerabilityReads < Gitlab::Database::Migration[2.1]
+ MIGRATION = "BackfillDismissalReasonInVulnerabilityReads"
+ DELAY_INTERVAL = 2.minutes
+ BATCH_SIZE = 5000
+ SUB_BATCH_SIZE = 500
+
+ restrict_gitlab_migration gitlab_schema: :gitlab_main
+
+ def up
+ queue_batched_background_migration(
+ MIGRATION,
+ :vulnerability_reads,
+ :id,
+ job_interval: DELAY_INTERVAL,
+ batch_size: BATCH_SIZE,
+ sub_batch_size: SUB_BATCH_SIZE
+ )
+ end
+
+ def down
+ delete_batched_background_migration(MIGRATION, :vulnerability_reads, :id, [])
+ end
+end
diff --git a/db/post_migrate/20230701043315_ensure_backfill_for_ci_pipeline_variables_pipeline_id_is_finished.rb b/db/post_migrate/20230701043315_ensure_backfill_for_ci_pipeline_variables_pipeline_id_is_finished.rb
new file mode 100644
index 00000000000..28c520fea6b
--- /dev/null
+++ b/db/post_migrate/20230701043315_ensure_backfill_for_ci_pipeline_variables_pipeline_id_is_finished.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+class EnsureBackfillForCiPipelineVariablesPipelineIdIsFinished < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ restrict_gitlab_migration gitlab_schema: :gitlab_ci
+ disable_ddl_transaction!
+
+ TABLE_NAME = :ci_pipeline_variables
+
+ def up
+ # no-op
+ end
+
+ def down
+ # no-op
+ end
+end
diff --git a/db/post_migrate/20230701053315_ensure_again_backfill_for_ci_pipeline_variables_pipeline_id_is_finished.rb b/db/post_migrate/20230701053315_ensure_again_backfill_for_ci_pipeline_variables_pipeline_id_is_finished.rb
new file mode 100644
index 00000000000..90e8b4d96b3
--- /dev/null
+++ b/db/post_migrate/20230701053315_ensure_again_backfill_for_ci_pipeline_variables_pipeline_id_is_finished.rb
@@ -0,0 +1,23 @@
+# frozen_string_literal: true
+
+class EnsureAgainBackfillForCiPipelineVariablesPipelineIdIsFinished < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ restrict_gitlab_migration gitlab_schema: :gitlab_ci
+ disable_ddl_transaction!
+
+ TABLE_NAME = :ci_pipeline_variables
+
+ def up
+ ensure_batched_background_migration_is_finished(
+ job_class_name: 'CopyColumnUsingBackgroundMigrationJob',
+ table_name: TABLE_NAME,
+ column_name: 'id',
+ job_arguments: [['pipeline_id'], ['pipeline_id_convert_to_bigint']]
+ )
+ end
+
+ def down
+ # no-op
+ end
+end
diff --git a/db/post_migrate/20230702053002_create_async_index_for_ci_pipline_variables_pipeline_id.rb b/db/post_migrate/20230702053002_create_async_index_for_ci_pipline_variables_pipeline_id.rb
new file mode 100644
index 00000000000..0919dceed7e
--- /dev/null
+++ b/db/post_migrate/20230702053002_create_async_index_for_ci_pipline_variables_pipeline_id.rb
@@ -0,0 +1,14 @@
+# frozen_string_literal: true
+
+class CreateAsyncIndexForCiPiplineVariablesPipelineId < Gitlab::Database::Migration[2.1]
+ TABLE_NAME = :ci_pipeline_variables
+ INDEX_NAME = "index_ci_pipeline_variables_on_pipeline_id_bigint_and_key"
+
+ def up
+ prepare_async_index TABLE_NAME, [:pipeline_id_convert_to_bigint, :key], unique: true, name: INDEX_NAME
+ end
+
+ def down
+ unprepare_async_index TABLE_NAME, [:pipeline_id_convert_to_bigint, :key], unique: true, name: INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230710024518_ensure_todos_bigint_backfill_completed_for_self_managed.rb b/db/post_migrate/20230710024518_ensure_todos_bigint_backfill_completed_for_self_managed.rb
new file mode 100644
index 00000000000..5ea4f4f0594
--- /dev/null
+++ b/db/post_migrate/20230710024518_ensure_todos_bigint_backfill_completed_for_self_managed.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+class EnsureTodosBigintBackfillCompletedForSelfManaged < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ restrict_gitlab_migration gitlab_schema: :gitlab_main
+ disable_ddl_transaction!
+
+ def up
+ return if should_skip?
+
+ ensure_batched_background_migration_is_finished(
+ job_class_name: 'CopyColumnUsingBackgroundMigrationJob',
+ table_name: 'todos',
+ column_name: 'id',
+ job_arguments: [['note_id'], ['note_id_convert_to_bigint']]
+ )
+ end
+
+ def down
+ # no-op
+ end
+
+ private
+
+ def should_skip?
+ com_or_dev_or_test_but_not_jh?
+ end
+end
diff --git a/db/post_migrate/20230710024903_swap_todos_note_id_to_bigint_for_self_managed.rb b/db/post_migrate/20230710024903_swap_todos_note_id_to_bigint_for_self_managed.rb
new file mode 100644
index 00000000000..2d92be1e7d1
--- /dev/null
+++ b/db/post_migrate/20230710024903_swap_todos_note_id_to_bigint_for_self_managed.rb
@@ -0,0 +1,62 @@
+# frozen_string_literal: true
+
+class SwapTodosNoteIdToBigintForSelfManaged < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ disable_ddl_transaction!
+
+ TABLE_NAME = 'todos'
+
+ def up
+ return if should_skip?
+ return if temp_column_removed?(TABLE_NAME, :note_id)
+ return if columns_swapped?(TABLE_NAME, :note_id)
+
+ swap
+ end
+
+ def down
+ return if should_skip?
+ return if temp_column_removed?(TABLE_NAME, :note_id)
+ return unless columns_swapped?(TABLE_NAME, :note_id)
+
+ swap
+ end
+
+ def swap
+ # This will replace the existing index_todos_on_note_id
+ add_concurrent_index TABLE_NAME, :note_id_convert_to_bigint,
+ name: 'index_todos_on_note_id_convert_to_bigint'
+
+ # This will replace the existing fk_91d1f47b13
+ add_concurrent_foreign_key TABLE_NAME, :notes, column: :note_id_convert_to_bigint,
+ name: 'fk_todos_note_id_convert_to_bigint',
+ on_delete: :cascade
+
+ with_lock_retries(raise_on_exhaustion: true) do
+ execute "LOCK TABLE notes, #{TABLE_NAME} IN ACCESS EXCLUSIVE MODE"
+
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN note_id TO note_id_tmp"
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN note_id_convert_to_bigint TO note_id"
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN note_id_tmp TO note_id_convert_to_bigint"
+
+ function_name = Gitlab::Database::UnidirectionalCopyTrigger
+ .on_table(TABLE_NAME, connection: connection)
+ .name(:note_id, :note_id_convert_to_bigint)
+ execute "ALTER FUNCTION #{quote_table_name(function_name)} RESET ALL"
+
+ execute 'DROP INDEX IF EXISTS index_todos_on_note_id'
+ rename_index TABLE_NAME, 'index_todos_on_note_id_convert_to_bigint',
+ 'index_todos_on_note_id'
+
+ execute "ALTER TABLE #{TABLE_NAME} DROP CONSTRAINT IF EXISTS fk_91d1f47b13"
+ rename_constraint(TABLE_NAME, 'fk_todos_note_id_convert_to_bigint', 'fk_91d1f47b13')
+ end
+ end
+
+ private
+
+ def should_skip?
+ com_or_dev_or_test_but_not_jh?
+ end
+end
diff --git a/db/post_migrate/20230711033030_backfill_ci_pipeline_chat_data_pipeline_id_bigint_conversion.rb b/db/post_migrate/20230711033030_backfill_ci_pipeline_chat_data_pipeline_id_bigint_conversion.rb
new file mode 100644
index 00000000000..f5274006d0e
--- /dev/null
+++ b/db/post_migrate/20230711033030_backfill_ci_pipeline_chat_data_pipeline_id_bigint_conversion.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class BackfillCiPipelineChatDataPipelineIdBigintConversion < Gitlab::Database::Migration[2.1]
+ restrict_gitlab_migration gitlab_schema: :gitlab_ci
+
+ TABLE = :ci_pipeline_chat_data
+ COLUMNS = %i[pipeline_id]
+ SUB_BATCH_SIZE = 500
+
+ 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/post_migrate/20230711182029_schedule_remove_temp_index_vulnerability_occurrences.rb b/db/post_migrate/20230711182029_schedule_remove_temp_index_vulnerability_occurrences.rb
new file mode 100644
index 00000000000..ee286f40b1a
--- /dev/null
+++ b/db/post_migrate/20230711182029_schedule_remove_temp_index_vulnerability_occurrences.rb
@@ -0,0 +1,14 @@
+# frozen_string_literal: true
+
+class ScheduleRemoveTempIndexVulnerabilityOccurrences < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = 'tmp_idx_vulnerability_occurrences_on_id_where_report_type_7_99'
+
+ # TODO: Index to be destroyed synchronously in https://gitlab.com/gitlab-org/gitlab/-/issues/417880
+ def up
+ prepare_async_index_removal :vulnerability_occurrences, :id, where: 'report_type IN (7, 99)', name: INDEX_NAME
+ end
+
+ def down
+ unprepare_async_index :vulnerability_occurrences, :id, where: 'report_type IN (7, 99)', name: INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230712052619_drop_index_deployments_on_project_id_and_status.rb b/db/post_migrate/20230712052619_drop_index_deployments_on_project_id_and_status.rb
new file mode 100644
index 00000000000..0c76b73dcdc
--- /dev/null
+++ b/db/post_migrate/20230712052619_drop_index_deployments_on_project_id_and_status.rb
@@ -0,0 +1,21 @@
+# 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 DropIndexDeploymentsOnProjectIdAndStatus < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = 'index_deployments_on_project_id_and_status'
+
+ disable_ddl_transaction!
+
+ def up
+ remove_concurrent_index_by_name :deployments, name: INDEX_NAME
+ end
+
+ def down
+ # This is based on the following `CREATE INDEX` command in db/init_structure.sql:
+ # CREATE INDEX index_deployments_on_project_id_and_status ON deployments
+ # USING btree (project_id, status)
+ add_concurrent_index :deployments, %i[project_id status], name: INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230712054057_drop_index_deployments_on_project_id_sha.rb b/db/post_migrate/20230712054057_drop_index_deployments_on_project_id_sha.rb
new file mode 100644
index 00000000000..9916ee7f9ba
--- /dev/null
+++ b/db/post_migrate/20230712054057_drop_index_deployments_on_project_id_sha.rb
@@ -0,0 +1,21 @@
+# 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 DropIndexDeploymentsOnProjectIdSha < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = 'index_deployments_on_project_id_sha'
+
+ disable_ddl_transaction!
+
+ def up
+ remove_concurrent_index_by_name :deployments, name: INDEX_NAME
+ end
+
+ def down
+ # This is based on the following `CREATE INDEX` command in db/init_structure.sql:
+ # CREATE INDEX index_deployments_on_project_id_sha ON deployments
+ # USING btree (project_id, sha);
+ add_concurrent_index :deployments, %i[project_id sha], name: INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230712055956_drop_index_deployments_on_environment_id_and_iid_and_project_id.rb b/db/post_migrate/20230712055956_drop_index_deployments_on_environment_id_and_iid_and_project_id.rb
new file mode 100644
index 00000000000..52a02e68d14
--- /dev/null
+++ b/db/post_migrate/20230712055956_drop_index_deployments_on_environment_id_and_iid_and_project_id.rb
@@ -0,0 +1,21 @@
+# 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 DropIndexDeploymentsOnEnvironmentIdAndIidAndProjectId < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = 'index_deployments_on_environment_id_and_iid_and_project_id'
+
+ disable_ddl_transaction!
+
+ def up
+ remove_concurrent_index_by_name :deployments, name: INDEX_NAME
+ end
+
+ def down
+ # This is based on the following `CREATE INDEX` command in db/init_structure.sql:
+ # CREATE INDEX index_deployments_on_environment_id_and_iid_and_project_id ON deployments
+ # USING btree (environment_id, iid, project_id);
+ add_concurrent_index :deployments, %i[environment_id iid project_id], name: INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230712135428_backfill_ci_pipeline_messages_pipeline_id_bigint_conversion.rb b/db/post_migrate/20230712135428_backfill_ci_pipeline_messages_pipeline_id_bigint_conversion.rb
new file mode 100644
index 00000000000..6ac036d0f76
--- /dev/null
+++ b/db/post_migrate/20230712135428_backfill_ci_pipeline_messages_pipeline_id_bigint_conversion.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class BackfillCiPipelineMessagesPipelineIdBigintConversion < Gitlab::Database::Migration[2.1]
+ restrict_gitlab_migration gitlab_schema: :gitlab_ci
+
+ TABLE = :ci_pipeline_messages
+ COLUMNS = %i[pipeline_id]
+ SUB_BATCH_SIZE = 500
+
+ 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/post_migrate/20230712141733_remove_chat_names_integration_id_column.rb b/db/post_migrate/20230712141733_remove_chat_names_integration_id_column.rb
new file mode 100644
index 00000000000..0c89a9e1bf8
--- /dev/null
+++ b/db/post_migrate/20230712141733_remove_chat_names_integration_id_column.rb
@@ -0,0 +1,11 @@
+# frozen_string_literal: true
+
+class RemoveChatNamesIntegrationIdColumn < Gitlab::Database::Migration[2.1]
+ def up
+ remove_column :chat_names, :integration_id
+ end
+
+ def down
+ add_column :chat_names, :integration_id, :integer, if_not_exists: true
+ end
+end
diff --git a/db/post_migrate/20230712145557_queue_backfill_missing_vulnerability_dismissal_details.rb b/db/post_migrate/20230712145557_queue_backfill_missing_vulnerability_dismissal_details.rb
new file mode 100644
index 00000000000..b3e54111562
--- /dev/null
+++ b/db/post_migrate/20230712145557_queue_backfill_missing_vulnerability_dismissal_details.rb
@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+
+class QueueBackfillMissingVulnerabilityDismissalDetails < Gitlab::Database::Migration[2.1]
+ MIGRATION = "BackfillMissingVulnerabilityDismissalDetails"
+ DELAY_INTERVAL = 2.minutes
+ BATCH_SIZE = 500
+ SUB_BATCH_SIZE = 100
+
+ disable_ddl_transaction!
+
+ restrict_gitlab_migration gitlab_schema: :gitlab_main
+
+ def up
+ queue_batched_background_migration(
+ MIGRATION,
+ :vulnerabilities,
+ :id,
+ job_interval: DELAY_INTERVAL,
+ batch_size: BATCH_SIZE,
+ sub_batch_size: SUB_BATCH_SIZE
+ )
+ end
+
+ def down
+ delete_batched_background_migration(MIGRATION, :vulnerabilities, :id, [])
+ end
+end
diff --git a/db/post_migrate/20230713021230_remove_namespaces_users_managing_group_id_fk.rb b/db/post_migrate/20230713021230_remove_namespaces_users_managing_group_id_fk.rb
new file mode 100644
index 00000000000..0cc76d129ef
--- /dev/null
+++ b/db/post_migrate/20230713021230_remove_namespaces_users_managing_group_id_fk.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+class RemoveNamespacesUsersManagingGroupIdFk < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ def up
+ return unless foreign_key_exists?(:users, :namespaces, name: "fk_a4b8fefe3e")
+
+ with_lock_retries do
+ remove_foreign_key_if_exists(:users, :namespaces,
+ name: "fk_a4b8fefe3e", reverse_lock_order: true)
+ end
+ end
+
+ def down
+ add_concurrent_foreign_key(:users, :namespaces,
+ name: "fk_a4b8fefe3e", column: :managing_group_id,
+ target_column: :id, on_delete: :nullify)
+ end
+end
diff --git a/db/post_migrate/20230713234121_create_index_vulnerability_findings_on_uuid_sync.rb b/db/post_migrate/20230713234121_create_index_vulnerability_findings_on_uuid_sync.rb
new file mode 100644
index 00000000000..abbf2390b0d
--- /dev/null
+++ b/db/post_migrate/20230713234121_create_index_vulnerability_findings_on_uuid_sync.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+class CreateIndexVulnerabilityFindingsOnUuidSync < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = 'index_vuln_findings_on_uuid_including_vuln_id'
+
+ disable_ddl_transaction!
+
+ def up
+ return if index_exists_by_name?(:vulnerability_occurrences, INDEX_NAME)
+
+ disable_statement_timeout do
+ execute <<~SQL
+ CREATE UNIQUE INDEX CONCURRENTLY #{INDEX_NAME} ON vulnerability_occurrences (uuid) include (vulnerability_id);
+ SQL
+ end
+ end
+
+ def down
+ remove_concurrent_index_by_name(:vulnerability_occurrences, INDEX_NAME)
+ end
+end
diff --git a/db/post_migrate/20230714015909_add_index_for_member_expiring_query.rb b/db/post_migrate/20230714015909_add_index_for_member_expiring_query.rb
new file mode 100644
index 00000000000..4d98d4792af
--- /dev/null
+++ b/db/post_migrate/20230714015909_add_index_for_member_expiring_query.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+class AddIndexForMemberExpiringQuery < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = 'index_members_on_expiring_at_access_level_id'
+
+ disable_ddl_transaction!
+
+ def up
+ add_concurrent_index :members,
+ [:expires_at, :access_level, :id],
+ where: 'requested_at IS NULL AND expiry_notified_at IS NULL',
+ name: INDEX_NAME
+ end
+
+ def down
+ remove_concurrent_index_by_name :members, INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230714095946_schedule_unique_index_project_authorizations_on_unique_project_user.rb b/db/post_migrate/20230714095946_schedule_unique_index_project_authorizations_on_unique_project_user.rb
new file mode 100644
index 00000000000..d4d51131dda
--- /dev/null
+++ b/db/post_migrate/20230714095946_schedule_unique_index_project_authorizations_on_unique_project_user.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+class ScheduleUniqueIndexProjectAuthorizationsOnUniqueProjectUser < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = 'index_unique_project_authorizations_on_unique_project_user'
+
+ def up
+ prepare_async_index :project_authorizations,
+ %i[project_id user_id],
+ unique: true,
+ where: "is_unique",
+ name: INDEX_NAME
+ end
+
+ def down
+ unprepare_async_index :project_authorizations,
+ %i[project_id user_id],
+ name: INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230717055730_backfill_ci_pipelines_auto_canceled_by_id_conversion.rb b/db/post_migrate/20230717055730_backfill_ci_pipelines_auto_canceled_by_id_conversion.rb
new file mode 100644
index 00000000000..58b91c44b79
--- /dev/null
+++ b/db/post_migrate/20230717055730_backfill_ci_pipelines_auto_canceled_by_id_conversion.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class BackfillCiPipelinesAutoCanceledByIdConversion < Gitlab::Database::Migration[2.1]
+ restrict_gitlab_migration gitlab_schema: :gitlab_ci
+
+ TABLE = :ci_pipelines
+ COLUMNS = %i[auto_canceled_by_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/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 00000000000..d93517fb49b
--- /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/post_migrate/20230717091811_add_prepared_at_index_to_merge_requests_sync.rb b/db/post_migrate/20230717091811_add_prepared_at_index_to_merge_requests_sync.rb
new file mode 100644
index 00000000000..4f05c5634b1
--- /dev/null
+++ b/db/post_migrate/20230717091811_add_prepared_at_index_to_merge_requests_sync.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+class AddPreparedAtIndexToMergeRequestsSync < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ INDEX_NAME = 'index_merge_requests_on_id_and_prepared_at'
+
+ def up
+ add_concurrent_index :merge_requests, :id, name: INDEX_NAME, where: "prepared_at IS NULL"
+ end
+
+ def down
+ remove_concurrent_index_by_name :merge_requests, INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230717144729_drop_ci_job_artifacts_partition_id_default_v2.rb b/db/post_migrate/20230717144729_drop_ci_job_artifacts_partition_id_default_v2.rb
new file mode 100644
index 00000000000..a3b2cd324fb
--- /dev/null
+++ b/db/post_migrate/20230717144729_drop_ci_job_artifacts_partition_id_default_v2.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+class DropCiJobArtifactsPartitionIdDefaultV2 < Gitlab::Database::Migration[2.1]
+ enable_lock_retries!
+
+ TABLE_NAME = :ci_job_artifacts
+ COLUMN_NAME = :partition_id
+
+ def up
+ remove_column_default(TABLE_NAME, COLUMN_NAME)
+ end
+
+ def down
+ change_column_default(TABLE_NAME, COLUMN_NAME, from: nil, to: 100)
+ end
+end
diff --git a/db/post_migrate/20230717144744_drop_ci_stages_partition_id_default_v2.rb b/db/post_migrate/20230717144744_drop_ci_stages_partition_id_default_v2.rb
new file mode 100644
index 00000000000..d32b8718281
--- /dev/null
+++ b/db/post_migrate/20230717144744_drop_ci_stages_partition_id_default_v2.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+class DropCiStagesPartitionIdDefaultV2 < Gitlab::Database::Migration[2.1]
+ enable_lock_retries!
+
+ TABLE_NAME = :ci_stages
+ COLUMN_NAME = :partition_id
+
+ def up
+ remove_column_default(TABLE_NAME, COLUMN_NAME)
+ end
+
+ def down
+ change_column_default(TABLE_NAME, COLUMN_NAME, from: nil, to: 100)
+ end
+end
diff --git a/db/post_migrate/20230717144802_drop_ci_build_trace_metadata_partition_id_default_v2.rb b/db/post_migrate/20230717144802_drop_ci_build_trace_metadata_partition_id_default_v2.rb
new file mode 100644
index 00000000000..e189801c79b
--- /dev/null
+++ b/db/post_migrate/20230717144802_drop_ci_build_trace_metadata_partition_id_default_v2.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+class DropCiBuildTraceMetadataPartitionIdDefaultV2 < Gitlab::Database::Migration[2.1]
+ enable_lock_retries!
+
+ TABLE_NAME = :ci_build_trace_metadata
+ COLUMN_NAME = :partition_id
+
+ def up
+ remove_column_default(TABLE_NAME, COLUMN_NAME)
+ end
+
+ def down
+ change_column_default(TABLE_NAME, COLUMN_NAME, from: nil, to: 100)
+ end
+end
diff --git a/db/post_migrate/20230717144817_drop_ci_pipeline_variable_partition_id_default_v2.rb b/db/post_migrate/20230717144817_drop_ci_pipeline_variable_partition_id_default_v2.rb
new file mode 100644
index 00000000000..c75c0226b15
--- /dev/null
+++ b/db/post_migrate/20230717144817_drop_ci_pipeline_variable_partition_id_default_v2.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+class DropCiPipelineVariablePartitionIdDefaultV2 < Gitlab::Database::Migration[2.1]
+ enable_lock_retries!
+
+ TABLE_NAME = :ci_pipeline_variables
+ COLUMN_NAME = :partition_id
+
+ def up
+ remove_column_default(TABLE_NAME, COLUMN_NAME)
+ end
+
+ def down
+ change_column_default(TABLE_NAME, COLUMN_NAME, from: nil, to: 100)
+ end
+end
diff --git a/db/post_migrate/20230718020825_swap_events_target_id_to_bigint_for_gitlab_dot_com.rb b/db/post_migrate/20230718020825_swap_events_target_id_to_bigint_for_gitlab_dot_com.rb
new file mode 100644
index 00000000000..03e95e39649
--- /dev/null
+++ b/db/post_migrate/20230718020825_swap_events_target_id_to_bigint_for_gitlab_dot_com.rb
@@ -0,0 +1,49 @@
+# frozen_string_literal: true
+
+class SwapEventsTargetIdToBigintForGitlabDotCom < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ disable_ddl_transaction!
+
+ TABLE_NAME = 'events'
+
+ def up
+ return unless should_run?
+
+ swap
+ end
+
+ def down
+ return unless should_run?
+
+ swap
+ end
+
+ def swap
+ # This will replace the existing index_events_on_target_type_and_target_id_and_fingerprint
+ add_concurrent_index TABLE_NAME, [:target_type, :target_id_convert_to_bigint, :fingerprint],
+ name: :index_events_on_target_type_and_target_id_bigint_fingerprint,
+ unique: true
+
+ with_lock_retries(raise_on_exhaustion: true) do
+ execute "LOCK TABLE #{TABLE_NAME} IN ACCESS EXCLUSIVE MODE"
+
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN target_id TO target_id_tmp"
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN target_id_convert_to_bigint TO target_id"
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN target_id_tmp TO target_id_convert_to_bigint"
+
+ function_name = Gitlab::Database::UnidirectionalCopyTrigger
+ .on_table(TABLE_NAME, connection: connection)
+ .name(:target_id, :target_id_convert_to_bigint)
+ execute "ALTER FUNCTION #{quote_table_name(function_name)} RESET ALL"
+
+ execute 'DROP INDEX IF EXISTS index_events_on_target_type_and_target_id_and_fingerprint'
+ rename_index TABLE_NAME, 'index_events_on_target_type_and_target_id_bigint_fingerprint',
+ 'index_events_on_target_type_and_target_id_and_fingerprint'
+ end
+ end
+
+ def should_run?
+ com_or_dev_or_test_but_not_jh?
+ end
+end
diff --git a/db/post_migrate/20230718025027_cleanup_bigint_conversion_for_events_for_gitlab_com.rb b/db/post_migrate/20230718025027_cleanup_bigint_conversion_for_events_for_gitlab_com.rb
new file mode 100644
index 00000000000..647052b987c
--- /dev/null
+++ b/db/post_migrate/20230718025027_cleanup_bigint_conversion_for_events_for_gitlab_com.rb
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+# Turning this migration to a no-op due to incident https://gitlab.com/gitlab-com/gl-infra/production/-/issues/16102
+# Migration will be retried in 20230801150214_retry_cleanup_bigint_conversion_for_events_for_gitlab_com.rb
+class CleanupBigintConversionForEventsForGitlabCom < Gitlab::Database::Migration[2.1]
+ def up
+ # no-op
+ end
+
+ def down
+ # no-op
+ end
+end
diff --git a/db/post_migrate/20230718094501_prepare_notes_namespace_id_index.rb b/db/post_migrate/20230718094501_prepare_notes_namespace_id_index.rb
new file mode 100644
index 00000000000..41f57814c41
--- /dev/null
+++ b/db/post_migrate/20230718094501_prepare_notes_namespace_id_index.rb
@@ -0,0 +1,14 @@
+# frozen_string_literal: true
+
+class PrepareNotesNamespaceIdIndex < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = 'index_notes_on_namespace_id'
+
+ # TODO: Index to be created synchronously as part of https://gitlab.com/gitlab-org/gitlab/-/issues/416127
+ def up
+ prepare_async_index :notes, :namespace_id, name: INDEX_NAME
+ end
+
+ def down
+ unprepare_async_index :notes, :namespace_id, name: INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230718145613_add_temp_index_for_project_statistics_pipeline_artifacts_size_migration.rb b/db/post_migrate/20230718145613_add_temp_index_for_project_statistics_pipeline_artifacts_size_migration.rb
new file mode 100644
index 00000000000..17e1e9e81f8
--- /dev/null
+++ b/db/post_migrate/20230718145613_add_temp_index_for_project_statistics_pipeline_artifacts_size_migration.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+class AddTempIndexForProjectStatisticsPipelineArtifactsSizeMigration < Gitlab::Database::Migration[2.1]
+ INDEX_PROJECT_STATSISTICS_PIPELINE_ARTIFACTS_SIZE = 'tmp_index_project_statistics_pipeline_artifacts_size'
+
+ disable_ddl_transaction!
+
+ def up
+ # Temporary index is to be used to trigger a refresh of project_statistics with
+ # pipeline_artifacts_size != 0
+ add_concurrent_index :project_statistics, [:project_id],
+ name: INDEX_PROJECT_STATSISTICS_PIPELINE_ARTIFACTS_SIZE,
+ where: "pipeline_artifacts_size != 0"
+ end
+
+ def down
+ remove_concurrent_index_by_name :project_statistics, INDEX_PROJECT_STATSISTICS_PIPELINE_ARTIFACTS_SIZE
+ end
+end
diff --git a/db/post_migrate/20230719083202_backfill_project_statistics_storage_size_without_pipeline_artifacts_size.rb b/db/post_migrate/20230719083202_backfill_project_statistics_storage_size_without_pipeline_artifacts_size.rb
new file mode 100644
index 00000000000..135d3596960
--- /dev/null
+++ b/db/post_migrate/20230719083202_backfill_project_statistics_storage_size_without_pipeline_artifacts_size.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+class BackfillProjectStatisticsStorageSizeWithoutPipelineArtifactsSize < Gitlab::Database::Migration[2.1]
+ restrict_gitlab_migration gitlab_schema: :gitlab_main
+
+ MIGRATION = "BackfillProjectStatisticsStorageSizeWithoutPipelineArtifactsSizeJob"
+ DELAY_INTERVAL = 2.minutes
+ BATCH_SIZE = 500
+ SUB_BATCH_SIZE = 100
+
+ def up
+ return unless Gitlab.dev_or_test_env? || Gitlab.org_or_com?
+
+ queue_batched_background_migration(
+ MIGRATION,
+ :project_statistics,
+ :project_id,
+ job_interval: DELAY_INTERVAL,
+ batch_size: BATCH_SIZE,
+ sub_batch_size: SUB_BATCH_SIZE
+ )
+ end
+
+ def down
+ return unless Gitlab.dev_or_test_env? || Gitlab.org_or_com?
+
+ delete_batched_background_migration(MIGRATION, :project_statistics, :project_id, [])
+ end
+end
diff --git a/db/post_migrate/20230721134004_index_project_id_and_package_manager_for_sbom_occurrences.rb b/db/post_migrate/20230721134004_index_project_id_and_package_manager_for_sbom_occurrences.rb
new file mode 100644
index 00000000000..8a97263d724
--- /dev/null
+++ b/db/post_migrate/20230721134004_index_project_id_and_package_manager_for_sbom_occurrences.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+class IndexProjectIdAndPackageManagerForSbomOccurrences < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = 'index_sbom_occurrences_on_project_id_and_package_manager'
+
+ disable_ddl_transaction!
+
+ def up
+ add_concurrent_index :sbom_occurrences, [:project_id, :package_manager], name: INDEX_NAME
+ end
+
+ def down
+ remove_concurrent_index_by_name :sbom_occurrences, INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230721134546_index_project_id_component_id_and_id_for_sbom_occurrences.rb b/db/post_migrate/20230721134546_index_project_id_component_id_and_id_for_sbom_occurrences.rb
new file mode 100644
index 00000000000..3eff5ee3b32
--- /dev/null
+++ b/db/post_migrate/20230721134546_index_project_id_component_id_and_id_for_sbom_occurrences.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+class IndexProjectIdComponentIdAndIdForSbomOccurrences < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = 'index_sbom_occurrences_on_project_id_and_component_id_and_id'
+
+ disable_ddl_transaction!
+
+ def up
+ add_concurrent_index :sbom_occurrences, [:project_id, :component_id, :id], name: INDEX_NAME
+ end
+
+ def down
+ remove_concurrent_index_by_name :sbom_occurrences, INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230721181046_drop_index_issues_on_project_id_and_created_at_issue_type_incident.rb b/db/post_migrate/20230721181046_drop_index_issues_on_project_id_and_created_at_issue_type_incident.rb
new file mode 100644
index 00000000000..3e62f6affad
--- /dev/null
+++ b/db/post_migrate/20230721181046_drop_index_issues_on_project_id_and_created_at_issue_type_incident.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+class DropIndexIssuesOnProjectIdAndCreatedAtIssueTypeIncident < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ INDEX_NAME = 'index_issues_on_project_id_and_created_at_issue_type_incident'
+
+ def up
+ remove_concurrent_index_by_name :issues, name: INDEX_NAME
+ end
+
+ def down
+ add_concurrent_index :issues, [:project_id, :created_at], where: 'issue_type = 1', name: INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230721194757_drop_index_issues_on_incident_issue_type.rb b/db/post_migrate/20230721194757_drop_index_issues_on_incident_issue_type.rb
new file mode 100644
index 00000000000..ad9b0da28e2
--- /dev/null
+++ b/db/post_migrate/20230721194757_drop_index_issues_on_incident_issue_type.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+class DropIndexIssuesOnIncidentIssueType < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ INDEX_NAME = 'index_issues_on_incident_issue_type'
+
+ def up
+ remove_concurrent_index_by_name :issues, name: INDEX_NAME
+ end
+
+ def down
+ add_concurrent_index :issues, :issue_type, where: 'issue_type = 1', name: INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230721200323_drop_index_on_issues_closed_incidents_by_project_id_and_closed_at.rb b/db/post_migrate/20230721200323_drop_index_on_issues_closed_incidents_by_project_id_and_closed_at.rb
new file mode 100644
index 00000000000..243c9f458d9
--- /dev/null
+++ b/db/post_migrate/20230721200323_drop_index_on_issues_closed_incidents_by_project_id_and_closed_at.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+class DropIndexOnIssuesClosedIncidentsByProjectIdAndClosedAt < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ INDEX_NAME = 'index_on_issues_closed_incidents_by_project_id_and_closed_at'
+
+ def up
+ remove_concurrent_index_by_name :issues, name: INDEX_NAME
+ end
+
+ def down
+ add_concurrent_index :issues, [:project_id, :closed_at], where: 'issue_type = 1 AND state_id = 2', name: INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230721200810_drop_index_on_issues_health_status_asc_order.rb b/db/post_migrate/20230721200810_drop_index_on_issues_health_status_asc_order.rb
new file mode 100644
index 00000000000..a3cc4ff7107
--- /dev/null
+++ b/db/post_migrate/20230721200810_drop_index_on_issues_health_status_asc_order.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+class DropIndexOnIssuesHealthStatusAscOrder < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ INDEX_NAME = 'index_on_issues_health_status_asc_order'
+
+ def up
+ remove_concurrent_index_by_name :issues, name: INDEX_NAME
+ end
+
+ def down
+ add_concurrent_index :issues,
+ [:project_id, :health_status, :id, :state_id, :issue_type],
+ order: { health_status: 'ASC NULLS LAST', id: :desc },
+ name: INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230721200849_drop_index_on_issues_health_status_desc_order.rb b/db/post_migrate/20230721200849_drop_index_on_issues_health_status_desc_order.rb
new file mode 100644
index 00000000000..a120268b800
--- /dev/null
+++ b/db/post_migrate/20230721200849_drop_index_on_issues_health_status_desc_order.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+class DropIndexOnIssuesHealthStatusDescOrder < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ INDEX_NAME = 'index_on_issues_health_status_desc_order'
+
+ def up
+ remove_concurrent_index_by_name :issues, name: INDEX_NAME
+ end
+
+ def down
+ add_concurrent_index :issues,
+ [:project_id, :health_status, :id, :state_id, :issue_type],
+ order: { health_status: 'DESC NULLS LAST', id: :desc },
+ name: INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230723171006_backfill_ci_sources_pipelines_source_conversion.rb b/db/post_migrate/20230723171006_backfill_ci_sources_pipelines_source_conversion.rb
new file mode 100644
index 00000000000..7b5cfa9f49a
--- /dev/null
+++ b/db/post_migrate/20230723171006_backfill_ci_sources_pipelines_source_conversion.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class BackfillCiSourcesPipelinesSourceConversion < Gitlab::Database::Migration[2.1]
+ restrict_gitlab_migration gitlab_schema: :gitlab_ci
+
+ TABLE = :ci_sources_pipelines
+ COLUMNS = %i[pipeline_id source_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/post_migrate/20230724071541_queue_backfill_default_branch_protection_namespace_setting.rb b/db/post_migrate/20230724071541_queue_backfill_default_branch_protection_namespace_setting.rb
new file mode 100644
index 00000000000..57debd9d0a2
--- /dev/null
+++ b/db/post_migrate/20230724071541_queue_backfill_default_branch_protection_namespace_setting.rb
@@ -0,0 +1,26 @@
+# frozen_string_literal: true
+
+class QueueBackfillDefaultBranchProtectionNamespaceSetting < Gitlab::Database::Migration[2.1]
+ MIGRATION = "BackfillDefaultBranchProtectionNamespaceSetting"
+ DELAY_INTERVAL = 2.minutes
+ BATCH_SIZE = 10_000
+ SUB_BATCH_SIZE = 100
+
+ disable_ddl_transaction!
+ restrict_gitlab_migration gitlab_schema: :gitlab_main
+
+ def up
+ queue_batched_background_migration(
+ MIGRATION,
+ :namespace_settings,
+ :namespace_id,
+ job_interval: DELAY_INTERVAL,
+ batch_size: BATCH_SIZE,
+ sub_batch_size: SUB_BATCH_SIZE
+ )
+ end
+
+ def down
+ delete_batched_background_migration(MIGRATION, :namespace_settings, :namespace_id, [])
+ end
+end
diff --git a/db/post_migrate/20230724085146_replace_old_fk_p_ci_builds_metadata_to_builds_v3.rb b/db/post_migrate/20230724085146_replace_old_fk_p_ci_builds_metadata_to_builds_v3.rb
new file mode 100644
index 00000000000..acfed3032eb
--- /dev/null
+++ b/db/post_migrate/20230724085146_replace_old_fk_p_ci_builds_metadata_to_builds_v3.rb
@@ -0,0 +1,47 @@
+# frozen_string_literal: true
+
+class ReplaceOldFkPCiBuildsMetadataToBuildsV3 < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::PartitioningMigrationHelpers
+
+ disable_ddl_transaction!
+
+ def up
+ return if new_foreign_key_exists?
+
+ with_lock_retries do
+ remove_foreign_key_if_exists :p_ci_builds_metadata, :ci_builds,
+ name: :fk_e20479742e_p, reverse_lock_order: true
+
+ rename_constraint :p_ci_builds_metadata, :temp_fk_e20479742e_p, :fk_e20479742e_p
+
+ Gitlab::Database::PostgresPartitionedTable.each_partition(:p_ci_builds_metadata) do |partition|
+ rename_constraint partition.identifier, :temp_fk_e20479742e_p, :fk_e20479742e_p
+ end
+ end
+ end
+
+ def down
+ return unless new_foreign_key_exists?
+
+ add_concurrent_partitioned_foreign_key :p_ci_builds_metadata, :ci_builds,
+ name: :temp_fk_e20479742e_p,
+ column: [:partition_id, :build_id],
+ target_column: [:partition_id, :id],
+ on_update: :cascade,
+ on_delete: :cascade,
+ validate: true,
+ reverse_lock_order: true
+
+ switch_constraint_names :p_ci_builds_metadata, :fk_e20479742e_p, :temp_fk_e20479742e_p
+
+ Gitlab::Database::PostgresPartitionedTable.each_partition(:p_ci_builds_metadata) do |partition|
+ switch_constraint_names partition.identifier, :fk_e20479742e_p, :temp_fk_e20479742e_p
+ end
+ end
+
+ private
+
+ def new_foreign_key_exists?
+ foreign_key_exists?(:p_ci_builds_metadata, :p_ci_builds, name: :fk_e20479742e_p)
+ end
+end
diff --git a/db/post_migrate/20230724085149_replace_old_fk_p_ci_runner_machine_builds_to_builds_v3.rb b/db/post_migrate/20230724085149_replace_old_fk_p_ci_runner_machine_builds_to_builds_v3.rb
new file mode 100644
index 00000000000..d3919148240
--- /dev/null
+++ b/db/post_migrate/20230724085149_replace_old_fk_p_ci_runner_machine_builds_to_builds_v3.rb
@@ -0,0 +1,47 @@
+# frozen_string_literal: true
+
+class ReplaceOldFkPCiRunnerMachineBuildsToBuildsV3 < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::PartitioningMigrationHelpers
+
+ disable_ddl_transaction!
+
+ def up
+ return if new_foreign_key_exists?
+
+ with_lock_retries do
+ remove_foreign_key_if_exists :p_ci_runner_machine_builds, :ci_builds,
+ name: :fk_bb490f12fe_p, reverse_lock_order: true
+
+ rename_constraint :p_ci_runner_machine_builds, :temp_fk_bb490f12fe_p, :fk_bb490f12fe_p
+
+ Gitlab::Database::PostgresPartitionedTable.each_partition(:p_ci_runner_machine_builds) do |partition|
+ rename_constraint partition.identifier, :temp_fk_bb490f12fe_p, :fk_bb490f12fe_p
+ end
+ end
+ end
+
+ def down
+ return unless new_foreign_key_exists?
+
+ add_concurrent_partitioned_foreign_key :p_ci_runner_machine_builds, :ci_builds,
+ name: :temp_fk_bb490f12fe_p,
+ column: [:partition_id, :build_id],
+ target_column: [:partition_id, :id],
+ on_update: :cascade,
+ on_delete: :cascade,
+ validate: true,
+ reverse_lock_order: true
+
+ switch_constraint_names :p_ci_runner_machine_builds, :fk_bb490f12fe_p, :temp_fk_bb490f12fe_p
+
+ Gitlab::Database::PostgresPartitionedTable.each_partition(:p_ci_runner_machine_builds) do |partition|
+ switch_constraint_names partition.identifier, :fk_bb490f12fe_p, :temp_fk_bb490f12fe_p
+ end
+ end
+
+ private
+
+ def new_foreign_key_exists?
+ foreign_key_exists?(:p_ci_runner_machine_builds, :p_ci_builds, name: :fk_bb490f12fe_p)
+ end
+end
diff --git a/db/post_migrate/20230724123547_cleanup_conversion_big_int_ci_build_needs_self_managed.rb b/db/post_migrate/20230724123547_cleanup_conversion_big_int_ci_build_needs_self_managed.rb
new file mode 100644
index 00000000000..5fbb5bd47cd
--- /dev/null
+++ b/db/post_migrate/20230724123547_cleanup_conversion_big_int_ci_build_needs_self_managed.rb
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+class CleanupConversionBigIntCiBuildNeedsSelfManaged < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ enable_lock_retries!
+
+ TABLE = :ci_build_needs
+
+ def up
+ return if should_skip?
+ return unless column_exists?(TABLE, :id_convert_to_bigint)
+
+ # rubocop:disable Migration/WithLockRetriesDisallowedMethod
+ with_lock_retries do
+ cleanup_conversion_of_integer_to_bigint(TABLE, :id)
+ end
+ # rubocop:enable Migration/WithLockRetriesDisallowedMethod
+ end
+
+ def down
+ return if should_skip?
+ return if column_exists?(TABLE, :id_convert_to_bigint)
+
+ restore_conversion_of_integer_to_bigint(TABLE, :id)
+ end
+
+ def should_skip?
+ com_or_dev_or_test_but_not_jh?
+ end
+end
diff --git a/db/post_migrate/20230724150939_index_projects_on_namespace_id_and_repository_size_limit.rb b/db/post_migrate/20230724150939_index_projects_on_namespace_id_and_repository_size_limit.rb
new file mode 100644
index 00000000000..b066cb248fb
--- /dev/null
+++ b/db/post_migrate/20230724150939_index_projects_on_namespace_id_and_repository_size_limit.rb
@@ -0,0 +1,18 @@
+# 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 IndexProjectsOnNamespaceIdAndRepositorySizeLimit < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = "index_projects_on_namespace_id_and_repository_size_limit"
+
+ disable_ddl_transaction!
+
+ def up
+ add_concurrent_index :projects, [:namespace_id, :repository_size_limit], name: INDEX_NAME
+ end
+
+ def down
+ remove_concurrent_index :projects, [:namespace_id, :repository_size_limit], name: INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230724164745_queue_delete_orphaned_transferred_project_approval_rules.rb b/db/post_migrate/20230724164745_queue_delete_orphaned_transferred_project_approval_rules.rb
new file mode 100644
index 00000000000..7cc1255f54a
--- /dev/null
+++ b/db/post_migrate/20230724164745_queue_delete_orphaned_transferred_project_approval_rules.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+class QueueDeleteOrphanedTransferredProjectApprovalRules < Gitlab::Database::Migration[2.1]
+ restrict_gitlab_migration gitlab_schema: :gitlab_main
+
+ MIGRATION = "DeleteOrphanedTransferredProjectApprovalRules"
+ DELAY_INTERVAL = 2.minutes
+ BATCH_SIZE = 500
+ SUB_BATCH_SIZE = 50
+
+ def up
+ queue_batched_background_migration(
+ MIGRATION,
+ :approval_project_rules,
+ :id,
+ job_interval: DELAY_INTERVAL,
+ batch_size: BATCH_SIZE,
+ sub_batch_size: SUB_BATCH_SIZE
+ )
+ end
+
+ def down
+ delete_batched_background_migration(MIGRATION, :approval_project_rules, :id, [])
+ end
+end
diff --git a/db/post_migrate/20230724212040_add_temporary_indexes_for_orphaned_approval_rules.rb b/db/post_migrate/20230724212040_add_temporary_indexes_for_orphaned_approval_rules.rb
new file mode 100644
index 00000000000..69e5f7d48ac
--- /dev/null
+++ b/db/post_migrate/20230724212040_add_temporary_indexes_for_orphaned_approval_rules.rb
@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+
+class AddTemporaryIndexesForOrphanedApprovalRules < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ LICENSE_SCANNING = 2
+ SCAN_FINDING = 4
+
+ TMP_PROJECT_INDEX_NAME = 'tmp_idx_orphaned_approval_project_rules'
+ TMP_MR_INDEX_NAME = 'tmp_idx_orphaned_approval_merge_request_rules'
+
+ def up
+ add_concurrent_index('approval_project_rules', :id, where: query_condition, name: TMP_PROJECT_INDEX_NAME)
+ add_concurrent_index('approval_merge_request_rules', :id, where: query_condition, name: TMP_MR_INDEX_NAME)
+ end
+
+ def down
+ remove_concurrent_index_by_name('approval_project_rules', TMP_PROJECT_INDEX_NAME)
+ remove_concurrent_index_by_name('approval_merge_request_rules', TMP_MR_INDEX_NAME)
+ end
+
+ private
+
+ def query_condition
+ "report_type IN (#{LICENSE_SCANNING}, #{SCAN_FINDING}) AND security_orchestration_policy_configuration_id IS NULL"
+ end
+end
diff --git a/db/post_migrate/20230725035942_create_sync_index_for_ci_pipline_variables_pipeline_id.rb b/db/post_migrate/20230725035942_create_sync_index_for_ci_pipline_variables_pipeline_id.rb
new file mode 100644
index 00000000000..9b2207fd10f
--- /dev/null
+++ b/db/post_migrate/20230725035942_create_sync_index_for_ci_pipline_variables_pipeline_id.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class CreateSyncIndexForCiPiplineVariablesPipelineId < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ TABLE_NAME = :ci_pipeline_variables
+ INDEX_NAME = 'index_ci_pipeline_variables_on_pipeline_id_bigint_and_key'
+ COLUMNS = [:pipeline_id_convert_to_bigint, :key]
+
+ def up
+ add_concurrent_index TABLE_NAME, COLUMNS, unique: true, name: INDEX_NAME
+ end
+
+ def down
+ remove_concurrent_index_by_name TABLE_NAME, INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230726024322_add_not_valid_foreign_key_for_ci_pipeline_variables_pipeline_id.rb b/db/post_migrate/20230726024322_add_not_valid_foreign_key_for_ci_pipeline_variables_pipeline_id.rb
new file mode 100644
index 00000000000..f9785aca1ae
--- /dev/null
+++ b/db/post_migrate/20230726024322_add_not_valid_foreign_key_for_ci_pipeline_variables_pipeline_id.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+class AddNotValidForeignKeyForCiPipelineVariablesPipelineId < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ SOURCE_TABLE = :ci_pipeline_variables
+ TARGET_TABLE = :ci_pipelines
+ COLUMN_NAME = :pipeline_id_convert_to_bigint
+ FK_NAME = 'temp_fk_rails_8d3b04e3e1'
+
+ def up
+ add_concurrent_foreign_key(
+ SOURCE_TABLE, TARGET_TABLE,
+ name: FK_NAME,
+ column: COLUMN_NAME,
+ on_delete: :cascade,
+ validate: false,
+ reverse_lock_order: true
+ )
+
+ prepare_async_foreign_key_validation SOURCE_TABLE, name: FK_NAME
+ end
+
+ def down
+ unprepare_async_foreign_key_validation SOURCE_TABLE, name: FK_NAME
+
+ remove_foreign_key_if_exists SOURCE_TABLE, name: FK_NAME
+ end
+end
diff --git a/db/post_migrate/20230726080827_add_issue_search_data_namespace_id_foreign_key.rb b/db/post_migrate/20230726080827_add_issue_search_data_namespace_id_foreign_key.rb
new file mode 100644
index 00000000000..43b4db7d9b5
--- /dev/null
+++ b/db/post_migrate/20230726080827_add_issue_search_data_namespace_id_foreign_key.rb
@@ -0,0 +1,24 @@
+# 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 AddIssueSearchDataNamespaceIdForeignKey < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::PartitioningMigrationHelpers
+
+ disable_ddl_transaction!
+
+ INDEX_NAME = 'index_issue_search_data_on_namespace_id'
+
+ def up
+ add_concurrent_partitioned_index :issue_search_data, :namespace_id, name: INDEX_NAME
+ add_concurrent_partitioned_foreign_key :issue_search_data, :namespaces,
+ column: :namespace_id,
+ on_delete: :cascade
+ end
+
+ def down
+ remove_foreign_key_if_exists :issue_search_data, column: :namespace_id
+ remove_concurrent_partitioned_index_by_name :issue_search_data, INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230726201351_remove_issues_issue_type_column.rb b/db/post_migrate/20230726201351_remove_issues_issue_type_column.rb
new file mode 100644
index 00000000000..4f066adbc97
--- /dev/null
+++ b/db/post_migrate/20230726201351_remove_issues_issue_type_column.rb
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+class RemoveIssuesIssueTypeColumn < Gitlab::Database::Migration[2.1]
+ enable_lock_retries!
+
+ def up
+ remove_column :issues, :issue_type
+ end
+
+ def down
+ add_column :issues, :issue_type, :smallint, default: 0, null: false
+ end
+end
diff --git a/db/post_migrate/20230726231029_remove_plan_limits_ci_active_pipelines_column.rb b/db/post_migrate/20230726231029_remove_plan_limits_ci_active_pipelines_column.rb
new file mode 100644
index 00000000000..ef0a67574ba
--- /dev/null
+++ b/db/post_migrate/20230726231029_remove_plan_limits_ci_active_pipelines_column.rb
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+class RemovePlanLimitsCiActivePipelinesColumn < Gitlab::Database::Migration[2.1]
+ enable_lock_retries!
+
+ def up
+ remove_column :plan_limits, :ci_active_pipelines
+ end
+
+ def down
+ add_column :plan_limits, :ci_active_pipelines, :integer, default: 0, null: false
+ end
+end
diff --git a/db/post_migrate/20230727102936_drop_prepared_at_index.rb b/db/post_migrate/20230727102936_drop_prepared_at_index.rb
new file mode 100644
index 00000000000..ee128a9d130
--- /dev/null
+++ b/db/post_migrate/20230727102936_drop_prepared_at_index.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class DropPreparedAtIndex < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ INDEX_NAME = 'index_merge_requests_on_id_and_prepared_at'
+
+ # TODO: Issue for sync deletion: https://gitlab.com/gitlab-org/gitlab/-/issues/419917
+
+ def up
+ prepare_async_index_removal :merge_requests, :id, name: INDEX_NAME
+ end
+
+ def down
+ unprepare_async_index :merge_requests, :id, name: INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230727103144_add_prepared_at_created_at_index.rb b/db/post_migrate/20230727103144_add_prepared_at_created_at_index.rb
new file mode 100644
index 00000000000..44c293bca2a
--- /dev/null
+++ b/db/post_migrate/20230727103144_add_prepared_at_created_at_index.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+class AddPreparedAtCreatedAtIndex < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ INDEX_NAME = 'index_merge_requests_id_created_at_prepared_at'
+
+ # TODO: Issue for sync deletion: https://gitlab.com/gitlab-org/gitlab/-/issues/419918
+
+ def up
+ prepare_async_index(:merge_requests,
+ [:created_at, :id],
+ name: INDEX_NAME,
+ where: "prepared_at IS NULL")
+ end
+
+ def down
+ unprepare_async_index(:merge_requests, [:created_at, :id], name: INDEX_NAME)
+ end
+end
diff --git a/db/post_migrate/20230727115635_index_vulnerability_reads_on_project_id_and_vulnerability_id.rb b/db/post_migrate/20230727115635_index_vulnerability_reads_on_project_id_and_vulnerability_id.rb
new file mode 100644
index 00000000000..379dfe6e42d
--- /dev/null
+++ b/db/post_migrate/20230727115635_index_vulnerability_reads_on_project_id_and_vulnerability_id.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+class IndexVulnerabilityReadsOnProjectIdAndVulnerabilityId < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = 'index_vulnerability_reads_on_project_id_and_vulnerability_id'
+
+ disable_ddl_transaction!
+
+ def up
+ add_concurrent_index :vulnerability_reads, [:project_id, :vulnerability_id], name: INDEX_NAME
+ end
+
+ def down
+ remove_concurrent_index_by_name :vulnerability_reads, INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230727132342_prepare_index_on_vulnerability_occurrences_uuid_async.rb b/db/post_migrate/20230727132342_prepare_index_on_vulnerability_occurrences_uuid_async.rb
new file mode 100644
index 00000000000..40670a60df0
--- /dev/null
+++ b/db/post_migrate/20230727132342_prepare_index_on_vulnerability_occurrences_uuid_async.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+class PrepareIndexOnVulnerabilityOccurrencesUuidAsync < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = "index_vulnerability_occurrences_on_uuid_1"
+
+ def up
+ prepare_async_index(
+ :vulnerability_occurrences,
+ :uuid_convert_string_to_uuid,
+ unique: true,
+ name: INDEX_NAME
+ )
+ end
+
+ def down
+ unprepare_async_index(
+ :vulnerability_occurrences,
+ INDEX_NAME
+ )
+ end
+end
diff --git a/db/post_migrate/20230728122928_prepare_index_on_vulnerability_occurrences_uuid_including_vulnerability_id_async.rb b/db/post_migrate/20230728122928_prepare_index_on_vulnerability_occurrences_uuid_including_vulnerability_id_async.rb
new file mode 100644
index 00000000000..abd296a308f
--- /dev/null
+++ b/db/post_migrate/20230728122928_prepare_index_on_vulnerability_occurrences_uuid_including_vulnerability_id_async.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+class PrepareIndexOnVulnerabilityOccurrencesUuidIncludingVulnerabilityIdAsync < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = "index_vuln_findings_on_uuid_including_vuln_id_1"
+
+ def up
+ index_sql = <<~SQL
+ CREATE UNIQUE INDEX CONCURRENTLY #{INDEX_NAME}
+ ON vulnerability_occurrences(uuid_convert_string_to_uuid)
+ INCLUDE(vulnerability_id)
+ SQL
+
+ prepare_async_index_from_sql(index_sql)
+ end
+
+ def down
+ unprepare_async_index_by_name(
+ :vulnerability_occurrences,
+ INDEX_NAME
+ )
+ end
+end
diff --git a/db/post_migrate/20230728134722_remove_application_settings_ignored_columns.rb b/db/post_migrate/20230728134722_remove_application_settings_ignored_columns.rb
new file mode 100644
index 00000000000..71bd6ae83eb
--- /dev/null
+++ b/db/post_migrate/20230728134722_remove_application_settings_ignored_columns.rb
@@ -0,0 +1,55 @@
+# frozen_string_literal: true
+
+class RemoveApplicationSettingsIgnoredColumns < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ def up
+ remove_column :application_settings, :encrypted_tofa_access_token_expires_in, if_exists: true
+ remove_column :application_settings, :encrypted_tofa_access_token_expires_in_iv, if_exists: true
+ remove_column :application_settings, :encrypted_tofa_client_library_args, if_exists: true
+ remove_column :application_settings, :encrypted_tofa_client_library_args_iv, if_exists: true
+ remove_column :application_settings, :encrypted_tofa_client_library_class, if_exists: true
+ remove_column :application_settings, :encrypted_tofa_client_library_class_iv, if_exists: true
+ remove_column :application_settings, :encrypted_tofa_client_library_create_credentials_method, if_exists: true
+ remove_column :application_settings, :encrypted_tofa_client_library_create_credentials_method_iv, if_exists: true
+ remove_column :application_settings, :encrypted_tofa_client_library_fetch_access_token_method, if_exists: true
+ remove_column :application_settings, :encrypted_tofa_client_library_fetch_access_token_method_iv, if_exists: true
+ remove_column :application_settings, :encrypted_tofa_host, if_exists: true
+ remove_column :application_settings, :encrypted_tofa_host_iv, if_exists: true
+ remove_column :application_settings, :encrypted_tofa_request_json_keys, if_exists: true
+ remove_column :application_settings, :encrypted_tofa_request_json_keys_iv, if_exists: true
+ remove_column :application_settings, :encrypted_tofa_request_payload, if_exists: true
+ remove_column :application_settings, :encrypted_tofa_request_payload_iv, if_exists: true
+ remove_column :application_settings, :encrypted_tofa_response_json_keys, if_exists: true
+ remove_column :application_settings, :encrypted_tofa_response_json_keys_iv, if_exists: true
+ remove_column :application_settings, :encrypted_tofa_url, if_exists: true
+ remove_column :application_settings, :encrypted_tofa_url_iv, if_exists: true
+ end
+
+ def down
+ add_column :application_settings, :encrypted_tofa_host, :binary, if_not_exists: true
+ add_column :application_settings, :encrypted_tofa_host_iv, :binary, if_not_exists: true
+ add_column :application_settings, :encrypted_tofa_url, :binary, if_not_exists: true
+ add_column :application_settings, :encrypted_tofa_url_iv, :binary, if_not_exists: true
+ add_column :application_settings, :encrypted_tofa_response_json_keys, :binary, if_not_exists: true
+ add_column :application_settings, :encrypted_tofa_response_json_keys_iv, :binary, if_not_exists: true
+ add_column :application_settings, :encrypted_tofa_request_json_keys, :binary, if_not_exists: true
+ add_column :application_settings, :encrypted_tofa_request_json_keys_iv, :binary, if_not_exists: true
+ add_column :application_settings, :encrypted_tofa_request_payload, :binary, if_not_exists: true
+ add_column :application_settings, :encrypted_tofa_request_payload_iv, :binary, if_not_exists: true
+ add_column :application_settings, :encrypted_tofa_client_library_class, :binary, if_not_exists: true
+ add_column :application_settings, :encrypted_tofa_client_library_class_iv, :binary, if_not_exists: true
+ add_column :application_settings, :encrypted_tofa_client_library_args, :binary, if_not_exists: true
+ add_column :application_settings, :encrypted_tofa_client_library_args_iv, :binary, if_not_exists: true
+ add_column :application_settings, :encrypted_tofa_client_library_create_credentials_method, :binary,
+ if_not_exists: true
+ add_column :application_settings, :encrypted_tofa_client_library_create_credentials_method_iv, :binary,
+ if_not_exists: true
+ add_column :application_settings, :encrypted_tofa_client_library_fetch_access_token_method, :binary,
+ if_not_exists: true
+ add_column :application_settings, :encrypted_tofa_client_library_fetch_access_token_method_iv, :binary,
+ if_not_exists: true
+ add_column :application_settings, :encrypted_tofa_access_token_expires_in, :binary, if_not_exists: true
+ add_column :application_settings, :encrypted_tofa_access_token_expires_in_iv, :binary, if_not_exists: true
+ end
+end
diff --git a/db/post_migrate/20230728151058_add_auditor_index_to_users_table.rb b/db/post_migrate/20230728151058_add_auditor_index_to_users_table.rb
new file mode 100644
index 00000000000..1a849e7b728
--- /dev/null
+++ b/db/post_migrate/20230728151058_add_auditor_index_to_users_table.rb
@@ -0,0 +1,14 @@
+# frozen_string_literal: true
+
+class AddAuditorIndexToUsersTable < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = 'index_users_for_auditors'
+ disable_ddl_transaction!
+
+ def up
+ add_concurrent_index :users, :id, where: 'auditor IS true', name: INDEX_NAME
+ end
+
+ def down
+ remove_concurrent_index_by_name :users, name: INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230728174927_add_epic_work_item_type.rb b/db/post_migrate/20230728174927_add_epic_work_item_type.rb
new file mode 100644
index 00000000000..49f70e5379a
--- /dev/null
+++ b/db/post_migrate/20230728174927_add_epic_work_item_type.rb
@@ -0,0 +1,82 @@
+# frozen_string_literal: true
+
+class AddEpicWorkItemType < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+ restrict_gitlab_migration gitlab_schema: :gitlab_main
+
+ ISSUE_ENUM_VALUE = 0
+ EPIC_ENUM_VALUE = 7
+ EPIC_NAME = 'Epic'
+ EPIC_WIDGETS = {
+ 'Assignees' => 0,
+ 'Description' => 1,
+ 'Hierarchy' => 2,
+ 'Labels' => 3,
+ 'Notes' => 5,
+ 'Start and due date' => 6,
+ 'Health status' => 7,
+ 'Status' => 11,
+ 'Notifications' => 14,
+ 'Award emoji' => 16
+ }.freeze
+
+ class MigrationWorkItemType < MigrationRecord
+ self.table_name = 'work_item_types'
+ end
+
+ class MigrationWidgetDefinition < MigrationRecord
+ self.table_name = 'work_item_widget_definitions'
+ end
+
+ class MigrationHierarchyRestriction < MigrationRecord
+ self.table_name = 'work_item_hierarchy_restrictions'
+ end
+
+ def up
+ # New instances will not run this migration and add this type via fixtures
+ # checking if record exists mostly because migration specs will run all migrations
+ # and that will conflict with the preloaded base work item types
+ existing_epic_work_item_type = MigrationWorkItemType.find_by(base_type: EPIC_ENUM_VALUE, namespace_id: nil)
+
+ return say('Epic work item type record exists, skipping creation') if existing_epic_work_item_type
+
+ new_epic_work_item_type = MigrationWorkItemType.create(
+ name: EPIC_NAME,
+ namespace_id: nil,
+ base_type: EPIC_ENUM_VALUE,
+ icon_name: 'issue-type-epic'
+ )
+
+ widgets = EPIC_WIDGETS.map do |widget_name, widget_enum_value|
+ {
+ work_item_type_id: new_epic_work_item_type.id,
+ name: widget_name,
+ widget_type: widget_enum_value
+ }
+ end
+
+ MigrationWidgetDefinition.upsert_all(
+ widgets,
+ unique_by: :index_work_item_widget_definitions_on_default_witype_and_name
+ )
+
+ issue_type = MigrationWorkItemType.find_by(base_type: ISSUE_ENUM_VALUE, namespace_id: nil)
+ return say('Issue work item type not found, skipping hierarchy restrictions creation') unless issue_type
+
+ restrictions = [
+ { parent_type_id: new_epic_work_item_type.id, child_type_id: new_epic_work_item_type.id, maximum_depth: 9 },
+ { parent_type_id: new_epic_work_item_type.id, child_type_id: issue_type.id, maximum_depth: 1 }
+ ]
+
+ MigrationHierarchyRestriction.upsert_all(
+ restrictions,
+ unique_by: :index_work_item_hierarchy_restrictions_on_parent_and_child
+ )
+ end
+
+ def down
+ # There's the remote possibility that issues could already be
+ # using this issue type, with a tight foreign constraint.
+ # Therefore we will not attempt to remove any data.
+ end
+end
diff --git a/db/post_migrate/20230731090319_add_notes_namespace_id_foreign_key.rb b/db/post_migrate/20230731090319_add_notes_namespace_id_foreign_key.rb
new file mode 100644
index 00000000000..5e06170a506
--- /dev/null
+++ b/db/post_migrate/20230731090319_add_notes_namespace_id_foreign_key.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+class AddNotesNamespaceIdForeignKey < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ INDEX_NAME = 'index_notes_on_namespace_id'
+
+ def up
+ add_concurrent_index :notes, :namespace_id, name: INDEX_NAME
+ add_concurrent_foreign_key :notes, :namespaces,
+ column: :namespace_id,
+ on_delete: :cascade
+ end
+
+ def down
+ remove_foreign_key_if_exists :notes, column: :namespace_id
+ remove_concurrent_index_by_name :notes, INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230731100513_add_index_on_vulnerability_reads_for_filtering.rb b/db/post_migrate/20230731100513_add_index_on_vulnerability_reads_for_filtering.rb
new file mode 100644
index 00000000000..bbed1e60d35
--- /dev/null
+++ b/db/post_migrate/20230731100513_add_index_on_vulnerability_reads_for_filtering.rb
@@ -0,0 +1,23 @@
+# frozen_string_literal: true
+
+class AddIndexOnVulnerabilityReadsForFiltering < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ INDEX_NAME = "idx_vuln_reads_for_filtering"
+
+ def up
+ add_concurrent_index(
+ :vulnerability_reads,
+ %i[project_id state dismissal_reason severity vulnerability_id],
+ order: { severity: :desc, vulnerability_id: "DESC NULLS LAST" },
+ name: INDEX_NAME
+ )
+ end
+
+ def down
+ remove_concurrent_index_by_name(
+ :vulnerability_reads,
+ INDEX_NAME
+ )
+ end
+end
diff --git a/db/post_migrate/20230731210422_remove_temp_index_vulnerability_occurrences.rb b/db/post_migrate/20230731210422_remove_temp_index_vulnerability_occurrences.rb
new file mode 100644
index 00000000000..a4be163cc64
--- /dev/null
+++ b/db/post_migrate/20230731210422_remove_temp_index_vulnerability_occurrences.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+class RemoveTempIndexVulnerabilityOccurrences < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = 'tmp_idx_vulnerability_occurrences_on_id_where_report_type_7_99'
+
+ disable_ddl_transaction!
+
+ def up
+ remove_concurrent_index_by_name :vulnerability_occurrences, INDEX_NAME
+ end
+
+ def down
+ add_concurrent_index :vulnerability_occurrences, :id, where: 'report_type IN (7, 99)', name: INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230801150214_retry_cleanup_bigint_conversion_for_events_for_gitlab_com.rb b/db/post_migrate/20230801150214_retry_cleanup_bigint_conversion_for_events_for_gitlab_com.rb
new file mode 100644
index 00000000000..231c28e538c
--- /dev/null
+++ b/db/post_migrate/20230801150214_retry_cleanup_bigint_conversion_for_events_for_gitlab_com.rb
@@ -0,0 +1,34 @@
+# frozen_string_literal: true
+
+class RetryCleanupBigintConversionForEventsForGitlabCom < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ enable_lock_retries!
+
+ TABLE = :events
+ COLUMNS = [:target_id]
+
+ # We first attempted to drop the temporary trigger and column at
+ # https://gitlab.com/gitlab-org/gitlab/-/merge_requests/126794
+ def up
+ return unless should_run?
+
+ return unless column_exists?(TABLE, :target_id_convert_to_bigint)
+
+ cleanup_conversion_of_integer_to_bigint(TABLE, COLUMNS)
+ end
+
+ def down
+ return unless should_run?
+
+ return if column_exists?(TABLE, :target_id_convert_to_bigint)
+
+ restore_conversion_of_integer_to_bigint(TABLE, COLUMNS)
+ end
+
+ private
+
+ def should_run?
+ com_or_dev_or_test_but_not_jh?
+ end
+end
diff --git a/db/post_migrate/20230802085923_queue_fix_allow_descendants_override_disabled_shared_runners.rb b/db/post_migrate/20230802085923_queue_fix_allow_descendants_override_disabled_shared_runners.rb
new file mode 100644
index 00000000000..fcac9e0209b
--- /dev/null
+++ b/db/post_migrate/20230802085923_queue_fix_allow_descendants_override_disabled_shared_runners.rb
@@ -0,0 +1,26 @@
+# frozen_string_literal: true
+
+class QueueFixAllowDescendantsOverrideDisabledSharedRunners < Gitlab::Database::Migration[2.1]
+ MIGRATION = "FixAllowDescendantsOverrideDisabledSharedRunners"
+ DELAY_INTERVAL = 2.minutes
+ BATCH_SIZE = 25000
+ SUB_BATCH_SIZE = 250
+
+ disable_ddl_transaction!
+ restrict_gitlab_migration gitlab_schema: :gitlab_main
+
+ def up
+ queue_batched_background_migration(
+ MIGRATION,
+ :namespaces,
+ :id,
+ job_interval: DELAY_INTERVAL,
+ batch_size: BATCH_SIZE,
+ sub_batch_size: SUB_BATCH_SIZE
+ )
+ end
+
+ def down
+ delete_batched_background_migration(MIGRATION, :namespaces, :id, [])
+ end
+end
diff --git a/db/post_migrate/20230802090519_drop_schema_inconsistencies_table.rb b/db/post_migrate/20230802090519_drop_schema_inconsistencies_table.rb
new file mode 100644
index 00000000000..ae95a8b2e6b
--- /dev/null
+++ b/db/post_migrate/20230802090519_drop_schema_inconsistencies_table.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+class DropSchemaInconsistenciesTable < Gitlab::Database::Migration[2.1]
+ def up
+ drop_table :schema_inconsistencies
+ end
+
+ def down
+ create_table :schema_inconsistencies do |t|
+ t.references :issue, index: true, null: false, foreign_key: { on_delete: :cascade }
+ t.timestamps_with_timezone null: false
+ t.text :object_name, null: false, limit: 63
+ t.text :table_name, null: false, limit: 63
+ t.text :valitador_name, null: false, limit: 63
+ t.text :diff, null: false, limit: 6144
+ end
+ end
+end
diff --git a/db/post_migrate/20230802092922_add_unique_index_project_authorizations_on_unique_project_user.rb b/db/post_migrate/20230802092922_add_unique_index_project_authorizations_on_unique_project_user.rb
new file mode 100644
index 00000000000..9a983cc1278
--- /dev/null
+++ b/db/post_migrate/20230802092922_add_unique_index_project_authorizations_on_unique_project_user.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+class AddUniqueIndexProjectAuthorizationsOnUniqueProjectUser < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = 'index_unique_project_authorizations_on_unique_project_user'
+
+ disable_ddl_transaction!
+
+ def up
+ add_concurrent_index :project_authorizations,
+ %i[project_id user_id],
+ unique: true,
+ where: "is_unique",
+ name: INDEX_NAME
+ end
+
+ def down
+ remove_concurrent_index_by_name :project_authorizations, INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230804053643_add_ticket_work_item_type.rb b/db/post_migrate/20230804053643_add_ticket_work_item_type.rb
new file mode 100644
index 00000000000..4d40622dfb4
--- /dev/null
+++ b/db/post_migrate/20230804053643_add_ticket_work_item_type.rb
@@ -0,0 +1,87 @@
+# frozen_string_literal: true
+
+class AddTicketWorkItemType < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+ restrict_gitlab_migration gitlab_schema: :gitlab_main
+
+ ISSUE_ENUM_VALUE = 0
+ TICKET_ENUM_VALUE = 8
+ TICKET_NAME = 'Ticket'
+ TICKET_WIDGETS = {
+ 'Assignees' => 0,
+ 'Description' => 1,
+ 'Hierarchy' => 2,
+ 'Labels' => 3,
+ 'Milestone' => 4,
+ 'Notes' => 5,
+ 'Start and due date' => 6,
+ 'Health status' => 7,
+ 'Weight' => 8,
+ 'Iteration' => 9,
+ 'Notifications' => 14,
+ 'Current user todos' => 15,
+ 'Award emoji' => 16
+ }.freeze
+
+ class MigrationWorkItemType < MigrationRecord
+ self.table_name = 'work_item_types'
+ end
+
+ class MigrationWidgetDefinition < MigrationRecord
+ self.table_name = 'work_item_widget_definitions'
+ end
+
+ class MigrationHierarchyRestriction < MigrationRecord
+ self.table_name = 'work_item_hierarchy_restrictions'
+ end
+
+ def up
+ # New instances will not run this migration and add this type via fixtures
+ # checking if record exists mostly because migration specs will run all migrations
+ # and that will conflict with the preloaded base work item types
+ existing_ticket_work_item_type = MigrationWorkItemType.find_by(base_type: TICKET_ENUM_VALUE, namespace_id: nil)
+
+ return say('Ticket work item type record exists, skipping creation') if existing_ticket_work_item_type
+
+ new_ticket_work_item_type = MigrationWorkItemType.create(
+ name: TICKET_NAME,
+ namespace_id: nil,
+ base_type: TICKET_ENUM_VALUE,
+ icon_name: 'issue-type-issue'
+ )
+
+ return say('Ticket work item type create record failed, skipping creation') if new_ticket_work_item_type.new_record?
+
+ widgets = TICKET_WIDGETS.map do |widget_name, widget_enum_value|
+ {
+ work_item_type_id: new_ticket_work_item_type.id,
+ name: widget_name,
+ widget_type: widget_enum_value
+ }
+ end
+
+ MigrationWidgetDefinition.upsert_all(
+ widgets,
+ unique_by: :index_work_item_widget_definitions_on_default_witype_and_name
+ )
+
+ issue_type = MigrationWorkItemType.find_by(base_type: ISSUE_ENUM_VALUE, namespace_id: nil)
+ return say('Issue work item type not found, skipping hierarchy restrictions creation') unless issue_type
+
+ restrictions = [
+ { parent_type_id: new_ticket_work_item_type.id, child_type_id: new_ticket_work_item_type.id, maximum_depth: 1 },
+ { parent_type_id: new_ticket_work_item_type.id, child_type_id: issue_type.id, maximum_depth: 1 }
+ ]
+
+ MigrationHierarchyRestriction.upsert_all(
+ restrictions,
+ unique_by: :index_work_item_hierarchy_restrictions_on_parent_and_child
+ )
+ end
+
+ def down
+ # There's the remote possibility that issues could already be
+ # using this issue type, with a tight foreign constraint.
+ # Therefore we will not attempt to remove any data.
+ end
+end
diff --git a/db/post_migrate/20230804121704_remove_namespaces_user_details_enterprise_group_id_fk.rb b/db/post_migrate/20230804121704_remove_namespaces_user_details_enterprise_group_id_fk.rb
new file mode 100644
index 00000000000..23c23587c43
--- /dev/null
+++ b/db/post_migrate/20230804121704_remove_namespaces_user_details_enterprise_group_id_fk.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+class RemoveNamespacesUserDetailsEnterpriseGroupIdFk < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ def up
+ return unless foreign_key_exists?(:user_details, :namespaces, name: "fk_c53c794142")
+
+ with_lock_retries do
+ remove_foreign_key_if_exists(:user_details, :namespaces,
+ name: "fk_c53c794142", reverse_lock_order: true)
+ end
+ end
+
+ def down
+ add_concurrent_foreign_key(:user_details, :namespaces,
+ name: "fk_c53c794142", column: :enterprise_group_id,
+ target_column: :id, on_delete: :nullify)
+ end
+end
diff --git a/db/post_migrate/20230804121705_remove_namespaces_user_details_provisioned_by_group_id_fk.rb b/db/post_migrate/20230804121705_remove_namespaces_user_details_provisioned_by_group_id_fk.rb
new file mode 100644
index 00000000000..0d43354b92c
--- /dev/null
+++ b/db/post_migrate/20230804121705_remove_namespaces_user_details_provisioned_by_group_id_fk.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+class RemoveNamespacesUserDetailsProvisionedByGroupIdFk < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ def up
+ return unless foreign_key_exists?(:user_details, :namespaces, name: "fk_190e4fcc88")
+
+ with_lock_retries do
+ remove_foreign_key_if_exists(:user_details, :namespaces,
+ name: "fk_190e4fcc88", reverse_lock_order: true)
+ end
+ end
+
+ def down
+ add_concurrent_foreign_key(:user_details, :namespaces,
+ name: "fk_190e4fcc88", column: :provisioned_by_group_id,
+ target_column: :id, on_delete: :nullify)
+ end
+end
diff --git a/db/post_migrate/20230807083334_add_linked_items_work_item_widget.rb b/db/post_migrate/20230807083334_add_linked_items_work_item_widget.rb
new file mode 100644
index 00000000000..041969a6e36
--- /dev/null
+++ b/db/post_migrate/20230807083334_add_linked_items_work_item_widget.rb
@@ -0,0 +1,58 @@
+# frozen_string_literal: true
+
+class AddLinkedItemsWorkItemWidget < Gitlab::Database::Migration[2.1]
+ class WorkItemType < MigrationRecord
+ self.table_name = 'work_item_types'
+ end
+
+ class WidgetDefinition < MigrationRecord
+ self.table_name = 'work_item_widget_definitions'
+ end
+
+ restrict_gitlab_migration gitlab_schema: :gitlab_main
+ disable_ddl_transaction!
+
+ WIDGET_NAME = 'Linked items'
+ WIDGET_ENUM_VALUE = 17
+ WORK_ITEM_TYPES = [
+ 'Issue',
+ 'Incident',
+ 'Test Case',
+ 'Requirement',
+ 'Task',
+ 'Objective',
+ 'Key Result',
+ 'Epic'
+ ].freeze
+
+ def up
+ widgets = []
+
+ WORK_ITEM_TYPES.each do |type_name|
+ type = WorkItemType.find_by_name_and_namespace_id(type_name, nil)
+
+ unless type
+ Gitlab::AppLogger.warn("type #{type_name} is missing, not adding widget")
+
+ next
+ end
+
+ widgets << {
+ work_item_type_id: type.id,
+ name: WIDGET_NAME,
+ widget_type: WIDGET_ENUM_VALUE
+ }
+ end
+
+ return if widgets.empty?
+
+ WidgetDefinition.upsert_all(
+ widgets,
+ unique_by: :index_work_item_widget_definitions_on_default_witype_and_name
+ )
+ end
+
+ def down
+ WidgetDefinition.where(name: WIDGET_NAME).delete_all
+ end
+end
diff --git a/db/post_migrate/20230807085752_ensure_id_uniqueness_for_p_ci_builds.rb b/db/post_migrate/20230807085752_ensure_id_uniqueness_for_p_ci_builds.rb
new file mode 100644
index 00000000000..cdd68e7f5dc
--- /dev/null
+++ b/db/post_migrate/20230807085752_ensure_id_uniqueness_for_p_ci_builds.rb
@@ -0,0 +1,49 @@
+# frozen_string_literal: true
+
+class EnsureIdUniquenessForPCiBuilds < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::SchemaHelpers
+ include Gitlab::Database::MigrationHelpers::WraparoundAutovacuum
+
+ enable_lock_retries!
+
+ TABLE_NAME = :p_ci_builds
+ FUNCTION_NAME = :assign_p_ci_builds_id_value
+ TRIGGER_NAME = :assign_p_ci_builds_id_trigger
+
+ def up
+ return unless should_run?
+
+ change_column_default(TABLE_NAME, :id, nil)
+
+ create_trigger_function(FUNCTION_NAME) do
+ <<~SQL
+ IF NEW."id" IS NOT NULL THEN
+ RAISE WARNING 'Manually assigning ids is not allowed, the value will be ignored';
+ END IF;
+ NEW."id" := nextval('ci_builds_id_seq'::regclass);
+ RETURN NEW;
+ SQL
+ end
+
+ Gitlab::Database::PostgresPartitionedTable.each_partition(TABLE_NAME) do |partition|
+ create_trigger(partition.identifier, TRIGGER_NAME, FUNCTION_NAME, fires: 'BEFORE INSERT')
+ end
+ end
+
+ def down
+ return unless should_run?
+
+ execute(<<~SQL.squish)
+ ALTER TABLE #{TABLE_NAME}
+ ALTER COLUMN id SET DEFAULT nextval('ci_builds_id_seq'::regclass);
+
+ DROP FUNCTION IF EXISTS #{FUNCTION_NAME} CASCADE;
+ SQL
+ end
+
+ private
+
+ def should_run?
+ can_execute_on?(:ci_builds)
+ end
+end
diff --git a/db/post_migrate/20230808123136_cleanup_application_settings_database_apdex_settings_rename.rb b/db/post_migrate/20230808123136_cleanup_application_settings_database_apdex_settings_rename.rb
new file mode 100644
index 00000000000..de1887677bf
--- /dev/null
+++ b/db/post_migrate/20230808123136_cleanup_application_settings_database_apdex_settings_rename.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+class CleanupApplicationSettingsDatabaseApdexSettingsRename < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ def up
+ cleanup_concurrent_column_rename(
+ :application_settings,
+ :database_apdex_settings,
+ :prometheus_alert_db_indicators_settings
+ )
+ end
+
+ def down
+ undo_cleanup_concurrent_column_rename(
+ :application_settings,
+ :database_apdex_settings,
+ :prometheus_alert_db_indicators_settings
+ )
+ end
+end
diff --git a/db/post_migrate/20230808170646_remove_free_user_cap_over_limt_notified_at_column.rb b/db/post_migrate/20230808170646_remove_free_user_cap_over_limt_notified_at_column.rb
new file mode 100644
index 00000000000..cd061499d67
--- /dev/null
+++ b/db/post_migrate/20230808170646_remove_free_user_cap_over_limt_notified_at_column.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+class RemoveFreeUserCapOverLimtNotifiedAtColumn < Gitlab::Database::Migration[2.1]
+ enable_lock_retries!
+
+ def change
+ remove_column :namespace_details, :free_user_cap_over_limt_notified_at, :datetime_with_timezone
+ end
+end
diff --git a/db/post_migrate/20230809090349_ensure_id_uniqueness_for_p_ci_builds_v2.rb b/db/post_migrate/20230809090349_ensure_id_uniqueness_for_p_ci_builds_v2.rb
new file mode 100644
index 00000000000..8170059624c
--- /dev/null
+++ b/db/post_migrate/20230809090349_ensure_id_uniqueness_for_p_ci_builds_v2.rb
@@ -0,0 +1,50 @@
+# frozen_string_literal: true
+
+class EnsureIdUniquenessForPCiBuildsV2 < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::SchemaHelpers
+ include Gitlab::Database::MigrationHelpers::WraparoundAutovacuum
+
+ enable_lock_retries!
+
+ TABLE_NAME = :p_ci_builds
+ FUNCTION_NAME = :assign_p_ci_builds_id_value
+ TRIGGER_NAME = :assign_p_ci_builds_id_trigger
+
+ def up
+ return unless should_run?
+ return if trigger_exists?(:ci_builds, TRIGGER_NAME)
+
+ change_column_default(TABLE_NAME, :id, nil)
+
+ create_trigger_function(FUNCTION_NAME) do
+ <<~SQL
+ IF NEW."id" IS NOT NULL THEN
+ RAISE WARNING 'Manually assigning ids is not allowed, the value will be ignored';
+ END IF;
+ NEW."id" := nextval('ci_builds_id_seq'::regclass);
+ RETURN NEW;
+ SQL
+ end
+
+ Gitlab::Database::PostgresPartitionedTable.each_partition(TABLE_NAME) do |partition|
+ create_trigger(partition.identifier, TRIGGER_NAME, FUNCTION_NAME, fires: 'BEFORE INSERT')
+ end
+ end
+
+ def down
+ return unless should_run?
+
+ execute(<<~SQL.squish)
+ ALTER TABLE #{TABLE_NAME}
+ ALTER COLUMN id SET DEFAULT nextval('ci_builds_id_seq'::regclass);
+
+ DROP FUNCTION IF EXISTS #{FUNCTION_NAME} CASCADE;
+ SQL
+ end
+
+ private
+
+ def should_run?
+ can_execute_on?(:ci_builds)
+ end
+end
diff --git a/db/post_migrate/20230809104007_ensure_epic_user_mentions_bigint_backfill_is_finished_for_self_hosts.rb b/db/post_migrate/20230809104007_ensure_epic_user_mentions_bigint_backfill_is_finished_for_self_hosts.rb
new file mode 100644
index 00000000000..e190c18c871
--- /dev/null
+++ b/db/post_migrate/20230809104007_ensure_epic_user_mentions_bigint_backfill_is_finished_for_self_hosts.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+class EnsureEpicUserMentionsBigintBackfillIsFinishedForSelfHosts < Gitlab::Database::Migration[2.1]
+ restrict_gitlab_migration gitlab_schema: :gitlab_main
+ disable_ddl_transaction!
+
+ def up
+ ensure_batched_background_migration_is_finished(
+ job_class_name: 'CopyColumnUsingBackgroundMigrationJob',
+ table_name: 'epic_user_mentions',
+ column_name: 'id',
+ job_arguments: [['note_id'], ['note_id_convert_to_bigint']]
+ )
+ end
+
+ def down
+ # no-op
+ end
+end
diff --git a/db/post_migrate/20230809104753_swap_epic_user_mentions_note_id_to_bigint_for_self_hosts.rb b/db/post_migrate/20230809104753_swap_epic_user_mentions_note_id_to_bigint_for_self_hosts.rb
new file mode 100644
index 00000000000..60d10b0ec65
--- /dev/null
+++ b/db/post_migrate/20230809104753_swap_epic_user_mentions_note_id_to_bigint_for_self_hosts.rb
@@ -0,0 +1,78 @@
+# frozen_string_literal: true
+
+class SwapEpicUserMentionsNoteIdToBigintForSelfHosts < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ disable_ddl_transaction!
+
+ TABLE_NAME = 'epic_user_mentions'
+
+ def up
+ return if should_skip?
+ return if temp_column_removed?(TABLE_NAME, :note_id)
+ return if columns_swapped?(TABLE_NAME, :note_id)
+
+ swap
+ end
+
+ def down
+ return if should_skip?
+ return if temp_column_removed?(TABLE_NAME, :note_id)
+ return unless columns_swapped?(TABLE_NAME, :note_id)
+
+ swap
+ end
+
+ def swap
+ # This will replace the existing epic_user_mentions_on_epic_id_and_note_id_index
+ add_concurrent_index TABLE_NAME, [:epic_id, :note_id_convert_to_bigint], unique: true,
+ name: 'epic_user_mentions_on_epic_id_and_note_id_convert_to_bigint'
+
+ # This will replace the existing epic_user_mentions_on_epic_id_index
+ add_concurrent_index TABLE_NAME, :epic_id, unique: true,
+ name: 'tmp_epic_user_mentions_on_epic_id_index',
+ where: 'note_id_convert_to_bigint IS NULL'
+
+ # This will replace the existing index_epic_user_mentions_on_note_id
+ add_concurrent_index TABLE_NAME, :note_id_convert_to_bigint, unique: true,
+ name: 'index_epic_user_mentions_on_note_id_convert_to_bigint',
+ where: 'note_id_convert_to_bigint IS NOT NULL'
+
+ # This will replace the existing fk_rails_1c65976a49
+ add_concurrent_foreign_key TABLE_NAME, :notes, column: :note_id_convert_to_bigint,
+ name: 'fk_epic_user_mentions_note_id_convert_to_bigint',
+ on_delete: :cascade
+
+ with_lock_retries(raise_on_exhaustion: true) do
+ execute "LOCK TABLE notes, #{TABLE_NAME} IN ACCESS EXCLUSIVE MODE"
+
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN note_id TO note_id_tmp"
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN note_id_convert_to_bigint TO note_id"
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN note_id_tmp TO note_id_convert_to_bigint"
+
+ function_name = Gitlab::Database::UnidirectionalCopyTrigger
+ .on_table(TABLE_NAME, connection: connection)
+ .name(:note_id, :note_id_convert_to_bigint)
+ execute "ALTER FUNCTION #{quote_table_name(function_name)} RESET ALL"
+
+ execute 'DROP INDEX IF EXISTS epic_user_mentions_on_epic_id_and_note_id_index'
+ rename_index TABLE_NAME, 'epic_user_mentions_on_epic_id_and_note_id_convert_to_bigint',
+ 'epic_user_mentions_on_epic_id_and_note_id_index'
+
+ execute 'DROP INDEX IF EXISTS epic_user_mentions_on_epic_id_index'
+ rename_index TABLE_NAME, 'tmp_epic_user_mentions_on_epic_id_index',
+ 'epic_user_mentions_on_epic_id_index'
+
+ execute 'DROP INDEX IF EXISTS index_epic_user_mentions_on_note_id'
+ rename_index TABLE_NAME, 'index_epic_user_mentions_on_note_id_convert_to_bigint',
+ 'index_epic_user_mentions_on_note_id'
+
+ execute "ALTER TABLE #{TABLE_NAME} DROP CONSTRAINT IF EXISTS fk_rails_1c65976a49"
+ rename_constraint(TABLE_NAME, 'fk_epic_user_mentions_note_id_convert_to_bigint', 'fk_rails_1c65976a49')
+ end
+ end
+
+ def should_skip?
+ com_or_dev_or_test_but_not_jh?
+ end
+end
diff --git a/db/post_migrate/20230809133249_index_sbom_occurrences_on_project_id_component_id_and_input_file_path.rb b/db/post_migrate/20230809133249_index_sbom_occurrences_on_project_id_component_id_and_input_file_path.rb
new file mode 100644
index 00000000000..5b048344fad
--- /dev/null
+++ b/db/post_migrate/20230809133249_index_sbom_occurrences_on_project_id_component_id_and_input_file_path.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+class IndexSbomOccurrencesOnProjectIdComponentIdAndInputFilePath < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = 'index_sbom_occurrences_for_input_file_path_search'
+
+ disable_ddl_transaction!
+
+ def up
+ add_concurrent_index :sbom_occurrences, %i[project_id component_id input_file_path], name: INDEX_NAME
+ end
+
+ def down
+ remove_concurrent_index_by_name :sbom_occurrences, INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230810103141_ensure_suggestions_note_id_bigint_backfill_is_finished_for_self_hosts.rb b/db/post_migrate/20230810103141_ensure_suggestions_note_id_bigint_backfill_is_finished_for_self_hosts.rb
new file mode 100644
index 00000000000..3c2050bedee
--- /dev/null
+++ b/db/post_migrate/20230810103141_ensure_suggestions_note_id_bigint_backfill_is_finished_for_self_hosts.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+class EnsureSuggestionsNoteIdBigintBackfillIsFinishedForSelfHosts < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ restrict_gitlab_migration gitlab_schema: :gitlab_main
+ disable_ddl_transaction!
+
+ def up
+ ensure_batched_background_migration_is_finished(
+ job_class_name: 'CopyColumnUsingBackgroundMigrationJob',
+ table_name: 'suggestions',
+ column_name: 'id',
+ job_arguments: [['note_id'], ['note_id_convert_to_bigint']]
+ )
+ end
+
+ def down
+ # no-op
+ end
+end
diff --git a/db/post_migrate/20230810103534_swap_suggestions_note_id_to_bigint_for_self_hosts.rb b/db/post_migrate/20230810103534_swap_suggestions_note_id_to_bigint_for_self_hosts.rb
new file mode 100644
index 00000000000..4f1362495a6
--- /dev/null
+++ b/db/post_migrate/20230810103534_swap_suggestions_note_id_to_bigint_for_self_hosts.rb
@@ -0,0 +1,64 @@
+# frozen_string_literal: true
+
+class SwapSuggestionsNoteIdToBigintForSelfHosts < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ disable_ddl_transaction!
+
+ TABLE_NAME = 'suggestions'
+
+ def up
+ return if should_skip?
+ return if temp_column_removed?(TABLE_NAME, :note_id)
+ return if columns_swapped?(TABLE_NAME, :note_id)
+
+ swap
+ end
+
+ def down
+ return if should_skip?
+ return if temp_column_removed?(TABLE_NAME, :note_id)
+ return unless columns_swapped?(TABLE_NAME, :note_id)
+
+ swap
+ end
+
+ def swap
+ # This will replace the existing index_suggestions_on_note_id_and_relative_order
+ add_concurrent_index TABLE_NAME, [:note_id_convert_to_bigint, :relative_order], unique: true,
+ name: 'index_suggestions_on_note_id_convert_to_bigint_relative_order'
+
+ # This will replace the existing fk_rails_33b03a535c
+ add_concurrent_foreign_key TABLE_NAME, :notes, column: :note_id_convert_to_bigint,
+ name: 'fk_suggestions_note_id_convert_to_bigint',
+ on_delete: :cascade
+
+ with_lock_retries(raise_on_exhaustion: true) do
+ execute "LOCK TABLE notes, #{TABLE_NAME} IN ACCESS EXCLUSIVE MODE"
+
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN note_id TO note_id_tmp"
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN note_id_convert_to_bigint TO note_id"
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN note_id_tmp TO note_id_convert_to_bigint"
+
+ function_name = Gitlab::Database::UnidirectionalCopyTrigger
+ .on_table(TABLE_NAME, connection: connection)
+ .name(:note_id, :note_id_convert_to_bigint)
+ execute "ALTER FUNCTION #{quote_table_name(function_name)} RESET ALL"
+
+ # Swap defaults
+ change_column_default TABLE_NAME, :note_id, nil
+ change_column_default TABLE_NAME, :note_id_convert_to_bigint, 0
+
+ execute 'DROP INDEX IF EXISTS index_suggestions_on_note_id_and_relative_order'
+ rename_index TABLE_NAME, 'index_suggestions_on_note_id_convert_to_bigint_relative_order',
+ 'index_suggestions_on_note_id_and_relative_order'
+
+ execute "ALTER TABLE #{TABLE_NAME} DROP CONSTRAINT IF EXISTS fk_rails_33b03a535c"
+ rename_constraint(TABLE_NAME, 'fk_suggestions_note_id_convert_to_bigint', 'fk_rails_33b03a535c')
+ end
+ end
+
+ def should_skip?
+ com_or_dev_or_test_but_not_jh?
+ end
+end
diff --git a/db/post_migrate/20230810122746_ensure_snippet_user_mentions_bigint_backfill_is_finished_for_self_hosts.rb b/db/post_migrate/20230810122746_ensure_snippet_user_mentions_bigint_backfill_is_finished_for_self_hosts.rb
new file mode 100644
index 00000000000..4dc5162176e
--- /dev/null
+++ b/db/post_migrate/20230810122746_ensure_snippet_user_mentions_bigint_backfill_is_finished_for_self_hosts.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+class EnsureSnippetUserMentionsBigintBackfillIsFinishedForSelfHosts < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ restrict_gitlab_migration gitlab_schema: :gitlab_main
+ disable_ddl_transaction!
+
+ def up
+ ensure_batched_background_migration_is_finished(
+ job_class_name: 'CopyColumnUsingBackgroundMigrationJob',
+ table_name: 'snippet_user_mentions',
+ column_name: 'id',
+ job_arguments: [['note_id'], ['note_id_convert_to_bigint']]
+ )
+ end
+
+ def down
+ # no-op
+ end
+end
diff --git a/db/post_migrate/20230810123044_swap_snippet_user_mentions_note_id_to_bigint_for_self_hosts.rb b/db/post_migrate/20230810123044_swap_snippet_user_mentions_note_id_to_bigint_for_self_hosts.rb
new file mode 100644
index 00000000000..4c8ac259f61
--- /dev/null
+++ b/db/post_migrate/20230810123044_swap_snippet_user_mentions_note_id_to_bigint_for_self_hosts.rb
@@ -0,0 +1,78 @@
+# frozen_string_literal: true
+
+class SwapSnippetUserMentionsNoteIdToBigintForSelfHosts < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ disable_ddl_transaction!
+
+ TABLE_NAME = 'snippet_user_mentions'
+
+ def up
+ return if should_skip?
+ return if temp_column_removed?(TABLE_NAME, :note_id)
+ return if columns_swapped?(TABLE_NAME, :note_id)
+
+ swap
+ end
+
+ def down
+ return if should_skip?
+ return if temp_column_removed?(TABLE_NAME, :note_id)
+ return unless columns_swapped?(TABLE_NAME, :note_id)
+
+ swap
+ end
+
+ def swap
+ # This will replace the existing index_snippet_user_mentions_on_note_id
+ add_concurrent_index TABLE_NAME, :note_id_convert_to_bigint, unique: true,
+ name: 'index_snippet_user_mentions_on_note_id_convert_to_bigint',
+ where: 'note_id_convert_to_bigint IS NOT NULL'
+
+ # This will replace the existing snippet_user_mentions_on_snippet_id_and_note_id_index
+ add_concurrent_index TABLE_NAME, [:snippet_id, :note_id_convert_to_bigint], unique: true,
+ name: 'tmp_snippet_user_mentions_on_snippet_id_and_note_id_index'
+
+ # This will replace the existing snippet_user_mentions_on_snippet_id_index
+ add_concurrent_index TABLE_NAME, :snippet_id, unique: true,
+ name: 'tmp_snippet_user_mentions_on_snippet_id_index',
+ where: 'note_id_convert_to_bigint IS NULL'
+
+ # This will replace the existing fk_rails_4d3f96b2cb
+ add_concurrent_foreign_key TABLE_NAME, :notes, column: :note_id_convert_to_bigint,
+ name: 'fk_snippet_user_mentions_note_id_convert_to_bigint',
+ on_delete: :cascade
+
+ with_lock_retries(raise_on_exhaustion: true) do
+ execute "LOCK TABLE notes, #{TABLE_NAME} IN ACCESS EXCLUSIVE MODE"
+
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN note_id TO note_id_tmp"
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN note_id_convert_to_bigint TO note_id"
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN note_id_tmp TO note_id_convert_to_bigint"
+
+ function_name = Gitlab::Database::UnidirectionalCopyTrigger
+ .on_table(TABLE_NAME, connection: connection)
+ .name(:note_id, :note_id_convert_to_bigint)
+ execute "ALTER FUNCTION #{quote_table_name(function_name)} RESET ALL"
+
+ execute 'DROP INDEX IF EXISTS index_snippet_user_mentions_on_note_id'
+ rename_index TABLE_NAME, 'index_snippet_user_mentions_on_note_id_convert_to_bigint',
+ 'index_snippet_user_mentions_on_note_id'
+
+ execute 'DROP INDEX IF EXISTS snippet_user_mentions_on_snippet_id_and_note_id_index'
+ rename_index TABLE_NAME, 'tmp_snippet_user_mentions_on_snippet_id_and_note_id_index',
+ 'snippet_user_mentions_on_snippet_id_and_note_id_index'
+
+ execute 'DROP INDEX IF EXISTS snippet_user_mentions_on_snippet_id_index'
+ rename_index TABLE_NAME, 'tmp_snippet_user_mentions_on_snippet_id_index',
+ 'snippet_user_mentions_on_snippet_id_index'
+
+ execute "ALTER TABLE #{TABLE_NAME} DROP CONSTRAINT IF EXISTS fk_rails_4d3f96b2cb"
+ rename_constraint(TABLE_NAME, 'fk_snippet_user_mentions_note_id_convert_to_bigint', 'fk_rails_4d3f96b2cb')
+ end
+ end
+
+ def should_skip?
+ com_or_dev_or_test_but_not_jh?
+ end
+end
diff --git a/db/post_migrate/20230811103654_ensure_vum_bigint_backfill_is_finished_for_self_hosts.rb b/db/post_migrate/20230811103654_ensure_vum_bigint_backfill_is_finished_for_self_hosts.rb
new file mode 100644
index 00000000000..6ca1f5727d4
--- /dev/null
+++ b/db/post_migrate/20230811103654_ensure_vum_bigint_backfill_is_finished_for_self_hosts.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+class EnsureVumBigintBackfillIsFinishedForSelfHosts < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ restrict_gitlab_migration gitlab_schema: :gitlab_main
+ disable_ddl_transaction!
+
+ def up
+ ensure_batched_background_migration_is_finished(
+ job_class_name: 'CopyColumnUsingBackgroundMigrationJob',
+ table_name: 'vulnerability_user_mentions',
+ column_name: 'id',
+ job_arguments: [['note_id'], ['note_id_convert_to_bigint']]
+ )
+ end
+
+ def down
+ # no-op
+ end
+end
diff --git a/db/post_migrate/20230811103941_swap_vulnerability_user_mentions_note_id_to_bigint_for_self_hosts.rb b/db/post_migrate/20230811103941_swap_vulnerability_user_mentions_note_id_to_bigint_for_self_hosts.rb
new file mode 100644
index 00000000000..25cff9464b1
--- /dev/null
+++ b/db/post_migrate/20230811103941_swap_vulnerability_user_mentions_note_id_to_bigint_for_self_hosts.rb
@@ -0,0 +1,78 @@
+# frozen_string_literal: true
+
+class SwapVulnerabilityUserMentionsNoteIdToBigintForSelfHosts < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ disable_ddl_transaction!
+
+ TABLE_NAME = 'vulnerability_user_mentions'
+
+ def up
+ return if should_skip?
+ return if temp_column_removed?(TABLE_NAME, :note_id)
+ return if columns_swapped?(TABLE_NAME, :note_id)
+
+ swap
+ end
+
+ def down
+ return if should_skip?
+ return if temp_column_removed?(TABLE_NAME, :note_id)
+ return unless columns_swapped?(TABLE_NAME, :note_id)
+
+ swap
+ end
+
+ def swap
+ # This will replace the existing index_vulnerability_user_mentions_on_note_id
+ add_concurrent_index TABLE_NAME, :note_id_convert_to_bigint, unique: true,
+ name: 'index_vulnerability_user_mentions_on_note_id_convert_to_bigint',
+ where: 'note_id_convert_to_bigint IS NOT NULL'
+
+ # This will replace the existing index_vulns_user_mentions_on_vulnerability_id
+ add_concurrent_index TABLE_NAME, :vulnerability_id, unique: true,
+ name: 'tmp_index_vulns_user_mentions_on_vulnerability_id',
+ where: 'note_id_convert_to_bigint IS NULL'
+
+ # This will replace the existing index_vulns_user_mentions_on_vulnerability_id_and_note_id
+ add_concurrent_index TABLE_NAME, [:vulnerability_id, :note_id_convert_to_bigint], unique: true,
+ name: 'tmp_index_vulns_user_mentions_on_vulnerability_id_and_note_id'
+
+ # This will replace the existing fk_rails_a18600f210
+ add_concurrent_foreign_key TABLE_NAME, :notes, column: :note_id_convert_to_bigint,
+ name: 'fk_vulnerability_user_mentions_note_id_convert_to_bigint',
+ on_delete: :cascade
+
+ with_lock_retries(raise_on_exhaustion: true) do
+ execute "LOCK TABLE notes, #{TABLE_NAME} IN ACCESS EXCLUSIVE MODE"
+
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN note_id TO note_id_tmp"
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN note_id_convert_to_bigint TO note_id"
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN note_id_tmp TO note_id_convert_to_bigint"
+
+ function_name = Gitlab::Database::UnidirectionalCopyTrigger
+ .on_table(TABLE_NAME, connection: connection)
+ .name(:note_id, :note_id_convert_to_bigint)
+ execute "ALTER FUNCTION #{quote_table_name(function_name)} RESET ALL"
+
+ execute 'DROP INDEX IF EXISTS index_vulnerability_user_mentions_on_note_id'
+ rename_index TABLE_NAME, 'index_vulnerability_user_mentions_on_note_id_convert_to_bigint',
+ 'index_vulnerability_user_mentions_on_note_id'
+
+ execute 'DROP INDEX IF EXISTS index_vulns_user_mentions_on_vulnerability_id'
+ rename_index TABLE_NAME, 'tmp_index_vulns_user_mentions_on_vulnerability_id',
+ 'index_vulns_user_mentions_on_vulnerability_id'
+
+ execute 'DROP INDEX IF EXISTS index_vulns_user_mentions_on_vulnerability_id_and_note_id'
+ rename_index TABLE_NAME, 'tmp_index_vulns_user_mentions_on_vulnerability_id_and_note_id',
+ 'index_vulns_user_mentions_on_vulnerability_id_and_note_id'
+
+ execute "ALTER TABLE #{TABLE_NAME} DROP CONSTRAINT IF EXISTS fk_rails_a18600f210"
+ rename_constraint(TABLE_NAME, 'fk_vulnerability_user_mentions_note_id_convert_to_bigint', 'fk_rails_a18600f210')
+ end
+ end
+
+ def should_skip?
+ com_or_dev_or_test_but_not_jh?
+ end
+end
diff --git a/db/post_migrate/20230811145848_ensure_commit_user_mentions_note_id_bigint_backfill_is_finished_for_self_managed.rb b/db/post_migrate/20230811145848_ensure_commit_user_mentions_note_id_bigint_backfill_is_finished_for_self_managed.rb
new file mode 100644
index 00000000000..9510e2673dc
--- /dev/null
+++ b/db/post_migrate/20230811145848_ensure_commit_user_mentions_note_id_bigint_backfill_is_finished_for_self_managed.rb
@@ -0,0 +1,23 @@
+# frozen_string_literal: true
+
+class EnsureCommitUserMentionsNoteIdBigintBackfillIsFinishedForSelfManaged < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ restrict_gitlab_migration gitlab_schema: :gitlab_main
+ disable_ddl_transaction!
+
+ def up
+ return if com_or_dev_or_test_but_not_jh?
+
+ ensure_batched_background_migration_is_finished(
+ job_class_name: 'CopyColumnUsingBackgroundMigrationJob',
+ table_name: 'commit_user_mentions',
+ column_name: 'id',
+ job_arguments: [['note_id'], ['note_id_convert_to_bigint']]
+ )
+ end
+
+ def down
+ # no-op
+ end
+end
diff --git a/db/post_migrate/20230811150636_swap_commit_user_mentions_note_id_to_bigint_for_self_managed.rb b/db/post_migrate/20230811150636_swap_commit_user_mentions_note_id_to_bigint_for_self_managed.rb
new file mode 100644
index 00000000000..e98cf61db98
--- /dev/null
+++ b/db/post_migrate/20230811150636_swap_commit_user_mentions_note_id_to_bigint_for_self_managed.rb
@@ -0,0 +1,69 @@
+# frozen_string_literal: true
+
+class SwapCommitUserMentionsNoteIdToBigintForSelfManaged < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ disable_ddl_transaction!
+
+ TABLE_NAME = 'commit_user_mentions'
+
+ def up
+ return if com_or_dev_or_test_but_not_jh?
+ return if temp_column_removed?(TABLE_NAME, :note_id)
+ return if columns_swapped?(TABLE_NAME, :note_id)
+
+ swap
+ end
+
+ def down
+ return if com_or_dev_or_test_but_not_jh?
+ return if temp_column_removed?(TABLE_NAME, :note_id)
+ return unless columns_swapped?(TABLE_NAME, :note_id)
+
+ swap
+ end
+
+ # Same as db/post_migrate/20230321003252_swap_commit_user_mentions_note_id_to_bigint_for_gitlab_dot_com.rb
+ def swap
+ # This will replace the existing commit_user_mentions_on_commit_id_and_note_id_unique_index
+ add_concurrent_index TABLE_NAME, [:commit_id, :note_id_convert_to_bigint], unique: true,
+ name: 'commit_user_mentions_on_commit_id_and_note_id_convert_to_bigint'
+
+ # This will replace the existing index_commit_user_mentions_on_note_id
+ add_concurrent_index TABLE_NAME, :note_id_convert_to_bigint, unique: true,
+ name: 'index_commit_user_mentions_on_note_id_convert_to_bigint'
+
+ # This will replace the existing fk_rails_a6760813e0
+ add_concurrent_foreign_key TABLE_NAME, :notes, column: :note_id_convert_to_bigint,
+ name: 'fk_commit_user_mentions_note_id_convert_to_bigint',
+ on_delete: :cascade
+
+ with_lock_retries(raise_on_exhaustion: true) do
+ execute "LOCK TABLE notes, #{TABLE_NAME} IN ACCESS EXCLUSIVE MODE"
+
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN note_id TO note_id_tmp"
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN note_id_convert_to_bigint TO note_id"
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN note_id_tmp TO note_id_convert_to_bigint"
+
+ function_name = Gitlab::Database::UnidirectionalCopyTrigger
+ .on_table(TABLE_NAME, connection: connection)
+ .name(:note_id, :note_id_convert_to_bigint)
+ execute "ALTER FUNCTION #{quote_table_name(function_name)} RESET ALL"
+
+ # Swap defaults
+ change_column_default TABLE_NAME, :note_id, nil
+ change_column_default TABLE_NAME, :note_id_convert_to_bigint, 0
+
+ execute 'DROP INDEX IF EXISTS commit_user_mentions_on_commit_id_and_note_id_unique_index'
+ rename_index TABLE_NAME, 'commit_user_mentions_on_commit_id_and_note_id_convert_to_bigint',
+ 'commit_user_mentions_on_commit_id_and_note_id_unique_index'
+
+ execute 'DROP INDEX IF EXISTS index_commit_user_mentions_on_note_id'
+ rename_index TABLE_NAME, 'index_commit_user_mentions_on_note_id_convert_to_bigint',
+ 'index_commit_user_mentions_on_note_id'
+
+ execute "ALTER TABLE #{TABLE_NAME} DROP CONSTRAINT IF EXISTS fk_rails_a6760813e0"
+ rename_constraint(TABLE_NAME, 'fk_commit_user_mentions_note_id_convert_to_bigint', 'fk_rails_a6760813e0')
+ end
+ end
+end
diff --git a/db/post_migrate/20230814143925_ensure_timelogs_note_id_bigint_backfill_is_finished_for_self_hosts.rb b/db/post_migrate/20230814143925_ensure_timelogs_note_id_bigint_backfill_is_finished_for_self_hosts.rb
new file mode 100644
index 00000000000..5aff282f699
--- /dev/null
+++ b/db/post_migrate/20230814143925_ensure_timelogs_note_id_bigint_backfill_is_finished_for_self_hosts.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+class EnsureTimelogsNoteIdBigintBackfillIsFinishedForSelfHosts < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ restrict_gitlab_migration gitlab_schema: :gitlab_main
+ disable_ddl_transaction!
+
+ def up
+ ensure_batched_background_migration_is_finished(
+ job_class_name: 'CopyColumnUsingBackgroundMigrationJob',
+ table_name: 'timelogs',
+ column_name: 'id',
+ job_arguments: [['note_id'], ['note_id_convert_to_bigint']]
+ )
+ end
+
+ def down
+ # no-op
+ end
+end
diff --git a/db/post_migrate/20230814144045_swap_timelogs_note_id_to_bigint_for_self_hosts.rb b/db/post_migrate/20230814144045_swap_timelogs_note_id_to_bigint_for_self_hosts.rb
new file mode 100644
index 00000000000..55a0d46d840
--- /dev/null
+++ b/db/post_migrate/20230814144045_swap_timelogs_note_id_to_bigint_for_self_hosts.rb
@@ -0,0 +1,58 @@
+# frozen_string_literal: true
+
+class SwapTimelogsNoteIdToBigintForSelfHosts < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ disable_ddl_transaction!
+
+ TABLE_NAME = 'timelogs'
+
+ def up
+ return if should_skip?
+ return if temp_column_removed?(TABLE_NAME, :note_id)
+ return if columns_swapped?(TABLE_NAME, :note_id)
+
+ swap
+ end
+
+ def down
+ return if should_skip?
+ return if temp_column_removed?(TABLE_NAME, :note_id)
+ return unless columns_swapped?(TABLE_NAME, :note_id)
+
+ swap
+ end
+
+ def swap
+ # This will replace the existing index_timelogs_on_note_id
+ add_concurrent_index TABLE_NAME, :note_id_convert_to_bigint, name: 'index_timelogs_on_note_id_convert_to_bigint'
+
+ # This will replace the existing fk_timelogs_note_id
+ add_concurrent_foreign_key :timelogs, :notes, column: :note_id_convert_to_bigint,
+ name: 'fk_timelogs_note_id_convert_to_bigint',
+ on_delete: :nullify
+
+ with_lock_retries(raise_on_exhaustion: true) do
+ execute "LOCK TABLE notes, #{TABLE_NAME} IN ACCESS EXCLUSIVE MODE"
+
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN note_id TO note_id_tmp"
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN note_id_convert_to_bigint TO note_id"
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN note_id_tmp TO note_id_convert_to_bigint"
+
+ function_name = Gitlab::Database::UnidirectionalCopyTrigger
+ .on_table(TABLE_NAME, connection: connection)
+ .name(:note_id, :note_id_convert_to_bigint)
+ execute "ALTER FUNCTION #{quote_table_name(function_name)} RESET ALL"
+
+ execute "DROP INDEX IF EXISTS index_timelogs_on_note_id"
+ rename_index TABLE_NAME, 'index_timelogs_on_note_id_convert_to_bigint', 'index_timelogs_on_note_id'
+
+ execute "ALTER TABLE #{TABLE_NAME} DROP CONSTRAINT IF EXISTS fk_timelogs_note_id"
+ rename_constraint(TABLE_NAME, 'fk_timelogs_note_id_convert_to_bigint', 'fk_timelogs_note_id')
+ end
+ end
+
+ def should_skip?
+ com_or_dev_or_test_but_not_jh?
+ end
+end