summaryrefslogtreecommitdiff
path: root/db/post_migrate/20170630111158_migrate_stages_statuses.rb
blob: 1641e550480be956e4d2b6236f7070e9b8dda59f (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
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

    execute <<-SQL.strip_heredoc
      UPDATE ci_stages SET status = null
    SQL
  end
end