summaryrefslogtreecommitdiff
path: root/db/post_migrate/20220830172142_reschedule_issue_work_item_type_id_backfill.rb
blob: 5495f0e53b468d3e957942d4b0fb71e4d58653f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# frozen_string_literal: true

class RescheduleIssueWorkItemTypeIdBackfill < Gitlab::Database::Migration[2.0]
  MIGRATION = 'BackfillWorkItemTypeIdForIssues'
  BATCH_SIZE = 10_000
  MAX_BATCH_SIZE = 30_000
  SUB_BATCH_SIZE = 100
  INTERVAL = 1.minute

  restrict_gitlab_migration gitlab_schema: :gitlab_main

  class MigrationWorkItemType < MigrationRecord
    self.table_name = 'work_item_types'

    def self.id_by_type
      where(namespace_id: nil).order(:base_type).pluck(:base_type, :id).to_h
    end
  end

  def up
    # We expect no more than 5 types. Only 3 of them are expected to have associated issues at the moment
    MigrationWorkItemType.id_by_type.each do |base_type, type_id|
      queue_batched_background_migration(
        MIGRATION,
        :issues,
        :id,
        base_type,
        type_id,
        job_interval: INTERVAL,
        batch_size: BATCH_SIZE,
        max_batch_size: MAX_BATCH_SIZE,
        sub_batch_size: SUB_BATCH_SIZE
      )
    end
  end

  def down
    Gitlab::Database::BackgroundMigration::BatchedMigration.where(job_class_name: MIGRATION).delete_all
  end
end