<?php

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

class CreateChecklistVersionsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('checklist_versions', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('version_id');
            $table->unsignedInteger('checklist_id');
            $table->unsignedInteger('user_id');
            $table->unique([
                'version_id',
                'checklist_id',
            ], 'checklist_id_version_id_unique');
            $table->timestamps();
        });

        Schema::table('checklist_versions', function (Blueprint $table) {
            $table->foreign('version_id')->references('id')->on('versions');
            $table->foreign('checklist_id')->references('id')->on('checklists');
            $table->foreign('user_id')->references('id')->on('users');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('checklist_versions', function (Blueprint $table) {
            $table->dropForeign('checklist_versions_version_id_foreign');
            $table->dropForeign('checklist_versions_checklist_id_foreign');
            $table->dropForeign('checklist_versions_user_id_foreign');
        });

        Schema::dropIfExists('checklist_versions');
    }
}
