<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddUniqueToNstUserLocations extends Migration
{
    public function up()
    {
        // First remove any existing duplicates
        \DB::statement('DELETE t1 FROM nst_user_locations t1
            INNER JOIN nst_user_locations t2
            WHERE t1.id > t2.id
            AND t1.business_id = t2.business_id
            AND t1.user_id = t2.user_id
            AND t1.type = t2.type
            AND t1.location_id = t2.location_id');

        Schema::table('nst_user_locations', function (Blueprint $table) {
            $table->unique(['business_id', 'user_id', 'type', 'location_id'], 'nst_user_loc_unique');
        });
    }

    public function down()
    {
        Schema::table('nst_user_locations', function (Blueprint $table) {
            $table->dropUnique('nst_user_loc_unique');
        });
    }
}
