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/20230314094213_remove_foreign_keys_from_u2f_registrations_table.rb15
-rw-r--r--db/post_migrate/20230314094215_drop_u2f_registrations_table.rb23
-rw-r--r--db/post_migrate/20230317004428_migrate_daily_redis_hll_events_to_weekly_aggregation.rb6
-rw-r--r--db/post_migrate/20230523101514_finalize_user_type_migration.rb22
-rw-r--r--db/post_migrate/20230528203340_drop_message_from_vulnerability_occurrences.rb17
-rw-r--r--db/post_migrate/20230530012406_finalize_backfill_resource_link_events.rb22
-rw-r--r--db/post_migrate/20230601211635_add_detected_at_id_index_to_vulnerabilities.rb15
-rw-r--r--db/post_migrate/20230601213750_add_uuid_project_id_state_id_index_to_vulnerability_reads.rb15
-rw-r--r--db/post_migrate/20230608145500_add_prepared_at_index_to_merge_requests.rb20
-rw-r--r--db/post_migrate/20230609065942_backfill_ci_pipeline_variables_for_pipeline_id_bigint_conversion.rb16
-rw-r--r--db/post_migrate/20230613192703_ensure_ci_build_needs_big_int_backfill_is_finished_for_self_hosts.rb24
-rw-r--r--db/post_migrate/20230613192802_swap_ci_build_needs_to_big_int_for_self_hosts.rb74
-rw-r--r--db/post_migrate/20230614181310_track_organization_deletions.rb17
-rw-r--r--db/post_migrate/20230614182049_add_index_to_namespaces_organization_id.rb19
-rw-r--r--db/post_migrate/20230615030402_remove_audit_events_group_index.rb40
-rw-r--r--db/post_migrate/20230615113748_replace_ci_job_artifacts_foreign_key_v3.rb20
-rw-r--r--db/post_migrate/20230615113840_replace_ci_running_builds_foreign_key_v3.rb20
-rw-r--r--db/post_migrate/20230615113858_replace_ci_job_variables_foreign_key_v3.rb20
-rw-r--r--db/post_migrate/20230615121103_replace_p_ci_builds_metadata_foreign_key_v3.rb37
-rw-r--r--db/post_migrate/20230615121122_replace_p_ci_runner_machine_builds_foreign_key_v3.rb37
-rw-r--r--db/post_migrate/20230615144627_remove_temporary_container_repository_indexes.rb82
-rw-r--r--db/post_migrate/20230616082958_add_unique_index_for_npm_packages_on_project_id_name_version.rb22
-rw-r--r--db/post_migrate/20230618020202_finish_user_type_migration.rb22
-rw-r--r--db/post_migrate/20230619005223_change_unconfirmed_created_at_index_on_users.rb24
-rw-r--r--db/post_migrate/20230619072744_schedule_index_events_on_project_id_and_id_desc_on_merged_action_for_removal.rb17
-rw-r--r--db/post_migrate/20230619081412_add_index_to_ci_triggers_token.rb15
-rw-r--r--db/post_migrate/20230619123701_schedule_removal_index_job_artifacts_id_and_expire_at.rb16
-rw-r--r--db/post_migrate/20230619134106_prepare_index_for_vulnerability_reads_on_common_project_filters.rb18
-rw-r--r--db/post_migrate/20230620072854_replace_old_fk_ci_build_pending_states_to_builds_v2.rb37
-rw-r--r--db/post_migrate/20230620072856_replace_old_fk_ci_build_trace_chunks_to_builds_v2.rb37
-rw-r--r--db/post_migrate/20230620072900_replace_old_fk_ci_unit_test_failures_to_builds_v2.rb37
-rw-r--r--db/post_migrate/20230620072905_replace_old_fk_ci_sources_pipelines_to_builds_v2.rb37
-rw-r--r--db/post_migrate/20230620072908_replace_old_fk_ci_resources_to_builds_v2.rb37
-rw-r--r--db/post_migrate/20230620072911_replace_old_fk_ci_build_report_results_to_builds_v2.rb37
-rw-r--r--db/post_migrate/20230620072915_replace_old_fk_ci_build_needs_to_builds_v2.rb37
-rw-r--r--db/post_migrate/20230620072917_replace_old_fk_ci_builds_runner_session_to_builds_v2.rb37
-rw-r--r--db/post_migrate/20230620072920_replace_old_fk_ci_pending_builds_to_builds_v2.rb37
-rw-r--r--db/post_migrate/20230620072925_replace_old_fk_ci_build_trace_metadata_to_builds_v2.rb37
-rw-r--r--db/post_migrate/20230620105919_replace_old_fk_ci_job_artifacts_to_builds.rb45
-rw-r--r--db/post_migrate/20230620110004_replace_old_fk_ci_running_builds_to_builds.rb45
-rw-r--r--db/post_migrate/20230620110021_replace_old_fk_ci_job_variables_to_builds.rb45
-rw-r--r--db/post_migrate/20230620134708_validate_user_type_constraint.rb13
-rw-r--r--db/post_migrate/20230620201738_add_index_issues_on_project_health_status_asc_work_item_type.rb18
-rw-r--r--db/post_migrate/20230620201817_add_index_issues_on_project_health_status_desc_work_item_type.rb18
-rw-r--r--db/post_migrate/20230621072848_add_text_limit_to_ci_variable_description.rb16
-rw-r--r--db/post_migrate/20230621083052_add_text_limit_to_ci_group_variable_description.rb16
-rw-r--r--db/post_migrate/20230621102941_replace_old_fk_ci_job_artifacts_to_builds_v2.rb37
-rw-r--r--db/post_migrate/20230621103000_replace_old_fk_ci_running_builds_to_builds_v2.rb37
-rw-r--r--db/post_migrate/20230621103043_replace_old_fk_ci_job_variables_to_builds_v2.rb37
-rw-r--r--db/post_migrate/20230622052046_remove_ci_pipeline_variables_trigger_and_old_column.rb16
-rw-r--r--db/post_migrate/20230622073752_ensure_backfill_ci_pipelines_bigint_id_is_completed.rb23
-rw-r--r--db/post_migrate/20230623073906_prepare_async_index_for_ci_pipelines_bigint_id.rb15
-rw-r--r--db/post_migrate/20230626070723_drop_unused_sent_notification_columns.rb17
-rw-r--r--db/post_migrate/20230626072436_drop_tmp_index_job_artifacts_id_and_expire_at.rb28
-rw-r--r--db/post_migrate/20230626101519_create_index_for_vulnerability_reads_on_common_project_filters.rb18
-rw-r--r--db/post_migrate/20230626115046_add_temporary_index_on_vulnerability_reads.rb24
-rw-r--r--db/post_migrate/20230627115305_replace_p_ci_builds_metadata_foreign_key_v4.rb40
-rw-r--r--db/post_migrate/20230627140707_async_index_for_vulnerabilities_uuid_type_migration.rb25
-rw-r--r--db/post_migrate/20230627174139_add_index_to_pool_repositories.rb21
-rw-r--r--db/post_migrate/20230628023103_queue_backfill_missing_ci_cd_settings.rb25
-rw-r--r--db/post_migrate/20230629011859_cleanup_bigint_conversion_for_notes_for_gitlab_com.rb28
-rw-r--r--db/post_migrate/20230629024032_ensure_events_bigint_backfill_is_finished_for_gitlab_dot_com.rb29
-rw-r--r--db/post_migrate/20230629024403_events_bigint_create_indexe_async_for_gitlab_dot_com.rb28
-rw-r--r--db/post_migrate/20230629095819_queue_backfill_uuid_conversion_column_in_vulnerability_occurrences.rb25
-rw-r--r--db/post_migrate/20230703024031_cleanup_project_pipeline_status_key.rb15
-rw-r--r--db/post_migrate/20230703122803_create_vulnerability_uuid_type_migration_index.rb11
-rw-r--r--db/post_migrate/20230704042302_prepare_removal_index_deployments_on_project_id_sha.rb17
-rw-r--r--db/post_migrate/20230704044541_prepare_removal_index_deployments_on_project_id_and_status.rb16
-rw-r--r--db/post_migrate/20230704050739_prepare_removal_index_deployments_on_environment_id_and_iid_and_project_id.rb16
-rw-r--r--db/post_migrate/20230704062132_replace_p_ci_builds_metadata_foreign_key_v5.rb26
-rw-r--r--db/post_migrate/20230704062136_replace_p_ci_runner_machine_builds_foreign_key_v4.rb26
-rw-r--r--db/post_migrate/20230704074120_replace_old_fk_p_ci_builds_metadata_to_builds.rb54
-rw-r--r--db/post_migrate/20230704074124_replace_old_fk_p_ci_runner_machine_builds_to_builds.rb54
-rw-r--r--db/post_migrate/20230705115215_remove_user_details_provisioned_by_group_at_column.rb9
-rw-r--r--db/post_migrate/20230705141703_rollback_vulnerability_advisories_foreign_key_on_vulnerable_component_versions.rb17
-rw-r--r--db/post_migrate/20230705141733_rollback_component_version_foreign_key_on_vulnerable_component_versions.rb17
-rw-r--r--db/post_migrate/20230705142241_drop_vulnerable_component_versions.rb22
-rw-r--r--db/post_migrate/20230705142334_drop_vulnerabilities_advisories.rb29
-rw-r--r--db/post_migrate/20230705145827_drop_wrong_index_on_vulnerability_occurrences.rb22
-rw-r--r--db/post_migrate/20230705150100_recreate_type_migration_index_on_vulnerability_occurrences.rb24
-rw-r--r--db/post_migrate/20230706011541_add_user_id_foreign_key_to_ml_experiments.rb31
-rw-r--r--db/post_migrate/20230706073937_remove_index_events_on_project_id_and_id_desc_on_merged_action.rb17
-rw-r--r--db/post_migrate/20230706080234_add_trigger_on_organizations.rb29
-rw-r--r--db/post_migrate/20230707114012_change_project_view_default.rb9
-rw-r--r--db/post_migrate/20230707211358_deduplicate_inactive_alert_integrations.rb40
-rw-r--r--db/post_migrate/20230707211359_change_alert_integrations_unique_index.rb33
-rw-r--r--db/post_migrate/20230707220646_add_index_to_vulnerability_findings_on_uuid_again.rb16
-rw-r--r--db/post_migrate/20230711093010_drop_default_partition_id_value_for_ci_tables.rb39
-rw-r--r--db/post_migrate/20230712020407_remove_temporary_index_from_system_note_metadata.rb18
-rw-r--r--db/post_migrate/20230712064637_replace_old_fk_p_ci_builds_metadata_to_builds_v2.rb54
-rw-r--r--db/post_migrate/20230712064655_replace_old_fk_p_ci_runner_machine_builds_to_builds_v2.rb54
-rw-r--r--db/post_migrate/20230712145323_drop_ci_job_artifacts_partition_id_default.rb24
-rw-r--r--db/post_migrate/20230712145821_drop_ci_stages_partition_id_default.rb24
-rw-r--r--db/post_migrate/20230712145926_drop_ci_build_trace_metadata_partition_id_default.rb24
-rw-r--r--db/post_migrate/20230713020247_change_pm_advisories_urls_constraint.rb17
-rw-r--r--db/post_migrate/20230713100100_drop_ci_pipeline_variable_partition_id_default.rb24
96 files changed, 2582 insertions, 0 deletions
diff --git a/db/post_migrate/20230314094213_remove_foreign_keys_from_u2f_registrations_table.rb b/db/post_migrate/20230314094213_remove_foreign_keys_from_u2f_registrations_table.rb
new file mode 100644
index 00000000000..d6185d6674d
--- /dev/null
+++ b/db/post_migrate/20230314094213_remove_foreign_keys_from_u2f_registrations_table.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+class RemoveForeignKeysFromU2fRegistrationsTable < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ def up
+ with_lock_retries do
+ remove_foreign_key :u2f_registrations, :users
+ end
+ end
+
+ def down
+ add_concurrent_foreign_key :u2f_registrations, :users, column: :user_id
+ end
+end
diff --git a/db/post_migrate/20230314094215_drop_u2f_registrations_table.rb b/db/post_migrate/20230314094215_drop_u2f_registrations_table.rb
new file mode 100644
index 00000000000..9a57c424592
--- /dev/null
+++ b/db/post_migrate/20230314094215_drop_u2f_registrations_table.rb
@@ -0,0 +1,23 @@
+# frozen_string_literal: true
+
+class DropU2fRegistrationsTable < Gitlab::Database::Migration[2.1]
+ enable_lock_retries!
+
+ def up
+ drop_table :u2f_registrations
+ end
+
+ def down
+ create_table :u2f_registrations do |t| # rubocop: disable Migration/SchemaAdditionMethodsNoPost
+ t.text :certificate
+ t.string :key_handle
+ t.string :public_key
+ t.integer :counter
+ t.references :user, foreign_key: false
+ t.datetime_with_timezone :created_at, null: false
+ t.datetime_with_timezone :updated_at, null: false
+ t.string :name
+ t.index [:key_handle], name: 'index_u2f_registrations_on_key_handle'
+ end
+ end
+end
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 59bff26f964..168354cd946 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,6 +3,12 @@
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/20230523101514_finalize_user_type_migration.rb b/db/post_migrate/20230523101514_finalize_user_type_migration.rb
new file mode 100644
index 00000000000..c6533bec587
--- /dev/null
+++ b/db/post_migrate/20230523101514_finalize_user_type_migration.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+class FinalizeUserTypeMigration < Gitlab::Database::Migration[2.1]
+ MIGRATION = 'MigrateHumanUserType'
+
+ restrict_gitlab_migration gitlab_schema: :gitlab_main
+
+ disable_ddl_transaction!
+
+ def up
+ ensure_batched_background_migration_is_finished(
+ job_class_name: MIGRATION,
+ table_name: :users,
+ column_name: :id,
+ job_arguments: []
+ )
+ end
+
+ def down
+ # no-op
+ end
+end
diff --git a/db/post_migrate/20230528203340_drop_message_from_vulnerability_occurrences.rb b/db/post_migrate/20230528203340_drop_message_from_vulnerability_occurrences.rb
new file mode 100644
index 00000000000..4214ba803f6
--- /dev/null
+++ b/db/post_migrate/20230528203340_drop_message_from_vulnerability_occurrences.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class DropMessageFromVulnerabilityOccurrences < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ def up
+ remove_column :vulnerability_occurrences, :message
+ end
+
+ def down
+ unless column_exists?(:vulnerability_occurrences, :message)
+ add_column :vulnerability_occurrences, :message, :text # rubocop:disable Migration/SchemaAdditionMethodsNoPost
+ end
+
+ add_check_constraint(:vulnerability_occurrences, 'char_length(message) <= 3000', 'check_df6dd20219')
+ end
+end
diff --git a/db/post_migrate/20230530012406_finalize_backfill_resource_link_events.rb b/db/post_migrate/20230530012406_finalize_backfill_resource_link_events.rb
new file mode 100644
index 00000000000..c86a341392f
--- /dev/null
+++ b/db/post_migrate/20230530012406_finalize_backfill_resource_link_events.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+class FinalizeBackfillResourceLinkEvents < Gitlab::Database::Migration[2.1]
+ MIGRATION = 'BackfillResourceLinkEvents'
+ disable_ddl_transaction!
+
+ restrict_gitlab_migration gitlab_schema: :gitlab_main
+
+ def up
+ ensure_batched_background_migration_is_finished(
+ job_class_name: MIGRATION,
+ table_name: :system_note_metadata,
+ column_name: :id,
+ job_arguments: [],
+ finalize: true
+ )
+ end
+
+ def down
+ # no-op
+ end
+end
diff --git a/db/post_migrate/20230601211635_add_detected_at_id_index_to_vulnerabilities.rb b/db/post_migrate/20230601211635_add_detected_at_id_index_to_vulnerabilities.rb
new file mode 100644
index 00000000000..e7ccfad1fd3
--- /dev/null
+++ b/db/post_migrate/20230601211635_add_detected_at_id_index_to_vulnerabilities.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+class AddDetectedAtIdIndexToVulnerabilities < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ INDEX_NAME = 'index_vulnerabilities_on_detected_at_and_id'
+
+ def up
+ add_concurrent_index :vulnerabilities, [:id, :detected_at], name: INDEX_NAME
+ end
+
+ def down
+ remove_concurrent_index_by_name :vulnerabilities, INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230601213750_add_uuid_project_id_state_id_index_to_vulnerability_reads.rb b/db/post_migrate/20230601213750_add_uuid_project_id_state_id_index_to_vulnerability_reads.rb
new file mode 100644
index 00000000000..f91394eb783
--- /dev/null
+++ b/db/post_migrate/20230601213750_add_uuid_project_id_state_id_index_to_vulnerability_reads.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+class AddUuidProjectIdStateIdIndexToVulnerabilityReads < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ INDEX_NAME = 'index_vulnerability_reads_on_uuid_project_id_and_state'
+
+ def up
+ add_concurrent_index :vulnerability_reads, [:uuid, :project_id, :state], name: INDEX_NAME
+ end
+
+ def down
+ remove_concurrent_index_by_name :vulnerability_reads, INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230608145500_add_prepared_at_index_to_merge_requests.rb b/db/post_migrate/20230608145500_add_prepared_at_index_to_merge_requests.rb
new file mode 100644
index 00000000000..3d3557ce543
--- /dev/null
+++ b/db/post_migrate/20230608145500_add_prepared_at_index_to_merge_requests.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+class AddPreparedAtIndexToMergeRequests < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ INDEX_NAME = 'index_merge_requests_on_id_and_prepared_at'
+
+ # TODO: Index to be created synchronously in https://gitlab.com/gitlab-org/gitlab/-/issues/415721
+
+ def up
+ prepare_async_index(:merge_requests,
+ :id,
+ name: INDEX_NAME,
+ where: "prepared_at IS NULL")
+ end
+
+ def down
+ unprepare_async_index(:merge_requests, :id, name: INDEX_NAME)
+ end
+end
diff --git a/db/post_migrate/20230609065942_backfill_ci_pipeline_variables_for_pipeline_id_bigint_conversion.rb b/db/post_migrate/20230609065942_backfill_ci_pipeline_variables_for_pipeline_id_bigint_conversion.rb
new file mode 100644
index 00000000000..c09820ad9d4
--- /dev/null
+++ b/db/post_migrate/20230609065942_backfill_ci_pipeline_variables_for_pipeline_id_bigint_conversion.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+class BackfillCiPipelineVariablesForPipelineIdBigintConversion < Gitlab::Database::Migration[2.1]
+ restrict_gitlab_migration gitlab_schema: :gitlab_ci
+
+ TABLE = :ci_pipeline_variables
+ COLUMNS = %i[pipeline_id]
+
+ def up
+ backfill_conversion_of_integer_to_bigint(TABLE, COLUMNS, sub_batch_size: 500)
+ end
+
+ def down
+ revert_backfill_conversion_of_integer_to_bigint(TABLE, COLUMNS)
+ end
+end
diff --git a/db/post_migrate/20230613192703_ensure_ci_build_needs_big_int_backfill_is_finished_for_self_hosts.rb b/db/post_migrate/20230613192703_ensure_ci_build_needs_big_int_backfill_is_finished_for_self_hosts.rb
new file mode 100644
index 00000000000..7616944567e
--- /dev/null
+++ b/db/post_migrate/20230613192703_ensure_ci_build_needs_big_int_backfill_is_finished_for_self_hosts.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+class EnsureCiBuildNeedsBigIntBackfillIsFinishedForSelfHosts < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ disable_ddl_transaction!
+
+ restrict_gitlab_migration gitlab_schema: :gitlab_ci
+
+ TABLE_NAME = 'ci_build_needs'
+
+ def up
+ ensure_batched_background_migration_is_finished(
+ job_class_name: 'CopyColumnUsingBackgroundMigrationJob',
+ table_name: TABLE_NAME,
+ column_name: 'id',
+ job_arguments: [['id'], ['id_convert_to_bigint']]
+ )
+ end
+
+ def down
+ # no-op
+ end
+end
diff --git a/db/post_migrate/20230613192802_swap_ci_build_needs_to_big_int_for_self_hosts.rb b/db/post_migrate/20230613192802_swap_ci_build_needs_to_big_int_for_self_hosts.rb
new file mode 100644
index 00000000000..7533de86a2a
--- /dev/null
+++ b/db/post_migrate/20230613192802_swap_ci_build_needs_to_big_int_for_self_hosts.rb
@@ -0,0 +1,74 @@
+# frozen_string_literal: true
+
+class SwapCiBuildNeedsToBigIntForSelfHosts < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ disable_ddl_transaction!
+
+ TABLE_NAME = 'ci_build_needs'
+
+ def up
+ return if should_skip?
+ return if temporary_column_already_dropped?
+ return if columns_already_swapped?
+
+ swap
+ end
+
+ def down
+ return if should_skip?
+ return if temporary_column_already_dropped?
+ return unless columns_already_swapped?
+
+ swap
+ end
+
+ private
+
+ def swap
+ add_concurrent_index TABLE_NAME, :id_convert_to_bigint, unique: true, name:
+ 'index_ci_build_needs_on_id_convert_to_bigint'
+
+ with_lock_retries(raise_on_exhaustion: true) do
+ execute "LOCK TABLE #{TABLE_NAME} IN ACCESS EXCLUSIVE MODE"
+
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN id TO id_tmp"
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN id_convert_to_bigint TO id"
+ execute "ALTER TABLE #{TABLE_NAME} RENAME COLUMN id_tmp TO id_convert_to_bigint"
+
+ function_name = Gitlab::Database::UnidirectionalCopyTrigger.on_table(
+ TABLE_NAME, connection: Ci::ApplicationRecord.connection
+ ).name(
+ :id, :id_convert_to_bigint
+ )
+
+ execute "ALTER FUNCTION #{quote_table_name(function_name)} RESET ALL"
+
+ execute "ALTER SEQUENCE ci_build_needs_id_seq OWNED BY #{TABLE_NAME}.id"
+ change_column_default TABLE_NAME, :id, -> { "nextval('ci_build_needs_id_seq'::regclass)" }
+ change_column_default TABLE_NAME, :id_convert_to_bigint, 0
+
+ execute "ALTER TABLE #{TABLE_NAME} DROP CONSTRAINT ci_build_needs_pkey CASCADE"
+ rename_index TABLE_NAME, 'index_ci_build_needs_on_id_convert_to_bigint', 'ci_build_needs_pkey'
+ execute "ALTER TABLE #{TABLE_NAME} ADD CONSTRAINT ci_build_needs_pkey PRIMARY KEY USING INDEX ci_build_needs_pkey"
+ end
+ end
+
+ def should_skip?
+ com_or_dev_or_test_but_not_jh?
+ end
+
+ def columns_already_swapped?
+ table_columns = columns(TABLE_NAME)
+ column_id = table_columns.find { |c| c.name == 'id' }
+ column_id_convert_to_bigint = table_columns.find { |c| c.name == 'id_convert_to_bigint' }
+
+ column_id.sql_type == 'bigint' && column_id_convert_to_bigint.sql_type == 'integer'
+ end
+
+ def temporary_column_already_dropped?
+ table_columns = columns(TABLE_NAME)
+
+ !table_columns.find { |c| c.name == 'id_convert_to_bigint' }
+ end
+end
diff --git a/db/post_migrate/20230614181310_track_organization_deletions.rb b/db/post_migrate/20230614181310_track_organization_deletions.rb
new file mode 100644
index 00000000000..9e936d6d285
--- /dev/null
+++ b/db/post_migrate/20230614181310_track_organization_deletions.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class TrackOrganizationDeletions < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::LooseForeignKeyHelpers
+
+ enable_lock_retries!
+
+ def up
+ # This LFK trigger already exists on some environments and it was reverted
+ # in MR: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/122809
+ track_record_deletions(:organizations) unless has_loose_foreign_key?('organizations')
+ end
+
+ def down
+ untrack_record_deletions(:organizations)
+ end
+end
diff --git a/db/post_migrate/20230614182049_add_index_to_namespaces_organization_id.rb b/db/post_migrate/20230614182049_add_index_to_namespaces_organization_id.rb
new file mode 100644
index 00000000000..edbb0a8b953
--- /dev/null
+++ b/db/post_migrate/20230614182049_add_index_to_namespaces_organization_id.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+class AddIndexToNamespacesOrganizationId < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = 'index_namespaces_on_organization_id'
+ TABLE = :namespaces
+
+ disable_ddl_transaction!
+
+ # This index was added on GitLab SaaS in
+ # https://gitlab.com/gitlab-org/gitlab/-/merge_requests/120822
+
+ def up
+ add_concurrent_index TABLE, :organization_id, name: INDEX_NAME
+ end
+
+ def down
+ remove_concurrent_index_by_name TABLE, INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230615030402_remove_audit_events_group_index.rb b/db/post_migrate/20230615030402_remove_audit_events_group_index.rb
new file mode 100644
index 00000000000..9a77d7a4d0d
--- /dev/null
+++ b/db/post_migrate/20230615030402_remove_audit_events_group_index.rb
@@ -0,0 +1,40 @@
+# 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 RemoveAuditEventsGroupIndex < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::PartitioningMigrationHelpers
+
+ TABLE_NAME = :audit_events
+ COLUMN_NAMES = [:entity_id, :entity_type, :created_at, :id]
+ INDEX_NAME = 'index_audit_events_on_entity_id_and_entity_type_and_created_at'
+
+ # TODO: Index to be destroyed synchronously in https://gitlab.com/gitlab-org/gitlab/-/issues/415424
+ def up
+ each_partition(TABLE_NAME) do |partition, partition_index_name|
+ unprepare_async_index_by_name(partition.identifier, partition_index_name)
+ end
+ end
+
+ def down
+ each_partition(TABLE_NAME) do |partition, partition_index_name|
+ prepare_async_index(
+ partition.identifier,
+ COLUMN_NAMES,
+ name: partition_index_name
+ )
+ end
+ end
+
+ private
+
+ def each_partition(table_name)
+ partitioned_table = find_partitioned_table(table_name)
+ partitioned_table.postgres_partitions.order(:name).each do |partition|
+ partition_index_name = generated_index_name(partition.identifier, INDEX_NAME)
+
+ yield partition, partition_index_name
+ end
+ end
+end
diff --git a/db/post_migrate/20230615113748_replace_ci_job_artifacts_foreign_key_v3.rb b/db/post_migrate/20230615113748_replace_ci_job_artifacts_foreign_key_v3.rb
new file mode 100644
index 00000000000..92b3008ff00
--- /dev/null
+++ b/db/post_migrate/20230615113748_replace_ci_job_artifacts_foreign_key_v3.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+class ReplaceCiJobArtifactsForeignKeyV3 < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ def up
+ add_concurrent_foreign_key :ci_job_artifacts, :p_ci_builds,
+ name: 'temp_fk_rails_c5137cb2c1_p',
+ column: [:partition_id, :job_id],
+ target_column: [:partition_id, :id],
+ on_update: :cascade,
+ on_delete: :cascade,
+ validate: true,
+ reverse_lock_order: true
+ end
+
+ def down
+ remove_foreign_key_if_exists :ci_job_artifacts, name: 'temp_fk_rails_c5137cb2c1_p'
+ end
+end
diff --git a/db/post_migrate/20230615113840_replace_ci_running_builds_foreign_key_v3.rb b/db/post_migrate/20230615113840_replace_ci_running_builds_foreign_key_v3.rb
new file mode 100644
index 00000000000..ef8f252bbf7
--- /dev/null
+++ b/db/post_migrate/20230615113840_replace_ci_running_builds_foreign_key_v3.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+class ReplaceCiRunningBuildsForeignKeyV3 < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ def up
+ add_concurrent_foreign_key :ci_running_builds, :p_ci_builds,
+ name: 'temp_fk_rails_da45cfa165_p',
+ column: [:partition_id, :build_id],
+ target_column: [:partition_id, :id],
+ on_update: :cascade,
+ on_delete: :cascade,
+ validate: true,
+ reverse_lock_order: true
+ end
+
+ def down
+ remove_foreign_key_if_exists :ci_running_builds, name: 'temp_fk_rails_da45cfa165_p'
+ end
+end
diff --git a/db/post_migrate/20230615113858_replace_ci_job_variables_foreign_key_v3.rb b/db/post_migrate/20230615113858_replace_ci_job_variables_foreign_key_v3.rb
new file mode 100644
index 00000000000..76c83c90ad0
--- /dev/null
+++ b/db/post_migrate/20230615113858_replace_ci_job_variables_foreign_key_v3.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+class ReplaceCiJobVariablesForeignKeyV3 < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ def up
+ add_concurrent_foreign_key :ci_job_variables, :p_ci_builds,
+ name: 'temp_fk_rails_fbf3b34792_p',
+ column: [:partition_id, :job_id],
+ target_column: [:partition_id, :id],
+ on_update: :cascade,
+ on_delete: :cascade,
+ validate: true,
+ reverse_lock_order: true
+ end
+
+ def down
+ remove_foreign_key_if_exists :ci_job_variables, name: 'temp_fk_rails_fbf3b34792_p'
+ end
+end
diff --git a/db/post_migrate/20230615121103_replace_p_ci_builds_metadata_foreign_key_v3.rb b/db/post_migrate/20230615121103_replace_p_ci_builds_metadata_foreign_key_v3.rb
new file mode 100644
index 00000000000..dca891fc064
--- /dev/null
+++ b/db/post_migrate/20230615121103_replace_p_ci_builds_metadata_foreign_key_v3.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+class ReplacePCiBuildsMetadataForeignKeyV3 < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::PartitioningMigrationHelpers
+ include Gitlab::Database::MigrationHelpers::WraparoundAutovacuum
+
+ disable_ddl_transaction!
+
+ def up
+ return unless should_run?
+
+ add_concurrent_partitioned_foreign_key :p_ci_builds_metadata, :p_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
+ end
+
+ def down
+ return unless should_run?
+
+ with_lock_retries do
+ remove_foreign_key_if_exists :p_ci_builds_metadata, :p_ci_builds,
+ name: :temp_fk_e20479742e_p,
+ reverse_lock_order: true
+ end
+ end
+
+ private
+
+ def should_run?
+ can_execute_on?(:ci_builds_metadata, :ci_builds)
+ end
+end
diff --git a/db/post_migrate/20230615121122_replace_p_ci_runner_machine_builds_foreign_key_v3.rb b/db/post_migrate/20230615121122_replace_p_ci_runner_machine_builds_foreign_key_v3.rb
new file mode 100644
index 00000000000..854d7358e5e
--- /dev/null
+++ b/db/post_migrate/20230615121122_replace_p_ci_runner_machine_builds_foreign_key_v3.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+class ReplacePCiRunnerMachineBuildsForeignKeyV3 < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::PartitioningMigrationHelpers
+ include Gitlab::Database::MigrationHelpers::WraparoundAutovacuum
+
+ disable_ddl_transaction!
+
+ def up
+ return unless should_run?
+
+ add_concurrent_partitioned_foreign_key :p_ci_runner_machine_builds, :p_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
+ end
+
+ def down
+ return unless should_run?
+
+ with_lock_retries do
+ remove_foreign_key_if_exists :p_ci_runner_machine_builds, :p_ci_builds,
+ name: :temp_fk_bb490f12fe_p,
+ reverse_lock_order: true
+ end
+ end
+
+ private
+
+ def should_run?
+ can_execute_on?(:ci_builds)
+ end
+end
diff --git a/db/post_migrate/20230615144627_remove_temporary_container_repository_indexes.rb b/db/post_migrate/20230615144627_remove_temporary_container_repository_indexes.rb
new file mode 100644
index 00000000000..c4d01f35607
--- /dev/null
+++ b/db/post_migrate/20230615144627_remove_temporary_container_repository_indexes.rb
@@ -0,0 +1,82 @@
+# frozen_string_literal: true
+
+class RemoveTemporaryContainerRepositoryIndexes < Gitlab::Database::Migration[2.1]
+ INDEX_CONTAINER_REPOS_ON_NON_MIGRATED = 'tmp_index_container_repos_on_non_migrated'
+ INDEX_CONTAINER_REPOSITORIES_ON_ID_MIGRATION_STATE = 'tmp_index_container_repositories_on_id_migration_state'
+ INDEX_MIGRATED_CONTAINER_REGISTRIES = 'tmp_index_migrated_container_registries'
+ INDEX_CONTAINER_REPOS_ON_IMPORT_STARTED_AT_WHEN_IMPORTING = 'idx_container_repos_on_import_started_at_when_importing'
+ INDEX_CONTAINER_REPOS_ON_MIGRATION_STATE_MIGRATION_PLAN_CREATED =
+ 'idx_container_repos_on_migration_state_migration_plan_created'
+ INDEX_CONTAINER_REPOS_ON_PRE_IMPORT_DONE_AT_WHEN_PRE_IMPORT_DONE =
+ 'idx_container_repos_on_pre_import_done_at_when_pre_import_done'
+ INDEX_CONTAINER_REPOS_ON_PRE_IMPORT_STARTED_AT_WHEN_PRE_IMPORTING =
+ 'idx_container_repos_on_pre_import_started_at_when_pre_importing'
+ INDEX_CONTAINER_REPOSITORIES_ON_GREATEST_COMPLETED_AT = 'index_container_repositories_on_greatest_completed_at'
+ INDEX_CONTAINER_REPOSITORIES_ON_MIGRATION_STATE_IMPORT_DONE_AT =
+ 'index_container_repositories_on_migration_state_import_done_at'
+
+ disable_ddl_transaction!
+
+ def up
+ remove_concurrent_index_by_name :container_repositories, INDEX_CONTAINER_REPOS_ON_NON_MIGRATED
+ remove_concurrent_index_by_name :container_repositories, INDEX_CONTAINER_REPOSITORIES_ON_ID_MIGRATION_STATE
+ remove_concurrent_index_by_name :container_repositories, INDEX_MIGRATED_CONTAINER_REGISTRIES
+ remove_concurrent_index_by_name :container_repositories, INDEX_CONTAINER_REPOS_ON_IMPORT_STARTED_AT_WHEN_IMPORTING
+ remove_concurrent_index_by_name :container_repositories,
+ INDEX_CONTAINER_REPOS_ON_MIGRATION_STATE_MIGRATION_PLAN_CREATED
+ remove_concurrent_index_by_name :container_repositories,
+ INDEX_CONTAINER_REPOS_ON_PRE_IMPORT_DONE_AT_WHEN_PRE_IMPORT_DONE
+ remove_concurrent_index_by_name :container_repositories,
+ INDEX_CONTAINER_REPOS_ON_PRE_IMPORT_STARTED_AT_WHEN_PRE_IMPORTING
+ remove_concurrent_index_by_name :container_repositories, INDEX_CONTAINER_REPOSITORIES_ON_GREATEST_COMPLETED_AT
+ remove_concurrent_index_by_name :container_repositories,
+ INDEX_CONTAINER_REPOSITORIES_ON_MIGRATION_STATE_IMPORT_DONE_AT
+ end
+
+ def down
+ add_concurrent_index :container_repositories,
+ [:project_id, :id],
+ name: INDEX_CONTAINER_REPOS_ON_NON_MIGRATED,
+ where: "migration_state != 'import_done'"
+
+ add_concurrent_index :container_repositories,
+ [:id, :migration_state],
+ name: INDEX_CONTAINER_REPOSITORIES_ON_ID_MIGRATION_STATE
+
+ add_concurrent_index :container_repositories,
+ [:project_id],
+ name: INDEX_MIGRATED_CONTAINER_REGISTRIES,
+ where: "migration_state = 'import_done' OR created_at >= '2022-01-23'"
+
+ add_concurrent_index :container_repositories,
+ [:migration_import_started_at],
+ name: INDEX_CONTAINER_REPOS_ON_IMPORT_STARTED_AT_WHEN_IMPORTING,
+ where: "migration_state = 'importing'::text"
+
+ add_concurrent_index :container_repositories,
+ [:migration_state, :migration_plan, :created_at],
+ name: INDEX_CONTAINER_REPOS_ON_MIGRATION_STATE_MIGRATION_PLAN_CREATED
+
+ add_concurrent_index :container_repositories,
+ [:migration_pre_import_done_at],
+ name: INDEX_CONTAINER_REPOS_ON_PRE_IMPORT_DONE_AT_WHEN_PRE_IMPORT_DONE,
+ where: "migration_state = 'pre_import_done'::text"
+
+ add_concurrent_index :container_repositories,
+ [:migration_pre_import_started_at],
+ name: INDEX_CONTAINER_REPOS_ON_PRE_IMPORT_STARTED_AT_WHEN_PRE_IMPORTING,
+ where: "migration_state = 'pre_importing'::text"
+
+ execute(
+ <<-SQL
+ CREATE INDEX CONCURRENTLY #{INDEX_CONTAINER_REPOSITORIES_ON_GREATEST_COMPLETED_AT} ON container_repositories
+ USING btree (GREATEST(migration_pre_import_done_at, migration_import_done_at, migration_aborted_at, migration_skipped_at))
+ WHERE (migration_state = ANY (ARRAY['import_done'::text, 'pre_import_done'::text, 'import_aborted'::text, 'import_skipped'::text]));
+ SQL
+ )
+
+ add_concurrent_index :container_repositories,
+ [:migration_state, :migration_import_done_at],
+ name: INDEX_CONTAINER_REPOSITORIES_ON_MIGRATION_STATE_IMPORT_DONE_AT
+ end
+end
diff --git a/db/post_migrate/20230616082958_add_unique_index_for_npm_packages_on_project_id_name_version.rb b/db/post_migrate/20230616082958_add_unique_index_for_npm_packages_on_project_id_name_version.rb
new file mode 100644
index 00000000000..646bbf414e2
--- /dev/null
+++ b/db/post_migrate/20230616082958_add_unique_index_for_npm_packages_on_project_id_name_version.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+class AddUniqueIndexForNpmPackagesOnProjectIdNameVersion < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ INDEX_NAME = 'idx_packages_on_project_id_name_version_unique_when_npm'
+ PACKAGE_TYPE_NPM = 2
+
+ def up
+ add_concurrent_index(
+ :packages_packages,
+ %i[project_id name version],
+ name: INDEX_NAME,
+ unique: true,
+ where: "package_type = #{PACKAGE_TYPE_NPM} AND status <> 4"
+ )
+ end
+
+ def down
+ remove_concurrent_index_by_name :packages_packages, INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230618020202_finish_user_type_migration.rb b/db/post_migrate/20230618020202_finish_user_type_migration.rb
new file mode 100644
index 00000000000..4a729d5d408
--- /dev/null
+++ b/db/post_migrate/20230618020202_finish_user_type_migration.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+class FinishUserTypeMigration < Gitlab::Database::Migration[2.1]
+ MIGRATION = 'MigrateHumanUserType'
+
+ disable_ddl_transaction!
+
+ restrict_gitlab_migration gitlab_schema: :gitlab_main
+
+ def up
+ ensure_batched_background_migration_is_finished(
+ job_class_name: MIGRATION,
+ table_name: :users,
+ column_name: :id,
+ job_arguments: []
+ )
+ end
+
+ def down
+ # no-op
+ end
+end
diff --git a/db/post_migrate/20230619005223_change_unconfirmed_created_at_index_on_users.rb b/db/post_migrate/20230619005223_change_unconfirmed_created_at_index_on_users.rb
new file mode 100644
index 00000000000..5b9b4e36512
--- /dev/null
+++ b/db/post_migrate/20230619005223_change_unconfirmed_created_at_index_on_users.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+class ChangeUnconfirmedCreatedAtIndexOnUsers < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ OLD_INDEX_NAME = 'index_users_on_unconfirmed_and_created_at_for_active_humans'
+ NEW_INDEX_NAME = 'index_users_on_unconfirmed_created_at_active_type_sign_in_count'
+
+ def up
+ add_concurrent_index :users, [:created_at, :id],
+ name: NEW_INDEX_NAME,
+ where: "confirmed_at IS NULL AND state = 'active' AND user_type IN (0) AND sign_in_count = 0"
+
+ remove_concurrent_index_by_name :users, OLD_INDEX_NAME
+ end
+
+ def down
+ add_concurrent_index :users, [:created_at, :id],
+ name: OLD_INDEX_NAME,
+ where: "confirmed_at IS NULL AND state = 'active' AND user_type IN (0)"
+
+ remove_concurrent_index_by_name :users, NEW_INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230619072744_schedule_index_events_on_project_id_and_id_desc_on_merged_action_for_removal.rb b/db/post_migrate/20230619072744_schedule_index_events_on_project_id_and_id_desc_on_merged_action_for_removal.rb
new file mode 100644
index 00000000000..b73ddfffad6
--- /dev/null
+++ b/db/post_migrate/20230619072744_schedule_index_events_on_project_id_and_id_desc_on_merged_action_for_removal.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class ScheduleIndexEventsOnProjectIdAndIdDescOnMergedActionForRemoval < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = 'index_events_on_project_id_and_id_desc_on_merged_action'
+
+ # TODO: Index to be destroyed synchronously in https://gitlab.com/gitlab-org/gitlab/-/issues/415091
+
+ def up
+ prepare_async_index_removal :events, [:project_id, :id], order: { id: :desc },
+ where: "action = 7", name: INDEX_NAME
+ end
+
+ def down
+ unprepare_async_index :events, [:project_id, :id], order: { id: :desc },
+ where: "action = 7", name: INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230619081412_add_index_to_ci_triggers_token.rb b/db/post_migrate/20230619081412_add_index_to_ci_triggers_token.rb
new file mode 100644
index 00000000000..861c9eafd9f
--- /dev/null
+++ b/db/post_migrate/20230619081412_add_index_to_ci_triggers_token.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+class AddIndexToCiTriggersToken < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ INDEX_NAME = 'index_ci_triggers_on_token'
+
+ def up
+ add_concurrent_index :ci_triggers, :token, unique: true, name: INDEX_NAME
+ end
+
+ def down
+ remove_concurrent_index_by_name(:ci_triggers, INDEX_NAME)
+ end
+end
diff --git a/db/post_migrate/20230619123701_schedule_removal_index_job_artifacts_id_and_expire_at.rb b/db/post_migrate/20230619123701_schedule_removal_index_job_artifacts_id_and_expire_at.rb
new file mode 100644
index 00000000000..0473032bb98
--- /dev/null
+++ b/db/post_migrate/20230619123701_schedule_removal_index_job_artifacts_id_and_expire_at.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+class ScheduleRemovalIndexJobArtifactsIdAndExpireAt < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = :tmp_index_ci_job_artifacts_on_id_expire_at_file_type_trace
+ TABLE_NAME = :ci_job_artifacts
+ COLUMN = :id
+
+ # Index to be destroyed synchronously in https://gitlab.com/gitlab-org/gitlab/-/issues/368979
+ def up
+ prepare_async_index_removal(TABLE_NAME, COLUMN, name: INDEX_NAME)
+ end
+
+ def down
+ unprepare_async_index(TABLE_NAME, COLUMN, name: INDEX_NAME)
+ end
+end
diff --git a/db/post_migrate/20230619134106_prepare_index_for_vulnerability_reads_on_common_project_filters.rb b/db/post_migrate/20230619134106_prepare_index_for_vulnerability_reads_on_common_project_filters.rb
new file mode 100644
index 00000000000..60335f572d5
--- /dev/null
+++ b/db/post_migrate/20230619134106_prepare_index_for_vulnerability_reads_on_common_project_filters.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+class PrepareIndexForVulnerabilityReadsOnCommonProjectFilters < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ INDEX_NAME = 'index_project_vulnerability_reads_common_finder_query_desc'
+
+ def up
+ prepare_async_index :vulnerability_reads,
+ [:project_id, :state, :report_type, :severity, :vulnerability_id],
+ order: { severity: :desc, vulnerability_id: :desc },
+ name: INDEX_NAME
+ end
+
+ def down
+ unprepare_async_index_by_name :vulnerability_reads, INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230620072854_replace_old_fk_ci_build_pending_states_to_builds_v2.rb b/db/post_migrate/20230620072854_replace_old_fk_ci_build_pending_states_to_builds_v2.rb
new file mode 100644
index 00000000000..dd6acb8a365
--- /dev/null
+++ b/db/post_migrate/20230620072854_replace_old_fk_ci_build_pending_states_to_builds_v2.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+class ReplaceOldFkCiBuildPendingStatesToBuildsV2 < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ def up
+ return if new_foreign_key_exists?
+
+ with_lock_retries do
+ remove_foreign_key_if_exists :ci_build_pending_states, :ci_builds,
+ name: :fk_861cd17da3_p, reverse_lock_order: true
+
+ rename_constraint :ci_build_pending_states, :temp_fk_861cd17da3_p, :fk_861cd17da3_p
+ end
+ end
+
+ def down
+ return unless new_foreign_key_exists?
+
+ add_concurrent_foreign_key :ci_build_pending_states, :ci_builds,
+ name: :temp_fk_861cd17da3_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 :ci_build_pending_states, :fk_861cd17da3_p, :temp_fk_861cd17da3_p
+ end
+
+ private
+
+ def new_foreign_key_exists?
+ foreign_key_exists?(:ci_build_pending_states, :p_ci_builds, name: :fk_861cd17da3_p)
+ end
+end
diff --git a/db/post_migrate/20230620072856_replace_old_fk_ci_build_trace_chunks_to_builds_v2.rb b/db/post_migrate/20230620072856_replace_old_fk_ci_build_trace_chunks_to_builds_v2.rb
new file mode 100644
index 00000000000..d715ba11500
--- /dev/null
+++ b/db/post_migrate/20230620072856_replace_old_fk_ci_build_trace_chunks_to_builds_v2.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+class ReplaceOldFkCiBuildTraceChunksToBuildsV2 < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ def up
+ return if new_foreign_key_exists?
+
+ with_lock_retries do
+ remove_foreign_key_if_exists :ci_build_trace_chunks, :ci_builds,
+ name: :fk_89e29fa5ee_p, reverse_lock_order: true
+
+ rename_constraint :ci_build_trace_chunks, :temp_fk_89e29fa5ee_p, :fk_89e29fa5ee_p
+ end
+ end
+
+ def down
+ return unless new_foreign_key_exists?
+
+ add_concurrent_foreign_key :ci_build_trace_chunks, :ci_builds,
+ name: :temp_fk_89e29fa5ee_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 :ci_build_trace_chunks, :fk_89e29fa5ee_p, :temp_fk_89e29fa5ee_p
+ end
+
+ private
+
+ def new_foreign_key_exists?
+ foreign_key_exists?(:ci_build_trace_chunks, :p_ci_builds, name: :fk_89e29fa5ee_p)
+ end
+end
diff --git a/db/post_migrate/20230620072900_replace_old_fk_ci_unit_test_failures_to_builds_v2.rb b/db/post_migrate/20230620072900_replace_old_fk_ci_unit_test_failures_to_builds_v2.rb
new file mode 100644
index 00000000000..fe305ccd29d
--- /dev/null
+++ b/db/post_migrate/20230620072900_replace_old_fk_ci_unit_test_failures_to_builds_v2.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+class ReplaceOldFkCiUnitTestFailuresToBuildsV2 < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ def up
+ return if new_foreign_key_exists?
+
+ with_lock_retries do
+ remove_foreign_key_if_exists :ci_unit_test_failures, :ci_builds,
+ name: :fk_9e0fc58930_p, reverse_lock_order: true
+
+ rename_constraint :ci_unit_test_failures, :temp_fk_9e0fc58930_p, :fk_9e0fc58930_p
+ end
+ end
+
+ def down
+ return unless new_foreign_key_exists?
+
+ add_concurrent_foreign_key :ci_unit_test_failures, :ci_builds,
+ name: :temp_fk_9e0fc58930_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 :ci_unit_test_failures, :fk_9e0fc58930_p, :temp_fk_9e0fc58930_p
+ end
+
+ private
+
+ def new_foreign_key_exists?
+ foreign_key_exists?(:ci_unit_test_failures, :p_ci_builds, name: :fk_9e0fc58930_p)
+ end
+end
diff --git a/db/post_migrate/20230620072905_replace_old_fk_ci_sources_pipelines_to_builds_v2.rb b/db/post_migrate/20230620072905_replace_old_fk_ci_sources_pipelines_to_builds_v2.rb
new file mode 100644
index 00000000000..44a24182382
--- /dev/null
+++ b/db/post_migrate/20230620072905_replace_old_fk_ci_sources_pipelines_to_builds_v2.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+class ReplaceOldFkCiSourcesPipelinesToBuildsV2 < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ def up
+ return if new_foreign_key_exists?
+
+ with_lock_retries do
+ remove_foreign_key_if_exists :ci_sources_pipelines, :ci_builds,
+ name: :fk_be5624bf37_p, reverse_lock_order: true
+
+ rename_constraint :ci_sources_pipelines, :temp_fk_be5624bf37_p, :fk_be5624bf37_p
+ end
+ end
+
+ def down
+ return unless new_foreign_key_exists?
+
+ add_concurrent_foreign_key :ci_sources_pipelines, :ci_builds,
+ name: :temp_fk_be5624bf37_p,
+ column: [:source_partition_id, :source_job_id],
+ target_column: [:partition_id, :id],
+ on_update: :cascade,
+ on_delete: :cascade,
+ validate: true,
+ reverse_lock_order: true
+
+ switch_constraint_names :ci_sources_pipelines, :fk_be5624bf37_p, :temp_fk_be5624bf37_p
+ end
+
+ private
+
+ def new_foreign_key_exists?
+ foreign_key_exists?(:ci_sources_pipelines, :p_ci_builds, name: :fk_be5624bf37_p)
+ end
+end
diff --git a/db/post_migrate/20230620072908_replace_old_fk_ci_resources_to_builds_v2.rb b/db/post_migrate/20230620072908_replace_old_fk_ci_resources_to_builds_v2.rb
new file mode 100644
index 00000000000..ad4773f5b9d
--- /dev/null
+++ b/db/post_migrate/20230620072908_replace_old_fk_ci_resources_to_builds_v2.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+class ReplaceOldFkCiResourcesToBuildsV2 < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ def up
+ return if new_foreign_key_exists?
+
+ with_lock_retries do
+ remove_foreign_key_if_exists :ci_resources, :ci_builds,
+ name: :fk_e169a8e3d5_p, reverse_lock_order: true
+
+ rename_constraint :ci_resources, :temp_fk_e169a8e3d5_p, :fk_e169a8e3d5_p
+ end
+ end
+
+ def down
+ return unless new_foreign_key_exists?
+
+ add_concurrent_foreign_key :ci_resources, :ci_builds,
+ name: :temp_fk_e169a8e3d5_p,
+ column: [:partition_id, :build_id],
+ target_column: [:partition_id, :id],
+ on_update: :cascade,
+ on_delete: :nullify,
+ validate: true,
+ reverse_lock_order: true
+
+ switch_constraint_names :ci_resources, :fk_e169a8e3d5_p, :temp_fk_e169a8e3d5_p
+ end
+
+ private
+
+ def new_foreign_key_exists?
+ foreign_key_exists?(:ci_resources, :p_ci_builds, name: :fk_e169a8e3d5_p)
+ end
+end
diff --git a/db/post_migrate/20230620072911_replace_old_fk_ci_build_report_results_to_builds_v2.rb b/db/post_migrate/20230620072911_replace_old_fk_ci_build_report_results_to_builds_v2.rb
new file mode 100644
index 00000000000..f7d5e703e61
--- /dev/null
+++ b/db/post_migrate/20230620072911_replace_old_fk_ci_build_report_results_to_builds_v2.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+class ReplaceOldFkCiBuildReportResultsToBuildsV2 < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ def up
+ return if new_foreign_key_exists?
+
+ with_lock_retries do
+ remove_foreign_key_if_exists :ci_build_report_results, :ci_builds,
+ name: :fk_rails_16cb1ff064_p, reverse_lock_order: true
+
+ rename_constraint :ci_build_report_results, :temp_fk_rails_16cb1ff064_p, :fk_rails_16cb1ff064_p
+ end
+ end
+
+ def down
+ return unless new_foreign_key_exists?
+
+ add_concurrent_foreign_key :ci_build_report_results, :ci_builds,
+ name: :temp_fk_rails_16cb1ff064_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 :ci_build_report_results, :fk_rails_16cb1ff064_p, :temp_fk_rails_16cb1ff064_p
+ end
+
+ private
+
+ def new_foreign_key_exists?
+ foreign_key_exists?(:ci_build_report_results, :p_ci_builds, name: :fk_rails_16cb1ff064_p)
+ end
+end
diff --git a/db/post_migrate/20230620072915_replace_old_fk_ci_build_needs_to_builds_v2.rb b/db/post_migrate/20230620072915_replace_old_fk_ci_build_needs_to_builds_v2.rb
new file mode 100644
index 00000000000..a6f93eee035
--- /dev/null
+++ b/db/post_migrate/20230620072915_replace_old_fk_ci_build_needs_to_builds_v2.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+class ReplaceOldFkCiBuildNeedsToBuildsV2 < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ def up
+ return if new_foreign_key_exists?
+
+ with_lock_retries do
+ remove_foreign_key_if_exists :ci_build_needs, :ci_builds,
+ name: :fk_rails_3cf221d4ed_p, reverse_lock_order: true
+
+ rename_constraint :ci_build_needs, :temp_fk_rails_3cf221d4ed_p, :fk_rails_3cf221d4ed_p
+ end
+ end
+
+ def down
+ return unless new_foreign_key_exists?
+
+ add_concurrent_foreign_key :ci_build_needs, :ci_builds,
+ name: :temp_fk_rails_3cf221d4ed_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 :ci_build_needs, :fk_rails_3cf221d4ed_p, :temp_fk_rails_3cf221d4ed_p
+ end
+
+ private
+
+ def new_foreign_key_exists?
+ foreign_key_exists?(:ci_build_needs, :p_ci_builds, name: :fk_rails_3cf221d4ed_p)
+ end
+end
diff --git a/db/post_migrate/20230620072917_replace_old_fk_ci_builds_runner_session_to_builds_v2.rb b/db/post_migrate/20230620072917_replace_old_fk_ci_builds_runner_session_to_builds_v2.rb
new file mode 100644
index 00000000000..01b9bbee633
--- /dev/null
+++ b/db/post_migrate/20230620072917_replace_old_fk_ci_builds_runner_session_to_builds_v2.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+class ReplaceOldFkCiBuildsRunnerSessionToBuildsV2 < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ def up
+ return if new_foreign_key_exists?
+
+ with_lock_retries do
+ remove_foreign_key_if_exists :ci_builds_runner_session, :ci_builds,
+ name: :fk_rails_70707857d3_p, reverse_lock_order: true
+
+ rename_constraint :ci_builds_runner_session, :temp_fk_rails_70707857d3_p, :fk_rails_70707857d3_p
+ end
+ end
+
+ def down
+ return unless new_foreign_key_exists?
+
+ add_concurrent_foreign_key :ci_builds_runner_session, :ci_builds,
+ name: :temp_fk_rails_70707857d3_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 :ci_builds_runner_session, :fk_rails_70707857d3_p, :temp_fk_rails_70707857d3_p
+ end
+
+ private
+
+ def new_foreign_key_exists?
+ foreign_key_exists?(:ci_builds_runner_session, :p_ci_builds, name: :fk_rails_70707857d3_p)
+ end
+end
diff --git a/db/post_migrate/20230620072920_replace_old_fk_ci_pending_builds_to_builds_v2.rb b/db/post_migrate/20230620072920_replace_old_fk_ci_pending_builds_to_builds_v2.rb
new file mode 100644
index 00000000000..4861bb646f5
--- /dev/null
+++ b/db/post_migrate/20230620072920_replace_old_fk_ci_pending_builds_to_builds_v2.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+class ReplaceOldFkCiPendingBuildsToBuildsV2 < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ def up
+ return if new_foreign_key_exists?
+
+ with_lock_retries do
+ remove_foreign_key_if_exists :ci_pending_builds, :ci_builds,
+ name: :fk_rails_725a2644a3_p, reverse_lock_order: true
+
+ rename_constraint :ci_pending_builds, :temp_fk_rails_725a2644a3_p, :fk_rails_725a2644a3_p
+ end
+ end
+
+ def down
+ return unless new_foreign_key_exists?
+
+ add_concurrent_foreign_key :ci_pending_builds, :ci_builds,
+ name: :temp_fk_rails_725a2644a3_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 :ci_pending_builds, :fk_rails_725a2644a3_p, :temp_fk_rails_725a2644a3_p
+ end
+
+ private
+
+ def new_foreign_key_exists?
+ foreign_key_exists?(:ci_pending_builds, :p_ci_builds, name: :fk_rails_725a2644a3_p)
+ end
+end
diff --git a/db/post_migrate/20230620072925_replace_old_fk_ci_build_trace_metadata_to_builds_v2.rb b/db/post_migrate/20230620072925_replace_old_fk_ci_build_trace_metadata_to_builds_v2.rb
new file mode 100644
index 00000000000..be004eb5121
--- /dev/null
+++ b/db/post_migrate/20230620072925_replace_old_fk_ci_build_trace_metadata_to_builds_v2.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+class ReplaceOldFkCiBuildTraceMetadataToBuildsV2 < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ def up
+ return if new_foreign_key_exists?
+
+ with_lock_retries do
+ remove_foreign_key_if_exists :ci_build_trace_metadata, :ci_builds,
+ name: :fk_rails_aebc78111f_p, reverse_lock_order: true
+
+ rename_constraint :ci_build_trace_metadata, :temp_fk_rails_aebc78111f_p, :fk_rails_aebc78111f_p
+ end
+ end
+
+ def down
+ return unless new_foreign_key_exists?
+
+ add_concurrent_foreign_key :ci_build_trace_metadata, :ci_builds,
+ name: :temp_fk_rails_aebc78111f_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 :ci_build_trace_metadata, :fk_rails_aebc78111f_p, :temp_fk_rails_aebc78111f_p
+ end
+
+ private
+
+ def new_foreign_key_exists?
+ foreign_key_exists?(:ci_build_trace_metadata, :p_ci_builds, name: :fk_rails_aebc78111f_p)
+ end
+end
diff --git a/db/post_migrate/20230620105919_replace_old_fk_ci_job_artifacts_to_builds.rb b/db/post_migrate/20230620105919_replace_old_fk_ci_job_artifacts_to_builds.rb
new file mode 100644
index 00000000000..33aca3bc858
--- /dev/null
+++ b/db/post_migrate/20230620105919_replace_old_fk_ci_job_artifacts_to_builds.rb
@@ -0,0 +1,45 @@
+# frozen_string_literal: true
+
+class ReplaceOldFkCiJobArtifactsToBuilds < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::WraparoundAutovacuum
+
+ disable_ddl_transaction!
+
+ def up
+ return unless should_run?
+ return if new_foreign_key_exists?
+
+ with_lock_retries do
+ remove_foreign_key_if_exists :ci_job_artifacts, :ci_builds,
+ name: :fk_rails_c5137cb2c1_p, reverse_lock_order: true
+
+ rename_constraint :ci_job_artifacts, :temp_fk_rails_c5137cb2c1_p, :fk_rails_c5137cb2c1_p
+ end
+ end
+
+ def down
+ return unless should_run?
+ return unless new_foreign_key_exists?
+
+ add_concurrent_foreign_key :ci_job_artifacts, :ci_builds,
+ name: :temp_fk_rails_c5137cb2c1_p,
+ column: [:partition_id, :job_id],
+ target_column: [:partition_id, :id],
+ on_update: :cascade,
+ on_delete: :cascade,
+ validate: true,
+ reverse_lock_order: true
+
+ switch_constraint_names :ci_job_artifacts, :fk_rails_c5137cb2c1_p, :temp_fk_rails_c5137cb2c1_p
+ end
+
+ private
+
+ def should_run?
+ can_execute_on?(:ci_job_artifacts, :ci_builds)
+ end
+
+ def new_foreign_key_exists?
+ foreign_key_exists?(:ci_job_artifacts, :p_ci_builds, name: :fk_rails_c5137cb2c1_p)
+ end
+end
diff --git a/db/post_migrate/20230620110004_replace_old_fk_ci_running_builds_to_builds.rb b/db/post_migrate/20230620110004_replace_old_fk_ci_running_builds_to_builds.rb
new file mode 100644
index 00000000000..77a6e24f4fa
--- /dev/null
+++ b/db/post_migrate/20230620110004_replace_old_fk_ci_running_builds_to_builds.rb
@@ -0,0 +1,45 @@
+# frozen_string_literal: true
+
+class ReplaceOldFkCiRunningBuildsToBuilds < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::WraparoundAutovacuum
+
+ disable_ddl_transaction!
+
+ def up
+ return unless should_run?
+ return if new_foreign_key_exists?
+
+ with_lock_retries do
+ remove_foreign_key_if_exists :ci_running_builds, :ci_builds,
+ name: :fk_rails_da45cfa165_p, reverse_lock_order: true
+
+ rename_constraint :ci_running_builds, :temp_fk_rails_da45cfa165_p, :fk_rails_da45cfa165_p
+ end
+ end
+
+ def down
+ return unless should_run?
+ return unless new_foreign_key_exists?
+
+ add_concurrent_foreign_key :ci_running_builds, :ci_builds,
+ name: :temp_fk_rails_da45cfa165_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 :ci_running_builds, :fk_rails_da45cfa165_p, :temp_fk_rails_da45cfa165_p
+ end
+
+ private
+
+ def should_run?
+ can_execute_on?(:ci_running_builds, :ci_builds)
+ end
+
+ def new_foreign_key_exists?
+ foreign_key_exists?(:ci_running_builds, :p_ci_builds, name: :fk_rails_da45cfa165_p)
+ end
+end
diff --git a/db/post_migrate/20230620110021_replace_old_fk_ci_job_variables_to_builds.rb b/db/post_migrate/20230620110021_replace_old_fk_ci_job_variables_to_builds.rb
new file mode 100644
index 00000000000..9d90c03d38a
--- /dev/null
+++ b/db/post_migrate/20230620110021_replace_old_fk_ci_job_variables_to_builds.rb
@@ -0,0 +1,45 @@
+# frozen_string_literal: true
+
+class ReplaceOldFkCiJobVariablesToBuilds < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::WraparoundAutovacuum
+
+ disable_ddl_transaction!
+
+ def up
+ return unless should_run?
+ return if new_foreign_key_exists?
+
+ with_lock_retries do
+ remove_foreign_key_if_exists :ci_job_variables, :ci_builds,
+ name: :fk_rails_fbf3b34792_p, reverse_lock_order: true
+
+ rename_constraint :ci_job_variables, :temp_fk_rails_fbf3b34792_p, :fk_rails_fbf3b34792_p
+ end
+ end
+
+ def down
+ return unless should_run?
+ return unless new_foreign_key_exists?
+
+ add_concurrent_foreign_key :ci_job_variables, :ci_builds,
+ name: :temp_fk_rails_fbf3b34792_p,
+ column: [:partition_id, :job_id],
+ target_column: [:partition_id, :id],
+ on_update: :cascade,
+ on_delete: :cascade,
+ validate: true,
+ reverse_lock_order: true
+
+ switch_constraint_names :ci_job_variables, :fk_rails_fbf3b34792_p, :temp_fk_rails_fbf3b34792_p
+ end
+
+ private
+
+ def should_run?
+ can_execute_on?(:ci_job_variables, :ci_builds)
+ end
+
+ def new_foreign_key_exists?
+ foreign_key_exists?(:ci_job_variables, :p_ci_builds, name: :fk_rails_fbf3b34792_p)
+ end
+end
diff --git a/db/post_migrate/20230620134708_validate_user_type_constraint.rb b/db/post_migrate/20230620134708_validate_user_type_constraint.rb
new file mode 100644
index 00000000000..cf06e3562d5
--- /dev/null
+++ b/db/post_migrate/20230620134708_validate_user_type_constraint.rb
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+class ValidateUserTypeConstraint < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ def up
+ validate_not_null_constraint(:users, :user_type)
+ end
+
+ def down
+ # no-op
+ end
+end
diff --git a/db/post_migrate/20230620201738_add_index_issues_on_project_health_status_asc_work_item_type.rb b/db/post_migrate/20230620201738_add_index_issues_on_project_health_status_asc_work_item_type.rb
new file mode 100644
index 00000000000..a1777baa1c2
--- /dev/null
+++ b/db/post_migrate/20230620201738_add_index_issues_on_project_health_status_asc_work_item_type.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+class AddIndexIssuesOnProjectHealthStatusAscWorkItemType < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = 'index_issues_on_project_health_status_asc_work_item_type'
+
+ disable_ddl_transaction!
+
+ def up
+ add_concurrent_index :issues,
+ [:project_id, :health_status, :id, :state_id, :work_item_type_id],
+ order: { health_status: 'ASC NULLS LAST', id: :desc },
+ name: INDEX_NAME
+ end
+
+ def down
+ remove_concurrent_index_by_name :issues, INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230620201817_add_index_issues_on_project_health_status_desc_work_item_type.rb b/db/post_migrate/20230620201817_add_index_issues_on_project_health_status_desc_work_item_type.rb
new file mode 100644
index 00000000000..7eaa85bff56
--- /dev/null
+++ b/db/post_migrate/20230620201817_add_index_issues_on_project_health_status_desc_work_item_type.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+class AddIndexIssuesOnProjectHealthStatusDescWorkItemType < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = 'index_issues_on_project_health_status_desc_work_item_type'
+
+ disable_ddl_transaction!
+
+ def up
+ add_concurrent_index :issues,
+ [:project_id, :health_status, :id, :state_id, :work_item_type_id],
+ order: { health_status: 'DESC NULLS LAST', id: :desc },
+ name: INDEX_NAME
+ end
+
+ def down
+ remove_concurrent_index_by_name :issues, INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230621072848_add_text_limit_to_ci_variable_description.rb b/db/post_migrate/20230621072848_add_text_limit_to_ci_variable_description.rb
new file mode 100644
index 00000000000..485ac8bae61
--- /dev/null
+++ b/db/post_migrate/20230621072848_add_text_limit_to_ci_variable_description.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+class AddTextLimitToCiVariableDescription < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ TABLE_NAME = :ci_variables
+ COLUMN_NAME = :description
+
+ def up
+ add_text_limit(TABLE_NAME, COLUMN_NAME, 255)
+ end
+
+ def down
+ remove_text_limit(TABLE_NAME, COLUMN_NAME)
+ end
+end
diff --git a/db/post_migrate/20230621083052_add_text_limit_to_ci_group_variable_description.rb b/db/post_migrate/20230621083052_add_text_limit_to_ci_group_variable_description.rb
new file mode 100644
index 00000000000..b0a5a3d539b
--- /dev/null
+++ b/db/post_migrate/20230621083052_add_text_limit_to_ci_group_variable_description.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+class AddTextLimitToCiGroupVariableDescription < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ TABLE_NAME = :ci_group_variables
+ COLUMN_NAME = :description
+
+ def up
+ add_text_limit(TABLE_NAME, COLUMN_NAME, 255)
+ end
+
+ def down
+ remove_text_limit(TABLE_NAME, COLUMN_NAME)
+ end
+end
diff --git a/db/post_migrate/20230621102941_replace_old_fk_ci_job_artifacts_to_builds_v2.rb b/db/post_migrate/20230621102941_replace_old_fk_ci_job_artifacts_to_builds_v2.rb
new file mode 100644
index 00000000000..416b1c629c0
--- /dev/null
+++ b/db/post_migrate/20230621102941_replace_old_fk_ci_job_artifacts_to_builds_v2.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+class ReplaceOldFkCiJobArtifactsToBuildsV2 < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ def up
+ return if new_foreign_key_exists?
+
+ with_lock_retries do
+ remove_foreign_key_if_exists :ci_job_artifacts, :ci_builds,
+ name: :fk_rails_c5137cb2c1_p, reverse_lock_order: true
+
+ rename_constraint :ci_job_artifacts, :temp_fk_rails_c5137cb2c1_p, :fk_rails_c5137cb2c1_p
+ end
+ end
+
+ def down
+ return unless new_foreign_key_exists?
+
+ add_concurrent_foreign_key :ci_job_artifacts, :ci_builds,
+ name: :temp_fk_rails_c5137cb2c1_p,
+ column: [:partition_id, :job_id],
+ target_column: [:partition_id, :id],
+ on_update: :cascade,
+ on_delete: :cascade,
+ validate: true,
+ reverse_lock_order: true
+
+ switch_constraint_names :ci_job_artifacts, :fk_rails_c5137cb2c1_p, :temp_fk_rails_c5137cb2c1_p
+ end
+
+ private
+
+ def new_foreign_key_exists?
+ foreign_key_exists?(:ci_job_artifacts, :p_ci_builds, name: :fk_rails_c5137cb2c1_p)
+ end
+end
diff --git a/db/post_migrate/20230621103000_replace_old_fk_ci_running_builds_to_builds_v2.rb b/db/post_migrate/20230621103000_replace_old_fk_ci_running_builds_to_builds_v2.rb
new file mode 100644
index 00000000000..32702eceb17
--- /dev/null
+++ b/db/post_migrate/20230621103000_replace_old_fk_ci_running_builds_to_builds_v2.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+class ReplaceOldFkCiRunningBuildsToBuildsV2 < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ def up
+ return if new_foreign_key_exists?
+
+ with_lock_retries do
+ remove_foreign_key_if_exists :ci_running_builds, :ci_builds,
+ name: :fk_rails_da45cfa165_p, reverse_lock_order: true
+
+ rename_constraint :ci_running_builds, :temp_fk_rails_da45cfa165_p, :fk_rails_da45cfa165_p
+ end
+ end
+
+ def down
+ return unless new_foreign_key_exists?
+
+ add_concurrent_foreign_key :ci_running_builds, :ci_builds,
+ name: :temp_fk_rails_da45cfa165_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 :ci_running_builds, :fk_rails_da45cfa165_p, :temp_fk_rails_da45cfa165_p
+ end
+
+ private
+
+ def new_foreign_key_exists?
+ foreign_key_exists?(:ci_running_builds, :p_ci_builds, name: :fk_rails_da45cfa165_p)
+ end
+end
diff --git a/db/post_migrate/20230621103043_replace_old_fk_ci_job_variables_to_builds_v2.rb b/db/post_migrate/20230621103043_replace_old_fk_ci_job_variables_to_builds_v2.rb
new file mode 100644
index 00000000000..68ae3dc56af
--- /dev/null
+++ b/db/post_migrate/20230621103043_replace_old_fk_ci_job_variables_to_builds_v2.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+class ReplaceOldFkCiJobVariablesToBuildsV2 < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ def up
+ return if new_foreign_key_exists?
+
+ with_lock_retries do
+ remove_foreign_key_if_exists :ci_job_variables, :ci_builds,
+ name: :fk_rails_fbf3b34792_p, reverse_lock_order: true
+
+ rename_constraint :ci_job_variables, :temp_fk_rails_fbf3b34792_p, :fk_rails_fbf3b34792_p
+ end
+ end
+
+ def down
+ return unless new_foreign_key_exists?
+
+ add_concurrent_foreign_key :ci_job_variables, :ci_builds,
+ name: :temp_fk_rails_fbf3b34792_p,
+ column: [:partition_id, :job_id],
+ target_column: [:partition_id, :id],
+ on_update: :cascade,
+ on_delete: :cascade,
+ validate: true,
+ reverse_lock_order: true
+
+ switch_constraint_names :ci_job_variables, :fk_rails_fbf3b34792_p, :temp_fk_rails_fbf3b34792_p
+ end
+
+ private
+
+ def new_foreign_key_exists?
+ foreign_key_exists?(:ci_job_variables, :p_ci_builds, name: :fk_rails_fbf3b34792_p)
+ end
+end
diff --git a/db/post_migrate/20230622052046_remove_ci_pipeline_variables_trigger_and_old_column.rb b/db/post_migrate/20230622052046_remove_ci_pipeline_variables_trigger_and_old_column.rb
new file mode 100644
index 00000000000..81ce42c3aab
--- /dev/null
+++ b/db/post_migrate/20230622052046_remove_ci_pipeline_variables_trigger_and_old_column.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+class RemoveCiPipelineVariablesTriggerAndOldColumn < Gitlab::Database::Migration[2.1]
+ enable_lock_retries!
+
+ TABLE = :ci_pipeline_variables
+ COLUMNS = [:id]
+
+ def up
+ cleanup_conversion_of_integer_to_bigint(TABLE, COLUMNS)
+ end
+
+ def down
+ restore_conversion_of_integer_to_bigint(TABLE, COLUMNS)
+ end
+end
diff --git a/db/post_migrate/20230622073752_ensure_backfill_ci_pipelines_bigint_id_is_completed.rb b/db/post_migrate/20230622073752_ensure_backfill_ci_pipelines_bigint_id_is_completed.rb
new file mode 100644
index 00000000000..56aeafc0a66
--- /dev/null
+++ b/db/post_migrate/20230622073752_ensure_backfill_ci_pipelines_bigint_id_is_completed.rb
@@ -0,0 +1,23 @@
+# frozen_string_literal: true
+
+class EnsureBackfillCiPipelinesBigintIdIsCompleted < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ restrict_gitlab_migration gitlab_schema: :gitlab_ci
+ disable_ddl_transaction!
+
+ TABLE_NAME = :ci_pipelines
+
+ def up
+ ensure_batched_background_migration_is_finished(
+ job_class_name: 'CopyColumnUsingBackgroundMigrationJob',
+ table_name: TABLE_NAME,
+ column_name: 'id',
+ job_arguments: [['id'], ['id_convert_to_bigint']]
+ )
+ end
+
+ def down
+ # no-op
+ end
+end
diff --git a/db/post_migrate/20230623073906_prepare_async_index_for_ci_pipelines_bigint_id.rb b/db/post_migrate/20230623073906_prepare_async_index_for_ci_pipelines_bigint_id.rb
new file mode 100644
index 00000000000..57d4e37ca7c
--- /dev/null
+++ b/db/post_migrate/20230623073906_prepare_async_index_for_ci_pipelines_bigint_id.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+class PrepareAsyncIndexForCiPipelinesBigintId < Gitlab::Database::Migration[2.1]
+ TABLE_NAME = :ci_pipelines
+ INDEX_NAME = "index_#{TABLE_NAME}_on_id_convert_to_bigint"
+
+ # TODO: Index to be created synchronously in https://gitlab.com/gitlab-org/gitlab/-/issues/397000
+ def up
+ prepare_async_index TABLE_NAME, :id_convert_to_bigint, unique: true, name: INDEX_NAME
+ end
+
+ def down
+ unprepare_async_index TABLE_NAME, :id_convert_to_bigint, unique: true, name: INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230626070723_drop_unused_sent_notification_columns.rb b/db/post_migrate/20230626070723_drop_unused_sent_notification_columns.rb
new file mode 100644
index 00000000000..82a125c1ff5
--- /dev/null
+++ b/db/post_migrate/20230626070723_drop_unused_sent_notification_columns.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class DropUnusedSentNotificationColumns < Gitlab::Database::Migration[2.1]
+ enable_lock_retries!
+
+ def up
+ remove_column :sent_notifications, :line_code
+ remove_column :sent_notifications, :note_type
+ remove_column :sent_notifications, :position
+ end
+
+ def down
+ add_column :sent_notifications, :line_code, :string
+ add_column :sent_notifications, :note_type, :string
+ add_column :sent_notifications, :position, :text
+ end
+end
diff --git a/db/post_migrate/20230626072436_drop_tmp_index_job_artifacts_id_and_expire_at.rb b/db/post_migrate/20230626072436_drop_tmp_index_job_artifacts_id_and_expire_at.rb
new file mode 100644
index 00000000000..3b986a0dc5d
--- /dev/null
+++ b/db/post_migrate/20230626072436_drop_tmp_index_job_artifacts_id_and_expire_at.rb
@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+
+class DropTmpIndexJobArtifactsIdAndExpireAt < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ TABLE_NAME = :ci_job_artifacts
+ INDEX_NAME = :tmp_index_ci_job_artifacts_on_id_expire_at_file_type_trace
+
+ EXPIRE_AT_ON_22_MIDNIGHT_IN_TIMEZONE_OR_TRACE = <<~SQL
+ (EXTRACT(day FROM timezone('UTC', expire_at)) IN (21, 22, 23)
+ AND EXTRACT(minute FROM timezone('UTC', expire_at)) IN (0, 30, 45)
+ AND EXTRACT(second FROM timezone('UTC', expire_at)) = 0)
+ OR file_type = 3
+ SQL
+
+ def up
+ remove_concurrent_index_by_name(TABLE_NAME, INDEX_NAME)
+ end
+
+ def down
+ add_concurrent_index(
+ TABLE_NAME,
+ :id,
+ where: EXPIRE_AT_ON_22_MIDNIGHT_IN_TIMEZONE_OR_TRACE,
+ name: INDEX_NAME
+ )
+ end
+end
diff --git a/db/post_migrate/20230626101519_create_index_for_vulnerability_reads_on_common_project_filters.rb b/db/post_migrate/20230626101519_create_index_for_vulnerability_reads_on_common_project_filters.rb
new file mode 100644
index 00000000000..209a2295f22
--- /dev/null
+++ b/db/post_migrate/20230626101519_create_index_for_vulnerability_reads_on_common_project_filters.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+class CreateIndexForVulnerabilityReadsOnCommonProjectFilters < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ INDEX_NAME = 'index_project_vulnerability_reads_common_finder_query_desc'
+
+ def up
+ add_concurrent_index :vulnerability_reads,
+ [:project_id, :state, :report_type, :severity, :vulnerability_id],
+ order: { severity: :desc, vulnerability_id: :desc },
+ name: INDEX_NAME
+ end
+
+ def down
+ remove_concurrent_index_by_name :vulnerability_reads, INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230626115046_add_temporary_index_on_vulnerability_reads.rb b/db/post_migrate/20230626115046_add_temporary_index_on_vulnerability_reads.rb
new file mode 100644
index 00000000000..8aa5cff2b96
--- /dev/null
+++ b/db/post_migrate/20230626115046_add_temporary_index_on_vulnerability_reads.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+class AddTemporaryIndexOnVulnerabilityReads < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ INDEX_NAME = "tmp_idx_vuln_reads_where_dismissal_reason_null"
+ DISMISSED_STATE = 2
+
+ def up
+ add_concurrent_index(
+ :vulnerability_reads,
+ %i[id],
+ where: "state = #{DISMISSED_STATE} AND dismissal_reason IS NULL",
+ name: INDEX_NAME
+ )
+ end
+
+ def down
+ remove_concurrent_index_by_name(
+ :vulnerability_reads,
+ INDEX_NAME
+ )
+ end
+end
diff --git a/db/post_migrate/20230627115305_replace_p_ci_builds_metadata_foreign_key_v4.rb b/db/post_migrate/20230627115305_replace_p_ci_builds_metadata_foreign_key_v4.rb
new file mode 100644
index 00000000000..cb5a5c96b3c
--- /dev/null
+++ b/db/post_migrate/20230627115305_replace_p_ci_builds_metadata_foreign_key_v4.rb
@@ -0,0 +1,40 @@
+# frozen_string_literal: true
+
+class ReplacePCiBuildsMetadataForeignKeyV4 < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::WraparoundAutovacuum
+
+ disable_ddl_transaction!
+
+ def up
+ return unless should_run?
+ return if foreign_key_exists?(:p_ci_builds_metadata, :p_ci_builds, name: :temp_fk_e20479742e_p)
+
+ with_lock_retries do
+ execute(<<~SQL.squish)
+ LOCK TABLE ci_builds, p_ci_builds, p_ci_builds_metadata IN ACCESS EXCLUSIVE MODE;
+
+ ALTER TABLE p_ci_builds_metadata
+ ADD CONSTRAINT temp_fk_e20479742e_p
+ FOREIGN KEY (partition_id, build_id)
+ REFERENCES p_ci_builds (partition_id, id)
+ ON UPDATE CASCADE ON DELETE CASCADE;
+ SQL
+ end
+ end
+
+ def down
+ return unless should_run?
+
+ with_lock_retries do
+ remove_foreign_key_if_exists :p_ci_builds_metadata, :p_ci_builds,
+ name: :temp_fk_e20479742e_p,
+ reverse_lock_order: true
+ end
+ end
+
+ private
+
+ def should_run?
+ can_execute_on?(:ci_builds_metadata, :ci_builds)
+ end
+end
diff --git a/db/post_migrate/20230627140707_async_index_for_vulnerabilities_uuid_type_migration.rb b/db/post_migrate/20230627140707_async_index_for_vulnerabilities_uuid_type_migration.rb
new file mode 100644
index 00000000000..cfca60eb02c
--- /dev/null
+++ b/db/post_migrate/20230627140707_async_index_for_vulnerabilities_uuid_type_migration.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+class AsyncIndexForVulnerabilitiesUuidTypeMigration < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ INDEX_NAME = "tmp_idx_vulns_on_converted_uuid"
+ WHERE_CLAUSE = "uuid_convert_string_to_uuid = '00000000-0000-0000-0000-000000000000'::uuid"
+
+ def up
+ prepare_async_index(
+ :vulnerability_occurrences,
+ %i[id uuid_convert_string_to_uuid],
+ name: INDEX_NAME,
+ where: WHERE_CLAUSE
+ )
+ end
+
+ def down
+ unprepare_async_index(
+ :vulnerability_occurrences,
+ %i[id uuid_convert_string_to_uuid],
+ name: INDEX_NAME
+ )
+ end
+end
diff --git a/db/post_migrate/20230627174139_add_index_to_pool_repositories.rb b/db/post_migrate/20230627174139_add_index_to_pool_repositories.rb
new file mode 100644
index 00000000000..bb0ea0609da
--- /dev/null
+++ b/db/post_migrate/20230627174139_add_index_to_pool_repositories.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+class AddIndexToPoolRepositories < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ TABLE_NAME = :pool_repositories
+ OLD_INDEX_NAME = :index_pool_repositories_on_disk_path
+ NEW_INDEX_NAME = :unique_pool_repositories_on_disk_path_and_shard_id
+
+ def up
+ add_concurrent_index(TABLE_NAME, [:disk_path, :shard_id], name: NEW_INDEX_NAME, unique: true)
+
+ remove_concurrent_index_by_name(TABLE_NAME, OLD_INDEX_NAME)
+ end
+
+ def down
+ add_concurrent_index(TABLE_NAME, [:disk_path], name: OLD_INDEX_NAME, unique: true)
+
+ remove_concurrent_index_by_name(TABLE_NAME, NEW_INDEX_NAME)
+ end
+end
diff --git a/db/post_migrate/20230628023103_queue_backfill_missing_ci_cd_settings.rb b/db/post_migrate/20230628023103_queue_backfill_missing_ci_cd_settings.rb
new file mode 100644
index 00000000000..0fc39e96e18
--- /dev/null
+++ b/db/post_migrate/20230628023103_queue_backfill_missing_ci_cd_settings.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+class QueueBackfillMissingCiCdSettings < Gitlab::Database::Migration[2.1]
+ MIGRATION = "BackfillMissingCiCdSettings"
+ DELAY_INTERVAL = 2.minutes
+ BATCH_SIZE = 10_000
+ SUB_BATCH_SIZE = 500
+
+ restrict_gitlab_migration gitlab_schema: :gitlab_main
+
+ def up
+ queue_batched_background_migration(
+ MIGRATION,
+ :projects,
+ :id,
+ job_interval: DELAY_INTERVAL,
+ batch_size: BATCH_SIZE,
+ sub_batch_size: SUB_BATCH_SIZE
+ )
+ end
+
+ def down
+ delete_batched_background_migration(MIGRATION, :projects, :id, [])
+ end
+end
diff --git a/db/post_migrate/20230629011859_cleanup_bigint_conversion_for_notes_for_gitlab_com.rb b/db/post_migrate/20230629011859_cleanup_bigint_conversion_for_notes_for_gitlab_com.rb
new file mode 100644
index 00000000000..5b99f4e4778
--- /dev/null
+++ b/db/post_migrate/20230629011859_cleanup_bigint_conversion_for_notes_for_gitlab_com.rb
@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+
+class CleanupBigintConversionForNotesForGitlabCom < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ enable_lock_retries!
+
+ TABLE = :notes
+ COLUMNS = [:id]
+
+ def up
+ return unless should_run?
+
+ cleanup_conversion_of_integer_to_bigint(TABLE, COLUMNS)
+ end
+
+ def down
+ return unless should_run?
+
+ 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/20230629024032_ensure_events_bigint_backfill_is_finished_for_gitlab_dot_com.rb b/db/post_migrate/20230629024032_ensure_events_bigint_backfill_is_finished_for_gitlab_dot_com.rb
new file mode 100644
index 00000000000..236b785c163
--- /dev/null
+++ b/db/post_migrate/20230629024032_ensure_events_bigint_backfill_is_finished_for_gitlab_dot_com.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+class EnsureEventsBigintBackfillIsFinishedForGitlabDotCom < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ restrict_gitlab_migration gitlab_schema: :gitlab_main
+ disable_ddl_transaction!
+
+ def up
+ return unless should_run?
+
+ ensure_batched_background_migration_is_finished(
+ job_class_name: 'CopyColumnUsingBackgroundMigrationJob',
+ table_name: 'events',
+ column_name: 'id',
+ job_arguments: [['target_id'], ['target_id_convert_to_bigint']]
+ )
+ end
+
+ def down
+ # no-op
+ end
+
+ private
+
+ def should_run?
+ com_or_dev_or_test_but_not_jh?
+ end
+end
diff --git a/db/post_migrate/20230629024403_events_bigint_create_indexe_async_for_gitlab_dot_com.rb b/db/post_migrate/20230629024403_events_bigint_create_indexe_async_for_gitlab_dot_com.rb
new file mode 100644
index 00000000000..13691034fd7
--- /dev/null
+++ b/db/post_migrate/20230629024403_events_bigint_create_indexe_async_for_gitlab_dot_com.rb
@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+
+class EventsBigintCreateIndexeAsyncForGitlabDotCom < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::ConvertToBigint
+
+ def up
+ return unless should_run?
+
+ prepare_async_index :events,
+ [:target_type, :target_id_convert_to_bigint, :fingerprint],
+ name: :index_events_on_target_type_and_target_id_bigint_fingerprint,
+ unique: true
+ end
+
+ def down
+ return unless should_run?
+
+ unprepare_async_index :events,
+ [:target_type, :target_id_convert_to_bigint, :fingerprint],
+ name: :index_events_on_target_type_and_target_id_bigint_fingerprint
+ end
+
+ private
+
+ def should_run?
+ com_or_dev_or_test_but_not_jh?
+ end
+end
diff --git a/db/post_migrate/20230629095819_queue_backfill_uuid_conversion_column_in_vulnerability_occurrences.rb b/db/post_migrate/20230629095819_queue_backfill_uuid_conversion_column_in_vulnerability_occurrences.rb
new file mode 100644
index 00000000000..0ea7e2da38c
--- /dev/null
+++ b/db/post_migrate/20230629095819_queue_backfill_uuid_conversion_column_in_vulnerability_occurrences.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+class QueueBackfillUuidConversionColumnInVulnerabilityOccurrences < Gitlab::Database::Migration[2.1]
+ MIGRATION = "BackfillUuidConversionColumnInVulnerabilityOccurrences"
+ DELAY_INTERVAL = 2.minutes
+ BATCH_SIZE = 10_000
+ SUB_BATCH_SIZE = 1000
+
+ restrict_gitlab_migration gitlab_schema: :gitlab_main
+
+ def up
+ queue_batched_background_migration(
+ MIGRATION,
+ :vulnerability_occurrences,
+ :id,
+ job_interval: DELAY_INTERVAL,
+ batch_size: BATCH_SIZE,
+ sub_batch_size: SUB_BATCH_SIZE
+ )
+ end
+
+ def down
+ delete_batched_background_migration(MIGRATION, :vulnerability_occurrences, :id, [])
+ end
+end
diff --git a/db/post_migrate/20230703024031_cleanup_project_pipeline_status_key.rb b/db/post_migrate/20230703024031_cleanup_project_pipeline_status_key.rb
new file mode 100644
index 00000000000..cb7fc04166f
--- /dev/null
+++ b/db/post_migrate/20230703024031_cleanup_project_pipeline_status_key.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+class CleanupProjectPipelineStatusKey < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ MIGRATION_WORKER_CLASS = 'BackfillProjectPipelineStatusTtl'
+
+ def up
+ queue_redis_migration_job(MIGRATION_WORKER_CLASS)
+ end
+
+ def down
+ # no-op
+ end
+end
diff --git a/db/post_migrate/20230703122803_create_vulnerability_uuid_type_migration_index.rb b/db/post_migrate/20230703122803_create_vulnerability_uuid_type_migration_index.rb
new file mode 100644
index 00000000000..f5ac2c2d0b9
--- /dev/null
+++ b/db/post_migrate/20230703122803_create_vulnerability_uuid_type_migration_index.rb
@@ -0,0 +1,11 @@
+# frozen_string_literal: true
+
+class CreateVulnerabilityUuidTypeMigrationIndex < Gitlab::Database::Migration[2.1]
+ def up
+ # no-op due to https://gitlab.com/gitlab-com/gl-infra/production/-/issues/15983
+ end
+
+ def down
+ # no-op due to https://gitlab.com/gitlab-com/gl-infra/production/-/issues/15983
+ end
+end
diff --git a/db/post_migrate/20230704042302_prepare_removal_index_deployments_on_project_id_sha.rb b/db/post_migrate/20230704042302_prepare_removal_index_deployments_on_project_id_sha.rb
new file mode 100644
index 00000000000..77ddd909215
--- /dev/null
+++ b/db/post_migrate/20230704042302_prepare_removal_index_deployments_on_project_id_sha.rb
@@ -0,0 +1,17 @@
+# 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 PrepareRemovalIndexDeploymentsOnProjectIdSha < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = 'index_deployments_on_project_id_sha'
+
+ # TODO: Index to be destroyed synchronously in https://gitlab.com/gitlab-org/gitlab/-/issues/402512
+ def up
+ prepare_async_index_removal :deployments, %i[project_id sha], name: INDEX_NAME
+ end
+
+ def down
+ unprepare_async_index :deployments, %i[project_id sha], name: INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230704044541_prepare_removal_index_deployments_on_project_id_and_status.rb b/db/post_migrate/20230704044541_prepare_removal_index_deployments_on_project_id_and_status.rb
new file mode 100644
index 00000000000..0c9a292eb8c
--- /dev/null
+++ b/db/post_migrate/20230704044541_prepare_removal_index_deployments_on_project_id_and_status.rb
@@ -0,0 +1,16 @@
+# 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 PrepareRemovalIndexDeploymentsOnProjectIdAndStatus < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = 'index_deployments_on_project_id_and_status'
+
+ def up
+ prepare_async_index_removal :deployments, %i[project_id status], name: INDEX_NAME
+ end
+
+ def down
+ unprepare_async_index :deployments, %i[project_id status], name: INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230704050739_prepare_removal_index_deployments_on_environment_id_and_iid_and_project_id.rb b/db/post_migrate/20230704050739_prepare_removal_index_deployments_on_environment_id_and_iid_and_project_id.rb
new file mode 100644
index 00000000000..6171b58ac53
--- /dev/null
+++ b/db/post_migrate/20230704050739_prepare_removal_index_deployments_on_environment_id_and_iid_and_project_id.rb
@@ -0,0 +1,16 @@
+# 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 PrepareRemovalIndexDeploymentsOnEnvironmentIdAndIidAndProjectId < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = 'index_deployments_on_environment_id_and_iid_and_project_id'
+
+ def up
+ prepare_async_index_removal :deployments, %i[environment_id iid project_id], name: INDEX_NAME
+ end
+
+ def down
+ unprepare_async_index :deployments, %i[environment_id iid project_id], name: INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230704062132_replace_p_ci_builds_metadata_foreign_key_v5.rb b/db/post_migrate/20230704062132_replace_p_ci_builds_metadata_foreign_key_v5.rb
new file mode 100644
index 00000000000..c535c54b022
--- /dev/null
+++ b/db/post_migrate/20230704062132_replace_p_ci_builds_metadata_foreign_key_v5.rb
@@ -0,0 +1,26 @@
+# frozen_string_literal: true
+
+class ReplacePCiBuildsMetadataForeignKeyV5 < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::PartitioningMigrationHelpers
+
+ disable_ddl_transaction!
+
+ def up
+ add_concurrent_partitioned_foreign_key :p_ci_builds_metadata, :p_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
+ end
+
+ def down
+ with_lock_retries do
+ remove_foreign_key_if_exists :p_ci_builds_metadata, :p_ci_builds,
+ name: :temp_fk_e20479742e_p,
+ reverse_lock_order: true
+ end
+ end
+end
diff --git a/db/post_migrate/20230704062136_replace_p_ci_runner_machine_builds_foreign_key_v4.rb b/db/post_migrate/20230704062136_replace_p_ci_runner_machine_builds_foreign_key_v4.rb
new file mode 100644
index 00000000000..6cb8440bee8
--- /dev/null
+++ b/db/post_migrate/20230704062136_replace_p_ci_runner_machine_builds_foreign_key_v4.rb
@@ -0,0 +1,26 @@
+# frozen_string_literal: true
+
+class ReplacePCiRunnerMachineBuildsForeignKeyV4 < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::PartitioningMigrationHelpers
+
+ disable_ddl_transaction!
+
+ def up
+ add_concurrent_partitioned_foreign_key :p_ci_runner_machine_builds, :p_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
+ end
+
+ def down
+ with_lock_retries do
+ remove_foreign_key_if_exists :p_ci_runner_machine_builds, :p_ci_builds,
+ name: :temp_fk_bb490f12fe_p,
+ reverse_lock_order: true
+ end
+ end
+end
diff --git a/db/post_migrate/20230704074120_replace_old_fk_p_ci_builds_metadata_to_builds.rb b/db/post_migrate/20230704074120_replace_old_fk_p_ci_builds_metadata_to_builds.rb
new file mode 100644
index 00000000000..49f2066d7bf
--- /dev/null
+++ b/db/post_migrate/20230704074120_replace_old_fk_p_ci_builds_metadata_to_builds.rb
@@ -0,0 +1,54 @@
+# frozen_string_literal: true
+
+class ReplaceOldFkPCiBuildsMetadataToBuilds < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::PartitioningMigrationHelpers
+ include Gitlab::Database::MigrationHelpers::WraparoundAutovacuum
+
+ disable_ddl_transaction!
+
+ def up
+ return unless should_run?
+ 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 should_run?
+ 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 should_run?
+ can_execute_on?(:ci_builds_metadata, :ci_builds)
+ end
+
+ 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/20230704074124_replace_old_fk_p_ci_runner_machine_builds_to_builds.rb b/db/post_migrate/20230704074124_replace_old_fk_p_ci_runner_machine_builds_to_builds.rb
new file mode 100644
index 00000000000..b1d58058cef
--- /dev/null
+++ b/db/post_migrate/20230704074124_replace_old_fk_p_ci_runner_machine_builds_to_builds.rb
@@ -0,0 +1,54 @@
+# frozen_string_literal: true
+
+class ReplaceOldFkPCiRunnerMachineBuildsToBuilds < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::PartitioningMigrationHelpers
+ include Gitlab::Database::MigrationHelpers::WraparoundAutovacuum
+
+ disable_ddl_transaction!
+
+ def up
+ return unless should_run?
+ 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 should_run?
+ 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 should_run?
+ can_execute_on?(:ci_builds)
+ end
+
+ 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/20230705115215_remove_user_details_provisioned_by_group_at_column.rb b/db/post_migrate/20230705115215_remove_user_details_provisioned_by_group_at_column.rb
new file mode 100644
index 00000000000..33844f6714e
--- /dev/null
+++ b/db/post_migrate/20230705115215_remove_user_details_provisioned_by_group_at_column.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+class RemoveUserDetailsProvisionedByGroupAtColumn < Gitlab::Database::Migration[2.1]
+ enable_lock_retries!
+
+ def change
+ remove_column :user_details, :provisioned_by_group_at, :datetime_with_timezone
+ end
+end
diff --git a/db/post_migrate/20230705141703_rollback_vulnerability_advisories_foreign_key_on_vulnerable_component_versions.rb b/db/post_migrate/20230705141703_rollback_vulnerability_advisories_foreign_key_on_vulnerable_component_versions.rb
new file mode 100644
index 00000000000..92feca76511
--- /dev/null
+++ b/db/post_migrate/20230705141703_rollback_vulnerability_advisories_foreign_key_on_vulnerable_component_versions.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class RollbackVulnerabilityAdvisoriesForeignKeyOnVulnerableComponentVersions < Gitlab::Database::Migration[2.1]
+ SOURCE_TABLE = :sbom_vulnerable_component_versions
+ TARGET_TABLE = :vulnerability_advisories
+ COLUMN = :vulnerability_advisory_id
+
+ disable_ddl_transaction!
+
+ def up
+ # Foreign key is removed when the table is dropped in the next migration.
+ end
+
+ def down
+ add_concurrent_foreign_key SOURCE_TABLE, TARGET_TABLE, column: COLUMN, on_delete: :cascade
+ end
+end
diff --git a/db/post_migrate/20230705141733_rollback_component_version_foreign_key_on_vulnerable_component_versions.rb b/db/post_migrate/20230705141733_rollback_component_version_foreign_key_on_vulnerable_component_versions.rb
new file mode 100644
index 00000000000..c54d4ebd1e3
--- /dev/null
+++ b/db/post_migrate/20230705141733_rollback_component_version_foreign_key_on_vulnerable_component_versions.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class RollbackComponentVersionForeignKeyOnVulnerableComponentVersions < Gitlab::Database::Migration[2.1]
+ SOURCE_TABLE = :sbom_vulnerable_component_versions
+ TARGET_TABLE = :sbom_component_versions
+ COLUMN = :sbom_component_version_id
+
+ disable_ddl_transaction!
+
+ def up
+ # Foreign key is removed when the table is dropped in the next migration.
+ end
+
+ def down
+ add_concurrent_foreign_key SOURCE_TABLE, TARGET_TABLE, column: COLUMN, on_delete: :cascade
+ end
+end
diff --git a/db/post_migrate/20230705142241_drop_vulnerable_component_versions.rb b/db/post_migrate/20230705142241_drop_vulnerable_component_versions.rb
new file mode 100644
index 00000000000..10432f6b515
--- /dev/null
+++ b/db/post_migrate/20230705142241_drop_vulnerable_component_versions.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+class DropVulnerableComponentVersions < Gitlab::Database::Migration[2.1]
+ ADVISORY_INDEX_NAME = "index_vulnerable_component_versions_on_vulnerability_advisory"
+ SBOM_COMPONENT_INDEX_NAME = "index_vulnerable_component_versions_on_sbom_component_version"
+
+ def up
+ drop_table :sbom_vulnerable_component_versions
+ end
+
+ def down
+ create_table :sbom_vulnerable_component_versions do |t|
+ t.references :vulnerability_advisory,
+ index: { name: ADVISORY_INDEX_NAME }
+
+ t.references :sbom_component_version,
+ index: { name: SBOM_COMPONENT_INDEX_NAME }
+
+ t.timestamps_with_timezone null: false
+ end
+ end
+end
diff --git a/db/post_migrate/20230705142334_drop_vulnerabilities_advisories.rb b/db/post_migrate/20230705142334_drop_vulnerabilities_advisories.rb
new file mode 100644
index 00000000000..e6bee52eb0c
--- /dev/null
+++ b/db/post_migrate/20230705142334_drop_vulnerabilities_advisories.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+class DropVulnerabilitiesAdvisories < Gitlab::Database::Migration[2.1]
+ def up
+ drop_table :vulnerability_advisories
+ end
+
+ def down
+ create_table :vulnerability_advisories, id: false do |t|
+ t.uuid :uuid, null: false
+ t.timestamps_with_timezone null: false
+ t.primary_key :id
+ t.date :created_date, null: false
+ t.date :published_date, null: false
+ t.text :description, limit: 2048
+ t.text :title, limit: 2048
+ t.text :component_name, limit: 2048
+ t.text :solution, limit: 2048
+ t.text :not_impacted, limit: 2048
+ t.text :cvss_v2, limit: 128
+ t.text :cvss_v3, limit: 128
+ t.text :affected_range, limit: 32
+ t.text :identifiers, array: true, default: []
+ t.text :fixed_versions, array: true, default: []
+ t.text :urls, array: true, default: []
+ t.text :links, array: true, default: []
+ end
+ end
+end
diff --git a/db/post_migrate/20230705145827_drop_wrong_index_on_vulnerability_occurrences.rb b/db/post_migrate/20230705145827_drop_wrong_index_on_vulnerability_occurrences.rb
new file mode 100644
index 00000000000..6b4089f72fe
--- /dev/null
+++ b/db/post_migrate/20230705145827_drop_wrong_index_on_vulnerability_occurrences.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+class DropWrongIndexOnVulnerabilityOccurrences < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ INDEX_NAME = 'tmp_idx_vulns_on_converted_uuid'
+
+ def up
+ # We do not want to drop this from Gitlab.com
+ # because it was created correctly there
+ return if Gitlab.com? && !Gitlab.dev_or_test_env?
+
+ remove_concurrent_index_by_name(
+ :vulnerability_occurrences,
+ INDEX_NAME
+ )
+ end
+
+ def down
+ # no-op
+ end
+end
diff --git a/db/post_migrate/20230705150100_recreate_type_migration_index_on_vulnerability_occurrences.rb b/db/post_migrate/20230705150100_recreate_type_migration_index_on_vulnerability_occurrences.rb
new file mode 100644
index 00000000000..9e426c61874
--- /dev/null
+++ b/db/post_migrate/20230705150100_recreate_type_migration_index_on_vulnerability_occurrences.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+class RecreateTypeMigrationIndexOnVulnerabilityOccurrences < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ INDEX_NAME = 'tmp_idx_vulns_on_converted_uuid'
+ WHERE_CLAUSE = "uuid_convert_string_to_uuid = '00000000-0000-0000-0000-000000000000'::uuid"
+
+ def up
+ add_concurrent_index(
+ :vulnerability_occurrences,
+ %i[id uuid_convert_string_to_uuid],
+ name: INDEX_NAME,
+ where: WHERE_CLAUSE
+ )
+ end
+
+ def down
+ remove_concurrent_index_by_name(
+ :vulnerability_occurrences,
+ INDEX_NAME
+ )
+ end
+end
diff --git a/db/post_migrate/20230706011541_add_user_id_foreign_key_to_ml_experiments.rb b/db/post_migrate/20230706011541_add_user_id_foreign_key_to_ml_experiments.rb
new file mode 100644
index 00000000000..77059d89d30
--- /dev/null
+++ b/db/post_migrate/20230706011541_add_user_id_foreign_key_to_ml_experiments.rb
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+class AddUserIdForeignKeyToMlExperiments < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ NEW_FK_NAME = 'fk_ml_experiments_on_user_id'
+ OLD_FK_NAME = 'fk_rails_1fbc5e001f'
+
+ def up
+ add_concurrent_foreign_key(:ml_experiments, :users, column: :user_id, on_delete: :nullify,
+ name: NEW_FK_NAME, validate: true)
+
+ with_lock_retries do
+ remove_foreign_key_if_exists(:ml_experiments, name: OLD_FK_NAME)
+ end
+ end
+
+ def down
+ unless foreign_key_exists?(:ml_experiments, :users, name: OLD_FK_NAME)
+ with_lock_retries do
+ execute(<<~SQL.squish)
+ ALTER TABLE ml_experiments ADD CONSTRAINT #{OLD_FK_NAME} FOREIGN KEY (user_id) REFERENCES users (id)
+ SQL
+ end
+ end
+
+ with_lock_retries do
+ remove_foreign_key_if_exists(:ml_experiments, name: NEW_FK_NAME)
+ end
+ end
+end
diff --git a/db/post_migrate/20230706073937_remove_index_events_on_project_id_and_id_desc_on_merged_action.rb b/db/post_migrate/20230706073937_remove_index_events_on_project_id_and_id_desc_on_merged_action.rb
new file mode 100644
index 00000000000..4d7cdc000d5
--- /dev/null
+++ b/db/post_migrate/20230706073937_remove_index_events_on_project_id_and_id_desc_on_merged_action.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class RemoveIndexEventsOnProjectIdAndIdDescOnMergedAction < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = 'index_events_on_project_id_and_id_desc_on_merged_action'
+
+ disable_ddl_transaction!
+
+ def up
+ remove_concurrent_index(:events, [:project_id, :id], order: { id: :desc },
+ where: "action = 7", name: INDEX_NAME)
+ end
+
+ def down
+ add_concurrent_index(:events, [:project_id, :id], order: { id: :desc },
+ where: "action = 7", name: INDEX_NAME)
+ end
+end
diff --git a/db/post_migrate/20230706080234_add_trigger_on_organizations.rb b/db/post_migrate/20230706080234_add_trigger_on_organizations.rb
new file mode 100644
index 00000000000..e21a5e0eb23
--- /dev/null
+++ b/db/post_migrate/20230706080234_add_trigger_on_organizations.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+class AddTriggerOnOrganizations < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::SchemaHelpers
+
+ TABLE_NAME = 'organizations'
+ FUNCTION_NAME = 'prevent_delete_of_default_organization'
+ TRIGGER_NAME = 'prevent_delete_of_default_organization_before_destroy'
+
+ def up
+ default_org_id = Organizations::Organization::DEFAULT_ORGANIZATION_ID
+
+ create_trigger_function(FUNCTION_NAME) do
+ <<~SQL
+ IF OLD.id = #{default_org_id} THEN
+ RAISE EXCEPTION 'Deletion of the default Organization is not allowed.';
+ END IF;
+ RETURN OLD;
+ SQL
+ end
+
+ create_trigger(TABLE_NAME, TRIGGER_NAME, FUNCTION_NAME, fires: 'BEFORE DELETE')
+ end
+
+ def down
+ drop_trigger(TABLE_NAME, TRIGGER_NAME)
+ drop_function(FUNCTION_NAME)
+ end
+end
diff --git a/db/post_migrate/20230707114012_change_project_view_default.rb b/db/post_migrate/20230707114012_change_project_view_default.rb
new file mode 100644
index 00000000000..6437a650261
--- /dev/null
+++ b/db/post_migrate/20230707114012_change_project_view_default.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+class ChangeProjectViewDefault < Gitlab::Database::Migration[2.1]
+ enable_lock_retries!
+
+ def change
+ change_column_default(:users, :project_view, from: 0, to: 2)
+ end
+end
diff --git a/db/post_migrate/20230707211358_deduplicate_inactive_alert_integrations.rb b/db/post_migrate/20230707211358_deduplicate_inactive_alert_integrations.rb
new file mode 100644
index 00000000000..041ce659da6
--- /dev/null
+++ b/db/post_migrate/20230707211358_deduplicate_inactive_alert_integrations.rb
@@ -0,0 +1,40 @@
+# frozen_string_literal: true
+
+class DeduplicateInactiveAlertIntegrations < Gitlab::Database::Migration[2.1]
+ restrict_gitlab_migration gitlab_schema: :gitlab_main
+
+ class HttpIntegration < MigrationRecord
+ self.table_name = 'alert_management_http_integrations'
+ end
+
+ # Existing unique index ensures that all active integrations are already unique.
+ # Any non-unique records must be inactive by definition, but dups are unlikely.
+ # At time of writing, gitlab.com has 0 records in this state.
+ # Of the ~1600 integrations, only ~100 are even inactive.
+ def up
+ duplicated_identifiers.each do |(project_id, endpoint_identifier)|
+ find_inactive_integrations(project_id, endpoint_identifier).delete_all
+ end
+ end
+
+ def down
+ # no-op -> unable to identify duplicates retroactively
+ end
+
+ private
+
+ def duplicated_identifiers
+ HttpIntegration
+ .group(:project_id, :endpoint_identifier)
+ .having('count(id) > 1')
+ .pluck(:project_id, :endpoint_identifier)
+ end
+
+ def find_inactive_integrations(project_id, endpoint_identifier)
+ HttpIntegration.where(
+ project_id: project_id,
+ endpoint_identifier: endpoint_identifier,
+ active: false
+ )
+ end
+end
diff --git a/db/post_migrate/20230707211359_change_alert_integrations_unique_index.rb b/db/post_migrate/20230707211359_change_alert_integrations_unique_index.rb
new file mode 100644
index 00000000000..eac5d057b40
--- /dev/null
+++ b/db/post_migrate/20230707211359_change_alert_integrations_unique_index.rb
@@ -0,0 +1,33 @@
+# frozen_string_literal: true
+
+# Swaps the unique index for AlertManagement::HttpIntegration to include
+# inactive integrations, making performance optimizations easier.
+#
+# At time of writing, gitlab.com has 0 records which would be invalidated
+# by the new index. Of the ~1600 integrations, only ~100 are inactive, so the
+# size of the index will not significantly change.
+class ChangeAlertIntegrationsUniqueIndex < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ UNIQUE_INDEX_NAME = 'index_http_integrations_on_project_and_endpoint'
+ OLD_INDEX_NAME = 'index_http_integrations_on_active_and_project_and_endpoint'
+
+ def up
+ add_concurrent_index :alert_management_http_integrations,
+ [:project_id, :endpoint_identifier],
+ name: UNIQUE_INDEX_NAME,
+ unique: true
+
+ remove_concurrent_index_by_name :alert_management_http_integrations, OLD_INDEX_NAME
+ end
+
+ def down
+ add_concurrent_index :alert_management_http_integrations,
+ [:active, :project_id, :endpoint_identifier],
+ unique: true,
+ name: OLD_INDEX_NAME,
+ where: 'active'
+
+ remove_concurrent_index_by_name :alert_management_http_integrations, UNIQUE_INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230707220646_add_index_to_vulnerability_findings_on_uuid_again.rb b/db/post_migrate/20230707220646_add_index_to_vulnerability_findings_on_uuid_again.rb
new file mode 100644
index 00000000000..06d0117d50e
--- /dev/null
+++ b/db/post_migrate/20230707220646_add_index_to_vulnerability_findings_on_uuid_again.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+class AddIndexToVulnerabilityFindingsOnUuidAgain < Gitlab::Database::Migration[2.1]
+ INDEX_NAME = 'index_vuln_findings_on_uuid_including_vuln_id'
+
+ def up
+ Gitlab::Database::AsyncIndexes::PostgresAsyncIndex.where(name: INDEX_NAME).find_each do |record|
+ record.definition = record.definition.strip
+ record.save!
+ end
+ end
+
+ def down
+ # no-op
+ end
+end
diff --git a/db/post_migrate/20230711093010_drop_default_partition_id_value_for_ci_tables.rb b/db/post_migrate/20230711093010_drop_default_partition_id_value_for_ci_tables.rb
new file mode 100644
index 00000000000..733cab057f6
--- /dev/null
+++ b/db/post_migrate/20230711093010_drop_default_partition_id_value_for_ci_tables.rb
@@ -0,0 +1,39 @@
+# frozen_string_literal: true
+
+class DropDefaultPartitionIdValueForCiTables < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ TABLES = {
+ ci_build_needs: [:partition_id],
+ ci_build_pending_states: [:partition_id],
+ ci_build_report_results: [:partition_id],
+ ci_build_trace_chunks: [:partition_id],
+ ci_builds_runner_session: [:partition_id],
+ ci_job_variables: [:partition_id],
+ ci_pending_builds: [:partition_id],
+ ci_pipelines: [:partition_id],
+ ci_running_builds: [:partition_id],
+ ci_sources_pipelines: [:partition_id, :source_partition_id],
+ ci_unit_test_failures: [:partition_id]
+ }
+
+ def up
+ TABLES.each do |table_name, columns|
+ with_lock_retries do
+ columns.each do |column_name| # rubocop:disable Migration/WithLockRetriesDisallowedMethod
+ change_column_default(table_name, column_name, from: 100, to: nil)
+ end
+ end
+ end
+ end
+
+ def down
+ TABLES.each do |table_name, columns|
+ with_lock_retries do
+ columns.each do |column_name| # rubocop:disable Migration/WithLockRetriesDisallowedMethod
+ change_column_default(table_name, column_name, from: nil, to: 100)
+ end
+ end
+ end
+ end
+end
diff --git a/db/post_migrate/20230712020407_remove_temporary_index_from_system_note_metadata.rb b/db/post_migrate/20230712020407_remove_temporary_index_from_system_note_metadata.rb
new file mode 100644
index 00000000000..9000ef208d6
--- /dev/null
+++ b/db/post_migrate/20230712020407_remove_temporary_index_from_system_note_metadata.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+class RemoveTemporaryIndexFromSystemNoteMetadata < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ INDEX_NAME = 'tmp_index_for_backfilling_resource_link_events'
+ CONDITION = "action='relate_to_parent' OR action='unrelate_from_parent'"
+
+ def up
+ remove_concurrent_index_by_name :system_note_metadata, INDEX_NAME
+ end
+
+ def down
+ add_concurrent_index :system_note_metadata, :id,
+ where: CONDITION,
+ name: INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20230712064637_replace_old_fk_p_ci_builds_metadata_to_builds_v2.rb b/db/post_migrate/20230712064637_replace_old_fk_p_ci_builds_metadata_to_builds_v2.rb
new file mode 100644
index 00000000000..6165029d855
--- /dev/null
+++ b/db/post_migrate/20230712064637_replace_old_fk_p_ci_builds_metadata_to_builds_v2.rb
@@ -0,0 +1,54 @@
+# frozen_string_literal: true
+
+class ReplaceOldFkPCiBuildsMetadataToBuildsV2 < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::PartitioningMigrationHelpers
+ include Gitlab::Database::MigrationHelpers::WraparoundAutovacuum
+
+ disable_ddl_transaction!
+
+ def up
+ return unless should_run?
+ 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 should_run?
+ 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 should_run?
+ can_execute_on?(:ci_builds_metadata, :ci_builds)
+ end
+
+ 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/20230712064655_replace_old_fk_p_ci_runner_machine_builds_to_builds_v2.rb b/db/post_migrate/20230712064655_replace_old_fk_p_ci_runner_machine_builds_to_builds_v2.rb
new file mode 100644
index 00000000000..8f1c1872e04
--- /dev/null
+++ b/db/post_migrate/20230712064655_replace_old_fk_p_ci_runner_machine_builds_to_builds_v2.rb
@@ -0,0 +1,54 @@
+# frozen_string_literal: true
+
+class ReplaceOldFkPCiRunnerMachineBuildsToBuildsV2 < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::PartitioningMigrationHelpers
+ include Gitlab::Database::MigrationHelpers::WraparoundAutovacuum
+
+ disable_ddl_transaction!
+
+ def up
+ return unless should_run?
+ 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 should_run?
+ 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 should_run?
+ can_execute_on?(:ci_builds)
+ end
+
+ 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/20230712145323_drop_ci_job_artifacts_partition_id_default.rb b/db/post_migrate/20230712145323_drop_ci_job_artifacts_partition_id_default.rb
new file mode 100644
index 00000000000..af475ba62d0
--- /dev/null
+++ b/db/post_migrate/20230712145323_drop_ci_job_artifacts_partition_id_default.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+class DropCiJobArtifactsPartitionIdDefault < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::WraparoundAutovacuum
+
+ enable_lock_retries!
+
+ TABLE_NAME = :ci_job_artifacts
+ COLUMN_NAME = :partition_id
+
+ def up
+ change_column_default(TABLE_NAME, COLUMN_NAME, from: 100, to: nil) if should_run?
+ end
+
+ def down
+ change_column_default(TABLE_NAME, COLUMN_NAME, from: nil, to: 100) if should_run?
+ end
+
+ private
+
+ def should_run?
+ can_execute_on?(TABLE_NAME)
+ end
+end
diff --git a/db/post_migrate/20230712145821_drop_ci_stages_partition_id_default.rb b/db/post_migrate/20230712145821_drop_ci_stages_partition_id_default.rb
new file mode 100644
index 00000000000..a7c23ac35d2
--- /dev/null
+++ b/db/post_migrate/20230712145821_drop_ci_stages_partition_id_default.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+class DropCiStagesPartitionIdDefault < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::WraparoundAutovacuum
+
+ enable_lock_retries!
+
+ TABLE_NAME = :ci_stages
+ COLUMN_NAME = :partition_id
+
+ def up
+ change_column_default(TABLE_NAME, COLUMN_NAME, from: 100, to: nil) if should_run?
+ end
+
+ def down
+ change_column_default(TABLE_NAME, COLUMN_NAME, from: nil, to: 100) if should_run?
+ end
+
+ private
+
+ def should_run?
+ can_execute_on?(TABLE_NAME)
+ end
+end
diff --git a/db/post_migrate/20230712145926_drop_ci_build_trace_metadata_partition_id_default.rb b/db/post_migrate/20230712145926_drop_ci_build_trace_metadata_partition_id_default.rb
new file mode 100644
index 00000000000..dad647246a5
--- /dev/null
+++ b/db/post_migrate/20230712145926_drop_ci_build_trace_metadata_partition_id_default.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+class DropCiBuildTraceMetadataPartitionIdDefault < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::WraparoundAutovacuum
+
+ enable_lock_retries!
+
+ TABLE_NAME = :ci_build_trace_metadata
+ COLUMN_NAME = :partition_id
+
+ def up
+ change_column_default(TABLE_NAME, COLUMN_NAME, from: 100, to: nil) if should_run?
+ end
+
+ def down
+ change_column_default(TABLE_NAME, COLUMN_NAME, from: nil, to: 100) if should_run?
+ end
+
+ private
+
+ def should_run?
+ can_execute_on?(TABLE_NAME)
+ end
+end
diff --git a/db/post_migrate/20230713020247_change_pm_advisories_urls_constraint.rb b/db/post_migrate/20230713020247_change_pm_advisories_urls_constraint.rb
new file mode 100644
index 00000000000..4611f657d0d
--- /dev/null
+++ b/db/post_migrate/20230713020247_change_pm_advisories_urls_constraint.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class ChangePmAdvisoriesUrlsConstraint < Gitlab::Database::Migration[2.1]
+ disable_ddl_transaction!
+
+ CONSTRAINT_NAME = "chk_rails_e73af9de76"
+
+ def up
+ remove_check_constraint :pm_advisories, CONSTRAINT_NAME
+ add_check_constraint :pm_advisories, "CARDINALITY(urls) <= 20", CONSTRAINT_NAME
+ end
+
+ def down
+ remove_check_constraint :pm_advisories, CONSTRAINT_NAME
+ add_check_constraint :pm_advisories, "CARDINALITY(urls) <= 10", CONSTRAINT_NAME
+ end
+end
diff --git a/db/post_migrate/20230713100100_drop_ci_pipeline_variable_partition_id_default.rb b/db/post_migrate/20230713100100_drop_ci_pipeline_variable_partition_id_default.rb
new file mode 100644
index 00000000000..5ef3a2caa82
--- /dev/null
+++ b/db/post_migrate/20230713100100_drop_ci_pipeline_variable_partition_id_default.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+class DropCiPipelineVariablePartitionIdDefault < Gitlab::Database::Migration[2.1]
+ include Gitlab::Database::MigrationHelpers::WraparoundAutovacuum
+
+ enable_lock_retries!
+
+ TABLE_NAME = :ci_pipeline_variables
+ COLUMN_NAME = :partition_id
+
+ def up
+ change_column_default(TABLE_NAME, COLUMN_NAME, from: 100, to: nil) if should_run?
+ end
+
+ def down
+ change_column_default(TABLE_NAME, COLUMN_NAME, from: nil, to: 100) if should_run?
+ end
+
+ private
+
+ def should_run?
+ can_execute_on?(TABLE_NAME)
+ end
+end