.\yii migrate/create create_test_table #创建一个数据库迁移
.\yii migrate #提交所有的迁移
.\yii migrate m160623_034801_create_test_table #指定类名,提交一个迁移
.\yii migrate/down #还原最近一次迁移:
.\yii migrate/redo #重做最近一次提交的迁移【先down后up】
.\yii migrate/history all #显示所有的提交迁移
.\yii migrate/new all #显示所有的未提交迁移
<?php
use yii\db\Migration;
/**
* Class m190222_074920_new_table
*/
class m190222_074920_new_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$tableOptions = null;
if ($this->db->driverName === 'mysql') {
// http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
}
//创建数据表test_table并且定义表中字段
$this->createTable('{{%new_table}}', [
'id' => $this->primaryKey(),
'parent_id' => $this->integer(11)->defaultValue(0)->comment('父级菜单id'),
'menu_name' => $this->string(100)->notNull()->comment('菜单名称'),
'menu_type' => $this->string(100)->notNull()->comment('菜单类型(menu菜单,sub_menu子菜单)'),
'menu_action' => $this->string(100)->notNull()->comment('菜单链接'),
'menu_roles' => $this->string(100)->comment('角色'),
'menu_depth' => $this->smallInteger(1)->defaultValue(0)->comment('菜单深度'),
'menu_icon' => $this->text()->comment('ICON代码:图标'),
'menu_des' => $this->text()->comment('菜单简介'),
'menu_order' => $this->smallInteger(1)->defaultValue(0)->comment('显示顺序'),
'menu_show' => $this->smallInteger(1)->defaultValue(0)->comment('是否显示(0:显示, 1:不显示)'),
'created_at' => $this->integer(),
'updated_at' => $this->integer(),
],$tableOptions);
//插入一条数据
$this->insert('{{%new_table}}',[
'id' => 1,
'menu_name' => '管理',
'menu_type' => 'menu'
]);
//添加字段
$this->addColumn('{{%new_table}}', 'aaa2', 'INT(10) NOT NULL COMMENT "审核人"');
//修改字段
$this->alterColumn('{{%new_table}}', 'menu_des', 'SMALLINT(4) NOT NULL DEFAULT 0 COMMENT "绑定状态,0:解绑 1:未绑定 2:审核中 3:审核通过 4:审核拒绝 5:禁用"');
//修改字段名
$this->renameColumn('{{%new_table}}','created_at','created_at2');
//增加索引
$this->createIndex('{{aaa}}', "{{%new_table}}", ['created_at2'],true);
//删除字段
$this->dropColumn('{{%new_table}}', 'updated_at');
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
//删除一条数据
$this->delete('{{%new_table}}',[
'id' => 1
]);
//drop掉整个表
$this->dropTable('{{%new_table}}');
}
/*
// Use up()/down() to run migration code without a transaction.
public function up()
{
}
public function down()
{
echo "m190222_074920_new_table cannot be reverted.\n";
return false;
}
*/
}
yii\db\Migration::execute(): 执行一条 SQL 语句
yii\db\Migration::insert(): 插入单行数据
yii\db\Migration::batchInsert(): 插入多行数据
yii\db\Migration::update(): 更新数据
yii\db\Migration::delete(): 删除数据
yii\db\Migration::createTable(): 创建表
yii\db\Migration::renameTable(): 重命名表名
yii\db\Migration::dropTable(): 删除一张表
yii\db\Migration::truncateTable(): 清空表中的所有数据
yii\db\Migration::addColumn(): 加一个字段
yii\db\Migration::renameColumn(): 重命名字段名称
yii\db\Migration::dropColumn(): 删除一个字段
yii\db\Migration::alterColumn(): 修改字段
yii\db\Migration::addPrimaryKey(): 添加一个主键
yii\db\Migration::dropPrimaryKey(): 删除一个主键
yii\db\Migration::addForeignKey(): 添加一个外键
yii\db\Migration::dropForeignKey(): 删除一个外键
yii\db\Migration::createIndex(): 创建一个索引
yii\db\Migration::dropIndex(): 删除一个索引