summaryrefslogtreecommitdiff
path: root/db/post_migrate/20170628080858_migrate_stage_id_reference_in_background.rb
blob: 30849ea1361147ea90c9606175b78360215f202f (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
class MigrateStageIdReferenceInBackground < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false
  BATCH_SIZE = 10000
  MIGRATION = 'MigrateBuildStageIdReference'.freeze

  disable_ddl_transaction!

  class Build < ActiveRecord::Base
    self.table_name = 'ci_builds'
  end

  def up
    index = 1

    Build.where(stage_id: nil).in_batches(of: BATCH_SIZE) do |relation|
      jobs = relation.pluck(:id).map { |id| [MIGRATION, [id]] }
      schedule = index * 5.minutes
      index += 1

      BackgroundMigrationWorker.perform_bulk_in(schedule, jobs)
    end
  end

  def down
    # noop
  end
end