1.創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)遷移:?
yii migrate/create <name>
這是一個(gè)通用的創(chuàng)建數(shù)據(jù)遷移格式,其中<name>是必填的參數(shù)爱致,用來(lái)描述當(dāng)前遷移。
ps:<name>這個(gè)只能字母脾还、數(shù)字缕棵、下劃線,因?yàn)檫@個(gè)指令會(huì)生成一個(gè)遷移類(lèi)赠潦,<name>會(huì)不是這個(gè)類(lèi)的類(lèi)名的一部分叫胖。
舉例說(shuō)明,執(zhí)行以下指令:
./yii migrate/create?ArticleInit
如果想配置migration 在?console/config/main.php ?中controllerMap添加配置
'migrate' => [
? ? 'class' => yxx\db\controllers\MigrateController::class,
?? ?'migrationNamespaces' => [
? ? ? ? 'console\migrations',
? ? ? ? 'yxx\user\migrations',
? ? ],
],
執(zhí)行完后在create后默認(rèn)會(huì)在console/migrations目錄下生成一個(gè)控制器
up方法
public function safeUp()
{
? ? if ($this->db->driverName === 'mysql') {
? ? ? ? $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB';
? ? }
? ? $this->createTable('{{%article}}', [
? ? ? ? 'id' => $this->pk()->comment('文章ID'),
? ? ? ? 'userId' => $this->integer()->notNull()->comment('用戶(hù)ID'),
? ? ? ? 'title' => $this->string(60)->notNull()->comment('標(biāo)題'),
? ? ? ? 'selfTypeId' => $this->integer()->notNull()->comment('自定義分類(lèi)ID'),
? ? ? ? 'sysTypeId' => $this->integer()->notNull()->comment('系統(tǒng)分類(lèi)ID'),
? ? ? ? 'isTuijian' => $this->tinyInteger()->notNull()->comment('是否推薦'),
? ? ? ? 'isComment' => $this->tinyInteger()->notNull()->comment('是否可評(píng)論'),
? ? ? ? 'isPrivate' => $this->tinyInteger()->notNull()->comment('是否僅自己可見(jiàn)'),
? ? ? ? 'publishTime' => $this->timestamp()->notNull()->comment('發(fā)布時(shí)間'),
? ? ? ? 'tags' => $this->string(60)->notNull()->comment('標(biāo)簽她奥,最多5個(gè)臭家,英文逗號(hào)分隔'),
? ? ? ? 'summary' => $this->string(200)->notNull()->comment('摘要'),
? ? ? ? 'thumbUrl' => $this->string(120)->notNull()->comment('縮略圖地址'),
? ? ? ? 'createTime' => $this->createTime()->comment('創(chuàng)建時(shí)間'),
? ? ? ? 'updateTime' => $this->updateTime()->comment('更新時(shí)間')
? ? ], $tableOptions);
}
down方法一般為回撤
public function safeDown()
{
? ? echo "M190116032929ArticleInit cannot be reverted.\n";
? ? return false;
}
如下是所有這些數(shù)據(jù)庫(kù)訪問(wèn)方法的列表:
yii\db\Migration::execute(): 執(zhí)行一條 SQL 語(yǔ)句
yii\db\Migration::insert(): 插入單行數(shù)據(jù)
yii\db\Migration::batchInsert(): 插入多行數(shù)據(jù)
yii\db\Migration::update(): 更新數(shù)據(jù)
yii\db\Migration::delete(): 刪除數(shù)據(jù)
yii\db\Migration::createTable(): 創(chuàng)建表
yii\db\Migration::renameTable(): 重命名表名
yii\db\Migration::dropTable(): 刪除一張表
yii\db\Migration::truncateTable(): 清空表中的所有數(shù)據(jù)
yii\db\Migration::addColumn(): 加一個(gè)字段
yii\db\Migration::renameColumn(): 重命名字段名稱(chēng)
yii\db\Migration::dropColumn(): 刪除一個(gè)字段
yii\db\Migration::alterColumn(): 修改字段
yii\db\Migration::addPrimaryKey(): 添加一個(gè)主鍵
yii\db\Migration::dropPrimaryKey(): 刪除一個(gè)主鍵
yii\db\Migration::addForeignKey(): 添加一個(gè)外鍵
yii\db\Migration::dropForeignKey(): 刪除一個(gè)外鍵
yii\db\Migration::createIndex(): 創(chuàng)建一個(gè)索引
yii\db\Migration::dropIndex(): 刪除一個(gè)索引
2.提交遷移:
可以執(zhí)行如下:?
./yii migrate?
這個(gè)指令會(huì)提交所有的遷移?
或者這樣,指定類(lèi)名方淤,提交一個(gè)遷移?
./yii migrate M190116032929ArticleInit?
刪除某字段:
public function down() {$this->dropColumn('{{app_base}}', 'manager_id');}
刪除某張表:
public function down() {$this->dropTable('{{%file_storage_item}}');}
3.撤銷(xiāo)
./yii migrate/down 執(zhí)行某些撤銷(xiāo)對(duì)表的操作 ./yii migratre/to (遷移文件名)執(zhí)行某個(gè)指定的遷移文件 在創(chuàng)建數(shù)據(jù)表的過(guò)程中可以同時(shí)聲稱(chēng)多張表钉赁,刪除多張表 執(zhí)行過(guò)的遷移文件,會(huì)在數(shù)據(jù)庫(kù)的migration 中生成一條記錄携茂,記錄此遷移文件已經(jīng)執(zhí)行過(guò)你踩,下次將執(zhí)行數(shù)據(jù)表中不存在的遷移文件 注意: ./yii migrate/down 此命令執(zhí)行不只刪除了對(duì)數(shù)據(jù)庫(kù)的操作同時(shí)也會(huì)刪除migration數(shù)據(jù)表中的執(zhí)行記錄