20170928100231_add_composite_index_on_merge_requests_merge_commit_sha.rb 940 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 AddCompositeIndexOnMergeRequestsMergeCommitSha < 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
  include Gitlab::Database::MigrationHelpers

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

  # The default index name is too long for PostgreSQL and would thus be
  # truncated.
  INDEX_NAME = 'index_merge_requests_on_tp_id_and_merge_commit_sha_and_id'

  COLUMNS = [:target_project_id, :merge_commit_sha, :id]

  disable_ddl_transaction!

  def up
    return if index_is_present?

    add_concurrent_index(:merge_requests, COLUMNS, name: INDEX_NAME)
  end

  def down
    return unless index_is_present?

    remove_concurrent_index(:merge_requests, COLUMNS, name: INDEX_NAME)
  end

  def index_is_present?
    index_exists?(:merge_requests, COLUMNS, name: INDEX_NAME)
  end
end