summaryrefslogtreecommitdiff
path: root/db/post_migrate/20170711145558_migrate_stages_statuses.rb
blob: 5a24fb1307f4725a83e524722e2d9da356e51d94 (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
class MigrateStagesStatuses < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  BATCH_SIZE = 10000
  RANGE_SIZE = 1000
  MIGRATION = 'MigrateStageStatus'.freeze

  class Stage < ActiveRecord::Base
    self.table_name = 'ci_stages'
    include ::EachBatch
  end

  def up
    Stage.where(status: nil).each_batch(of: BATCH_SIZE) do |relation, index|
      relation.each_batch(of: RANGE_SIZE) do |batch|
        range = relation.pluck('MIN(id)', 'MAX(id)').first
        schedule = index * 5.minutes

        BackgroundMigrationWorker.perform_in(schedule, MIGRATION, range)
      end
    end
  end

  def down
    disable_statement_timeout

    update_column_in_batches(:ci_stages, :status, nil)
  end
end