summaryrefslogtreecommitdiff
path: root/db/post_migrate/20190911251732_sync_issuables_state_id.rb
blob: 031837122fbba8e3551086634daa5cbc356a9b9e (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# frozen_string_literal: true

# Sync remaining records for issues/merge_requests tables where state_id
# is still null.
# For more information check: https://gitlab.com/gitlab-org/gitlab/issues/26823
# It creates a temporary index before performing the UPDATES to sync values.
#
# In 09-11-2019 we have the following numbers for records with state_id == nil:
#
# 1348 issues - default batch size for each update 67
# 10247 merge requests - default batch size for each update 511

class SyncIssuablesStateId < ActiveRecord::Migration[5.2]
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  def up
    %i(issues merge_requests).each do |table|
      temp_index_name = index_name_for(table)

      add_concurrent_index(
        table,
        'id',
        name: temp_index_name,
        where: 'state_id IS NULL'
      )

      update_value = update_condition_for(table)

      update_column_in_batches(table, :state_id, update_value) do |table, query|
        query.where(table[:state_id].eq(nil))
      end
    ensure
      remove_concurrent_index_by_name(table, temp_index_name)
    end
  end

  def down
    # NO OP
  end

  def update_condition_for(table)
    value_expresson =
      if table == :issues
        issues_state_id_condition
      else
        merge_requests_state_id_condition
      end

    Arel.sql(value_expresson)
  end

  def index_name_for(table)
    "idx_tmp_on_#{table}_where_state_id_is_null"
  end

  def issues_state_id_condition
    <<~SQL
      CASE state
      WHEN 'opened' THEN 1
      WHEN 'closed' THEN 2
      ELSE 2
      END
    SQL
  end

  def merge_requests_state_id_condition
    <<~SQL
      CASE state
      WHEN 'opened' THEN 1
      WHEN 'closed' THEN 2
      WHEN 'merged' THEN 3
      WHEN 'locked' THEN 4
      ELSE 2
      END
    SQL
  end
end