# frozen_string_literal: true

class AddCompoundIndexToVulnerabilityRemediationsTable < ActiveRecord::Migration[6.0]
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false
  NEW_INDEX_NAME = 'index_vulnerability_remediations_on_project_id_and_checksum'
  OLD_INDEX_NAME = 'index_vulnerability_remediations_on_checksum'

  disable_ddl_transaction!

  def up
    add_concurrent_index :vulnerability_remediations, [:project_id, :checksum], unique: true, name: NEW_INDEX_NAME
    add_concurrent_foreign_key :vulnerability_remediations, :projects, column: :project_id

    remove_concurrent_index_by_name :vulnerability_remediations, OLD_INDEX_NAME
  end

  def down
    add_concurrent_index :vulnerability_remediations, :checksum, unique: true, name: OLD_INDEX_NAME

    remove_concurrent_index_by_name :vulnerability_remediations, NEW_INDEX_NAME

    with_lock_retries do
      remove_foreign_key_if_exists :vulnerability_remediations, column: :project_id
    end
  end
end
