diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-04-21 02:50:22 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-04-21 02:50:22 +0300 |
commit | 9dc93a4519d9d5d7be48ff274127136236a3adb3 (patch) | |
tree | 70467ae3692a0e35e5ea56bcb803eb512a10bedb /db/post_migrate | |
parent | 4b0f34b6d759d6299322b3a54453e930c6121ff0 (diff) |
Add latest changes from gitlab-org/gitlab@13-11-stable-eev13.11.0-rc43
Diffstat (limited to 'db/post_migrate')
45 files changed, 881 insertions, 31 deletions
diff --git a/db/post_migrate/20190214112022_schedule_sync_issuables_state_id.rb b/db/post_migrate/20190214112022_schedule_sync_issuables_state_id.rb index 34536e22bbf..de062937fbe 100644 --- a/db/post_migrate/20190214112022_schedule_sync_issuables_state_id.rb +++ b/db/post_migrate/20190214112022_schedule_sync_issuables_state_id.rb @@ -19,8 +19,8 @@ class ScheduleSyncIssuablesStateId < ActiveRecord::Migration[5.0] # BATCH_SIZE = 5000 DELAY_INTERVAL = 120.seconds.to_i - ISSUES_MIGRATION = 'SyncIssuesStateId'.freeze - MERGE_REQUESTS_MIGRATION = 'SyncMergeRequestsStateId'.freeze + ISSUES_MIGRATION = 'SyncIssuesStateId' + MERGE_REQUESTS_MIGRATION = 'SyncMergeRequestsStateId' disable_ddl_transaction! diff --git a/db/post_migrate/20191105094625_set_report_type_for_vulnerabilities.rb b/db/post_migrate/20191105094625_set_report_type_for_vulnerabilities.rb index 5a8529c24d7..40e9e3bddc8 100644 --- a/db/post_migrate/20191105094625_set_report_type_for_vulnerabilities.rb +++ b/db/post_migrate/20191105094625_set_report_type_for_vulnerabilities.rb @@ -7,7 +7,7 @@ class SetReportTypeForVulnerabilities < ActiveRecord::Migration[5.2] # set report_type based on vulnerability_occurrences from which the vulnerabilities were promoted, # that is, first vulnerability_occurrences among those having the same vulnerability_id execute <<~SQL - WITH first_findings_for_vulnerabilities AS ( + WITH first_findings_for_vulnerabilities AS #{Gitlab::Database::AsWithMaterialized.materialized_if_supported} ( SELECT MIN(id) AS id, vulnerability_id FROM vulnerability_occurrences WHERE vulnerability_id IS NOT NULL diff --git a/db/post_migrate/20191114173624_set_resolved_state_on_vulnerabilities.rb b/db/post_migrate/20191114173624_set_resolved_state_on_vulnerabilities.rb index b28aecdc0a3..2900ef852a5 100644 --- a/db/post_migrate/20191114173624_set_resolved_state_on_vulnerabilities.rb +++ b/db/post_migrate/20191114173624_set_resolved_state_on_vulnerabilities.rb @@ -6,7 +6,7 @@ class SetResolvedStateOnVulnerabilities < ActiveRecord::Migration[5.2] def up execute <<~SQL -- selecting IDs for all non-orphan Findings that either have no feedback or it's a non-dismissal feedback - WITH resolved_vulnerability_ids AS ( + WITH resolved_vulnerability_ids AS #{Gitlab::Database::AsWithMaterialized.materialized_if_supported} ( SELECT DISTINCT vulnerability_id AS id FROM vulnerability_occurrences LEFT JOIN vulnerability_feedback ON vulnerability_feedback.project_fingerprint = ENCODE(vulnerability_occurrences.project_fingerprint::bytea, 'HEX') diff --git a/db/post_migrate/20200305082754_remove_duplicate_labels_from_project.rb b/db/post_migrate/20200305082754_remove_duplicate_labels_from_project.rb index 33f8118534d..4bb43da43bb 100644 --- a/db/post_migrate/20200305082754_remove_duplicate_labels_from_project.rb +++ b/db/post_migrate/20200305082754_remove_duplicate_labels_from_project.rb @@ -55,7 +55,7 @@ class RemoveDuplicateLabelsFromProject < ActiveRecord::Migration[6.0] # project_id title template description type color duplicate_labels = ApplicationRecord.connection.execute(<<-SQL.squish) -WITH data AS ( +WITH data AS #{Gitlab::Database::AsWithMaterialized.materialized_if_supported} ( SELECT labels.*, row_number() OVER (PARTITION BY labels.project_id, labels.title, labels.template, labels.description, labels.type, labels.color ORDER BY labels.id) AS row_number, #{CREATE} AS restore_action @@ -83,7 +83,7 @@ WITH data AS ( # then add `_duplicate#{ID}` soft_duplicates = ApplicationRecord.connection.execute(<<-SQL.squish) -WITH data AS ( +WITH data AS #{Gitlab::Database::AsWithMaterialized.materialized_if_supported} ( SELECT *, substring(title from 1 for 245 - length(id::text)) || '_duplicate' || id::text as new_title, @@ -108,7 +108,7 @@ WHERE labels.id IN (#{soft_duplicates.map { |dup| dup["id"] }.join(", ")}); def restore_renamed_labels(start_id, stop_id) # the backup label IDs are not incremental, they are copied directly from the Labels table ApplicationRecord.connection.execute(<<-SQL.squish) -WITH backups AS ( +WITH backups AS #{Gitlab::Database::AsWithMaterialized.materialized_if_supported} ( SELECT id, title FROM backup_labels WHERE project_id BETWEEN #{start_id} AND #{stop_id} AND diff --git a/db/post_migrate/20200325162730_schedule_backfill_push_rules_id_in_projects.rb b/db/post_migrate/20200325162730_schedule_backfill_push_rules_id_in_projects.rb index 8ff3ab6aa0c..984f4f20441 100644 --- a/db/post_migrate/20200325162730_schedule_backfill_push_rules_id_in_projects.rb +++ b/db/post_migrate/20200325162730_schedule_backfill_push_rules_id_in_projects.rb @@ -5,7 +5,7 @@ class ScheduleBackfillPushRulesIdInProjects < ActiveRecord::Migration[6.0] disable_ddl_transaction! - MIGRATION = 'BackfillPushRulesIdInProjects'.freeze + MIGRATION = 'BackfillPushRulesIdInProjects' BATCH_SIZE = 1_000 class PushRules < ActiveRecord::Base diff --git a/db/post_migrate/20200406102120_backfill_deployment_clusters_from_deployments.rb b/db/post_migrate/20200406102120_backfill_deployment_clusters_from_deployments.rb index 76b00796d1a..ab217ba92ab 100644 --- a/db/post_migrate/20200406102120_backfill_deployment_clusters_from_deployments.rb +++ b/db/post_migrate/20200406102120_backfill_deployment_clusters_from_deployments.rb @@ -17,7 +17,7 @@ class BackfillDeploymentClustersFromDeployments < ActiveRecord::Migration[6.0] class Deployment < ActiveRecord::Base include EachBatch - default_scope { where('cluster_id IS NOT NULL') } # rubocop:disable Cop/DefaultScope + default_scope { where.not(cluster_id: nil) } # rubocop:disable Cop/DefaultScope self.table_name = 'deployments' end diff --git a/db/post_migrate/20200416111111_migrate_vulnerability_dismissals.rb b/db/post_migrate/20200416111111_migrate_vulnerability_dismissals.rb index 6ec8b991968..6ca3db8902b 100644 --- a/db/post_migrate/20200416111111_migrate_vulnerability_dismissals.rb +++ b/db/post_migrate/20200416111111_migrate_vulnerability_dismissals.rb @@ -7,7 +7,7 @@ class MigrateVulnerabilityDismissals < ActiveRecord::Migration[6.0] disable_ddl_transaction! - MIGRATION = 'UpdateVulnerabilitiesToDismissed'.freeze + MIGRATION = 'UpdateVulnerabilitiesToDismissed' BATCH_SIZE = 500 DELAY_INTERVAL = 2.minutes.to_i diff --git a/db/post_migrate/20200716234259_remove_duplicate_labels_from_group.rb b/db/post_migrate/20200716234259_remove_duplicate_labels_from_group.rb index f19a209092b..114276df875 100644 --- a/db/post_migrate/20200716234259_remove_duplicate_labels_from_group.rb +++ b/db/post_migrate/20200716234259_remove_duplicate_labels_from_group.rb @@ -59,7 +59,7 @@ class RemoveDuplicateLabelsFromGroup < ActiveRecord::Migration[6.0] # group_id title template description type color duplicate_labels = ApplicationRecord.connection.execute(<<-SQL.squish) -WITH data AS ( +WITH data AS #{Gitlab::Database::AsWithMaterialized.materialized_if_supported} ( SELECT labels.*, row_number() OVER (PARTITION BY labels.group_id, labels.title, labels.template, labels.description, labels.type, labels.color ORDER BY labels.id) AS row_number, #{CREATE} AS restore_action @@ -87,7 +87,7 @@ WITH data AS ( # then add `_duplicate#{ID}` soft_duplicates = ApplicationRecord.connection.execute(<<-SQL.squish) -WITH data AS ( +WITH data AS #{Gitlab::Database::AsWithMaterialized.materialized_if_supported} ( SELECT *, substring(title from 1 for 245 - length(id::text)) || '_duplicate' || id::text as new_title, @@ -112,7 +112,7 @@ WHERE labels.id IN (#{soft_duplicates.map { |dup| dup["id"] }.join(", ")}); def restore_renamed_labels(start_id, stop_id) # the backup label IDs are not incremental, they are copied directly from the Labels table ApplicationRecord.connection.execute(<<-SQL.squish) -WITH backups AS ( +WITH backups AS #{Gitlab::Database::AsWithMaterialized.materialized_if_supported} ( SELECT id, title FROM backup_labels WHERE id BETWEEN #{start_id} AND #{stop_id} diff --git a/db/post_migrate/20200809221641_migrate_license_management_artifacts_to_license_scanning.rb b/db/post_migrate/20200809221641_migrate_license_management_artifacts_to_license_scanning.rb index 0a5dfd72392..66ef4b35dfa 100644 --- a/db/post_migrate/20200809221641_migrate_license_management_artifacts_to_license_scanning.rb +++ b/db/post_migrate/20200809221641_migrate_license_management_artifacts_to_license_scanning.rb @@ -26,7 +26,7 @@ class MigrateLicenseManagementArtifactsToLicenseScanning < ActiveRecord::Migrati min, max = relation.pluck('MIN(job_id)', 'MAX(job_id)').flatten ActiveRecord::Base.connection.execute <<~SQL - WITH ci_job_artifacts_with_row_number as ( + WITH ci_job_artifacts_with_row_number as #{Gitlab::Database::AsWithMaterialized.materialized_if_supported} ( SELECT job_id, id, ROW_NUMBER() OVER (PARTITION BY job_id ORDER BY id ASC) as row_number FROM ci_job_artifacts WHERE (file_type = #{LICENSE_SCANNING_FILE_TYPE} OR file_type = #{LICENSE_MANAGEMENT_FILE_TYPE}) diff --git a/db/post_migrate/20200811130433_create_missing_vulnerabilities_issue_links.rb b/db/post_migrate/20200811130433_create_missing_vulnerabilities_issue_links.rb index 891201eaa52..031d9ea49e2 100644 --- a/db/post_migrate/20200811130433_create_missing_vulnerabilities_issue_links.rb +++ b/db/post_migrate/20200811130433_create_missing_vulnerabilities_issue_links.rb @@ -18,11 +18,11 @@ class CreateMissingVulnerabilitiesIssueLinks < ActiveRecord::Migration[6.0] disable_ddl_transaction! def up - VulnerabilitiesFeedback.where('issue_id IS NOT NULL').each_batch do |relation| + VulnerabilitiesFeedback.where.not(issue_id: nil).each_batch do |relation| timestamp = Time.now issue_links = relation .joins("JOIN vulnerability_occurrences vo ON vo.project_id = vulnerability_feedback.project_id AND vo.report_type = vulnerability_feedback.category AND encode(vo.project_fingerprint, 'hex') = vulnerability_feedback.project_fingerprint") - .where('vo.vulnerability_id IS NOT NULL') + .where.not('vo.vulnerability_id' => nil) .pluck(:vulnerability_id, :issue_id) .map do |v_id, i_id| { diff --git a/db/post_migrate/20200831065705_ensure_target_project_id_is_filled.rb b/db/post_migrate/20200831065705_ensure_target_project_id_is_filled.rb index 9b267933b04..8693dca0000 100644 --- a/db/post_migrate/20200831065705_ensure_target_project_id_is_filled.rb +++ b/db/post_migrate/20200831065705_ensure_target_project_id_is_filled.rb @@ -32,7 +32,7 @@ class EnsureTargetProjectIdIsFilled < ActiveRecord::Migration[6.0] ) MergeRequestMetrics.connection.execute <<-SQL - WITH target_project_id_and_metrics_id as ( + WITH target_project_id_and_metrics_id as #{Gitlab::Database::AsWithMaterialized.materialized_if_supported} ( #{query_for_cte.to_sql} ) UPDATE #{MergeRequestMetrics.connection.quote_table_name(MergeRequestMetrics.table_name)} diff --git a/db/post_migrate/20200930144340_set_job_waiter_ttl.rb b/db/post_migrate/20200930144340_set_job_waiter_ttl.rb index b15faa61dea..347fa4be5a0 100644 --- a/db/post_migrate/20200930144340_set_job_waiter_ttl.rb +++ b/db/post_migrate/20200930144340_set_job_waiter_ttl.rb @@ -3,7 +3,7 @@ class SetJobWaiterTtl < ActiveRecord::Migration[6.0] DOWNTIME = false - SCRIPT = <<~LUA.freeze + SCRIPT = <<~LUA if redis.call("ttl", KEYS[1]) < 0 then redis.call("expire", KEYS[1], 21600) end diff --git a/db/post_migrate/20201014142521_schedule_sync_blocking_issues_count.rb b/db/post_migrate/20201014142521_schedule_sync_blocking_issues_count.rb index 61b2b2aaad5..30a8ea591da 100644 --- a/db/post_migrate/20201014142521_schedule_sync_blocking_issues_count.rb +++ b/db/post_migrate/20201014142521_schedule_sync_blocking_issues_count.rb @@ -7,7 +7,7 @@ class ScheduleSyncBlockingIssuesCount < ActiveRecord::Migration[6.0] BATCH_SIZE = 50 DELAY_INTERVAL = 120.seconds.to_i - MIGRATION = 'SyncBlockingIssuesCount'.freeze + MIGRATION = 'SyncBlockingIssuesCount' disable_ddl_transaction! diff --git a/db/post_migrate/20201208175117_schedule_backfilling_artifact_expiry_migration.rb b/db/post_migrate/20201208175117_schedule_backfilling_artifact_expiry_migration.rb index 1ffe9abbc58..56db148afe6 100644 --- a/db/post_migrate/20201208175117_schedule_backfilling_artifact_expiry_migration.rb +++ b/db/post_migrate/20201208175117_schedule_backfilling_artifact_expiry_migration.rb @@ -5,7 +5,7 @@ class ScheduleBackfillingArtifactExpiryMigration < ActiveRecord::Migration[6.0] DOWNTIME = false SWITCH_DATE = Time.utc(2020, 6, 22).freeze - INDEX_NAME = 'expired_artifacts_temp_index'.freeze + INDEX_NAME = 'expired_artifacts_temp_index' INDEX_CONDITION = "expire_at IS NULL AND created_at < '#{SWITCH_DATE}'" disable_ddl_transaction! diff --git a/db/post_migrate/20210105030125_cleanup_projects_with_bad_has_external_wiki_data.rb b/db/post_migrate/20210105030125_cleanup_projects_with_bad_has_external_wiki_data.rb index 73725062bb3..bc90a5f48ea 100644 --- a/db/post_migrate/20210105030125_cleanup_projects_with_bad_has_external_wiki_data.rb +++ b/db/post_migrate/20210105030125_cleanup_projects_with_bad_has_external_wiki_data.rb @@ -4,7 +4,7 @@ class CleanupProjectsWithBadHasExternalWikiData < ActiveRecord::Migration[6.0] include Gitlab::Database::MigrationHelpers DOWNTIME = false - TMP_INDEX_NAME = 'tmp_index_projects_on_id_where_has_external_wiki_is_true'.freeze + TMP_INDEX_NAME = 'tmp_index_projects_on_id_where_has_external_wiki_is_true' BATCH_SIZE = 100 disable_ddl_transaction! @@ -45,7 +45,7 @@ class CleanupProjectsWithBadHasExternalWikiData < ActiveRecord::Migration[6.0] .merge(Project.where(has_external_wiki: false).where(pending_delete: false).where(archived: false)) execute(<<~SQL) - WITH project_ids_to_update (id) AS ( + WITH project_ids_to_update (id) AS #{Gitlab::Database::AsWithMaterialized.materialized_if_supported} ( #{scope_with_projects.to_sql} ) UPDATE projects SET has_external_wiki = true WHERE id IN (SELECT id FROM project_ids_to_update) @@ -75,7 +75,7 @@ class CleanupProjectsWithBadHasExternalWikiData < ActiveRecord::Migration[6.0] Project.where(index_where).each_batch(of: BATCH_SIZE) do |relation| relation_with_exists_query = relation.where('NOT EXISTS (?)', services_sub_query) execute(<<~SQL) - WITH project_ids_to_update (id) AS ( + WITH project_ids_to_update (id) AS #{Gitlab::Database::AsWithMaterialized.materialized_if_supported} ( #{relation_with_exists_query.select(:id).to_sql} ) UPDATE projects SET has_external_wiki = false WHERE id IN (SELECT id FROM project_ids_to_update) diff --git a/db/post_migrate/20210115220610_schedule_artifact_expiry_backfill.rb b/db/post_migrate/20210115220610_schedule_artifact_expiry_backfill.rb index 44a76321495..4f49e8b75af 100644 --- a/db/post_migrate/20210115220610_schedule_artifact_expiry_backfill.rb +++ b/db/post_migrate/20210115220610_schedule_artifact_expiry_backfill.rb @@ -4,11 +4,11 @@ class ScheduleArtifactExpiryBackfill < ActiveRecord::Migration[6.0] include Gitlab::Database::MigrationHelpers DOWNTIME = false - MIGRATION = 'BackfillArtifactExpiryDate'.freeze + MIGRATION = 'BackfillArtifactExpiryDate' SWITCH_DATE = Date.new(2020, 06, 22).freeze - INDEX_NAME = 'expired_artifacts_temp_index'.freeze + INDEX_NAME = 'expired_artifacts_temp_index' OLD_INDEX_CONDITION = "expire_at IS NULL AND created_at < '#{SWITCH_DATE}'" - INDEX_CONDITION = "expire_at IS NULL AND date(created_at AT TIME ZONE 'UTC') < '2020-06-22'::date".freeze + INDEX_CONDITION = "expire_at IS NULL AND date(created_at AT TIME ZONE 'UTC') < '2020-06-22'::date" disable_ddl_transaction! diff --git a/db/post_migrate/20210210221006_cleanup_projects_with_bad_has_external_issue_tracker_data.rb b/db/post_migrate/20210210221006_cleanup_projects_with_bad_has_external_issue_tracker_data.rb index 4b8bf014066..7b17faeb4b4 100644 --- a/db/post_migrate/20210210221006_cleanup_projects_with_bad_has_external_issue_tracker_data.rb +++ b/db/post_migrate/20210210221006_cleanup_projects_with_bad_has_external_issue_tracker_data.rb @@ -4,7 +4,7 @@ class CleanupProjectsWithBadHasExternalIssueTrackerData < ActiveRecord::Migratio include Gitlab::Database::MigrationHelpers DOWNTIME = false - TMP_INDEX_NAME = 'tmp_idx_projects_on_id_where_has_external_issue_tracker_is_true'.freeze + TMP_INDEX_NAME = 'tmp_idx_projects_on_id_where_has_external_issue_tracker_is_true' BATCH_SIZE = 100 disable_ddl_transaction! @@ -44,7 +44,7 @@ class CleanupProjectsWithBadHasExternalIssueTrackerData < ActiveRecord::Migratio .merge(Project.where(has_external_issue_tracker: false).where(pending_delete: false)) execute(<<~SQL) - WITH project_ids_to_update (id) AS ( + WITH project_ids_to_update (id) AS #{Gitlab::Database::AsWithMaterialized.materialized_if_supported} ( #{scope_with_projects.to_sql} ) UPDATE projects SET has_external_issue_tracker = true WHERE id IN (SELECT id FROM project_ids_to_update) @@ -71,7 +71,7 @@ class CleanupProjectsWithBadHasExternalIssueTrackerData < ActiveRecord::Migratio Project.where(index_where).each_batch(of: BATCH_SIZE) do |relation| relation_with_exists_query = relation.where('NOT EXISTS (?)', services_sub_query) execute(<<~SQL) - WITH project_ids_to_update (id) AS ( + WITH project_ids_to_update (id) AS #{Gitlab::Database::AsWithMaterialized.materialized_if_supported} ( #{relation_with_exists_query.select(:id).to_sql} ) UPDATE projects SET has_external_issue_tracker = false WHERE id IN (SELECT id FROM project_ids_to_update) diff --git a/db/post_migrate/20210224150506_reschedule_artifact_expiry_backfill.rb b/db/post_migrate/20210224150506_reschedule_artifact_expiry_backfill.rb index af5c474e2ba..2c0fe405490 100644 --- a/db/post_migrate/20210224150506_reschedule_artifact_expiry_backfill.rb +++ b/db/post_migrate/20210224150506_reschedule_artifact_expiry_backfill.rb @@ -4,7 +4,7 @@ class RescheduleArtifactExpiryBackfill < ActiveRecord::Migration[6.0] include Gitlab::Database::MigrationHelpers DOWNTIME = false - MIGRATION = 'BackfillArtifactExpiryDate'.freeze + MIGRATION = 'BackfillArtifactExpiryDate' SWITCH_DATE = Date.new(2020, 06, 22).freeze disable_ddl_transaction! diff --git a/db/post_migrate/20210226120851_move_container_registry_enabled_to_project_features.rb b/db/post_migrate/20210226120851_move_container_registry_enabled_to_project_features.rb index 7bc7a0e49f7..fce31110866 100644 --- a/db/post_migrate/20210226120851_move_container_registry_enabled_to_project_features.rb +++ b/db/post_migrate/20210226120851_move_container_registry_enabled_to_project_features.rb @@ -16,7 +16,10 @@ class MoveContainerRegistryEnabledToProjectFeatures < ActiveRecord::Migration[6. end def up - queue_background_migration_jobs_by_range_at_intervals(Project, MIGRATION, 2.minutes, batch_size: BATCH_SIZE) + # no-op + # Superceded by db/post_migrate/20210401131948_move_container_registry_enabled_to_project_features2.rb + + # queue_background_migration_jobs_by_range_at_intervals(Project, MIGRATION, 2.minutes, batch_size: BATCH_SIZE) end def down diff --git a/db/post_migrate/20210226141517_dedup_issue_metrics.rb b/db/post_migrate/20210226141517_dedup_issue_metrics.rb new file mode 100644 index 00000000000..8228d509e07 --- /dev/null +++ b/db/post_migrate/20210226141517_dedup_issue_metrics.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +class DedupIssueMetrics < ActiveRecord::Migration[6.0] + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + TMP_INDEX_NAME = 'tmp_unique_issue_metrics_by_issue_id' + OLD_INDEX_NAME = 'index_issue_metrics' + INDEX_NAME = 'index_unique_issue_metrics_issue_id' + BATCH_SIZE = 1_000 + + disable_ddl_transaction! + + class IssueMetrics < ActiveRecord::Base + self.table_name = 'issue_metrics' + + include EachBatch + end + + def up + IssueMetrics.reset_column_information + + last_metrics_record_id = IssueMetrics.maximum(:id) || 0 + + # This index will disallow further duplicates while we're deduplicating the data. + add_concurrent_index(:issue_metrics, :issue_id, where: "id > #{Integer(last_metrics_record_id)}", unique: true, name: TMP_INDEX_NAME) + + IssueMetrics.each_batch(of: BATCH_SIZE) do |relation| + duplicated_issue_ids = IssueMetrics + .where(issue_id: relation.select(:issue_id)) + .select(:issue_id) + .group(:issue_id) + .having('COUNT(issue_metrics.issue_id) > 1') + .pluck(:issue_id) + + duplicated_issue_ids.each do |issue_id| + deduplicate_item(issue_id) + end + end + + add_concurrent_index(:issue_metrics, :issue_id, unique: true, name: INDEX_NAME) + remove_concurrent_index_by_name(:issue_metrics, TMP_INDEX_NAME) + remove_concurrent_index_by_name(:issue_metrics, OLD_INDEX_NAME) + end + + def down + add_concurrent_index(:issue_metrics, :issue_id, name: OLD_INDEX_NAME) + remove_concurrent_index_by_name(:issue_metrics, TMP_INDEX_NAME) + remove_concurrent_index_by_name(:issue_metrics, INDEX_NAME) + end + + private + + def deduplicate_item(issue_id) + issue_metrics_records = IssueMetrics.where(issue_id: issue_id).order(updated_at: :asc).to_a + + attributes = {} + issue_metrics_records.each do |issue_metrics_record| + params = issue_metrics_record.attributes.except('id') + attributes.merge!(params.compact) + end + + ActiveRecord::Base.transaction do + record_to_keep = issue_metrics_records.pop + records_to_delete = issue_metrics_records + + IssueMetrics.where(id: records_to_delete.map(&:id)).delete_all + record_to_keep.update!(attributes) + end + end +end diff --git a/db/post_migrate/20210302150310_schedule_migrate_pages_to_zip_storage.rb b/db/post_migrate/20210302150310_schedule_migrate_pages_to_zip_storage.rb new file mode 100644 index 00000000000..7f6d7ffe9b7 --- /dev/null +++ b/db/post_migrate/20210302150310_schedule_migrate_pages_to_zip_storage.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +class ScheduleMigratePagesToZipStorage < ActiveRecord::Migration[6.0] + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + MIGRATION = 'MigratePagesToZipStorage' + BATCH_SIZE = 10 + BATCH_TIME = 5.minutes + + disable_ddl_transaction! + + class ProjectPagesMetadatum < ActiveRecord::Base + extend SuppressCompositePrimaryKeyWarning + + include EachBatch + + self.primary_key = :project_id + self.table_name = 'project_pages_metadata' + self.inheritance_column = :_type_disabled + + scope :deployed, -> { where(deployed: true) } + scope :only_on_legacy_storage, -> { deployed.where(pages_deployment_id: nil) } + end + + def up + queue_background_migration_jobs_by_range_at_intervals( + ProjectPagesMetadatum.only_on_legacy_storage, + MIGRATION, + BATCH_TIME, + batch_size: BATCH_SIZE, + primary_column_name: :project_id + ) + end +end diff --git a/db/post_migrate/20210311045138_set_traversal_ids_for_gitlab_org_group_staging.rb b/db/post_migrate/20210311045138_set_traversal_ids_for_gitlab_org_group_staging.rb new file mode 100644 index 00000000000..bcf872ded54 --- /dev/null +++ b/db/post_migrate/20210311045138_set_traversal_ids_for_gitlab_org_group_staging.rb @@ -0,0 +1,88 @@ +# frozen_string_literal: true + +class SetTraversalIdsForGitlabOrgGroupStaging < ActiveRecord::Migration[6.0] + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + def up + return unless Gitlab.staging? + + # namespace ID 9970 is gitlab-org on staging. + with_lock_retries do + execute(<<~SQL) + UPDATE + namespaces + SET + traversal_ids = cte.traversal_ids + FROM + ( + WITH RECURSIVE cte(id, traversal_ids, cycle) AS ( + VALUES + (9970, ARRAY[9970], false) + UNION ALL + SELECT + n.id, + cte.traversal_ids || n.id, + n.id = ANY(cte.traversal_ids) + FROM + namespaces n, + cte + WHERE + n.parent_id = cte.id + AND NOT cycle + ) + SELECT + id, + traversal_ids + FROM + cte FOR + UPDATE + ) as cte + WHERE + namespaces.id = cte.id + AND namespaces.traversal_ids <> cte.traversal_ids + SQL + end + end + + def down + return unless Gitlab.staging? + + # namespace ID 9970 is gitlab-org on staging. + with_lock_retries do + execute(<<~SQL) + UPDATE + namespaces + SET + traversal_ids = '{}' + FROM + ( + WITH RECURSIVE cte(id, traversal_ids, cycle) AS ( + VALUES + (9970, ARRAY[9970], false) + UNION ALL + SELECT + n.id, + cte.traversal_ids || n.id, + n.id = ANY(cte.traversal_ids) + FROM + namespaces n, + cte + WHERE + n.parent_id = cte.id + AND NOT cycle + ) + SELECT + id, + traversal_ids + FROM + cte FOR + UPDATE + ) as cte + WHERE + namespaces.id = cte.id + SQL + end + end +end diff --git a/db/post_migrate/20210311045139_set_traversal_ids_for_gitlab_org_group_com.rb b/db/post_migrate/20210311045139_set_traversal_ids_for_gitlab_org_group_com.rb new file mode 100644 index 00000000000..8cef1f1cc2b --- /dev/null +++ b/db/post_migrate/20210311045139_set_traversal_ids_for_gitlab_org_group_com.rb @@ -0,0 +1,88 @@ +# frozen_string_literal: true + +class SetTraversalIdsForGitlabOrgGroupCom < ActiveRecord::Migration[6.0] + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + def up + return unless Gitlab.com? + + # namespace ID 9970 is gitlab-org on .com + with_lock_retries do + execute(<<~SQL) + UPDATE + namespaces + SET + traversal_ids = cte.traversal_ids + FROM + ( + WITH RECURSIVE cte(id, traversal_ids, cycle) AS ( + VALUES + (9970, ARRAY[9970], false) + UNION ALL + SELECT + n.id, + cte.traversal_ids || n.id, + n.id = ANY(cte.traversal_ids) + FROM + namespaces n, + cte + WHERE + n.parent_id = cte.id + AND NOT cycle + ) + SELECT + id, + traversal_ids + FROM + cte FOR + UPDATE + ) as cte + WHERE + namespaces.id = cte.id + AND namespaces.traversal_ids <> cte.traversal_ids + SQL + end + end + + def down + return unless Gitlab.com? + + # namespace ID 9970 is gitlab-org on .com + with_lock_retries do + execute(<<~SQL) + UPDATE + namespaces + SET + traversal_ids = '{}' + FROM + ( + WITH RECURSIVE cte(id, traversal_ids, cycle) AS ( + VALUES + (9970, ARRAY[9970], false) + UNION ALL + SELECT + n.id, + cte.traversal_ids || n.id, + n.id = ANY(cte.traversal_ids) + FROM + namespaces n, + cte + WHERE + n.parent_id = cte.id + AND NOT cycle + ) + SELECT + id, + traversal_ids + FROM + cte FOR + UPDATE + ) as cte + WHERE + namespaces.id = cte.id + SQL + end + end +end diff --git a/db/post_migrate/20210311093723_add_partial_index_on_ci_pipelines_by_cancelable_status_and_users.rb b/db/post_migrate/20210311093723_add_partial_index_on_ci_pipelines_by_cancelable_status_and_users.rb new file mode 100644 index 00000000000..176548be6e6 --- /dev/null +++ b/db/post_migrate/20210311093723_add_partial_index_on_ci_pipelines_by_cancelable_status_and_users.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +class AddPartialIndexOnCiPipelinesByCancelableStatusAndUsers < ActiveRecord::Migration[6.0] + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + INDEX_NAME = 'index_ci_pipelines_on_user_id_and_id_and_cancelable_status' + INDEX_FILTER_CONDITION = <<~SQL + ((status)::text = ANY ( + ARRAY[ + ('running'::character varying)::text, + ('waiting_for_resource'::character varying)::text, + ('preparing'::character varying)::text, + ('pending'::character varying)::text, + ('created'::character varying)::text, + ('scheduled'::character varying)::text + ] + )) + SQL + + disable_ddl_transaction! + + def up + add_concurrent_index :ci_pipelines, [:user_id, :id], where: INDEX_FILTER_CONDITION, name: INDEX_NAME + end + + def down + remove_concurrent_index_by_name :ci_pipelines, INDEX_NAME + end +end diff --git a/db/post_migrate/20210311120155_backfill_events_id_for_bigint_conversion.rb b/db/post_migrate/20210311120155_backfill_events_id_for_bigint_conversion.rb new file mode 100644 index 00000000000..01e81c65eee --- /dev/null +++ b/db/post_migrate/20210311120155_backfill_events_id_for_bigint_conversion.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +class BackfillEventsIdForBigintConversion < ActiveRecord::Migration[6.0] + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + def up + return unless should_run? + + backfill_conversion_of_integer_to_bigint :events, :id, batch_size: 15000, sub_batch_size: 100 + end + + def down + return unless should_run? + + Gitlab::Database::BackgroundMigration::BatchedMigration + .where(job_class_name: 'CopyColumnUsingBackgroundMigrationJob') + .where(table_name: 'events', column_name: 'id') + .where('job_arguments = ?', %w[id id_convert_to_bigint].to_json) + .delete_all + end + + private + + def should_run? + Gitlab.dev_or_test_env? || Gitlab.com? + end +end diff --git a/db/post_migrate/20210311120156_backfill_push_event_payload_event_id_for_bigint_conversion.rb b/db/post_migrate/20210311120156_backfill_push_event_payload_event_id_for_bigint_conversion.rb new file mode 100644 index 00000000000..6b8595b3ad3 --- /dev/null +++ b/db/post_migrate/20210311120156_backfill_push_event_payload_event_id_for_bigint_conversion.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +class BackfillPushEventPayloadEventIdForBigintConversion < ActiveRecord::Migration[6.0] + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + def up + return unless should_run? + + backfill_conversion_of_integer_to_bigint :push_event_payloads, :event_id, primary_key: :event_id, + batch_size: 15000, sub_batch_size: 100 + end + + def down + return unless should_run? + + Gitlab::Database::BackgroundMigration::BatchedMigration + .where(job_class_name: 'CopyColumnUsingBackgroundMigrationJob') + .where(table_name: 'push_event_payloads', column_name: 'event_id') + .where('job_arguments = ?', %w[event_id event_id_convert_to_bigint].to_json) + .delete_all + end + + private + + def should_run? + Gitlab.dev_or_test_env? || Gitlab.com? + end +end diff --git a/db/post_migrate/20210317104032_set_iteration_cadence_automatic_to_false.rb b/db/post_migrate/20210317104032_set_iteration_cadence_automatic_to_false.rb new file mode 100644 index 00000000000..c151551ae64 --- /dev/null +++ b/db/post_migrate/20210317104032_set_iteration_cadence_automatic_to_false.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class SetIterationCadenceAutomaticToFalse < ActiveRecord::Migration[6.0] + DOWNTIME = false + + def up + ActiveRecord::Base.connection.execute <<~SQL + UPDATE iterations_cadences + SET automatic = FALSE + WHERE iterations_cadences.automatic = TRUE + SQL + end + + def down + # no-op + end +end diff --git a/db/post_migrate/20210317155207_validate_not_null_constraint_on_cluster_token_name.rb b/db/post_migrate/20210317155207_validate_not_null_constraint_on_cluster_token_name.rb new file mode 100644 index 00000000000..b77523c3a44 --- /dev/null +++ b/db/post_migrate/20210317155207_validate_not_null_constraint_on_cluster_token_name.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +class ValidateNotNullConstraintOnClusterTokenName < ActiveRecord::Migration[6.0] + include Gitlab::Database::MigrationHelpers + DOWNTIME = false + + disable_ddl_transaction! + + def up + validate_not_null_constraint :cluster_agent_tokens, :name + end + + def down + # no-op + end +end diff --git a/db/post_migrate/20210322115438_validate_not_null_constraint_on_gitlab_subscriptions_namespace_id.rb b/db/post_migrate/20210322115438_validate_not_null_constraint_on_gitlab_subscriptions_namespace_id.rb new file mode 100644 index 00000000000..96d9f383406 --- /dev/null +++ b/db/post_migrate/20210322115438_validate_not_null_constraint_on_gitlab_subscriptions_namespace_id.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class ValidateNotNullConstraintOnGitlabSubscriptionsNamespaceId < ActiveRecord::Migration[6.0] + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + disable_ddl_transaction! + + def up + validate_not_null_constraint :gitlab_subscriptions, :namespace_id + end + + def down + # no-op + end +end diff --git a/db/post_migrate/20210326121537_backfill_cleanup_for_partitioned_web_hook_logs.rb b/db/post_migrate/20210326121537_backfill_cleanup_for_partitioned_web_hook_logs.rb new file mode 100644 index 00000000000..5816d02561e --- /dev/null +++ b/db/post_migrate/20210326121537_backfill_cleanup_for_partitioned_web_hook_logs.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class BackfillCleanupForPartitionedWebHookLogs < ActiveRecord::Migration[6.0] + include Gitlab::Database::PartitioningMigrationHelpers + + DOWNTIME = false + + disable_ddl_transaction! + + def up + finalize_backfilling_partitioned_table :web_hook_logs + end + + def down + # no op + end +end diff --git a/db/post_migrate/20210328214434_remove_temporary_index_from_vulnerabilities_table.rb b/db/post_migrate/20210328214434_remove_temporary_index_from_vulnerabilities_table.rb new file mode 100644 index 00000000000..f3da1cc69c4 --- /dev/null +++ b/db/post_migrate/20210328214434_remove_temporary_index_from_vulnerabilities_table.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +class RemoveTemporaryIndexFromVulnerabilitiesTable < ActiveRecord::Migration[6.0] + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + INDEX_NAME = 'temporary_index_vulnerabilities_on_id' + + disable_ddl_transaction! + + def up + remove_concurrent_index_by_name :vulnerabilities, INDEX_NAME + end + + def down + add_concurrent_index :vulnerabilities, :id, where: "state = 2 AND (dismissed_at IS NULL OR dismissed_by_id IS NULL)", name: INDEX_NAME + end +end diff --git a/db/post_migrate/20210329102724_add_new_trail_plans.rb b/db/post_migrate/20210329102724_add_new_trail_plans.rb new file mode 100644 index 00000000000..b142f6385f7 --- /dev/null +++ b/db/post_migrate/20210329102724_add_new_trail_plans.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +class AddNewTrailPlans < ActiveRecord::Migration[6.0] + class Plan < ActiveRecord::Base + self.inheritance_column = :_type_disabled + + has_one :limits, class_name: 'PlanLimits' + + def actual_limits + self.limits || self.build_limits + end + end + + class PlanLimits < ActiveRecord::Base + self.inheritance_column = :_type_disabled + + belongs_to :plan + end + + def create_plan_limits(plan_limit_name, plan) + plan_limit = Plan.find_or_initialize_by(name: plan_limit_name).actual_limits.dup + plan_limit.plan = plan + plan_limit.save! + end + + def up + return unless Gitlab.dev_env_or_com? + + ultimate_trial = Plan.create!(name: 'ultimate_trial', title: 'Ultimate Trial') + premium_trial = Plan.create!(name: 'premium_trial', title: 'Premium Trial') + + create_plan_limits('gold', ultimate_trial) + create_plan_limits('silver', premium_trial) + end + + def down + return unless Gitlab.dev_env_or_com? + + Plan.where(name: %w(ultimate_trial premium_trial)).delete_all + end +end diff --git a/db/post_migrate/20210330091751_remove_records_without_group_from_webhooks_table.rb b/db/post_migrate/20210330091751_remove_records_without_group_from_webhooks_table.rb new file mode 100644 index 00000000000..c384aa25ac4 --- /dev/null +++ b/db/post_migrate/20210330091751_remove_records_without_group_from_webhooks_table.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +class RemoveRecordsWithoutGroupFromWebhooksTable < ActiveRecord::Migration[6.0] + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + class WebHook < ActiveRecord::Base + include EachBatch + + self.table_name = 'web_hooks' + end + + class Group < ActiveRecord::Base + self.inheritance_column = :_type_disabled + self.table_name = 'namespaces' + end + + def up + subquery = Group.select(1).where(Group.arel_table[:id].eq(WebHook.arel_table[:group_id])) + + WebHook.each_batch(of: 500, column: :id) do |relation| + relation.where(type: 'GroupHook').where.not('EXISTS (?)', subquery).delete_all + end + end + + def down + # no-op + end +end diff --git a/db/post_migrate/20210330130420_drop_finding_fingerprint_table.rb b/db/post_migrate/20210330130420_drop_finding_fingerprint_table.rb new file mode 100644 index 00000000000..fd77d4d7a4b --- /dev/null +++ b/db/post_migrate/20210330130420_drop_finding_fingerprint_table.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +class DropFindingFingerprintTable < ActiveRecord::Migration[6.0] + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + FINGERPRINT_IDX = :idx_vuln_fingerprints_on_occurrences_id_and_fingerprint_sha256 + UNIQ_IDX = :idx_vuln_fingerprints_uniqueness_fingerprint_sha256 + + def up + with_lock_retries do + drop_table :vulnerability_finding_fingerprints + end + end + + def down + with_lock_retries do + create_table :vulnerability_finding_fingerprints do |t| + t.references :finding, + index: true, + null: false, + foreign_key: { to_table: :vulnerability_occurrences, column: :finding_id, on_delete: :cascade } + + t.timestamps_with_timezone null: false + + t.integer :algorithm_type, null: false, limit: 2 + t.binary :fingerprint_sha256, null: false + + t.index %i[finding_id fingerprint_sha256], + name: FINGERPRINT_IDX, + unique: true # only one link should exist between occurrence and the fingerprint + + t.index %i[finding_id algorithm_type fingerprint_sha256], + name: UNIQ_IDX, + unique: true # these should be unique + end + end + end +end diff --git a/db/post_migrate/20210331105335_drop_non_partitioned_audit_events.rb b/db/post_migrate/20210331105335_drop_non_partitioned_audit_events.rb new file mode 100644 index 00000000000..7c32fc61711 --- /dev/null +++ b/db/post_migrate/20210331105335_drop_non_partitioned_audit_events.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +class DropNonPartitionedAuditEvents < ActiveRecord::Migration[6.0] + include Gitlab::Database::MigrationHelpers + include Gitlab::Database::PartitioningMigrationHelpers::TableManagementHelpers + + DOWNTIME = false + + def up + drop_nonpartitioned_archive_table(:audit_events) + end + + def down + execute(<<~SQL) + CREATE TABLE audit_events_archived ( + id integer NOT NULL, + author_id integer NOT NULL, + entity_id integer NOT NULL, + entity_type character varying NOT NULL, + details text, + created_at timestamp without time zone, + ip_address inet, + author_name text, + entity_path text, + target_details text, + target_type text, + target_id bigint, + CONSTRAINT check_492aaa021d CHECK ((char_length(entity_path) <= 5500)), + CONSTRAINT check_82294106dd CHECK ((char_length(target_type) <= 255)), + CONSTRAINT check_83ff8406e2 CHECK ((char_length(author_name) <= 255)), + CONSTRAINT check_d493ec90b5 CHECK ((char_length(target_details) <= 5500)) + ); + + ALTER TABLE ONLY audit_events_archived ADD CONSTRAINT audit_events_archived_pkey PRIMARY KEY (id); + + CREATE INDEX analytics_index_audit_events_on_created_at_and_author_id ON audit_events_archived USING btree (created_at, author_id); + CREATE INDEX idx_audit_events_on_entity_id_desc_author_id_created_at ON audit_events_archived USING btree (entity_id, entity_type, id DESC, author_id, created_at); + SQL + + with_lock_retries do + create_trigger_to_sync_tables(:audit_events, :audit_events_archived, 'id') + end + end +end diff --git a/db/post_migrate/20210401131948_move_container_registry_enabled_to_project_features2.rb b/db/post_migrate/20210401131948_move_container_registry_enabled_to_project_features2.rb new file mode 100644 index 00000000000..6e3f7ae34d1 --- /dev/null +++ b/db/post_migrate/20210401131948_move_container_registry_enabled_to_project_features2.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +class MoveContainerRegistryEnabledToProjectFeatures2 < ActiveRecord::Migration[6.0] + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + BATCH_SIZE = 21_000 + MIGRATION = 'MoveContainerRegistryEnabledToProjectFeature' + + disable_ddl_transaction! + + class Project < ActiveRecord::Base + include EachBatch + self.table_name = 'projects' + end + + def up + delete_queued_jobs('MoveContainerRegistryEnabledToProjectFeature') + + queue_background_migration_jobs_by_range_at_intervals(Project, MIGRATION, 2.minutes, batch_size: BATCH_SIZE, track_jobs: true) + end + + def down + # no-op + end +end diff --git a/db/post_migrate/20210402005225_add_source_and_level_index_on_notification_settings.rb b/db/post_migrate/20210402005225_add_source_and_level_index_on_notification_settings.rb new file mode 100644 index 00000000000..a29babca93e --- /dev/null +++ b/db/post_migrate/20210402005225_add_source_and_level_index_on_notification_settings.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +class AddSourceAndLevelIndexOnNotificationSettings < ActiveRecord::Migration[6.0] + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + INDEX_WITH_SOURCE_LEVEL_USER_NAME = 'index_notification_settings_on_source_and_level_and_user' + INDEX_WITH_SOURCE_NAME = 'index_notification_settings_on_source_id_and_source_type' + INDEX_WITH_USER_NAME = 'index_notification_settings_on_user_id' + + disable_ddl_transaction! + + def up + add_concurrent_index :notification_settings, [:source_id, :source_type, :level, :user_id], name: INDEX_WITH_SOURCE_LEVEL_USER_NAME + remove_concurrent_index_by_name :notification_settings, INDEX_WITH_SOURCE_NAME # Above index expands this index + remove_concurrent_index_by_name :notification_settings, INDEX_WITH_USER_NAME # It is redundant as we already have unique index on (user_id, source_id, source_type) + end + + def down + add_concurrent_index :notification_settings, [:source_id, :source_type], name: INDEX_WITH_SOURCE_NAME + add_concurrent_index :notification_settings, [:user_id], name: INDEX_WITH_USER_NAME + remove_concurrent_index_by_name :notification_settings, INDEX_WITH_SOURCE_LEVEL_USER_NAME + end +end diff --git a/db/post_migrate/20210406144743_backfill_total_tuple_count_for_batched_migrations.rb b/db/post_migrate/20210406144743_backfill_total_tuple_count_for_batched_migrations.rb new file mode 100644 index 00000000000..5fc5a5b2b6e --- /dev/null +++ b/db/post_migrate/20210406144743_backfill_total_tuple_count_for_batched_migrations.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +class BackfillTotalTupleCountForBatchedMigrations < ActiveRecord::Migration[6.0] + DOWNTIME = false + + def up + return unless should_run? + + Gitlab::Database::BackgroundMigration::BatchedMigration.all.each do |migration| + total_tuple_count = Gitlab::Database::PgClass.for_table(migration.table_name)&.cardinality_estimate + + migration.update(total_tuple_count: total_tuple_count) + end + end + + def down + return unless should_run? + + Gitlab::Database::BackgroundMigration::BatchedMigration.update_all(total_tuple_count: nil) + end + + private + + def should_run? + Gitlab.dev_or_test_env? || Gitlab.com? + end +end diff --git a/db/post_migrate/20210407150240_confirm_support_bot_user.rb b/db/post_migrate/20210407150240_confirm_support_bot_user.rb new file mode 100644 index 00000000000..c26ae153128 --- /dev/null +++ b/db/post_migrate/20210407150240_confirm_support_bot_user.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +class ConfirmSupportBotUser < ActiveRecord::Migration[6.0] + SUPPORT_BOT_TYPE = 1 + + def up + users = Arel::Table.new(:users) + um = Arel::UpdateManager.new + um.table(users) + .where(users[:user_type].eq(SUPPORT_BOT_TYPE)) + .where(users[:confirmed_at].eq(nil)) + .set([[users[:confirmed_at], Arel::Nodes::NamedFunction.new('COALESCE', [users[:created_at], Arel::Nodes::SqlLiteral.new('NOW()')])]]) + connection.execute(um.to_sql) + end + + def down + # no op + + # The up migration allows for the possibility that the support user might + # have already been manually confirmed. It's not reversible as this data is + # subsequently lost. + end +end diff --git a/db/post_migrate/20210409185501_index_members_on_user_id_source_id_source_type.rb b/db/post_migrate/20210409185501_index_members_on_user_id_source_id_source_type.rb new file mode 100644 index 00000000000..d20eb5e4ea3 --- /dev/null +++ b/db/post_migrate/20210409185501_index_members_on_user_id_source_id_source_type.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class IndexMembersOnUserIdSourceIdSourceType < ActiveRecord::Migration[6.0] + include Gitlab::Database::MigrationHelpers + + disable_ddl_transaction! + + INDEX_NAME = 'index_members_on_user_id_source_id_source_type' + + def up + add_concurrent_index(:members, [:user_id, :source_id, :source_type], name: INDEX_NAME) + end + + def down + remove_concurrent_index_by_name(:members, INDEX_NAME) + end +end diff --git a/db/post_migrate/20210409185531_remove_members_index_on_user_id.rb b/db/post_migrate/20210409185531_remove_members_index_on_user_id.rb new file mode 100644 index 00000000000..3f330e44408 --- /dev/null +++ b/db/post_migrate/20210409185531_remove_members_index_on_user_id.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class RemoveMembersIndexOnUserId < ActiveRecord::Migration[6.0] + include Gitlab::Database::MigrationHelpers + + disable_ddl_transaction! + + INDEX_NAME = 'index_members_on_user_id' + + def up + remove_concurrent_index_by_name(:members, INDEX_NAME) + end + + def down + add_concurrent_index(:members, :user_id, name: INDEX_NAME) + end +end diff --git a/db/post_migrate/20210413092922_add_index_to_packages_maven_metadata_path.rb b/db/post_migrate/20210413092922_add_index_to_packages_maven_metadata_path.rb new file mode 100644 index 00000000000..697b9f9cbf7 --- /dev/null +++ b/db/post_migrate/20210413092922_add_index_to_packages_maven_metadata_path.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class AddIndexToPackagesMavenMetadataPath < ActiveRecord::Migration[6.0] + include Gitlab::Database::MigrationHelpers + + disable_ddl_transaction! + + INDEX_NAME = 'index_packages_maven_metadata_on_path' + + def up + add_concurrent_index :packages_maven_metadata, :path, name: INDEX_NAME + end + + def down + remove_concurrent_index :packages_maven_metadata, :path, name: INDEX_NAME + end +end diff --git a/db/post_migrate/20210413130011_add_partitioned_web_hook_log_fk.rb b/db/post_migrate/20210413130011_add_partitioned_web_hook_log_fk.rb new file mode 100644 index 00000000000..6453993bd51 --- /dev/null +++ b/db/post_migrate/20210413130011_add_partitioned_web_hook_log_fk.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +class AddPartitionedWebHookLogFk < ActiveRecord::Migration[6.0] + include Gitlab::Database::PartitioningMigrationHelpers + + DOWNTIME = false + + disable_ddl_transaction! + + def up + add_concurrent_partitioned_foreign_key :web_hook_logs_part_0c5294f417, + :web_hooks, + column: :web_hook_id, + on_delete: :cascade + end + + def down + with_lock_retries do + remove_foreign_key_if_exists :web_hook_logs_part_0c5294f417, column: :web_hook_id + end + end +end diff --git a/db/post_migrate/20210413132500_reschedule_artifact_expiry_backfill_again.rb b/db/post_migrate/20210413132500_reschedule_artifact_expiry_backfill_again.rb new file mode 100644 index 00000000000..b4570c8398b --- /dev/null +++ b/db/post_migrate/20210413132500_reschedule_artifact_expiry_backfill_again.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +class RescheduleArtifactExpiryBackfillAgain < ActiveRecord::Migration[6.0] + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + MIGRATION = 'BackfillArtifactExpiryDate' + SWITCH_DATE = Date.new(2020, 06, 22).freeze + + disable_ddl_transaction! + + class JobArtifact < ActiveRecord::Base + include EachBatch + + self.inheritance_column = :_type_disabled + self.table_name = 'ci_job_artifacts' + + scope :without_expiry_date, -> { where(expire_at: nil) } + scope :before_switch, -> { where("date(created_at AT TIME ZONE 'UTC') < ?::date", SWITCH_DATE) } + end + + def up + Gitlab::BackgroundMigration.steal(MIGRATION) do |job| + job.delete + + false + end + + queue_background_migration_jobs_by_range_at_intervals( + JobArtifact.without_expiry_date.before_switch, + MIGRATION, + 2.minutes, + batch_size: 200_000 + ) + end + + def down + Gitlab::BackgroundMigration.steal(MIGRATION) do |job| + job.delete + + false + end + end +end diff --git a/db/post_migrate/20210415074645_index_members_on_user_id_access_level_requested_at_is_null.rb b/db/post_migrate/20210415074645_index_members_on_user_id_access_level_requested_at_is_null.rb new file mode 100644 index 00000000000..fb379d48b2a --- /dev/null +++ b/db/post_migrate/20210415074645_index_members_on_user_id_access_level_requested_at_is_null.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class IndexMembersOnUserIdAccessLevelRequestedAtIsNull < ActiveRecord::Migration[6.0] + include Gitlab::Database::MigrationHelpers + + disable_ddl_transaction! + + INDEX_NAME = 'index_members_on_user_id_and_access_level_requested_at_is_null' + + def up + add_concurrent_index(:members, [:user_id, :access_level], where: 'requested_at IS NULL', name: INDEX_NAME) + end + + def down + remove_concurrent_index_by_name(:members, INDEX_NAME) + end +end |