summaryrefslogtreecommitdiff
path: root/db/post_migrate/20170508170547_add_head_pipeline_for_each_merge_request.rb
blob: 81e9d05066863bff761c0cbcecb52d0e5ff7b6f5 (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 AddHeadPipelineForEachMergeRequest < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  def up
    disable_statement_timeout

    pipelines = Arel::Table.new(:ci_pipelines)
    merge_requests = Arel::Table.new(:merge_requests)

    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]))

    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)
  end

  def down
  end
end