20170508170547_add_head_pipeline_for_each_merge_request.rb 801 Bytes
Newer Older
1
# rubocop:disable Migration/UpdateLargeTable
2
class AddHeadPipelineForEachMergeRequest < ActiveRecord::Migration
Felipe's avatar
Felipe committed
3 4
  include Gitlab::Database::MigrationHelpers

5 6
  DOWNTIME = false

7 8
  disable_ddl_transaction!

Felipe's avatar
Felipe committed
9 10
  def up
    disable_statement_timeout
11

Felipe's avatar
Felipe committed
12 13
    pipelines = Arel::Table.new(:ci_pipelines)
    merge_requests = Arel::Table.new(:merge_requests)
14

15 16 17 18 19
    head_id = pipelines
      .project(Arel::Nodes::NamedFunction.new('max', [pipelines[:id]]))
      .from(pipelines)
      .where(pipelines[:ref].eq(merge_requests[:source_branch]))
      .where(pipelines[:project_id].eq(merge_requests[:source_project_id]))
20

Felipe's avatar
Felipe committed
21 22 23
    sub_query = Arel::Nodes::SqlLiteral.new(Arel::Nodes::Grouping.new(head_id).to_sql)

    update_column_in_batches(:merge_requests, :head_pipeline_id, sub_query)
24 25 26 27 28
  end

  def down
  end
end