<?php

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

class CreateMessagesTable extends Migration
{

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('messages', function (Blueprint $table) {
            $table->increments('oid');
            $table->integer('useroid')->unsigned();
            $table->integer('groupoid')->unsigned();
            $table->mediumText('text')->nullable();
            $table->string('file', 255)->nullable();
            $table->string('file_original_name', 255)->nullable();
            $table->timestamps();

            $table->foreign('useroid')->references('oid')->on('user')->onDelete('cascade');
            $table->foreign('groupoid')->references('oid')->on('group')->onDelete('cascade');

            $table->index([ 'useroid' ]);
            $table->index([ 'groupoid' ]);
            $table->index([ 'created_at' ]);
            $table->index([ 'updated_at' ]);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        DB::statement('SET FOREIGN_KEY_CHECKS = 0');
        Schema::dropIfExists('messages');
        DB::statement('SET FOREIGN_KEY_CHECKS = 1');
    }
}
