<?php

use yii\db\Migration;

class m160324_075409_product_option extends Migration
{
    public function up()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            // Only for MySQL
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';

            // @todo https://habrahabr.ru/post/138947/
        } elseif ($this->db->driverName === 'pgsql') {
            // Only for PostgreSQL
            // @todo use intarray field for tax_options
        }

        $this->createTable('{{%product_option}}', [
            'product_id' => $this->integer()->notNull(),
            'option_id' => $this->integer()->notNull(),
        ], $tableOptions);
        $this->addPrimaryKey('product_option_pkey', 'product_option', ['product_id', 'option_id']);
        $this->addForeignKey('product_option_product_fkey', 'product_option', 'product_id', 'product', 'product_id', 'NO ACTION', 'NO ACTION');
        $this->addForeignKey('product_option_option_fkey', 'product_option', 'option_id', 'tax_option', 'tax_option_id', 'NO ACTION', 'NO ACTION');
    }

    public function down()
    {
        $this->dropTable('{{%product_option}}');
    }

    /*
    // Use safeUp/safeDown to run migration code within a transaction
    public function safeUp()
    {
    }

    public function safeDown()
    {
    }
    */
}
