<?php

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

class ChangeSectorBlockRelation extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('sector_blocks', function (Blueprint $table) {
            DB::statement('ALTER TABLE sector_blocks CHARACTER SET utf8 COLLATE utf8_general_ci');
            $table->dropForeign(['sector_id']);
            $table->dropForeign(['block_id']);
            $table->dropPrimary();
            $table->string('language_id', 10)->default('en');

            $table->primary(['sector_id', 'block_id', 'language_id']);
            $table->foreign('sector_id')
              ->references('id')->on('sectors')
              ->onDelete('CASCADE')->onUpdate('CASCADE');
            $table->foreign('block_id')
              ->references('id')->on('blocks')
              ->onDelete('CASCADE')->onUpdate('CASCADE');
            $table->foreign('language_id')
              ->references('id')->on('languages')
              ->onDelete('CASCADE')->onUpdate('CASCADE');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('sector_blocks', function (Blueprint $table) {
            $table->dropForeign(['sector_id']);
            $table->dropForeign(['block_id']);
            $table->dropForeign(['language_id']);
            $table->dropPrimary();
            $table->dropColumn('language_id');

            $table->primary(['sector_id', 'block_id']);
            $table->foreign('sector_id')
              ->references('id')->on('sectors')
              ->onDelete('CASCADE')->onUpdate('CASCADE');
            $table->foreign('block_id')
              ->references('id')->on('blocks')
              ->onDelete('CASCADE')->onUpdate('CASCADE');
        });
    }
}
