m150307_162720_create_form_email_table.php 2.52 KB
Newer Older
semenov committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
<?php
/**
 * @author Semenov Alexander <semenov@skeeks.com>
 * @link http://skeeks.com/
 * @copyright 2010 SkeekS (СкикС)
 * @date 07.03.2015
 */
use yii\db\Schema;
use yii\db\Migration;

class m150307_162720_create_form_email_table extends Migration
{
    public function up()
    {
        $tableExist = $this->db->getTableSchema("{{%form_email}}", true);
        if ($tableExist)
        {
            return true;
        }

        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB';
        }

        $this->createTable("{{%form_email}}", [
            'id'                    => Schema::TYPE_PK,

            'created_by'            => Schema::TYPE_INTEGER . ' NULL',
            'updated_by'            => Schema::TYPE_INTEGER . ' NULL',

            'created_at'            => Schema::TYPE_INTEGER . ' NULL',
            'updated_at'            => Schema::TYPE_INTEGER . ' NULL',

            'value'                  => Schema::TYPE_STRING . '(255) NOT NULL',
            'form_id'                => Schema::TYPE_INTEGER . '(255) NOT NULL',

        ], $tableOptions);

        $this->execute("ALTER TABLE {{%form_email}} ADD INDEX(updated_by);");
        $this->execute("ALTER TABLE {{%form_email}} ADD INDEX(created_by);");

        $this->execute("ALTER TABLE {{%form_email}} ADD INDEX(created_at);");
        $this->execute("ALTER TABLE {{%form_email}} ADD INDEX(updated_at);");

        $this->execute("ALTER TABLE {{%form_email}} ADD INDEX(value);");
        $this->execute("ALTER TABLE {{%form_email}} ADD UNIQUE(form_id,value);");

        $this->execute("ALTER TABLE {{%form_email}} COMMENT = 'Email адреса форм';");

        $this->addForeignKey(
            'form_email_created_by', "{{%form_email}}",
            'created_by', '{{%cms_user}}', 'id', 'SET NULL', 'SET NULL'
        );

        $this->addForeignKey(
            'form_email_updated_by', "{{%form_email}}",
            'updated_by', '{{%cms_user}}', 'id', 'SET NULL', 'SET NULL'
        );

        $this->addForeignKey(
            'form_email_form_id', "{{%form_email}}",
            'form_id', '{{%form_form}}', 'id', 'CASCADE', 'CASCADE'
        );
    }

    public function down()
    {
        $this->dropForeignKey("form_email_created_by", "{{%form_email}}");
        $this->dropForeignKey("form_email_updated_by", "{{%form_email}}");

        $this->dropForeignKey("form_email_form_id", "{{%form_email}}");

        $this->dropTable("{{%form_email}}");
    }
}