diff options
Diffstat (limited to 'db/post_migrate')
68 files changed, 1281 insertions, 34 deletions
diff --git a/db/post_migrate/20210415155043_move_container_registry_enabled_to_project_features3.rb b/db/post_migrate/20210415155043_move_container_registry_enabled_to_project_features3.rb index 6fd8d280c97..f63d7c5138b 100644 --- a/db/post_migrate/20210415155043_move_container_registry_enabled_to_project_features3.rb +++ b/db/post_migrate/20210415155043_move_container_registry_enabled_to_project_features3.rb @@ -2,7 +2,6 @@ class MoveContainerRegistryEnabledToProjectFeatures3 < ActiveRecord::Migration[6.0] include Gitlab::Database::MigrationHelpers - include Gitlab::Database::DynamicModelHelpers BATCH_SIZE = 21_000 MIGRATION = 'MoveContainerRegistryEnabledToProjectFeature' diff --git a/db/post_migrate/20210525075724_clean_up_pending_builds_table.rb b/db/post_migrate/20210525075724_clean_up_pending_builds_table.rb index c380f15188a..59b41dd2008 100644 --- a/db/post_migrate/20210525075724_clean_up_pending_builds_table.rb +++ b/db/post_migrate/20210525075724_clean_up_pending_builds_table.rb @@ -10,7 +10,7 @@ class CleanUpPendingBuildsTable < ActiveRecord::Migration[6.0] def up return unless Gitlab.dev_or_test_env? || Gitlab.com? - each_batch_range('ci_pending_builds', of: BATCH_SIZE) do |min, max| + each_batch_range('ci_pending_builds', connection: connection, of: BATCH_SIZE) do |min, max| execute <<~SQL DELETE FROM ci_pending_builds USING ci_builds diff --git a/db/post_migrate/20210610102413_migrate_protected_attribute_to_pending_builds.rb b/db/post_migrate/20210610102413_migrate_protected_attribute_to_pending_builds.rb index f47ff244d7a..47a6e39e87a 100644 --- a/db/post_migrate/20210610102413_migrate_protected_attribute_to_pending_builds.rb +++ b/db/post_migrate/20210610102413_migrate_protected_attribute_to_pending_builds.rb @@ -8,7 +8,7 @@ class MigrateProtectedAttributeToPendingBuilds < ActiveRecord::Migration[6.1] def up return unless Gitlab.dev_or_test_env? || Gitlab.com? - each_batch_range('ci_pending_builds', of: 1000) do |min, max| + each_batch_range('ci_pending_builds', connection: connection, of: 1000) do |min, max| execute <<~SQL UPDATE ci_pending_builds SET protected = true diff --git a/db/post_migrate/20211022214523_schedule_recalculate_vulnerability_finding_signatures_for_findings.rb b/db/post_migrate/20211022214523_schedule_recalculate_vulnerability_finding_signatures_for_findings.rb index bccbc4e3209..105d5ec98f4 100644 --- a/db/post_migrate/20211022214523_schedule_recalculate_vulnerability_finding_signatures_for_findings.rb +++ b/db/post_migrate/20211022214523_schedule_recalculate_vulnerability_finding_signatures_for_findings.rb @@ -8,15 +8,7 @@ class ScheduleRecalculateVulnerabilityFindingSignaturesForFindings < Gitlab::Dat disable_ddl_transaction! def up - return unless Gitlab.ee? - - queue_background_migration_jobs_by_range_at_intervals( - define_batchable_model('vulnerability_finding_signatures'), - MIGRATION, - DELAY_INTERVAL, - batch_size: BATCH_SIZE, - track_jobs: true - ) + # no-op based on https://docs.gitlab.com/ee/development/background_migrations.html#rescheduling-background-migrations end def down diff --git a/db/post_migrate/20211104165220_remove_vulnerability_finding_links.rb b/db/post_migrate/20211104165220_remove_vulnerability_finding_links.rb index fc50aa812a7..1625d24ef92 100644 --- a/db/post_migrate/20211104165220_remove_vulnerability_finding_links.rb +++ b/db/post_migrate/20211104165220_remove_vulnerability_finding_links.rb @@ -1,21 +1,14 @@ # frozen_string_literal: true class RemoveVulnerabilityFindingLinks < Gitlab::Database::Migration[1.0] - BATCH_SIZE = 50_000 - MIGRATION = 'RemoveVulnerabilityFindingLinks' - - disable_ddl_transaction! + # This migration has been moved to a TRUNCATE in db/post_migrate/20220201193033_add_unique_index_to_vulnerability_finding_links_with_truncate.rb + # Previously, this was causing an bug where there was a conflict between the table cleanup and the index creation. def up - queue_background_migration_jobs_by_range_at_intervals( - define_batchable_model('vulnerability_finding_links'), - MIGRATION, - 2.minutes, - batch_size: BATCH_SIZE - ) + # no op end def down - # no ops + # no op end end diff --git a/db/post_migrate/20211209103048_backfill_project_namespaces_for_group.rb b/db/post_migrate/20211209103048_backfill_project_namespaces_for_group.rb new file mode 100644 index 00000000000..0b65db7aab4 --- /dev/null +++ b/db/post_migrate/20211209103048_backfill_project_namespaces_for_group.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +class BackfillProjectNamespacesForGroup < Gitlab::Database::Migration[1.0] + MIGRATION = 'ProjectNamespaces::BackfillProjectNamespaces' + DELAY_INTERVAL = 2.minutes + GROUP_ID = 9970 # picking gitlab-org group. + + disable_ddl_transaction! + + def up + return unless Gitlab.com? || Gitlab.staging? + + projects_table = ::Gitlab::BackgroundMigration::ProjectNamespaces::Models::Project.arel_table + hierarchy_cte_sql = Arel.sql(::Gitlab::BackgroundMigration::ProjectNamespaces::BackfillProjectNamespaces.hierarchy_cte(GROUP_ID)) + group_projects = ::Gitlab::BackgroundMigration::ProjectNamespaces::Models::Project.where(projects_table[:namespace_id].in(hierarchy_cte_sql)) + + min_id = group_projects.minimum(:id) + max_id = group_projects.maximum(:id) + + return if min_id.blank? || max_id.blank? + + queue_batched_background_migration( + MIGRATION, + :projects, + :id, + GROUP_ID, + 'up', + job_interval: DELAY_INTERVAL, + batch_min_value: min_id, + batch_max_value: max_id, + sub_batch_size: 25, + batch_class_name: 'BackfillProjectNamespacePerGroupBatchingStrategy' + ) + end + + def down + return unless Gitlab.com? || Gitlab.staging? + + Gitlab::Database::BackgroundMigration::BatchedMigration + .for_configuration(MIGRATION, :projects, :id, [GROUP_ID, 'up']).delete_all + end +end diff --git a/db/post_migrate/20211210173137_remove_vulnerability_finding_links_again.rb b/db/post_migrate/20211210173137_remove_vulnerability_finding_links_again.rb index 98ac4433193..ffe8bf0d794 100644 --- a/db/post_migrate/20211210173137_remove_vulnerability_finding_links_again.rb +++ b/db/post_migrate/20211210173137_remove_vulnerability_finding_links_again.rb @@ -1,21 +1,14 @@ # frozen_string_literal: true class RemoveVulnerabilityFindingLinksAgain < Gitlab::Database::Migration[1.0] - BATCH_SIZE = 50_000 - MIGRATION = 'RemoveVulnerabilityFindingLinks' - - disable_ddl_transaction! + # This migration has been moved to a TRUNCATE in db/post_migrate/20220201193033_add_unique_index_to_vulnerability_finding_links_with_truncate.rb + # Previously, this was causing an bug where there was a conflict between the table cleanup and the index creation. def up - queue_background_migration_jobs_by_range_at_intervals( - define_batchable_model('vulnerability_finding_links'), - MIGRATION, - 2.minutes, - batch_size: BATCH_SIZE - ) + # no op end def down - # no ops + # no op end end diff --git a/db/post_migrate/20220106185033_remove_finding_evidence_summary.rb b/db/post_migrate/20220106185033_remove_finding_evidence_summary.rb new file mode 100644 index 00000000000..d28e8bb81f6 --- /dev/null +++ b/db/post_migrate/20220106185033_remove_finding_evidence_summary.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class RemoveFindingEvidenceSummary < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + remove_column :vulnerability_finding_evidences, :summary, :text + end + + def down + add_column :vulnerability_finding_evidences, :summary, :text + + add_text_limit :vulnerability_finding_evidences, :summary, 8_000_000 + end +end diff --git a/db/post_migrate/20220107064845_populate_vulnerability_reads.rb b/db/post_migrate/20220107064845_populate_vulnerability_reads.rb new file mode 100644 index 00000000000..58826c5f929 --- /dev/null +++ b/db/post_migrate/20220107064845_populate_vulnerability_reads.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +class PopulateVulnerabilityReads < Gitlab::Database::Migration[1.0] + BATCH_SIZE = 10_000 + DELAY_INTERVAL = 2.minutes + MIGRATION_NAME = 'PopulateVulnerabilityReads' + SUB_BATCH_SIZE = 1_000 + + disable_ddl_transaction! + + def up + queue_background_migration_jobs_by_range_at_intervals( + define_batchable_model('vulnerabilities'), + MIGRATION_NAME, + DELAY_INTERVAL, + batch_size: BATCH_SIZE, + track_jobs: true, + other_job_arguments: [SUB_BATCH_SIZE] + ) + end + + def down + # no-op + end +end diff --git a/db/post_migrate/20220111093534_remove_index_on_auto_stop_in.rb b/db/post_migrate/20220111093534_remove_index_on_auto_stop_in.rb new file mode 100644 index 00000000000..bcb78b28376 --- /dev/null +++ b/db/post_migrate/20220111093534_remove_index_on_auto_stop_in.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class RemoveIndexOnAutoStopIn < Gitlab::Database::Migration[1.0] + TABLE = :environments + INDEX_NAME = 'index_environments_on_auto_stop_at' + COLUMN = :auto_stop_at + + disable_ddl_transaction! + + def up + remove_concurrent_index TABLE, COLUMN, where: 'auto_stop_at IS NOT NULL', name: INDEX_NAME + end + + def down + add_concurrent_index TABLE, COLUMN, where: 'auto_stop_at IS NOT NULL', name: INDEX_NAME + end +end diff --git a/db/post_migrate/20220111101421_remove_index_on_merge_request_id.rb b/db/post_migrate/20220111101421_remove_index_on_merge_request_id.rb new file mode 100644 index 00000000000..38cc259312d --- /dev/null +++ b/db/post_migrate/20220111101421_remove_index_on_merge_request_id.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class RemoveIndexOnMergeRequestId < Gitlab::Database::Migration[1.0] + TABLE = :merge_request_context_commits + INDEX_NAME = 'index_merge_request_context_commits_on_merge_request_id' + COLUMN = :merge_request_id + + disable_ddl_transaction! + + def up + remove_concurrent_index TABLE, COLUMN, name: INDEX_NAME + end + + def down + add_concurrent_index TABLE, COLUMN, name: INDEX_NAME + end +end diff --git a/db/post_migrate/20220113111440_schedule_fix_incorrect_max_seats_used.rb b/db/post_migrate/20220113111440_schedule_fix_incorrect_max_seats_used.rb new file mode 100644 index 00000000000..f39b026bd77 --- /dev/null +++ b/db/post_migrate/20220113111440_schedule_fix_incorrect_max_seats_used.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +class ScheduleFixIncorrectMaxSeatsUsed < Gitlab::Database::Migration[1.0] + DOWNTIME = false + TMP_IDX_NAME = 'tmp_gitlab_subscriptions_max_seats_used_migration' + + disable_ddl_transaction! + + def up + add_concurrent_index :gitlab_subscriptions, :id, where: "start_date >= '2021-08-02' AND start_date <= '2021-11-20' AND max_seats_used != 0 AND max_seats_used > seats_in_use AND max_seats_used > seats", name: TMP_IDX_NAME + + return unless Gitlab.com? + + migrate_in(1.hour, 'FixIncorrectMaxSeatsUsed') + end + + def down + remove_concurrent_index_by_name :gitlab_subscriptions, TMP_IDX_NAME + end +end diff --git a/db/post_migrate/20220117034056_remove_index_cluster_agent_tokens_on_agent_id_and_last_used_at.rb b/db/post_migrate/20220117034056_remove_index_cluster_agent_tokens_on_agent_id_and_last_used_at.rb new file mode 100644 index 00000000000..5e306be016a --- /dev/null +++ b/db/post_migrate/20220117034056_remove_index_cluster_agent_tokens_on_agent_id_and_last_used_at.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class RemoveIndexClusterAgentTokensOnAgentIdAndLastUsedAt < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + INDEX = 'index_cluster_agent_tokens_on_agent_id_and_last_used_at' + + def up + remove_concurrent_index_by_name :cluster_agent_tokens, name: INDEX + end + + def down + add_concurrent_index :cluster_agent_tokens, 'agent_id, last_used_at DESC NULLS LAST', name: INDEX + end +end diff --git a/db/post_migrate/20220118204039_self_managed_reschedule_recalculate_vulnerability_finding_signatures_for_findings.rb b/db/post_migrate/20220118204039_self_managed_reschedule_recalculate_vulnerability_finding_signatures_for_findings.rb new file mode 100644 index 00000000000..18f68150e45 --- /dev/null +++ b/db/post_migrate/20220118204039_self_managed_reschedule_recalculate_vulnerability_finding_signatures_for_findings.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +class SelfManagedRescheduleRecalculateVulnerabilityFindingSignaturesForFindings < Gitlab::Database::Migration[1.0] + MIGRATION = 'RecalculateVulnerabilityFindingSignaturesForFindings' + BATCH_SIZE = 1_000 + DELAY_INTERVAL = 2.minutes + + disable_ddl_transaction! + + def up + # Only run migration for self-managed + return if ::Gitlab.com? + + # Vulnerability Finding Signatures is an EE only feature + return unless Gitlab.ee? + + delete_queued_jobs(MIGRATION) + + requeue_background_migration_jobs_by_range_at_intervals( + MIGRATION, + DELAY_INTERVAL, + batch_size: BATCH_SIZE + ) + end + + def down + # no-op + end +end diff --git a/db/post_migrate/20220119094503_populate_audit_event_streaming_verification_token.rb b/db/post_migrate/20220119094503_populate_audit_event_streaming_verification_token.rb new file mode 100644 index 00000000000..482f873739a --- /dev/null +++ b/db/post_migrate/20220119094503_populate_audit_event_streaming_verification_token.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class PopulateAuditEventStreamingVerificationToken < Gitlab::Database::Migration[1.0] + class ExternalAuditEventDestination < ActiveRecord::Base + self.table_name = 'audit_events_external_audit_event_destinations' + + def regenerate_verification_token + update!(verification_token: SecureRandom.base58(24)) + end + end + + def up + ExternalAuditEventDestination.all.each { |destination| destination.regenerate_verification_token } + end + + def down + # no-op + end +end diff --git a/db/post_migrate/20220119143130_remove_projects_ci_sources_pipelines_source_project_id_fk.rb b/db/post_migrate/20220119143130_remove_projects_ci_sources_pipelines_source_project_id_fk.rb new file mode 100644 index 00000000000..7fe9f1c856b --- /dev/null +++ b/db/post_migrate/20220119143130_remove_projects_ci_sources_pipelines_source_project_id_fk.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class RemoveProjectsCiSourcesPipelinesSourceProjectIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + with_lock_retries do + execute('LOCK projects, ci_sources_pipelines IN ACCESS EXCLUSIVE MODE') + + remove_foreign_key_if_exists(:ci_sources_pipelines, :projects, name: "fk_acd9737679") + end + end + + def down + add_concurrent_foreign_key(:ci_sources_pipelines, :projects, name: "fk_acd9737679", column: :source_project_id, target_column: :id, on_delete: :cascade) + end +end diff --git a/db/post_migrate/20220119144458_remove_users_ci_triggers_owner_id_fk.rb b/db/post_migrate/20220119144458_remove_users_ci_triggers_owner_id_fk.rb new file mode 100644 index 00000000000..9acf53a6ea2 --- /dev/null +++ b/db/post_migrate/20220119144458_remove_users_ci_triggers_owner_id_fk.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class RemoveUsersCiTriggersOwnerIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + with_lock_retries do + execute('LOCK users, ci_triggers IN ACCESS EXCLUSIVE MODE') + + remove_foreign_key_if_exists(:ci_triggers, :users, name: "fk_e8e10d1964") + end + end + + def down + add_concurrent_foreign_key(:ci_triggers, :users, name: "fk_e8e10d1964", column: :owner_id, target_column: :id, on_delete: :cascade) + end +end diff --git a/db/post_migrate/20220119151221_remove_merge_requests_ci_pipelines_merge_request_id_fk.rb b/db/post_migrate/20220119151221_remove_merge_requests_ci_pipelines_merge_request_id_fk.rb new file mode 100644 index 00000000000..c7ced0ae3b7 --- /dev/null +++ b/db/post_migrate/20220119151221_remove_merge_requests_ci_pipelines_merge_request_id_fk.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class RemoveMergeRequestsCiPipelinesMergeRequestIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + return unless foreign_key_exists?(:ci_pipelines, :merge_requests, name: "fk_a23be95014") + + with_lock_retries do + execute('LOCK merge_requests, ci_pipelines IN ACCESS EXCLUSIVE MODE') if transaction_open? + + remove_foreign_key_if_exists(:ci_pipelines, :merge_requests, name: "fk_a23be95014") + end + end + + def down + add_concurrent_foreign_key(:ci_pipelines, :merge_requests, name: "fk_a23be95014", column: :merge_request_id, target_column: :id, on_delete: :cascade) + end +end diff --git a/db/post_migrate/20220119153706_remove_ci_pipelines_merge_trains_pipeline_id_fk.rb b/db/post_migrate/20220119153706_remove_ci_pipelines_merge_trains_pipeline_id_fk.rb new file mode 100644 index 00000000000..92d68ff4fbb --- /dev/null +++ b/db/post_migrate/20220119153706_remove_ci_pipelines_merge_trains_pipeline_id_fk.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class RemoveCiPipelinesMergeTrainsPipelineIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + with_lock_retries do + execute('LOCK ci_pipelines, merge_trains IN ACCESS EXCLUSIVE MODE') + + remove_foreign_key_if_exists(:merge_trains, :ci_pipelines, name: "fk_rails_f90820cb08") + end + end + + def down + add_concurrent_foreign_key(:merge_trains, :ci_pipelines, name: "fk_rails_f90820cb08", column: :pipeline_id, target_column: :id, on_delete: :nullify) + end +end diff --git a/db/post_migrate/20220119154442_remove_ci_pipelines_merge_requests_head_pipeline_id_fk.rb b/db/post_migrate/20220119154442_remove_ci_pipelines_merge_requests_head_pipeline_id_fk.rb new file mode 100644 index 00000000000..6a978ce2ab1 --- /dev/null +++ b/db/post_migrate/20220119154442_remove_ci_pipelines_merge_requests_head_pipeline_id_fk.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class RemoveCiPipelinesMergeRequestsHeadPipelineIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + with_lock_retries do + execute('LOCK ci_pipelines, merge_requests IN ACCESS EXCLUSIVE MODE') + + remove_foreign_key_if_exists(:merge_requests, :ci_pipelines, name: "fk_fd82eae0b9") + end + end + + def down + add_concurrent_foreign_key(:merge_requests, :ci_pipelines, name: "fk_fd82eae0b9", column: :head_pipeline_id, target_column: :id, on_delete: :nullify) + end +end diff --git a/db/post_migrate/20220119193130_remove_ci_pipelines_dast_profiles_pipelines_ci_pipeline_id_fk.rb b/db/post_migrate/20220119193130_remove_ci_pipelines_dast_profiles_pipelines_ci_pipeline_id_fk.rb new file mode 100644 index 00000000000..f4eedd0fa70 --- /dev/null +++ b/db/post_migrate/20220119193130_remove_ci_pipelines_dast_profiles_pipelines_ci_pipeline_id_fk.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class RemoveCiPipelinesDastProfilesPipelinesCiPipelineIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + with_lock_retries do + execute('LOCK ci_pipelines, dast_profiles_pipelines IN ACCESS EXCLUSIVE MODE') + + remove_foreign_key_if_exists(:dast_profiles_pipelines, :ci_pipelines, name: "fk_a60cad829d") + end + end + + def down + add_concurrent_foreign_key(:dast_profiles_pipelines, :ci_pipelines, name: "fk_a60cad829d", column: :ci_pipeline_id, target_column: :id, on_delete: :cascade) + end +end diff --git a/db/post_migrate/20220119201340_remove_ci_pipelines_vulnerability_statistics_latest_pipeline_id_fk.rb b/db/post_migrate/20220119201340_remove_ci_pipelines_vulnerability_statistics_latest_pipeline_id_fk.rb new file mode 100644 index 00000000000..bd80767cf6a --- /dev/null +++ b/db/post_migrate/20220119201340_remove_ci_pipelines_vulnerability_statistics_latest_pipeline_id_fk.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class RemoveCiPipelinesVulnerabilityStatisticsLatestPipelineIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + with_lock_retries do + execute('LOCK ci_pipelines, vulnerability_statistics IN ACCESS EXCLUSIVE MODE') + + remove_foreign_key_if_exists(:vulnerability_statistics, :ci_pipelines, name: "fk_e8b13c928f") + end + end + + def down + add_concurrent_foreign_key(:vulnerability_statistics, :ci_pipelines, name: "fk_e8b13c928f", column: :latest_pipeline_id, target_column: :id, on_delete: :nullify) + end +end diff --git a/db/post_migrate/20220119203119_remove_ci_pipelines_vulnerability_occurrence_pipelines_pipeline_id_fk.rb b/db/post_migrate/20220119203119_remove_ci_pipelines_vulnerability_occurrence_pipelines_pipeline_id_fk.rb new file mode 100644 index 00000000000..b6397486dd0 --- /dev/null +++ b/db/post_migrate/20220119203119_remove_ci_pipelines_vulnerability_occurrence_pipelines_pipeline_id_fk.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class RemoveCiPipelinesVulnerabilityOccurrencePipelinesPipelineIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + with_lock_retries do + execute('LOCK ci_pipelines, vulnerability_occurrence_pipelines IN ACCESS EXCLUSIVE MODE') + + remove_foreign_key_if_exists(:vulnerability_occurrence_pipelines, :ci_pipelines, name: "fk_rails_6421e35d7d") + end + end + + def down + add_concurrent_foreign_key(:vulnerability_occurrence_pipelines, :ci_pipelines, name: "fk_rails_6421e35d7d", column: :pipeline_id, target_column: :id, on_delete: :cascade) + end +end diff --git a/db/post_migrate/20220120094340_drop_position_from_security_findings.rb b/db/post_migrate/20220120094340_drop_position_from_security_findings.rb new file mode 100644 index 00000000000..56af346ad1b --- /dev/null +++ b/db/post_migrate/20220120094340_drop_position_from_security_findings.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class DropPositionFromSecurityFindings < Gitlab::Database::Migration[1.0] + enable_lock_retries! + + def change + remove_column :security_findings, :position, :integer + end +end diff --git a/db/post_migrate/20220120123700_add_tmp_index_routes_id_for_namespaces.rb b/db/post_migrate/20220120123700_add_tmp_index_routes_id_for_namespaces.rb new file mode 100644 index 00000000000..2b8cecd0e57 --- /dev/null +++ b/db/post_migrate/20220120123700_add_tmp_index_routes_id_for_namespaces.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class AddTmpIndexRoutesIdForNamespaces < Gitlab::Database::Migration[1.0] + INDEX_NAME = 'tmp_index_for_namespace_id_migration_on_routes' + + disable_ddl_transaction! + + def up + # Temporary index to be removed in 14.9 + # https://gitlab.com/gitlab-org/gitlab/-/issues/352353 + add_concurrent_index :routes, :id, where: "routes.namespace_id is null and routes.source_type = 'Namespace'", name: INDEX_NAME + end + + def down + remove_concurrent_index_by_name :routes, INDEX_NAME + end +end diff --git a/db/post_migrate/20220120123800_backfill_namespace_id_for_namespace_routes.rb b/db/post_migrate/20220120123800_backfill_namespace_id_for_namespace_routes.rb new file mode 100644 index 00000000000..4aac0903502 --- /dev/null +++ b/db/post_migrate/20220120123800_backfill_namespace_id_for_namespace_routes.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +class BackfillNamespaceIdForNamespaceRoutes < Gitlab::Database::Migration[1.0] + MIGRATION = 'BackfillNamespaceIdForNamespaceRoute' + INTERVAL = 2.minutes + BATCH_SIZE = 1_000 + MAX_BATCH_SIZE = 10_000 + SUB_BATCH_SIZE = 200 + + def up + queue_batched_background_migration( + MIGRATION, + :routes, + :id, + job_interval: INTERVAL, + batch_size: BATCH_SIZE, + max_batch_size: MAX_BATCH_SIZE, + sub_batch_size: SUB_BATCH_SIZE + ) + end + + def down + Gitlab::Database::BackgroundMigration::BatchedMigration + .for_configuration(MIGRATION, :routes, :id, []) + .delete_all + end +end diff --git a/db/post_migrate/20220121214752_remove_projects_ci_stages_project_id_fk.rb b/db/post_migrate/20220121214752_remove_projects_ci_stages_project_id_fk.rb new file mode 100644 index 00000000000..93f0b30571a --- /dev/null +++ b/db/post_migrate/20220121214752_remove_projects_ci_stages_project_id_fk.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +class RemoveProjectsCiStagesProjectIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + return if Gitlab.com? # unsafe migration, skip on GitLab.com due to https://gitlab.com/groups/gitlab-org/-/epics/7249#note_819625526 + return unless foreign_key_exists?(:ci_stages, :projects, name: "fk_2360681d1d") + + with_lock_retries do + execute('LOCK projects, ci_stages IN ACCESS EXCLUSIVE MODE') if transaction_open? + + remove_foreign_key_if_exists(:ci_stages, :projects, name: "fk_2360681d1d") + end + end + + def down + add_concurrent_foreign_key(:ci_stages, :projects, name: "fk_2360681d1d", column: :project_id, target_column: :id, on_delete: :cascade) + end +end diff --git a/db/post_migrate/20220121214753_re_remove_projects_ci_stages_project_id_fk.rb b/db/post_migrate/20220121214753_re_remove_projects_ci_stages_project_id_fk.rb new file mode 100644 index 00000000000..45dec15a5a7 --- /dev/null +++ b/db/post_migrate/20220121214753_re_remove_projects_ci_stages_project_id_fk.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class ReRemoveProjectsCiStagesProjectIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + return unless foreign_key_exists?(:ci_stages, :projects, name: "fk_2360681d1d") + + with_lock_retries do + execute('LOCK projects, ci_stages IN ACCESS EXCLUSIVE MODE') if transaction_open? + + remove_foreign_key_if_exists(:ci_stages, :projects, name: "fk_2360681d1d") + end + end + + def down + # no-op, since the FK will be added via rollback by prior-migration + end +end diff --git a/db/post_migrate/20220121221651_remove_projects_ci_variables_project_id_fk.rb b/db/post_migrate/20220121221651_remove_projects_ci_variables_project_id_fk.rb new file mode 100644 index 00000000000..f647d4376ac --- /dev/null +++ b/db/post_migrate/20220121221651_remove_projects_ci_variables_project_id_fk.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class RemoveProjectsCiVariablesProjectIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + return unless foreign_key_exists?(:ci_variables, :projects, name: "fk_ada5eb64b3") + + with_lock_retries do + execute('LOCK projects, ci_variables IN ACCESS EXCLUSIVE MODE') if transaction_open? + + remove_foreign_key_if_exists(:ci_variables, :projects, name: "fk_ada5eb64b3") + end + end + + def down + add_concurrent_foreign_key(:ci_variables, :projects, name: "fk_ada5eb64b3", column: :project_id, target_column: :id, on_delete: :cascade) + end +end diff --git a/db/post_migrate/20220124130028_dedup_runner_projects.rb b/db/post_migrate/20220124130028_dedup_runner_projects.rb new file mode 100644 index 00000000000..1a4c895f1c4 --- /dev/null +++ b/db/post_migrate/20220124130028_dedup_runner_projects.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +class DedupRunnerProjects < Gitlab::Database::Migration[1.0] + TABLE_NAME = :ci_runner_projects + TMP_INDEX_NAME = 'tmp_unique_ci_runner_projects_by_runner_id_and_project_id' + OLD_INDEX_NAME = 'index_ci_runner_projects_on_runner_id_and_project_id' + INDEX_NAME = 'index_unique_ci_runner_projects_on_runner_id_and_project_id' + BATCH_SIZE = 5000 + + disable_ddl_transaction! + + module Ci + class RunnerProject < ActiveRecord::Base + include EachBatch + + self.table_name = 'ci_runner_projects' + end + end + + def up + last_runner_project_record_id = Ci::RunnerProject.maximum(:id) || 0 + + # This index will disallow further duplicates while we're deduplicating the data. + add_concurrent_index(TABLE_NAME, [:runner_id, :project_id], where: "id > #{Integer(last_runner_project_record_id)}", unique: true, name: TMP_INDEX_NAME) + + Ci::RunnerProject.each_batch(of: BATCH_SIZE) do |relation| + duplicated_runner_projects = Ci::RunnerProject + .select('COUNT(*)', :runner_id, :project_id) + .where('(runner_id, project_id) IN (?)', relation.select(:runner_id, :project_id)) + .group(:runner_id, :project_id) + .having('COUNT(*) > 1') + + duplicated_runner_projects.each do |runner_project| + deduplicate_item(runner_project) + end + end + + add_concurrent_index(TABLE_NAME, [:runner_id, :project_id], unique: true, name: INDEX_NAME) + remove_concurrent_index_by_name(TABLE_NAME, TMP_INDEX_NAME) + remove_concurrent_index_by_name(TABLE_NAME, OLD_INDEX_NAME) + end + + def down + add_concurrent_index(TABLE_NAME, [:runner_id, :project_id], name: OLD_INDEX_NAME) + remove_concurrent_index_by_name(TABLE_NAME, TMP_INDEX_NAME) + remove_concurrent_index_by_name(TABLE_NAME, INDEX_NAME) + end + + private + + def deduplicate_item(runner_project) + runner_projects_records = Ci::RunnerProject + .where(project_id: runner_project.project_id, runner_id: runner_project.runner_id) + .order(updated_at: :asc) + .to_a + + attributes = {} + runner_projects_records.each do |runner_projects_record| + params = runner_projects_record.attributes.except('id') + attributes.merge!(params.compact) + end + + ApplicationRecord.transaction do + record_to_keep = runner_projects_records.pop + records_to_delete = runner_projects_records + + Ci::RunnerProject.where(id: records_to_delete.map(&:id)).delete_all + record_to_keep.update!(attributes) + end + end +end diff --git a/db/post_migrate/20220124145019_remove_projects_external_pull_requests_project_id_fk.rb b/db/post_migrate/20220124145019_remove_projects_external_pull_requests_project_id_fk.rb new file mode 100644 index 00000000000..b16234fc812 --- /dev/null +++ b/db/post_migrate/20220124145019_remove_projects_external_pull_requests_project_id_fk.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class RemoveProjectsExternalPullRequestsProjectIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + return unless foreign_key_exists?(:external_pull_requests, :projects, name: "fk_rails_bcae9b5c7b") + + with_lock_retries do + execute('LOCK projects, external_pull_requests IN ACCESS EXCLUSIVE MODE') if transaction_open? + + remove_foreign_key_if_exists(:external_pull_requests, :projects, name: "fk_rails_bcae9b5c7b") + end + end + + def down + add_concurrent_foreign_key(:external_pull_requests, :projects, name: "fk_rails_bcae9b5c7b", column: :project_id, target_column: :id, on_delete: :cascade) + end +end diff --git a/db/post_migrate/20220124151456_remove_projects_ci_triggers_project_id_fk.rb b/db/post_migrate/20220124151456_remove_projects_ci_triggers_project_id_fk.rb new file mode 100644 index 00000000000..684d363eb96 --- /dev/null +++ b/db/post_migrate/20220124151456_remove_projects_ci_triggers_project_id_fk.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class RemoveProjectsCiTriggersProjectIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + return unless foreign_key_exists?(:ci_triggers, :projects, name: "fk_e3e63f966e") + + with_lock_retries do + execute('LOCK projects, ci_triggers IN ACCESS EXCLUSIVE MODE') if transaction_open? + + remove_foreign_key_if_exists(:ci_triggers, :projects, name: "fk_e3e63f966e") + end + end + + def down + add_concurrent_foreign_key(:ci_triggers, :projects, name: "fk_e3e63f966e", column: :project_id, target_column: :id, on_delete: :cascade) + end +end diff --git a/db/post_migrate/20220124151949_remove_projects_ci_runner_projects_project_id_fk.rb b/db/post_migrate/20220124151949_remove_projects_ci_runner_projects_project_id_fk.rb new file mode 100644 index 00000000000..de8feb96a06 --- /dev/null +++ b/db/post_migrate/20220124151949_remove_projects_ci_runner_projects_project_id_fk.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class RemoveProjectsCiRunnerProjectsProjectIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + return unless foreign_key_exists?(:ci_runner_projects, :projects, name: "fk_4478a6f1e4") + + with_lock_retries do + execute('LOCK projects, ci_runner_projects IN ACCESS EXCLUSIVE MODE') if transaction_open? + + remove_foreign_key_if_exists(:ci_runner_projects, :projects, name: "fk_4478a6f1e4") + end + end + + def down + add_concurrent_foreign_key(:ci_runner_projects, :projects, name: "fk_4478a6f1e4", column: :project_id, target_column: :id, on_delete: :cascade) + end +end diff --git a/db/post_migrate/20220124152824_remove_projects_ci_subscriptions_projects_downstream_project_id_fk.rb b/db/post_migrate/20220124152824_remove_projects_ci_subscriptions_projects_downstream_project_id_fk.rb new file mode 100644 index 00000000000..8596b1f14ba --- /dev/null +++ b/db/post_migrate/20220124152824_remove_projects_ci_subscriptions_projects_downstream_project_id_fk.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class RemoveProjectsCiSubscriptionsProjectsDownstreamProjectIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + return unless foreign_key_exists?(:ci_subscriptions_projects, :projects, name: "fk_rails_0818751483") + + with_lock_retries do + execute('LOCK projects, ci_subscriptions_projects IN ACCESS EXCLUSIVE MODE') if transaction_open? + + remove_foreign_key_if_exists(:ci_subscriptions_projects, :projects, name: "fk_rails_0818751483") + end + end + + def down + add_concurrent_foreign_key(:ci_subscriptions_projects, :projects, name: "fk_rails_0818751483", column: :downstream_project_id, target_column: :id, on_delete: :cascade) + end +end diff --git a/db/post_migrate/20220124153233_remove_projects_ci_job_artifacts_project_id_fk.rb b/db/post_migrate/20220124153233_remove_projects_ci_job_artifacts_project_id_fk.rb new file mode 100644 index 00000000000..1948a78916d --- /dev/null +++ b/db/post_migrate/20220124153233_remove_projects_ci_job_artifacts_project_id_fk.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +class RemoveProjectsCiJobArtifactsProjectIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + return if Gitlab.com? # unsafe migration, skip on GitLab.com due to https://gitlab.com/groups/gitlab-org/-/epics/7249#note_819625526 + return unless foreign_key_exists?(:ci_job_artifacts, :projects, name: "fk_rails_9862d392f9") + + with_lock_retries do + execute('LOCK projects, ci_job_artifacts IN ACCESS EXCLUSIVE MODE') if transaction_open? + + remove_foreign_key_if_exists(:ci_job_artifacts, :projects, name: "fk_rails_9862d392f9") + end + end + + def down + add_concurrent_foreign_key(:ci_job_artifacts, :projects, name: "fk_rails_9862d392f9", column: :project_id, target_column: :id, on_delete: :cascade) + end +end diff --git a/db/post_migrate/20220124153234_re_remove_projects_ci_job_artifacts_project_id_fk.rb b/db/post_migrate/20220124153234_re_remove_projects_ci_job_artifacts_project_id_fk.rb new file mode 100644 index 00000000000..bb59134b927 --- /dev/null +++ b/db/post_migrate/20220124153234_re_remove_projects_ci_job_artifacts_project_id_fk.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class ReRemoveProjectsCiJobArtifactsProjectIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + return unless foreign_key_exists?(:ci_job_artifacts, :projects, name: "fk_rails_9862d392f9") + + with_lock_retries do + execute('LOCK projects, ci_job_artifacts IN ACCESS EXCLUSIVE MODE') if transaction_open? + + remove_foreign_key_if_exists(:ci_job_artifacts, :projects, name: "fk_rails_9862d392f9") + end + end + + def down + # no-op, since the FK will be added via rollback by prior-migration + end +end diff --git a/db/post_migrate/20220124180704_remove_projects_ci_builds_metadata_project_id_fk.rb b/db/post_migrate/20220124180704_remove_projects_ci_builds_metadata_project_id_fk.rb new file mode 100644 index 00000000000..fc9dc2cc6dc --- /dev/null +++ b/db/post_migrate/20220124180704_remove_projects_ci_builds_metadata_project_id_fk.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +class RemoveProjectsCiBuildsMetadataProjectIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + return if Gitlab.com? # unsafe migration, skip on GitLab.com due to https://gitlab.com/groups/gitlab-org/-/epics/7249#note_819625526 + return unless foreign_key_exists?(:ci_builds_metadata, :projects, name: "fk_rails_ffcf702a02") + + with_lock_retries do + execute('LOCK projects, ci_builds_metadata IN ACCESS EXCLUSIVE MODE') if transaction_open? + + remove_foreign_key_if_exists(:ci_builds_metadata, :projects, name: "fk_rails_ffcf702a02") + end + end + + def down + add_concurrent_foreign_key(:ci_builds_metadata, :projects, name: "fk_rails_ffcf702a02", column: :project_id, target_column: :id, on_delete: :cascade) + end +end diff --git a/db/post_migrate/20220124180705_re_remove_projects_ci_builds_metadata_project_id_fk.rb b/db/post_migrate/20220124180705_re_remove_projects_ci_builds_metadata_project_id_fk.rb new file mode 100644 index 00000000000..6483b8e0643 --- /dev/null +++ b/db/post_migrate/20220124180705_re_remove_projects_ci_builds_metadata_project_id_fk.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class ReRemoveProjectsCiBuildsMetadataProjectIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + return unless foreign_key_exists?(:ci_builds_metadata, :projects, name: "fk_rails_ffcf702a02") + + with_lock_retries do + execute('LOCK projects, ci_builds_metadata IN ACCESS EXCLUSIVE MODE') if transaction_open? + + remove_foreign_key_if_exists(:ci_builds_metadata, :projects, name: "fk_rails_ffcf702a02") + end + end + + def down + # no-op, since the FK will be added via rollback by prior-migration + end +end diff --git a/db/post_migrate/20220124184338_remove_projects_ci_subscriptions_projects_upstream_project_id_fk.rb b/db/post_migrate/20220124184338_remove_projects_ci_subscriptions_projects_upstream_project_id_fk.rb new file mode 100644 index 00000000000..50448f4a884 --- /dev/null +++ b/db/post_migrate/20220124184338_remove_projects_ci_subscriptions_projects_upstream_project_id_fk.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class RemoveProjectsCiSubscriptionsProjectsUpstreamProjectIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + return unless foreign_key_exists?(:ci_subscriptions_projects, :projects, name: "fk_rails_7871f9a97b") + + with_lock_retries do + execute('LOCK projects, ci_subscriptions_projects IN ACCESS EXCLUSIVE MODE') if transaction_open? + + remove_foreign_key_if_exists(:ci_subscriptions_projects, :projects, name: "fk_rails_7871f9a97b") + end + end + + def down + add_concurrent_foreign_key(:ci_subscriptions_projects, :projects, name: "fk_rails_7871f9a97b", column: :upstream_project_id, target_column: :id, on_delete: :cascade) + end +end diff --git a/db/post_migrate/20220124204046_remove_projects_ci_sources_pipelines_project_id_fk.rb b/db/post_migrate/20220124204046_remove_projects_ci_sources_pipelines_project_id_fk.rb new file mode 100644 index 00000000000..e15d337045e --- /dev/null +++ b/db/post_migrate/20220124204046_remove_projects_ci_sources_pipelines_project_id_fk.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class RemoveProjectsCiSourcesPipelinesProjectIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + return unless foreign_key_exists?(:ci_sources_pipelines, :projects, name: "fk_1e53c97c0a") + + with_lock_retries do + execute('LOCK projects, ci_sources_pipelines IN ACCESS EXCLUSIVE MODE') if transaction_open? + + remove_foreign_key_if_exists(:ci_sources_pipelines, :projects, name: "fk_1e53c97c0a") + end + end + + def down + add_concurrent_foreign_key(:ci_sources_pipelines, :projects, name: "fk_1e53c97c0a", column: :project_id, target_column: :id, on_delete: :cascade) + end +end diff --git a/db/post_migrate/20220124214131_remove_projects_ci_refs_project_id_fk.rb b/db/post_migrate/20220124214131_remove_projects_ci_refs_project_id_fk.rb new file mode 100644 index 00000000000..f90d477673a --- /dev/null +++ b/db/post_migrate/20220124214131_remove_projects_ci_refs_project_id_fk.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class RemoveProjectsCiRefsProjectIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + return unless foreign_key_exists?(:ci_refs, :projects, name: "fk_rails_4249db8cc3") + + with_lock_retries do + execute('LOCK projects, ci_refs IN ACCESS EXCLUSIVE MODE') if transaction_open? + + remove_foreign_key_if_exists(:ci_refs, :projects, name: "fk_rails_4249db8cc3") + end + end + + def down + add_concurrent_foreign_key(:ci_refs, :projects, name: "fk_rails_4249db8cc3", column: :project_id, target_column: :id, on_delete: :cascade) + end +end diff --git a/db/post_migrate/20220124215857_remove_projects_ci_job_token_project_scope_links_source_project_id_fk.rb b/db/post_migrate/20220124215857_remove_projects_ci_job_token_project_scope_links_source_project_id_fk.rb new file mode 100644 index 00000000000..8bfe9586a50 --- /dev/null +++ b/db/post_migrate/20220124215857_remove_projects_ci_job_token_project_scope_links_source_project_id_fk.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class RemoveProjectsCiJobTokenProjectScopeLinksSourceProjectIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + return unless foreign_key_exists?(:ci_job_token_project_scope_links, :projects, name: "fk_rails_4b2ee3290b") + + with_lock_retries do + execute('LOCK projects, ci_job_token_project_scope_links IN ACCESS EXCLUSIVE MODE') if transaction_open? + + remove_foreign_key_if_exists(:ci_job_token_project_scope_links, :projects, name: "fk_rails_4b2ee3290b") + end + end + + def down + add_concurrent_foreign_key(:ci_job_token_project_scope_links, :projects, name: "fk_rails_4b2ee3290b", column: :source_project_id, target_column: :id, on_delete: :cascade) + end +end diff --git a/db/post_migrate/20220124221521_remove_projects_ci_project_monthly_usages_project_id_fk.rb b/db/post_migrate/20220124221521_remove_projects_ci_project_monthly_usages_project_id_fk.rb new file mode 100644 index 00000000000..cff78342d21 --- /dev/null +++ b/db/post_migrate/20220124221521_remove_projects_ci_project_monthly_usages_project_id_fk.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class RemoveProjectsCiProjectMonthlyUsagesProjectIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + return unless foreign_key_exists?(:ci_project_monthly_usages, :projects, name: "fk_rails_508bcd4aa6") + + with_lock_retries do + execute('LOCK projects, ci_project_monthly_usages IN ACCESS EXCLUSIVE MODE') if transaction_open? + + remove_foreign_key_if_exists(:ci_project_monthly_usages, :projects, name: "fk_rails_508bcd4aa6") + end + end + + def down + add_concurrent_foreign_key(:ci_project_monthly_usages, :projects, name: "fk_rails_508bcd4aa6", column: :project_id, target_column: :id, on_delete: :cascade) + end +end diff --git a/db/post_migrate/20220125083520_remove_ci_pipelines_dast_site_profiles_pipelines_ci_pipeline_id_fk.rb b/db/post_migrate/20220125083520_remove_ci_pipelines_dast_site_profiles_pipelines_ci_pipeline_id_fk.rb new file mode 100644 index 00000000000..b111b95fccf --- /dev/null +++ b/db/post_migrate/20220125083520_remove_ci_pipelines_dast_site_profiles_pipelines_ci_pipeline_id_fk.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class RemoveCiPipelinesDastSiteProfilesPipelinesCiPipelineIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + return unless foreign_key_exists?(:dast_site_profiles_pipelines, :ci_pipelines, name: "fk_53849b0ad5") + + with_lock_retries do + execute('LOCK ci_pipelines, dast_site_profiles_pipelines IN ACCESS EXCLUSIVE MODE') if transaction_open? + + remove_foreign_key_if_exists(:dast_site_profiles_pipelines, :ci_pipelines, name: "fk_53849b0ad5") + end + end + + def down + add_concurrent_foreign_key(:dast_site_profiles_pipelines, :ci_pipelines, name: "fk_53849b0ad5", column: :ci_pipeline_id, target_column: :id, on_delete: :cascade) + end +end diff --git a/db/post_migrate/20220125084348_remove_ci_pipelines_vulnerability_feedback_pipeline_id_fk.rb b/db/post_migrate/20220125084348_remove_ci_pipelines_vulnerability_feedback_pipeline_id_fk.rb new file mode 100644 index 00000000000..1b932d084bb --- /dev/null +++ b/db/post_migrate/20220125084348_remove_ci_pipelines_vulnerability_feedback_pipeline_id_fk.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class RemoveCiPipelinesVulnerabilityFeedbackPipelineIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + return unless foreign_key_exists?(:vulnerability_feedback, :ci_pipelines, name: "fk_rails_20976e6fd9") + + with_lock_retries do + execute('LOCK ci_pipelines, vulnerability_feedback IN ACCESS EXCLUSIVE MODE') if transaction_open? + + remove_foreign_key_if_exists(:vulnerability_feedback, :ci_pipelines, name: "fk_rails_20976e6fd9") + end + end + + def down + add_concurrent_foreign_key(:vulnerability_feedback, :ci_pipelines, name: "fk_rails_20976e6fd9", column: :pipeline_id, target_column: :id, on_delete: :nullify) + end +end diff --git a/db/post_migrate/20220125122640_schedule_populate_topics_non_private_projects_count.rb b/db/post_migrate/20220125122640_schedule_populate_topics_non_private_projects_count.rb new file mode 100644 index 00000000000..dee86a70553 --- /dev/null +++ b/db/post_migrate/20220125122640_schedule_populate_topics_non_private_projects_count.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +class SchedulePopulateTopicsNonPrivateProjectsCount < Gitlab::Database::Migration[1.0] + MIGRATION = 'PopulateTopicsNonPrivateProjectsCount' + BATCH_SIZE = 10_000 + DELAY_INTERVAL = 2.minutes + + disable_ddl_transaction! + + def up + queue_background_migration_jobs_by_range_at_intervals( + define_batchable_model('topics'), + MIGRATION, + DELAY_INTERVAL, + batch_size: BATCH_SIZE, + track_jobs: true + ) + end + + def down + # no-op + end +end diff --git a/db/post_migrate/20220126201752_remove_projects_ci_job_token_project_scope_links_target_project_id_fk.rb b/db/post_migrate/20220126201752_remove_projects_ci_job_token_project_scope_links_target_project_id_fk.rb new file mode 100644 index 00000000000..a33e02f2408 --- /dev/null +++ b/db/post_migrate/20220126201752_remove_projects_ci_job_token_project_scope_links_target_project_id_fk.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class RemoveProjectsCiJobTokenProjectScopeLinksTargetProjectIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + return unless foreign_key_exists?(:ci_job_token_project_scope_links, :projects, name: "fk_rails_6904b38465") + + with_lock_retries do + execute('LOCK projects, ci_job_token_project_scope_links IN ACCESS EXCLUSIVE MODE') if transaction_open? + + remove_foreign_key_if_exists(:ci_job_token_project_scope_links, :projects, name: "fk_rails_6904b38465") + end + end + + def down + add_concurrent_foreign_key(:ci_job_token_project_scope_links, :projects, name: "fk_rails_6904b38465", column: :target_project_id, target_column: :id, on_delete: :cascade) + end +end diff --git a/db/post_migrate/20220126202654_remove_projects_ci_sources_projects_source_project_id_fk.rb b/db/post_migrate/20220126202654_remove_projects_ci_sources_projects_source_project_id_fk.rb new file mode 100644 index 00000000000..a69cd43b921 --- /dev/null +++ b/db/post_migrate/20220126202654_remove_projects_ci_sources_projects_source_project_id_fk.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class RemoveProjectsCiSourcesProjectsSourceProjectIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + return unless foreign_key_exists?(:ci_sources_projects, :projects, name: "fk_rails_64b6855cbc") + + with_lock_retries do + execute('LOCK projects, ci_sources_projects IN ACCESS EXCLUSIVE MODE') if transaction_open? + + remove_foreign_key_if_exists(:ci_sources_projects, :projects, name: "fk_rails_64b6855cbc") + end + end + + def down + add_concurrent_foreign_key(:ci_sources_projects, :projects, name: "fk_rails_64b6855cbc", column: :source_project_id, target_column: :id, on_delete: :cascade) + end +end diff --git a/db/post_migrate/20220126203421_remove_projects_ci_pipeline_schedules_project_id_fk.rb b/db/post_migrate/20220126203421_remove_projects_ci_pipeline_schedules_project_id_fk.rb new file mode 100644 index 00000000000..870127ab168 --- /dev/null +++ b/db/post_migrate/20220126203421_remove_projects_ci_pipeline_schedules_project_id_fk.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class RemoveProjectsCiPipelineSchedulesProjectIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + return unless foreign_key_exists?(:ci_pipeline_schedules, :projects, name: "fk_8ead60fcc4") + + with_lock_retries do + execute('LOCK projects, ci_pipeline_schedules IN ACCESS EXCLUSIVE MODE') if transaction_open? + + remove_foreign_key_if_exists(:ci_pipeline_schedules, :projects, name: "fk_8ead60fcc4") + end + end + + def down + add_concurrent_foreign_key(:ci_pipeline_schedules, :projects, name: "fk_8ead60fcc4", column: :project_id, target_column: :id, on_delete: :cascade) + end +end diff --git a/db/post_migrate/20220126210021_remove_projects_ci_builds_project_id_fk.rb b/db/post_migrate/20220126210021_remove_projects_ci_builds_project_id_fk.rb new file mode 100644 index 00000000000..8caa6db7507 --- /dev/null +++ b/db/post_migrate/20220126210021_remove_projects_ci_builds_project_id_fk.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +class RemoveProjectsCiBuildsProjectIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + return if Gitlab.com? # unsafe migration, skip on GitLab.com due to https://gitlab.com/groups/gitlab-org/-/epics/7249#note_819625526 + return unless foreign_key_exists?(:ci_builds, :projects, name: "fk_befce0568a") + + with_lock_retries do + execute('LOCK projects, ci_builds IN ACCESS EXCLUSIVE MODE') if transaction_open? + + remove_foreign_key_if_exists(:ci_builds, :projects, name: "fk_befce0568a") + end + end + + def down + add_concurrent_foreign_key(:ci_builds, :projects, name: "fk_befce0568a", column: :project_id, target_column: :id, on_delete: :cascade) + end +end diff --git a/db/post_migrate/20220126210022_re_remove_projects_ci_builds_project_id_fk.rb b/db/post_migrate/20220126210022_re_remove_projects_ci_builds_project_id_fk.rb new file mode 100644 index 00000000000..2a026388bbf --- /dev/null +++ b/db/post_migrate/20220126210022_re_remove_projects_ci_builds_project_id_fk.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class ReRemoveProjectsCiBuildsProjectIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + return unless foreign_key_exists?(:ci_builds, :projects, name: "fk_befce0568a") + + with_lock_retries do + execute('LOCK projects, ci_builds IN ACCESS EXCLUSIVE MODE') if transaction_open? + + remove_foreign_key_if_exists(:ci_builds, :projects, name: "fk_befce0568a") + end + end + + def down + # no-op, since the FK will be added via rollback by prior-migration + end +end diff --git a/db/post_migrate/20220126210657_remove_projects_ci_pipelines_project_id_fk.rb b/db/post_migrate/20220126210657_remove_projects_ci_pipelines_project_id_fk.rb new file mode 100644 index 00000000000..6867cf720f3 --- /dev/null +++ b/db/post_migrate/20220126210657_remove_projects_ci_pipelines_project_id_fk.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class RemoveProjectsCiPipelinesProjectIdFk < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + def up + return unless foreign_key_exists?(:ci_pipelines, :projects, name: "fk_86635dbd80") + + with_lock_retries do + execute('LOCK projects, ci_pipelines IN ACCESS EXCLUSIVE MODE') if transaction_open? + + remove_foreign_key_if_exists(:ci_pipelines, :projects, name: "fk_86635dbd80") + end + end + + def down + add_concurrent_foreign_key(:ci_pipelines, :projects, name: "fk_86635dbd80", column: :project_id, target_column: :id, on_delete: :cascade) + end +end diff --git a/db/post_migrate/20220127112243_add_index_to_merge_request_assignees_state.rb b/db/post_migrate/20220127112243_add_index_to_merge_request_assignees_state.rb new file mode 100644 index 00000000000..e6bb43af760 --- /dev/null +++ b/db/post_migrate/20220127112243_add_index_to_merge_request_assignees_state.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class AddIndexToMergeRequestAssigneesState < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + INDEX_NAME = 'index_on_merge_request_assignees_state' + + def up + add_concurrent_index :merge_request_assignees, :state, where: 'state = 2', name: INDEX_NAME + end + + def down + remove_concurrent_index_by_name :merge_request_assignees, INDEX_NAME + end +end diff --git a/db/post_migrate/20220127112412_add_index_to_merge_request_reviewers_state.rb b/db/post_migrate/20220127112412_add_index_to_merge_request_reviewers_state.rb new file mode 100644 index 00000000000..13f4e05c15b --- /dev/null +++ b/db/post_migrate/20220127112412_add_index_to_merge_request_reviewers_state.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class AddIndexToMergeRequestReviewersState < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + INDEX_NAME = 'index_on_merge_request_reviewers_state' + + def up + add_concurrent_index :merge_request_reviewers, :state, where: 'state = 2', name: INDEX_NAME + end + + def down + remove_concurrent_index_by_name :merge_request_reviewers, INDEX_NAME + end +end diff --git a/db/post_migrate/20220127132200_cleanup_backfill_ci_namespace_mirrors.rb b/db/post_migrate/20220127132200_cleanup_backfill_ci_namespace_mirrors.rb new file mode 100644 index 00000000000..65ec2f9a282 --- /dev/null +++ b/db/post_migrate/20220127132200_cleanup_backfill_ci_namespace_mirrors.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class CleanupBackfillCiNamespaceMirrors < Gitlab::Database::Migration[1.0] + MIGRATION = 'BackfillCiNamespaceMirrors' + + disable_ddl_transaction! + + def up + finalize_background_migration(MIGRATION) + end + + def down + # no-op + end +end diff --git a/db/post_migrate/20220127132201_cleanup_backfill_ci_project_mirrors.rb b/db/post_migrate/20220127132201_cleanup_backfill_ci_project_mirrors.rb new file mode 100644 index 00000000000..8c7d9a945ba --- /dev/null +++ b/db/post_migrate/20220127132201_cleanup_backfill_ci_project_mirrors.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class CleanupBackfillCiProjectMirrors < Gitlab::Database::Migration[1.0] + MIGRATION = 'BackfillCiProjectMirrors' + + disable_ddl_transaction! + + def up + finalize_background_migration(MIGRATION) + end + + def down + # no-op + end +end diff --git a/db/post_migrate/20220128155251_remove_dangling_running_builds.rb b/db/post_migrate/20220128155251_remove_dangling_running_builds.rb new file mode 100644 index 00000000000..f86a21ced00 --- /dev/null +++ b/db/post_migrate/20220128155251_remove_dangling_running_builds.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +class RemoveDanglingRunningBuilds < Gitlab::Database::Migration[1.0] + BATCH_SIZE = 100 + + disable_ddl_transaction! + + def up + each_batch_range('ci_running_builds', of: BATCH_SIZE) do |min, max| + execute <<~SQL + DELETE FROM ci_running_builds + USING ci_builds + WHERE ci_builds.id = ci_running_builds.build_id + AND ci_builds.status = 'failed' + AND ci_builds.type = 'Ci::Build' + AND ci_running_builds.id BETWEEN #{min} AND #{max} + SQL + end + end + + def down + # no-op + # This migration deletes data and it can not be reversed + end +end diff --git a/db/post_migrate/20220128155814_fix_approval_rules_code_owners_rule_type_index.rb b/db/post_migrate/20220128155814_fix_approval_rules_code_owners_rule_type_index.rb new file mode 100644 index 00000000000..eccfab25126 --- /dev/null +++ b/db/post_migrate/20220128155814_fix_approval_rules_code_owners_rule_type_index.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +class FixApprovalRulesCodeOwnersRuleTypeIndex < Gitlab::Database::Migration[1.0] + INDEX_NAME = 'index_approval_rules_code_owners_rule_type' + OLD_INDEX_NAME = 'index_approval_rules_code_owners_rule_type_old' + TABLE = :approval_merge_request_rules + COLUMN = :merge_request_id + WHERE_CONDITION = 'rule_type = 2' + + disable_ddl_transaction! + + def up + rename_index TABLE, INDEX_NAME, OLD_INDEX_NAME if index_exists_by_name?(TABLE, INDEX_NAME) && !index_exists_by_name?(TABLE, OLD_INDEX_NAME) + + add_concurrent_index TABLE, COLUMN, where: WHERE_CONDITION, name: INDEX_NAME + + remove_concurrent_index_by_name TABLE, OLD_INDEX_NAME + end + + def down + # No-op + end +end diff --git a/db/post_migrate/20220201034731_remove_index_clusters_kubernetes_namespaces_on_cluster_id.rb b/db/post_migrate/20220201034731_remove_index_clusters_kubernetes_namespaces_on_cluster_id.rb new file mode 100644 index 00000000000..0e2ef3b3324 --- /dev/null +++ b/db/post_migrate/20220201034731_remove_index_clusters_kubernetes_namespaces_on_cluster_id.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class RemoveIndexClustersKubernetesNamespacesOnClusterId < Gitlab::Database::Migration[1.0] + INDEX = 'index_clusters_kubernetes_namespaces_on_cluster_id' + + disable_ddl_transaction! + + def up + remove_concurrent_index_by_name :clusters_kubernetes_namespaces, INDEX + end + + def down + add_concurrent_index :clusters_kubernetes_namespaces, :cluster_id, name: INDEX + end +end diff --git a/db/post_migrate/20220201141705_cleanup_background_migration_populate_test_reports_issue_id.rb b/db/post_migrate/20220201141705_cleanup_background_migration_populate_test_reports_issue_id.rb new file mode 100644 index 00000000000..252b4a01d04 --- /dev/null +++ b/db/post_migrate/20220201141705_cleanup_background_migration_populate_test_reports_issue_id.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class CleanupBackgroundMigrationPopulateTestReportsIssueId < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + MIGRATION = 'PopulateTestReportsIssueId' + + def up + finalize_background_migration(MIGRATION) + end + + def down + # no-op + end +end diff --git a/db/post_migrate/20220201173212_add_user_details_provisioning_index.rb b/db/post_migrate/20220201173212_add_user_details_provisioning_index.rb new file mode 100644 index 00000000000..a864ec7e395 --- /dev/null +++ b/db/post_migrate/20220201173212_add_user_details_provisioning_index.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +class AddUserDetailsProvisioningIndex < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + INDEX_NAME = 'idx_user_details_on_provisioned_by_group_id_user_id' + OLD_INDEX_NAME = 'index_user_details_on_provisioned_by_group_id' + + def up + add_concurrent_index :user_details, [:provisioned_by_group_id, :user_id], name: INDEX_NAME + remove_concurrent_index_by_name :user_details, OLD_INDEX_NAME + end + + def down + add_concurrent_index :user_details, :provisioned_by_group_id, name: OLD_INDEX_NAME + remove_concurrent_index_by_name :user_details, INDEX_NAME + end +end diff --git a/db/post_migrate/20220201193033_add_unique_index_to_vulnerability_finding_links_with_truncate.rb b/db/post_migrate/20220201193033_add_unique_index_to_vulnerability_finding_links_with_truncate.rb new file mode 100644 index 00000000000..cc9dabdf624 --- /dev/null +++ b/db/post_migrate/20220201193033_add_unique_index_to_vulnerability_finding_links_with_truncate.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +class AddUniqueIndexToVulnerabilityFindingLinksWithTruncate < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + NAME_URL_INDEX_NAME = 'finding_link_name_url_idx' + URL_INDEX_NAME = 'finding_link_url_idx' + + def up + execute('TRUNCATE TABLE vulnerability_finding_links') + + add_concurrent_index :vulnerability_finding_links, [:vulnerability_occurrence_id, :name, :url], unique: true, name: NAME_URL_INDEX_NAME + add_concurrent_index :vulnerability_finding_links, [:vulnerability_occurrence_id, :url], unique: true, where: 'name is null', name: URL_INDEX_NAME + end + + def down + remove_concurrent_index :vulnerability_finding_links, [:vulnerability_occurrence_id, :name, :url], name: NAME_URL_INDEX_NAME + remove_concurrent_index :vulnerability_finding_links, [:vulnerability_occurrence_id, :url], name: URL_INDEX_NAME + end +end diff --git a/db/post_migrate/20220202105733_delete_service_template_records.rb b/db/post_migrate/20220202105733_delete_service_template_records.rb new file mode 100644 index 00000000000..b70bacc83aa --- /dev/null +++ b/db/post_migrate/20220202105733_delete_service_template_records.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class DeleteServiceTemplateRecords < Gitlab::Database::Migration[1.0] + class Integration < ActiveRecord::Base + # Disable single-table inheritance + self.inheritance_column = :_type_disabled + end + def up + Integration.where(template: true).delete_all + end + + def down + # no-op + end +end diff --git a/db/post_migrate/20220204053655_remove_index_epic_issues_on_epic_id.rb b/db/post_migrate/20220204053655_remove_index_epic_issues_on_epic_id.rb new file mode 100644 index 00000000000..7815829d36a --- /dev/null +++ b/db/post_migrate/20220204053655_remove_index_epic_issues_on_epic_id.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class RemoveIndexEpicIssuesOnEpicId < Gitlab::Database::Migration[1.0] + INDEX = 'index_epic_issues_on_epic_id' + + disable_ddl_transaction! + + def up + remove_concurrent_index_by_name :epic_issues, name: INDEX + end + + def down + add_concurrent_index :epic_issues, :epic_id, name: INDEX + end +end diff --git a/db/post_migrate/20220208115439_start_backfill_ci_queuing_tables.rb b/db/post_migrate/20220208115439_start_backfill_ci_queuing_tables.rb new file mode 100644 index 00000000000..82e36174290 --- /dev/null +++ b/db/post_migrate/20220208115439_start_backfill_ci_queuing_tables.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +class StartBackfillCiQueuingTables < Gitlab::Database::Migration[1.0] + MIGRATION = 'BackfillCiQueuingTables' + BATCH_SIZE = 500 + DELAY_INTERVAL = 2.minutes + + disable_ddl_transaction! + + def up + return if Gitlab.com? + + queue_background_migration_jobs_by_range_at_intervals( + Gitlab::BackgroundMigration::BackfillCiQueuingTables::Ci::Build.pending, + MIGRATION, + DELAY_INTERVAL, + batch_size: BATCH_SIZE, + track_jobs: true) + end + + def down + # no-op + end +end diff --git a/db/post_migrate/20220209111007_add_partial_index_for_batching_active_cluster_image_scanning_vulnerabilities.rb b/db/post_migrate/20220209111007_add_partial_index_for_batching_active_cluster_image_scanning_vulnerabilities.rb new file mode 100644 index 00000000000..d38509ed8c2 --- /dev/null +++ b/db/post_migrate/20220209111007_add_partial_index_for_batching_active_cluster_image_scanning_vulnerabilities.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +class AddPartialIndexForBatchingActiveClusterImageScanningVulnerabilities < Gitlab::Database::Migration[1.0] + INDEX_NAME = 'index_vulnerabilities_on_project_id_and_id_active_cis' + INDEX_FILTER_CONDITION = 'report_type = 7 AND state = ANY(ARRAY[1, 4])' + + disable_ddl_transaction! + + def up + add_concurrent_index :vulnerabilities, [:project_id, :id], where: INDEX_FILTER_CONDITION, name: INDEX_NAME + end + + def down + remove_concurrent_index :vulnerabilities, [:project_id, :id], name: INDEX_NAME + end +end diff --git a/db/post_migrate/20220212120735_schedule_fix_incorrect_max_seats_used2.rb b/db/post_migrate/20220212120735_schedule_fix_incorrect_max_seats_used2.rb new file mode 100644 index 00000000000..c8a6bd0a15d --- /dev/null +++ b/db/post_migrate/20220212120735_schedule_fix_incorrect_max_seats_used2.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +class ScheduleFixIncorrectMaxSeatsUsed2 < Gitlab::Database::Migration[1.0] + MIGRATION = 'FixIncorrectMaxSeatsUsed' + TMP_IDX_NAME = 'tmp_gitlab_subscriptions_max_seats_used_migration_2' + + disable_ddl_transaction! + + def up + add_concurrent_index :gitlab_subscriptions, :id, where: "start_date < '2021-08-02' AND max_seats_used != 0 AND max_seats_used > seats_in_use AND max_seats_used > seats", name: TMP_IDX_NAME + + return unless Gitlab.com? + + migrate_in(1.hour, MIGRATION, ['batch_2_for_start_date_before_02_aug_2021']) + end + + def down + remove_concurrent_index_by_name :gitlab_subscriptions, TMP_IDX_NAME + end +end diff --git a/db/post_migrate/20220213104531_create_indexes_on_integration_type_new.rb b/db/post_migrate/20220213104531_create_indexes_on_integration_type_new.rb new file mode 100644 index 00000000000..3a9f48dec44 --- /dev/null +++ b/db/post_migrate/20220213104531_create_indexes_on_integration_type_new.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +# Reproduce the indices on integrations.type on integrations.type_new +class CreateIndexesOnIntegrationTypeNew < Gitlab::Database::Migration[1.0] + disable_ddl_transaction! + + TABLE_NAME = :integrations + COLUMN = :type_new + + def indices + [ + { + name: "index_integrations_on_project_and_#{COLUMN}_where_inherit_null", + columns: [:project_id, COLUMN], + where: 'inherit_from_id IS NULL' + }, + { + name: "index_integrations_on_project_id_and_#{COLUMN}_unique", + columns: [:project_id, COLUMN], + unique: true + }, + { + name: "index_integrations_on_#{COLUMN}", + columns: [COLUMN] + }, + { + name: "index_integrations_on_#{COLUMN}_and_instance_partial", + columns: [COLUMN, :instance], + where: 'instance = true' + }, + { + name: "index_integrations_on_#{COLUMN}_and_template_partial", + columns: [COLUMN, :template], + where: 'template = true' + }, + { + # column names are limited to 63 characters, so this one is re-worded for clarity + name: "index_integrations_on_#{COLUMN}_id_when_active_and_has_project", + columns: [COLUMN, :id], + where: '((active = true) AND (project_id IS NOT NULL))' + }, + { + name: "index_integrations_on_unique_group_id_and_#{COLUMN}", + columns: [:group_id, COLUMN] + } + ] + end + + def up + indices.each do |index| + add_concurrent_index TABLE_NAME, index[:columns], index.except(:columns) + end + end + + def down + indices.each do |index| + remove_concurrent_index_by_name TABLE_NAME, index[:name] + end + end +end |