class AddIndexForShixunSecretRepositories < ActiveRecord::Migration[5.2]
  def change
    shixun_ids = ShixunSecretRepository.pluck(:shixun_id).uniq
    shixuns = Shixun.where(id: shixun_ids)
    shixuns.find_each do |shixun|
      id = shixun.shixun_secret_repository.id
      shixun_secret_repositories = ShixunSecretRepository.where(shixun_id: shixun.id).where.not(id: id)
      shixun_secret_repositories.destroy_all
    end

    remove_index :shixun_secret_repositories, :shixun_id
    add_index :shixun_secret_repositories, :shixun_id, unique: true



  end
end
