<?php

use yii\db\Migration;

/**
 * Handles the creation of table `event_lang`.
 */
class m180507_123248_create_event_lang_table extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $this->createTable('event_lang', [
            'id' => $this->primaryKey(),
            'event_id' => $this->integer()
                                      ->notNull(),
            'language_id'     => $this->integer()
                                      ->notNull(),
            'title'           => $this->string(255),
            'body'            => $this->text(),
            'body_preview'    => $this->text(),
            'alias_id'        => $this->integer(),
        ]);
    
        $this->createIndex(
            'event_lang_uk',
            'event_lang',
            [
                'event_id',
                'language_id',
            ],
            true
        );
    
        $this->createIndex(
            'event_alias_uk',
            'event_lang',
            'alias_id',
            true
        );
    
        /**
         * Add foreign keys in blog_articles and language tables
         */
        $this->addForeignKey(
            'event_fk',
            'event_lang',
            'event_id',
            'event',
            'id',
            'CASCADE',
            'CASCADE'
        );
    
        $this->addForeignKey(
            'event_lang_fk',
            'event_lang',
            'language_id',
            'language',
            'id',
            'RESTRICT',
            'CASCADE'
        );
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $this->dropForeignKey('event_lang_fk', 'event_lang');
        $this->dropForeignKey('event_fk', 'event_lang');
        $this->dropIndex('event_alias_uk', 'event_lang');
        $this->dropIndex('event_lang_uk', 'event_lang');
        $this->dropTable('event_lang');
    }
}
