class AddUserLoginUniqIndex < ActiveRecord::Migration[5.2]
  def change

    users = User.group("login").having("count(1) >1")
    ids = users.pluck(:id)
    logins = users.pluck(:login)
    User.where(login: logins).where.not(id: ids).destroy_all

    remove_index :users, :login
    add_index :users, :login, unique: true
  end
end
