Yii2数据库迁移

Posted 2020-02-28updated 2020-04-23Words 1.5kReading time 11m

简介

众所周知,生产环境的数据库、测试环境的数据库以及线上环境的数据库结构是一样的,但是在开发环境中,难免会遇到添加字段或者修改字段这种修改数据库表的操作,所以Yii 提供了一个 数据库迁移 功能,该功能可以记录数据库的变化, 以便使数据库和源代码一起受版本控制。

其实大概也就这些操作最熟悉不过了,其他具体请浏览Yii官方文档;不得不吐槽的是Yii2官网的实例少之又少,又因为在共同开发中需要用到数据库迁移,所以写这篇文章,后续有补充会在更新

数据库迁移之命名规则

  • 创建数据库表: create_tablename_{数据库表名}
  • 数据库表添加字段: add_column_to_{数据库表名}
  • 数据库表添加数据: add_auth_on_{数据库表名}
  • 数据库表删除字段: drop_xxx_from_{数据库表名}
  • 添加连接表: create_junction_xxx_and_yyy

创建数据库表

在控制台输入 php yii migrate/create create_tablename_{数据库表名}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public function safeUp()
{
//设置数据表的属性
$tableOptions = null;
if ($this->db->driverName === 'mysql') {
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB';
}

//创建一个表
$this->createTable('{{oa_employee_interview}}', [
'id' => $this->primaryKey(),
'employee_id' => $this->integer(11)->notNull()->comment('被访谈员工id'),
'jobnum' => $this->string(11)->notNull()->comment('被访谈员工工号'),
'name' => $this->string(11)->notNull()->comment('被访谈员工姓名'),
'jobstation' => $this->string(11)->notNull()->comment('被访谈员工岗位'),
'resign' => $this->string(11)->notNull()->comment('被访谈员工在职情况'),
'entryDate' => $this->string(11)->notNull()->comment('被访谈员工入职时间'),
'job_rank' => $this->string(30)->notNull()->comment('被访谈员工岗位等级'),
'dept' => $this->string(30)->notNull()->comment('被访谈员工部门'),
'is_transfer' => $this->string(50)->notNull()->defaultValue('N')->comment('是否愿意调岗:N/否,(描述)/是'),
'work_record' => $this->string(500)->notNull()->defaultValue('')->comment('工作能力记录'),
'work_level' => $this->string(100)->notNull()->defaultValue('')->comment('工作能力评级'),
'worth_record' => $this->string(500)->notNull()->defaultValue('')->comment('价值观记录'),
'worth_level' => $this->string(100)->notNull()->defaultValue('')->comment('价值观评级'),
'other' => $this->string(255)->notNull()->defaultValue('')->comment('其他'),
'delete_at' => $this->integer(11)->notNull()->defaultValue(0)->comment('删除时间'),
], $tableOptions);

//增加或者修改一个表备注
$this->addCommentOnTable('{{oa_employee_interview}}','员工访谈信息表');
}

添加字段

在控制台输入 php yii migrate/create add_column_to_{数据库表名}

1
2
3
4
5
6
public function safeUp()
{
//addColumn('表名','字段', '属性')
$this->addColumn('oa_product_negative_review','choose', $this->tinyInteger(1)->notNull()->defaultValue(0)->comment('是否自动分类(1:自动分类 2:否)'));
$this->addColumn('oa_product_comment_answer','file', $this->string(255)->notNull()->defaultValue('')->comment('数据分析图路径'));
}

添加索引

在控制台输入 php yii migrate/create add_index_on_{数据库表名}

1
2
3
4
public function safeUp()
{
$this->createIndex('idx_order_num', 'oa_product_evaluation_product', ['order_num']);
}

添加数据

在控制台输入 php yii migrate/create add_auth_to_{数据库表名}

1
2
3
4
5
6
7
8
9
10
11
public function safeUp()
{
$data = [
['AnnualMeetingDrawLots.Index', 0, '行政管理-活动管理-年会抽签-列表' , 'N;'],
['AnnualMeetingDrawLots.gift-giving', 0, '行政管理-活动管理-年会抽签-转增' , 'N;'],
['AnnualMeetingDrawLots.import-invitation', 0, '行政管理-活动管理-年会抽签-导入邀请名单' , 'N;'],
['AnnualMeetingDrawLots.import-draw-lots', 0, '行政管理-活动管理-年会抽签-导入抽签名单' , 'N;'],
];
$column = ['name', 'type', 'description', 'data'];
Yii::$app->db->createCommand()->batchInsert('{{%auth_item}}', $column, $data)->execute();
}

修改数据

在控制台输入 php yii migrate/create update_auth_to_{数据库表名}

1
2
3
4
5
public function safeUp()
{
Yii::$app->db->createCommand()->update('oa_auth_item', ['description' => '人力资源-员工访谈-访谈问题-回复'] ,['name' => 'EmployeeInterviewReply.ReplyAddInterview'])->execute();
Yii::$app->db->createCommand()->update('oa_auth_item', ['description' => '人力资源-员工访谈-访谈问题-修改'] ,['name' => 'EmployeeInterviewReply.ReplyEditInterview'])->execute();
}

删除数据

在控制台输入 php yii migrate/create del_auth_to_{数据库表名}

1
2
3
4
5
public function safeUp()
{
Yii::$app->db->createCommand()->delete('oa_auth_item', ['name' => 'EmployeeInterviewReply.AddInterview'])->execute();
Yii::$app->db->createCommand()->delete('oa_auth_item', ['name' => 'EmployeeInterviewReply.EditInterview'])->execute();
}

提交迁移

1
2
3
./yii migrate   //提交所有迁移,但是已经迁移过的文件不会被执行
./
./yii migrate XXX_xxx_XXX_table //**指定类名**,提交一个迁移文件

还原迁移

1
2
3
./yii migrate/down      //还原最近一次迁移
./yii migrate 3 //提交前三个可用的迁移
./yii migrate/down 3 //还原最近三次迁移

重做迁移

重做迁移的意思是先还原指定的迁移,然后再次提交:

1
2
./yii migrate/redo  //重做最近一次提交的迁移
./yii migrate/redo //重做最近三次提交的迁移

列出迁移

可以通过指令列出提交或者未提交的迁移:

1
2
3
4
5
6
7
./yii migrate/history  //显示最近10次提交的迁移
./yii migrate/history 6 //显示最近5次提交的迁移
./yii migrate/history all //显示所有的提交迁移

./yii migrate/new //显示最近10次未提交的迁移
./yii migrate/new 6 //显示最近6次未提交的迁移
./yii migrate/new all //显示所有的未提交迁移

其他操作数据库的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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(): 删除一个索引