<?php

use yii\db\Migration;

class m160324_145913_add_the_cases_sending_schedule_table extends Migration
{

    // Use safeUp/safeDown to run migration code within a transaction
    public function safeUp()
    {
        $this->createTable(
            'cases_schedule',
            [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer(11)->notNull(),
                'case_id' => $this->integer(11)->notNull(),
                'template_id' => $this->integer(11)->defaultValue(null),
                'send_date' => $this->date()->notNull(),
                'sended_date' => $this->dateTime()->defaultValue(null),
                'sended' => $this->boolean()->defaultValue(0),
                'actual' => $this->boolean()->defaultValue(1)
            ]
        );
        $this->addForeignKey(
            'fk_cases_schedule_user',
            'cases_schedule', 'user_id',
            'users', 'id'
        );
        $this->addForeignKey(
            'fk_cases_schedule_case_id',
            'cases_schedule', 'case_id',
            'co_content', 'id'
        );
        $this->addForeignKey(
            'fk_cases_schedule_template_id',
            'cases_schedule', 'template_id',
            'message_template', 'id'
        );
    }

    public function safeDown()
    {
        $this->dropForeignKey(
            'fk_cases_schedule_user',
            'cases_schedule'
        );
        $this->dropForeignKey(
            'fk_cases_schedule_case_id',
            'cases_schedule'
        );
        $this->dropForeignKey(
            'fk_cases_schedule_template_id',
            'cases_schedule'
        );
        $this->dropTable(
            'cases_schedule'
        );
    }

}
