summaryrefslogtreecommitdiff
path: root/db/migrate/20170803130232_reorganise_issues_indexes_for_faster_sorting.rb
blob: eb7d1be17320f46266d3d31202d6239b4694bbc2 (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
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.

class ReorganiseIssuesIndexesForFasterSorting < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers

  # Set this constant to true if this migration requires downtime.
  DOWNTIME = false

  disable_ddl_transaction!

  REMOVE_INDEX_COLUMNS = %i[project_id created_at due_date updated_at].freeze

  ADD_INDEX_COLUMNS = [
    %i[project_id created_at id state],
    %i[project_id due_date id state],
    %i[project_id updated_at id state]
  ].freeze

  TABLE = :issues

  def up
    add_indexes(ADD_INDEX_COLUMNS)
    remove_indexes(REMOVE_INDEX_COLUMNS)
  end

  def down
    add_indexes(REMOVE_INDEX_COLUMNS)
    remove_indexes(ADD_INDEX_COLUMNS)
  end

  def add_indexes(columns)
    columns.each do |column|
      add_concurrent_index(TABLE, column) unless index_exists?(TABLE, column)
    end
  end

  def remove_indexes(columns)
    columns.each do |column|
      remove_concurrent_index(TABLE, column) if index_exists?(TABLE, column)
    end
  end
end