<?php

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

class AddItemSizeIdFieldToSubscriptionItems extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('subscription_items', function(Blueprint $table) {
            if (!Schema::hasColumn('subscription_items', 'shop_item_size_id')) {
                $table->integer('shop_item_size_id')->nullable()->after('shop_item_id');

                $table->foreign('shop_item_size_id', 'subscription_items__shop_item_size_id')
                    ->references('id')
                    ->on('items_sizes')
                    ->constrained()
                    ->onDelete('restrict')
                    ->onUpdate('cascade')
                ;
            }
        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('subscription_items', function(Blueprint $table) {
            $table->dropForeign('subscription_items__shop_item_size_id');
            $table->dropColumn('shop_item_size_id');
        });
    }
}

