summaryrefslogtreecommitdiff
path: root/db/post_migrate/20210825193652_backfill_cadence_id_for_boards_scoped_to_iteration.rb
blob: 1f6650140d43790acecac58423227cfc649bbc1c (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
41
42
43
44
45
46
47
48
49
# frozen_string_literal: true

class BackfillCadenceIdForBoardsScopedToIteration < Gitlab::Database::Migration[1.0]
  disable_ddl_transaction!

  BATCH_SIZE = 1000
  DELAY = 2.minutes.to_i
  MIGRATION = 'BackfillIterationCadenceIdForBoards'

  class MigrationBoard < ApplicationRecord
    include EachBatch

    self.table_name = 'boards'
  end

  def up
    schedule_backfill_group_boards
    schedule_backfill_project_boards
  end

  def down
    MigrationBoard.where.not(iteration_cadence_id: nil).each_batch(of: BATCH_SIZE) do |batch, index|
      range = batch.pick(Arel.sql('MIN(id)'), Arel.sql('MAX(id)'))
      delay = index * DELAY

      migrate_in(delay, MIGRATION, ['none', 'down', *range])
    end
  end

  private

  def schedule_backfill_project_boards
    MigrationBoard.where(iteration_id: -4).where.not(project_id: nil).where(iteration_cadence_id: nil).each_batch(of: BATCH_SIZE) do |batch, index|
      range = batch.pick(Arel.sql('MIN(id)'), Arel.sql('MAX(id)'))
      delay = index * DELAY

      migrate_in(delay, MIGRATION, ['project', 'up', *range])
    end
  end

  def schedule_backfill_group_boards
    MigrationBoard.where(iteration_id: -4).where.not(group_id: nil).where(iteration_cadence_id: nil).each_batch(of: BATCH_SIZE) do |batch, index|
      range = batch.pick(Arel.sql('MIN(id)'), Arel.sql('MAX(id)'))
      delay = index * DELAY

      migrate_in(delay, MIGRATION, ['group', 'up', *range])
    end
  end
end