adminmatrix / matrix_migration
There is no license information available for the latest version (dev-master) of this package.
基于 Phinx 的数据库迁移工具,支持 ThinkPHP 框架,提供数据库版本控制和迁移管理功能
This package's canonical repository appears to be gone and the package has been frozen as a result.
dev-master
2025-04-28 10:47 UTC
Requires
- php: ^8.3
- topthink/framework: ^8.1
Requires (Dev)
- fzaninotto/faker: ^1.8
This package is auto-updated.
Last update: 2025-05-10 00:18:44 UTC
README
一个基于 Phinx 的数据库迁移工具,专为 ThinkPHP 框架设计。
功能特点
- 基于 Phinx 框架的数据库迁移工具
- 支持 ThinkPHP 框架集成
- 提供命令行工具进行数据库迁移管理
- 支持数据库版本控制
- 支持迁移回滚
- 自动执行迁移脚本
环境要求
- PHP >= 8.3
- ThinkPHP 框架
- MySQL 或其他支持的数据库
安装
通过 Composer 安装:
composer require adminmatrix/matrix_migration
使用教程
1. 创建迁移文件
php think migrate:create CreateUsersTable [--app=app_name] [--connection=connection_name]
参数说明:
--app
: 指定应用名称,默认为空(使用默认应用)--connection
: 指定数据库连接,默认为配置文件中的默认连接
这将在 database/migrations
目录下创建一个新的迁移文件。
2. 编写迁移文件
在生成的迁移文件中,您可以定义 up()
和 down()
方法:
<?php
use Phinx\Migration\AbstractMigration;
class CreateUsersTable extends AbstractMigration
{
public function up()
{
$table = $this->table('users');
$table->addColumn('username', 'string')
->addColumn('password', 'string')
->addColumn('email', 'string')
->addColumn('created_at', 'datetime')
->addColumn('updated_at', 'datetime')
->create();
}
public function down()
{
$this->table('users')->drop()->save();
}
}
3. 执行迁移
php think migrate:run [--app=app_name] [--connection=connection_name] [--target=version]
参数说明:
--app
: 指定应用名称,默认为空(使用默认应用)--connection
: 指定数据库连接,默认为配置文件中的默认连接--target
: 指定要迁移到的版本号,默认为最新版本
4. 回滚迁移
php think migrate:rollback [--app=app_name] [--connection=connection_name] [--target=version] [--date=YYYY-MM-DD]
参数说明:
--app
: 指定应用名称,默认为空(使用默认应用)--connection
: 指定数据库连接,默认为配置文件中的默认连接--target
: 指定要回滚到的版本号--date
: 指定要回滚到的日期
5. 查看迁移状态
php think migrate:status [--app=app_name] [--connection=connection_name] [--format=format]
参数说明:
--app
: 指定应用名称,默认为空(使用默认应用)--connection
: 指定数据库连接,默认为配置文件中的默认连接--format
: 输出格式,可选值:json, array, 默认为表格格式
6. 重置迁移
php think migrate:reset [--app=app_name] [--connection=connection_name]
参数说明:
--app
: 指定应用名称,默认为空(使用默认应用)--connection
: 指定数据库连接,默认为配置文件中的默认连接
7. 刷新迁移
php think migrate:refresh [--app=app_name] [--connection=connection_name] [--seed]
参数说明:
--app
: 指定应用名称,默认为空(使用默认应用)--connection
: 指定数据库连接,默认为配置文件中的默认连接--seed
: 是否同时运行数据填充
常用命令
命令 | 描述 | 参数 |
---|---|---|
migrate:create | 创建新的迁移文件 | --app , --connection |
migrate:run | 执行所有未执行的迁移 | --app , --connection , --target |
migrate:rollback | 回滚最后一次迁移 | --app , --connection , --target , --date |
migrate:reset | 回滚所有迁移 | --app , --connection |
migrate:refresh | 重置并重新运行所有迁移 | --app , --connection , --seed |
migrate:status | 显示迁移状态 | --app , --connection , --format |
注意事项
- 迁移文件命名建议使用驼峰命名法
- 每次迁移后会自动记录在
phinxlog
表中 - 确保数据库配置正确
- 建议在开发环境中测试迁移脚本
- 使用
--app
参数可以指定多应用场景下的迁移 - 使用
--connection
参数可以切换不同的数据库连接
常见问题
迁移失败怎么办?
- 检查数据库配置是否正确
- 查看错误日志
- 确保数据库用户有足够的权限
- 检查迁移文件语法是否正确
如何修改迁移文件?
- 不建议直接修改已执行的迁移文件
- 创建新的迁移文件来修改表结构
- 使用
migrate:rollback
回滚后再修改
如何添加种子数据?
- 使用
migrate:seed
命令 - 在
database/seeds
目录下创建种子文件 - 使用
migrate:refresh --seed
同时执行迁移和填充
- 使用
如何处理多应用场景?
- 使用
--app
参数指定应用名称 - 迁移文件会保存在对应应用的
database/migrations
目录下
- 使用
贡献
欢迎提交 Issue 和 Pull Request。
许可证
MIT License