二进制打包后使用数据库迁移工具phinx

sdffghy

数据库迁移工具phinx的使用和安装,请参考官方文档:https://www.workerman.net/doc/webman/db/migration.html

  1. 打开根目录的webman文件

  2. $cli->run();之前增加如下代码 :

$phinxApp = new PhinxApplication();
$prefix = "phinx:";
foreach ($phinxApp->all() as $command) {
    $command->setName($prefix . $command->getName());
    // 将 Phinx 的每个命令添加到 Symfony Console 应用程序中
    $cli->add($command);
}

附上完整代码:

#!/usr/bin/env php
<?php

use Phinx\Console\PhinxApplication;
use Webman\Config;
use Webman\Console\Command;
use Webman\Console\Util;
use support\Container;

require_once __DIR__ . '/vendor/autoload.php';

if (!in_array($argv[1] ?? '', ['start', 'restart', 'stop', 'status', 'reload', 'connections'])) {
    require_once __DIR__ . '/support/bootstrap.php';
} else {
    if (class_exists('Support\App')) {
        Support\App::loadAllConfig(['route']);
    } else {
        Config::reload(config_path(), ['route', 'container']);
    }
}

$cli = new Command();
$cli->setName('webman cli');
$cli->installInternalCommands();
if (is_dir($command_path = Util::guessPath(app_path(), '/command', true))) {
    $cli->installCommands($command_path);
}

foreach (config('plugin', []) as $firm => $projects) {
    if (isset($projects['app'])) {
        if ($command_str = Util::guessPath(base_path() . "/plugin/$firm", 'command')) {
            $command_path = base_path() . "/plugin/$firm/$command_str";
            $cli->installCommands($command_path, "plugin\\$firm\\$command_str");
        }
    }
    foreach ($projects as $name => $project) {
        if (!is_array($project)) {
            continue;
        }
        foreach ($project['command'] ?? [] as $command) {
            $cli->add(Container::get($command));
        }
    }
}

$phinxApp = new PhinxApplication();
$prefix = "phinx:";
foreach ($phinxApp->all() as $command) {
    $command->setName($prefix . $command->getName());
    // 将 Phinx 的每个命令添加到 Symfony Console 应用程序中
    $cli->add($command);
}

$cli->run();

使用方法

只需在原有的phinx命令前加上前缀phinx:即可。

例如:vendor/bin/phinx init 变成:php webman phinx:init

使用phar运行: php webman.phar phinx:init

二进制运行: ./webman.bin phinx:init

20230901补充

  1. 命令确实可执行,但发现有个问题,在打包后运行,提示没有配置文件,在根目录创建后,发现无法读取到迁移记录。

解决方案:

  1. 在根目录创建phinx.php或yml配置文件
  2. db/migrations放在打包文件的同级目录,执行 ./webman.bin phinx:status 查看效果
  3. 自定义二进制打包:https://www.workerman.net/a/1565 在打包过程自动完成拷贝目录。
582 0 1
0个评论

sdffghy

330
积分
0
获赞数
0
粉丝数
2023-08-25 加入
🔝