m150307_162718_create_form_table.php 2.38 KB
Newer Older
semenov committed
1 2 3 4 5 6 7 8 9 10
<?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;

semenov committed
11
class m150307_162718_create_form_table extends Migration
semenov committed
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
{
    public function up()
    {
        $tableExist = $this->db->getTableSchema("{{%form_form}}", 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_form}}", [
            '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',

            'name'                  => Schema::TYPE_STRING . '(255) NOT NULL',
            'description'           => Schema::TYPE_TEXT . ' NULL',
semenov committed
37
            'template'              => Schema::TYPE_TEXT . ' NULL',
semenov committed
38

semenov committed
39
            'code'                  => Schema::TYPE_STRING . '(32) NULL',
1  
semenov committed
40

semenov committed
41 42 43 44 45 46 47 48 49
        ], $tableOptions);

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

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

        $this->execute("ALTER TABLE {{%form_form}} ADD INDEX(name);");
semenov committed
50
        $this->execute("ALTER TABLE {{%form_form}} ADD UNIQUE(code);");
semenov committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

        $this->execute("ALTER TABLE {{%form_form}} COMMENT = 'Конструктор форм';");

        $this->addForeignKey(
            'form_form_created_by', "{{%form_form}}",
            'created_by', '{{%cms_user}}', 'id', 'RESTRICT', 'RESTRICT'
        );

        $this->addForeignKey(
            'form_form_updated_by', "{{%form_form}}",
            'updated_by', '{{%cms_user}}', 'id', 'RESTRICT', 'RESTRICT'
        );
    }

    public function down()
    {
        $this->dropForeignKey("form_form_created_by", "{{%form_form}}");
        $this->dropForeignKey("form_form_updated_by", "{{%form_form}}");

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