<?php

use yii\db\Schema;
use yii\db\Migration;

class m160220_060520_update_sessions extends Migration
{
    // Use safeUp/safeDown to run migration code within a transaction
    public function safeUp()
    {
        $this->dropColumn('sessions', 'url');
        $this->dropColumn('sessions', 'referer');

        $this->createTable('sessions_url', [
            'id' => Schema::TYPE_PK,
            'session_id' => Schema::TYPE_INTEGER . '(11) NOT NULL',
            'url' => 'text DEFAULT NULL',
            'referer' => 'text DEFAULT NULL',
            'created_at' => Schema::TYPE_INTEGER . '(11) DEFAULT NULL',
            'updated_at' => Schema::TYPE_INTEGER . '(11) DEFAULT NULL',
        ]);

        $this->truncateTable('sessions');

        $this->addForeignKey(
            'fk_sessions_url_session_id_sessions_id',
            'sessions_url', 'session_id',
            'sessions', 'id'
        );
    }

    public function safeDown()
    {
        $this->addColumn('sessions', 'url', 'text DEFAULT NULL AFTER `ip`');
        $this->addColumn('sessions', 'referer', 'text DEFAULT NULL AFTER `url`');

        $this->dropForeignKey('fk_sessions_url_session_id_sessions_id', 'sessions_url');

        $this->dropTable('sessions_url');
    }
}
