summaryrefslogtreecommitdiff
path: root/db/post_migrate/20170503004427_upate_retried_for_ci_build.rb
blob: a0eb4fa818cfcc9e89a011e7db4d5c00e0401a17 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class UpateRetriedForCiBuild < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  def up
    disable_statement_timeout

    with_temporary_partial_index do
      update_column_in_batches(:ci_builds, :retried, &method(:retried_query)) do |table, query|
        query = query.where(table[:retried].eq(nil))
      end
    end
  end

  def down
  end

  private

  def retried_query(table, query, start_id, stop_id)
    latest_commits = <<-SQL.strip_heredoc
      SELECT DISTINCT ci_builds3.commit_id
        FROM ci_builds3
        WHERE #{start_id} <= id
    SQL

    latest_commits = "#{latest_commits} AND id < #{stop_id}" if stop_id

    latest_ids = <<-SQL.strip_heredoc
      SELECT MAX(ci_builds2.id) FROM ci_builds2
        WHERE commit_id IN (#{latest_commits})
        GROUP BY commit_id, name
    SQL

    latest_ids = <<-SQL.strip_heredoc
      SELECT * FROM (#{latest_ids}) AS latest_ids
    SQL

    value = Arel.sql("(ci_builds.id NOT IN (#{latest_ids}))")
    
    query.set([[table[:retried], value]])
  end

  def with_temporary_partial_index
    if Gitlab::Database.postgresql?
      execute 'CREATE INDEX CONCURRENTLY IF NOT EXISTS index_for_ci_builds_retried_migration ON ci_builds (id) WHERE retried IS NULL;'
    end

    yield

    if Gitlab::Database.postgresql?
      execute 'DROP INDEX CONCURRENTLY IF EXISTS index_for_ci_builds_retried_migration'
    end
  end
end