基于phinx封装的一个thinkphp6数据库迁移工具
相对于tp官方的top-think/think-migration不同的是,think-phinx是把phinx
单独通过composer安装,可以自动安装phinx官方的最新版本,bug修复还有特性支持交给phinx官方去做,think-phinx只做了一个桥梁的作用。
composer require ajiho/think-phinx
composer require ajiho/think-phinx:dev-master
安装完毕后运行php think
会得到如下信息
phinx
phinx:breakpoint Run the phinx [Breakpoint] command
phinx:create Run the phinx [Create] command
phinx:migrate Run the phinx [Migrate] command
phinx:region-table Create a migration for the region database table
phinx:rollback Run the phinx [Rollback] command
phinx:seed:create Run the phinx [Seed Create] command
phinx:seed:run Run the phinx [Seed Run] command
phinx:status Run the phinx [Status] command
执行php think 指令 -h
获取选项的详细说明,比如php think phinx:breakpoint -h
因为tp官方的数据库迁移工具也注册了一个命名空间"Phinx\\": "phinx"
{
"name": "topthink/think-migration",
"authors": [
{
"name": "yunwuxin",
"email": "448901948@qq.com"
}
],
"license": "Apache-2.0",
"autoload": {
"psr-4": {
"Phinx\\": "phinx",
"think\\migration\\": "src"
}
}
....
因此会和单独安装的phinx
容易产生相同命名空间的冲突,所以最佳实践是不要同时安装think-phinx
和top-think/think-migration
如果你的tp版本是8.0,且此时你composer所指向的php版本是8.0, 那么你是安装不上phinx的,这个因为它和topthink/framework
的依赖产生了冲突
/config/phinx.php
<?php
return [
// 路径
'paths' => [
'migrations' => 'db/migrations',
'seeds' => 'db/seeds'
],
// 迁移记录表名
'migration_table' => 'migrations',
// 创建文件排序规则
'version_order' => 'creation',
// faker本地化
'faker_locale' => 'zh_CN',
];
和phinx官方文档用法一样
PS:建议表时最好保持一个命名规范Create表名Table
,必须是大驼峰命名规范
php think phinx:create CreateUserTable
think-phinx提供的特有选项--table
用于指定表名
php think phinx:create CreateUserTable --table user
默认生成的结果如下
<?php
use ajiho\phinx\Migrator;
class CreateUserTable extends Migrator
{
public function up()
{
$table = $this->table('user',['engine'=>'INNODB','collation'=>'utf8mb4_unicode_ci']);
$table->addColumn('username', 'string', ['comment'=>'账号','limit' => 50])
->addColumn('truename', 'string', ['comment'=>'真实姓名','limit' => 50,'null'=>false,'default'=>''])
->addColumn('password', 'string', ['comment'=>'密码','limit' => 255])
->addColumn('email', 'string', ['comment'=>'邮箱','limit' => 50])
->addColumn('phone', 'string', ['comment'=>'手机号码','limit' => 15])
->addColumn('gender', 'enum', ['comment'=>'性别','values' => ['先生', '女士'],'null' => false,'default'=>'先生'])
->addColumn('last_ip', 'char', ['comment'=>'登录IP','limit' => 15])
->addColumn('create_time', 'timestamp', ['comment'=>'创建时间','default' => 'CURRENT_TIMESTAMP'])
->addColumn('update_time', 'timestamp', ['comment'=>'更新时间','default' => 'CURRENT_TIMESTAMP'])
->addColumn('delete_time', 'timestamp', ['comment'=>'删除时间','default' => 'CURRENT_TIMESTAMP'])
->create();
}
public function down()
{
$this->table('user')->drop()->save();
}
}
设置表引擎
$table = $this->table('user',['engine'=>'INNODB','collation'=>'utf8mb4_unicode_ci']);
$table->addColumn('username', 'string', ['comment'=>'账号','limit' => 50])
...
->create();
如果你喜欢手撸原生SQL语句,那么你可以使用execute
方法,
<?php
use ajiho\phinx\Migrator;
class CreateUserTable extends Migrator
{
public function up()
{
$this->execute(
"create table `{$this->_prefix}user`
(
`id` int not null auto_increment primary key,
`username` varchar(50) comment '账号',
`truename` varchar(50) not null default '' comment '真实姓名',
`password` varchar(255) comment '密码',
`email` varchar(50) comment '邮箱',
`phone` varchar(15) comment '手机号码',
`sex` enum('先生','女士') default '先生' comment '性别',
`last_ip` char(15) comment '登录IP',
`create_time` timestamp null comment '创建时间',
`update_time` timestamp null comment '更新时间',
`delete_time` timestamp null comment '删除时间'
) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 collate utf8mb4_unicode_ci;"
);
}
public function down()
{
$this->execute(
"drop table if exists {$this->exec_table_prefix}user;"
);
}
}
默认迁移指定的默认环境和所有的迁移文件
php think phinx:migrate
指定环境(database.php配置文件里connections下的数组的key值,也就是连接配置标识)
php think phinx:migrate -e mysql
php think phinx:migrate -e mysql2
指定环境同时指定指定的版本或者日期之前的迁移文件
# 表示只执行到20110103081132迁移文件就结束
php think phinx:migrate -e mysql -t 20110103081132
# 表示只执行到2011年1月3号的迁移文件就停止迁移
php think phinx:migrate -e mysql -d 20110103
Breakpoint 命令用于设置断点,允许您限制回滚。您可以通过不提供任何参数来切换最近迁移的断点。
php think phinx:breakpoint
php think phinx:breakpoint -e mysql
php think phinx:breakpoint -e mysql -t 20230801141644
php think phinx:breakpoint -e mysql -t 20230801141644 --set
php think phinx:breakpoint -e mysql -t 20230801141644 --unset
# 删除所有断点
php think phinx:breakpoint -e mysql -r
不指定任何参数只回退最后一个迁移文件,步长为1
php think phinx:rollback
指定执行环境,默认环境根据配置文件可以设置
php think phinx:rollback -e mysql
要将所有迁移回滚到特定版本,请使用-t参数
php think phinx:rollback -e mysql -t 20230801141644
回滚所有迁移 -t 0
php think phinx:rollback -e mysql -t 0
要将所有迁移回滚到特定日期,请使用-d参数
php think phinx:rollback -e mysql -d 2012
php think phinx:rollback -e mysql -d 201201
php think phinx:rollback -e mysql -d 20120103
php think phinx:rollback -e mysql -d 2012010312
php think phinx:rollback -e mysql -d 201201031205
php think phinx:rollback -e mysql -d 20120103120530
设置了断点,阻止了进一步的回滚,您可以使用-f 参数覆盖断点。
php think phinx:rollback -e mysql -t 0 -f
将查询打印到标准输出而不执行它们,使用 --dry-run 参数
php think phinx:rollback --dry-run
php think phinx:rollback -t 0 --dry-run
Status 命令打印所有迁移的列表及其当前状态。您可以使用此命令来确定已运行哪些迁移。
php think phinx:status
php think phinx:status -e mysql
PS:建议创建时最好保持一个命名规范填充文件名+Seeder
,必须是大驼峰命名规范
php think phinx:seed:create UserSeeder
php think phinx:seed:run
php think phinx:seed:run -e mysql
php think phinx:seed:run -s UserSeeder
同时指定多个填充文件,通过 -s 追加
php think phinx:seed:run -s UserSeeder -s PermissionSeeder -s LogSeeder
seeder填充文件默认是随机的,有的时候你需要先填充某个表再填充某个表就需要通过getDependencies()
方法指定特定的顺序进行填充
<?php
use ajiho\phinx\Seeder;
class UserSeeder extends Seeder
{
//让填充文件按顺序执行
public function getDependencies()
{
return [
'UserSeeder',
'ShopItemSeeder'
];
}
...
}
也支持指定自己的表名
php think phinx:region-table --table youtablename
会生成一个数据库迁移文件。执行该迁移文件会自动填充全国省/市/区/街道四级地区递归数据,数据来源是从高德地图api获取,放心使用。 总共是44949条数据,经过优化处理仅仅只需要大约5秒即可光速填充数据,在开发测试中特别方便。
需要帮助,给出建议,bug反馈,可以直接联系我
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型