<?php

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

class CreateUserSessionsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user_session', function (Blueprint $table) {
            $table->increments('oid');
            $table->integer('user_oid')->unsigned();
            $table->string('ip_address', 155)->nullable();
            $table->string('token', 155)->nullable();

            $table->dateTime('created_at');
            $table->dateTime('updated_at');

            $table->foreign('user_oid')->references('oid')->on('user')->onDelete('cascade');

            $table->unique('token');
            $table->index('user_oid');
        });
    }

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