20191016072826_replace_ci_trigger_requests_index.rb 922 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
# frozen_string_literal: true

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

  DOWNTIME = false

  disable_ddl_transaction!

  def up
    add_concurrent_index :ci_trigger_requests, [:trigger_id, :id], order: { id: :desc }

13 14 15 16 17 18
    # Some installations have legacy, duplicate indexes on
    # ci_trigger_requests.trigger_id. Rails won't drop them without an
    # explicit name: https://gitlab.com/gitlab-org/gitlab/issues/34818
    old_index_names.each do |name|
      remove_concurrent_index :ci_trigger_requests, [:trigger_id], name: name
    end
19 20 21 22 23 24 25
  end

  def down
    add_concurrent_index :ci_trigger_requests, [:trigger_id]

    remove_concurrent_index :ci_trigger_requests, [:trigger_id, :id], order: { id: :desc }
  end
26 27 28 29 30 31

  private

  def old_index_names
    indexes(:ci_trigger_requests).select { |i| i.columns == ['trigger_id'] }.map(&:name)
  end
32
end