summaryrefslogtreecommitdiff
path: root/db/post_migrate/20170526190000_migrate_build_stage_reference_again.rb
blob: 01b6cf3a15895a4d18f184e622e62ffa60718148 (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
# rubocop:disable Migration/UpdateLargeTable
class MigrateBuildStageReferenceAgain < ActiveRecord::Migration[4.2]
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  def up
    stage_id = Arel.sql <<-SQL.strip_heredoc
      (SELECT id FROM ci_stages
         WHERE ci_stages.pipeline_id = ci_builds.commit_id
           AND ci_stages.name = ci_builds.stage)
    SQL

    disable_statement_timeout do
      update_column_in_batches(:ci_builds, :stage_id, stage_id) do |table, query|
        query.where(table[:stage_id].eq(nil))
      end
    end
  end

  def down
    disable_statement_timeout do
      update_column_in_batches(:ci_builds, :stage_id, nil)
    end
  end
end