<?php

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use MtcPharmacy\Subscriptions\Classes\Subscription;

class AddUniqueKeyToSubscriptionsOrdersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $indexExists = collect(DB::select("SHOW INDEX FROM subscriptions_orders WHERE Key_name = 'subscriptions_orders__unique_sub_id_order_id'"))->isNotEmpty();

        if (!$indexExists) {
            Schema::table('subscriptions_orders', function (Blueprint $table) {
                $table->unique(['subscription_id', 'order_id'], 'subscriptions_orders__unique_sub_id_order_id');
            });
        }
   }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('subscriptions_orders', function (Blueprint $table) {
            $table->dropUnique('subscriptions_orders__unique_sub_id_order_id');
        });
    }
}

