<?php

use yii\db\Migration;

/**
 * Handles the creation of table `event_to_tag`.
 */
class m180507_124405_create_event_to_tag_table extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $this->createTable('event_to_tag', [
            'event_id' => $this->integer()
                                      ->notNull(),
            'event_tag_id'     => $this->integer()
                                      ->notNull(),
        ]);
    
        /**
         * Create indexes and foreign keys for junction table
         */
        $this->createIndex(
            'event_to_tag_uk',
            'event_to_tag',
            [
                'event_id',
                'event_tag_id',
            ],
            true
        );
    
        $this->addForeignKey(
            'event_to_tag_tag_fk',
            'event_to_tag',
            'event_tag_id',
            'event_tag',
            'id',
            'CASCADE',
            'CASCADE'
        );
    
        $this->addForeignKey(
            'event_to_tag_art_fk',
            'event_to_tag',
            'event_id',
            'event',
            'id',
            'CASCADE',
            'CASCADE'
        );
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $this->dropForeignKey('event_to_tag_art_fk', 'event_to_tag');
        $this->dropForeignKey('event_to_tag_tag_fk', 'event_to_tag');
        $this->dropIndex('event_to_tag_uk', 'event_to_tag');
        $this->dropTable('event_to_tag');
    }
}
