数据库迁移工具phinx的使用和安装,请参考官方文档:https://www.workerman.net/doc/webman/db/migration.html
打开根目录的webman
文件
在$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
解决方案:
db/migrations
放在打包文件的同级目录,执行 ./webman.bin phinx:status
查看效果