2 Star 12 Fork 1

欲饮琵琶码上催 / think-phinx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

think-phinx

基于phinx封装的一个thinkphp6数据库迁移工具

为什么要封装think-phinx

相对于tp官方的top-think/think-migration不同的是,think-phinx是把phinx 单独通过composer安装,可以自动安装phinx官方的最新版本,bug修复还有特性支持交给phinx官方去做,think-phinx只做了一个桥梁的作用。

think-phinx的优点

  • 自动依赖最新的phinx
  • 保持和phinx官方一样的指令和使用方式
  • 自动数据库连接
  • 相对于tp官方的迁移工具,think-phinx支持多数据库(环境,-e选项)迁移
  • 像laravel数据库迁移一样、默认模板中保持干净的up、down方法
  • 在默认的迁移模板文件中有给出默认的参考示例,方便快速建表
  • 集成fakerphp/faker方便假数据填充
  • 集成全国省、市、区、街道四级数据光速填充功能

安装

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

安装注意事项

  • 注意问题1

因为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-phinxtop-think/think-migration

  • 注意问题2

如果你的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'
        ];
    }
    ...
}

拓展指令说明

phinx:region-table

也支持指定自己的表名

php think phinx:region-table --table youtablename

会生成一个数据库迁移文件。执行该迁移文件会自动填充全国省/市/区/街道四级地区递归数据,数据来源是从高德地图api获取,放心使用。 总共是44949条数据,经过优化处理仅仅只需要大约5秒即可光速填充数据,在开发测试中特别方便。

反馈

需要帮助,给出建议,bug反馈,可以直接联系我

MIT License Copyright (c) 2022 ajiho Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

基于phinx封装的一个tp6数据库迁移工具 展开 收起
PHP
MIT
取消

发行版 (3)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/ajiho/think-phinx.git
git@gitee.com:ajiho/think-phinx.git
ajiho
think-phinx
think-phinx
master

搜索帮助