m150401_100620_create_table__shop_delivery.php 4.03 KB
Newer Older
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
<?php
/**
 * @author Semenov Alexander <semenov@skeeks.com>
 * @link http://skeeks.com/
 * @copyright 2010 SkeekS (СкикС)
 * @date 01.04.2015
 */
use yii\db\Schema;
use yii\db\Migration;

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

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

        $this->createTable("{{%shop_delivery}}", [
            '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',

            'currency'              => Schema::TYPE_STRING. '(3) NULL DEFAULT "RUB"',

            'name'                  => Schema::TYPE_STRING . '(255) NOT NULL',
            'description'           => Schema::TYPE_TEXT . ' NULL',

            'period_from'           => Schema::TYPE_INTEGER . ' NULL',
            'period_to'             => Schema::TYPE_INTEGER . ' NULL',

            'period_type'           => Schema::TYPE_STRING . '(1) NULL',

            'weight_from'           => Schema::TYPE_INTEGER . ' NULL',
            'weight_to'             => Schema::TYPE_INTEGER . ' NULL',

            'order_price_from'      => Schema::TYPE_DECIMAL . '(18,2) NULL',
            'order_price_to'        => Schema::TYPE_DECIMAL . '(18,2) NULL',
            'order_currency'        => Schema::TYPE_STRING . '(3) NULL',

52
            'active'                => Schema::TYPE_SMALLINT . ' NOT NULL DEFAULT 1',
53 54 55 56 57 58 59 60 61 62 63 64 65

            'price'                 => Schema::TYPE_DECIMAL . '(18,2)',
            'currency'              => Schema::TYPE_STRING. '(3) DEFAULT "RUB"',

        ], $tableOptions);

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

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

        $this->execute("ALTER TABLE {{%shop_delivery}} ADD INDEX(currency);");
66
        $this->execute("ALTER TABLE {{%shop_delivery}} ADD INDEX(active);");
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        $this->execute("ALTER TABLE {{%shop_delivery}} ADD INDEX(name);");

        $this->execute("ALTER TABLE {{%shop_delivery}} ADD INDEX(period_from);");
        $this->execute("ALTER TABLE {{%shop_delivery}} ADD INDEX(period_to);");

        $this->execute("ALTER TABLE {{%shop_delivery}} ADD INDEX(weight_from);");
        $this->execute("ALTER TABLE {{%shop_delivery}} ADD INDEX(weight_to);");

        $this->execute("ALTER TABLE {{%shop_delivery}} ADD INDEX(order_price_from);");
        $this->execute("ALTER TABLE {{%shop_delivery}} ADD INDEX(order_price_to);");
        $this->execute("ALTER TABLE {{%shop_delivery}} ADD INDEX(order_currency);");

        $this->execute("ALTER TABLE {{%shop_delivery}} ADD INDEX(price);");
        $this->execute("ALTER TABLE {{%shop_delivery}} ADD INDEX(currency);");


83
        $this->execute("ALTER TABLE {{%shop_delivery}} COMMENT = 'Система доставки';");
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

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

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

    public function safeDown()
    {
        $this->dropForeignKey("shop_delivery_updated_by", "{{%shop_delivery}}");
        $this->dropForeignKey("shop_delivery_updated_by", "{{%shop_delivery}}");

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