20170914135630_add_index_for_recent_push_events.rb 1017 Bytes
Newer Older
1 2 3
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.

4
class AddIndexForRecentPushEvents < ActiveRecord::Migration[4.2]
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
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  def up
    add_concurrent_index_if_not_present(
      :merge_requests,
      [:source_project_id, :source_branch]
    )

    remove_concurrent_index_if_present(:merge_requests, :source_project_id)
  end

  def down
    add_concurrent_index_if_not_present(:merge_requests, :source_project_id)

    remove_concurrent_index_if_present(
      :merge_requests,
      [:source_project_id, :source_branch]
    )
  end

  def add_concurrent_index_if_not_present(table, columns)
    return if index_exists?(table, columns)

    add_concurrent_index(table, columns)
  end

  def remove_concurrent_index_if_present(table, columns)
    return unless index_exists?(table, columns)

    remove_concurrent_index(table, columns)
  end
end