summaryrefslogtreecommitdiff
path: root/db/post_migrate/20170628080858_migrate_stage_id_reference_in_background.rb
blob: 36aac3df071f3d161e03a3a6391bc3a83b3177ba (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 MigrateStageIdReferenceInBackground < ActiveRecord::Migration[4.2]
  include Gitlab::Database::MigrationHelpers

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

  disable_ddl_transaction!

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

  ##
  # It will take around 3 days to process 20M ci_builds.
  #
  def up
    Build.where(stage_id: nil).each_batch(of: BATCH_SIZE) do |relation, index|
      relation.each_batch(of: RANGE_SIZE) do |relation|
        range = relation.pluck('MIN(id)', 'MAX(id)').first

        BackgroundMigrationWorker
          .perform_in(index * 2.minutes, MIGRATION, range)
      end
    end
  end

  def down
    # noop
  end
end