YII2.0实战之-数据库迁移

建立迁移

建立新的迁移请运行以下命令:

 1 yii migrate/create <name></name> 

必须的 name 参数指定了迁移的简要描述。例如,如果迁移建立名为 goods的新表,使用以下命令:

 1 yii migrate/create create_goods_table 

你很快将看到,name 参数用作迁移版本中 PHP 类名的一部分。因此,这个参数只能够是包含字母、数字或下划线的字符。

以上命令将建立一个名为 m101129_185401_create_goods_table.php 的新文件。该文件将创建在@app/migrations 目录内。

注意类名和文件名相同,都遵循 m<timestamp>_<name> 模式,其中:

  • <timestamp> 指迁移创建时的 UTC 时间戳 (格式是 yymmdd_hhmmss),
  • <name> 从命令中的 name 参数获取。

这个类中。 up() 方法应包括实际实现数据库迁移的代码。换言之, up() 方法执行了实际改变数据库的代码。down()方法包括回退前版本的代码。

有时,用 down() 撤销数据库迁移是不可能的。例如,如果迁移删除表的某些行或整个表,那些数据将不能在 down() 方法里恢复。这种情况,该迁移称为不可逆迁移,即数据库不能回退到前一状态。当迁移是不可逆的,在以上生成代码的down() 方法将返回 false 来表明这个迁移版本不能回退。

建立新表

 1 <?php
 2 
 3 use yiidbMigration;
 4 
 5 /**
 6  * Handles the creation for table `admin_table`.
 7  */
 8 class m170604_103939_create_goods_table extends Migration
 9 {
10     /**
11      * @inheritdoc
12      */
13     public function up()//数据库结构代码
14     {
15         $this->createTable('goods', [
16             //主键id
17             'id' => $this->primaryKey(),//id  int(11) PrimaryKey
18             //商品名称
19             'goods_name' => $this->string(32)->notNull(),//godds_name varchar(32) not null
20             //商品数量
21             'goods_num' => $this->bigInteger()->notNull(),//godds_name bigint not null
22             //市场价格
23             'market_price' => $this->money(10,2)->notNull(),// market_price  decimal(10,2) not null 
24             //商铺价格
25             'shop_price' => $this->money(10,2)->notNull(),// shopprice decimal(10,2) not null
26             //属性id
27             'attr_id'=> $this->bigInteger()->notNull(),//attr_id big int not null
28             //分类id
29             'cat_id'=> $this->bigInteger()->notNull(),//cat_id big int not null
30             //商品评分
31             'goods_score'=>$this->smallInteger(1)->notNull(),//goods_score small int(1) not null
32             //商品大图地址
33             'goods_big_pic'=>$this->string(32)->notNull()->defaultValue(''),//goods_big_pic varchar(32) not null default''
34             //商品中图地址
35             'goods_medium_pic'=>$this->string(32)->notNull()->defaultValue(''),//goods_medium_pic varchar(32) not null default''
36             //商品小图地址
37             'goods_small_pic'=>$this->string(32)->notNull()->defaultValue(''),//goods_small_pic varchar(32) not null default''
38             'create_time' =>$this->dateTime()->notNull()->defaultValue(date('Y-m-d H:i:s')),
39             'last_update_time'=>$this->timestamp()->notNull()->defaultValue(date('Y-m-d H:i:s')),
40             //
41         ]);
42 
43        //插入一条数据
44         $this->insert('goods',[
45               //主键id
46             'id' => 1,
47             //商品名称
48             'goods_name' => 'mac pro',
49             //商品数量
50             'goods_num' => 12,
51             //市场价格
52             'market_price' => 9288,
53             //商铺价格
54             'shop_price' => 7500,
55             //属性id
56             'attr_id'=> 1,
57             //分类id
58             'cat_id'=> 1,
59             //商品评分
60             'goods_score'=> 5,
61             //商品大图地址
62             'goods_big_pic'=>'',
63             //商品中图地址
64             'goods_medium_pic'=>'',
65             //商品小图地址
66             'goods_small_pic'=>date('Y-m-d H:i:s'),
67             'create_time' =>'’,
68             'last_update_time'=>'',
69             
70         ]);
71 
72         //插入一列
73         // $this->addColumn('admin','last_login_ip',$this->smallInteger()->notNull());
74     }
75 
76      /**
77      * 还原数据表改动的方法
78      * 执行顺序要与up方法内部操作顺序相反
79      * @inheritdoc
80      */
81     public function down()//改写
82     {
83         $this->dropTable('goods');
84 
85     }
86 }

可以参考类中的方法增加列或删除列,更新列

可用参考

中的方法建表

应用迁移

要应用所有可用的新迁移(如,升级本地数据库),运行以下命令:

 1 yii migrate 

该命令将显示所有新迁移列表。如果你确认应用这些迁移,它将会按类名的时间戳一个接一个地运行每个新迁移类的up() 方法。

应用迁移成功后,迁移工具将在名为 migration 的数据库表保持迁移记录。这就允许该工具区分应用和未应用的迁移。如果 migration 表不存在,迁移工具将通过 db 组件自动在数据库中创建。

有时,我们只想应用一个或少量的新迁移,可以使用以下命令:

 1 yii migrate/up 3 

这个命令将应用3个新的迁移,改变这个值就改变拟应用的迁移数量。

也可以迁移数据库到特定版本,命令如下:

 1 yii migrate/to 101129_185401 

那就是,使用迁移数据表名的时间戳部分来指定需要迁移数据库的版本。如果在最后应用的迁移和指定迁移间有多个迁移,所有这些迁移将被应用。如果指定的迁移已经被应用,那么所有在其后应用的迁移将回退(指南下一节将描述)。

参考资料:http://www.yiifans.com/yii2/guide/db-migrations.html

 

原文地址:https://www.cnblogs.com/zhoupufelix/p/6974297.html