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 yii\db\Migration;
 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

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM