Blame view

console/migrations/m160304_065108_product.php 4.89 KB
a8370482   Alexander Karnovsky   init project
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  <?php
  
  use yii\db\Migration;
  
  class m160304_065108_product 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
          }
9d33ce37   Karnovsky A   New product migra...
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  
          $this->createTable('{{%category_name}}', [
              'category_name_id' => $this->primaryKey(),
              'category_id' => $this->integer()->notNull(),
              'value' => $this->string(250),
          ], $tableOptions);
  
          $this->createTable('{{%category}}', [
              'category_id' => $this->primaryKey(),
              'parent_id' => $this->integer()->notNull()->defaultValue(0),
              'path' => 'INT[]',
              'depth' => $this->integer()->notNull()->defaultValue(0),
              'alias' => $this->string(250),
              'image' => $this->string(255),
              'meta_title' => $this->string(255),
              'meta_desc' => $this->text(),
              'meta_robots' => $this->string(50),
              'seo_text' => $this->text(),
              'category_name_id' => $this->integer(),
              'product_unit_id' => $this->integer()
a8370482   Alexander Karnovsky   init project
39
40
          ], $tableOptions);
  
9d33ce37   Karnovsky A   New product migra...
41
42
43
          $this->addForeignKey('category_name_fkey', 'category', 'category_name_id', 'category_name', 'category_name_id', 'CASCADE', 'CASCADE');
          $this->addForeignKey('category_name_category_fkey', 'category_name', 'category_id', 'category', 'category_id', 'NO ACTION', 'NO ACTION');
  
a8370482   Alexander Karnovsky   init project
44
45
46
47
          $this->createTable('{{%product_category}}', [
              'product_id' => $this->integer()->notNull(),
              'category_id' => $this->integer()->notNull(),
          ], $tableOptions);
9d33ce37   Karnovsky A   New product migra...
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
  
          $this->createTable('{{%brand_name}}', [
              'brand_name_id' => $this->primaryKey(),
              'brand_id' => $this->integer()->notNull(),
              'value' => $this->string(250),
          ], $tableOptions);
  
          $this->createTable('{{%brand}}', [
              'brand_id' => $this->primaryKey(),
              'brand_name_id' => $this->integer(),
              'alias' => $this->string(250),
              'image' => $this->string(255),
              'meta_title' => $this->string(255),
              'meta_desc' => $this->text(),
              'meta_robots' => $this->string(50),
              'seo_text' => $this->text(),
          ], $tableOptions);
  
          $this->addForeignKey('brand_name_fkey', 'brand', 'brand_name_id', 'brand_name', 'brand_name_id', 'CASCADE', 'CASCADE');
          $this->addForeignKey('brand_name_brand_fkey', 'brand_name', 'brand_id', 'brand', 'brand_id', 'NO ACTION', 'NO ACTION');
  
          $this->createTable('{{%product}}', [
              'product_id' => $this->primaryKey(),
              'name' => $this->string(255)->notNull(),
              'brand_id' => $this->integer(),
          ], $tableOptions);
  
a8370482   Alexander Karnovsky   init project
75
          $this->addForeignKey('fki_product_id', 'product_category', 'product_id', 'product', 'product_id', 'NO ACTION', 'NO ACTION');
9d33ce37   Karnovsky A   New product migra...
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
          $this->addForeignKey('fki_category_id', 'product_category', 'category_id', 'category', 'category_id', 'NO ACTION', 'NO ACTION');
          $this->addForeignKey('fki_brand_id', 'product', 'brand_id', 'brand', 'brand_id', 'NO ACTION', 'CASCADE');
  
          $this->createTable('{{%product_variant}}', [
              'product_variant_id' => $this->primaryKey(),
              'product_id' => $this->integer()->notNull(),
              'name' => $this->string(255)->notNull(),
              'sku' => $this->string(255)->notNull(),
              'price' => $this->float(),
              'price_old' => $this->float(),
              'stock' => $this->float(),
              'product_unit_id' => $this->integer()->notNull(),
          ], $tableOptions);
  
          $this->createTable('{{%product_unit}}', [
              'product_unit_id' => $this->primaryKey(),
              'name' => $this->string(255)->notNull(),
              'code' => $this->string(50)->notNull(),
              'is_default' => $this->boolean()
          ], $tableOptions);
  
          $this->addForeignKey('product_variant_product_unit_fkey', 'product_variant', 'product_unit_id', 'product_unit', 'product_unit_id', 'CASCADE', 'NO ACTION');
          $this->addForeignKey('category_product_unit_fkey', 'category', 'product_unit_id', 'product_unit', 'product_unit_id', 'NO ACTION', 'NO ACTION');
a8370482   Alexander Karnovsky   init project
99
100
101
102
      }
  
      public function down()
      {
9d33ce37   Karnovsky A   New product migra...
103
104
          $this->dropTable('{{%category}}');
          $this->dropTable('{{%category_name}}');
a8370482   Alexander Karnovsky   init project
105
          $this->dropTable('{{%product_category}}');
9d33ce37   Karnovsky A   New product migra...
106
107
108
          $this->dropTable('{{%product}}');
          $this->dropTable('{{%product_variant}}');
          $this->dropTable('{{%product_unit}}');
a8370482   Alexander Karnovsky   init project
109
110
111
112
113
114
115
116
117
118
119
120
121
      }
  
      /*
      // Use safeUp/safeDown to run migration code within a transaction
      public function safeUp()
      {
      }
  
      public function safeDown()
      {
      }
      */
  }