class RenameEventIdToRequestIdInEvents < ActiveRecord::Migration[8.1]
  def change
    # Only rename the column if it still exists as event_id
    if column_exists?(:events, :event_id)
      rename_column :events, :event_id, :request_id
    end

    # Rename the unique index if it still exists with the old name
    if index_name_exists?(:events, :index_events_on_event_id)
      rename_index :events, :index_events_on_event_id, :index_events_on_request_id
    elsif !index_name_exists?(:events, :index_events_on_request_id)
      # Create the index with the new name if neither exists
      add_index :events, :request_id, unique: true, name: :index_events_on_request_id
    end
  end
end
