首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用多个应用引导程序定制Phinx Symfony命令

使用多个应用引导程序定制Phinx Symfony命令
EN

Stack Overflow用户
提问于 2016-10-13 08:48:49
回答 1查看 703关注 0票数 0

我正在使用Phinx在多台服务器上执行跨越1000多个应用程序的迁移。每个应用程序都应该执行相同的迁移。

为了做到这一点,在中央服务器上有一个app实例,它知道执行引导过程所需的所有信任和其他信息(这是基于applicationId的)。

这个中心实例(我们称之为adminapp)通过STDIN执行命令并接收applicationIds,然后执行一个引导应用程序并运行迁移命令的循环。

代码语言:javascript
复制
<?php
namespace Command\Db;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use App\Command\AppCommand;

class MigrateBulkCommand extends AppCommand
{

    protected function configure()
    {
        $this
            ->setName('command:blah')
            ->setDescription('Executes SQL migrations accross multiple applications. Expects ApplicationIDs to be passed as new line delimited string on STDIN.')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $stdin = $this->getStdin();
        if ($stdin === false) {
             throw new \RuntimeException("Bulk migration command requires applicationIds to be passed to STDIN.");
        }
        $applicationIds = explode("\n", $stdin);

        foreach($applicationIds as $applicationId) {
            try {
                $this->bootstrap($applicationId);
            } catch (\Exception $e) {
                $output->writeln(sprintf("<error>Bootstrap process failed for applicationId `%s`</error>", $applicationId));
            }
            $command = new \Phinx\Console\Command\Migrate();
            $migrationInput = new \Symfony\Component\Console\Input\ArrayInput([

            ]);
            $returnCode = $command->run($migrationInput, $output);
            $output->writeln(sprinf("<info>Migrations for applicationId `%s` executed successfully.</info>", $applicationId));
        }
    }

}

现在,Phinx希望它的配置以配置文件的形式出现。我要做的是重用DB连接资源(PDO),并动态地将它传递给Phinx命令Phinx\Console\Command\Migrate,以及db名称。

我在Phinx文档中看到,这是Phinx文件中的一个选项,但我无法找到一种方法(在Phinx\Console\Command\Migrate类初始化期间)来执行此操作。

Phinx doc建议:

代码语言:javascript
复制
require 'app/init.php';

global $app;
$pdo = $app->getDatabase()->getPdo();

return array('environments' =>
         array(
           'default_database' => 'development',
           'development' => array(
             'name' => 'devdb',
             'connection' => $pdo
           )
         )
       );

有没有办法,在没有可怕的黑客攻击的情况下,将PDO连接资源和db名称传递给\Phinx\Console\Command\Migrate

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-13 09:45:10

最后,我扩展了Phinx类\Phinx\Config\Config并创建了fromArray方法。

代码语言:javascript
复制
$command = new \Phinx\Console\Command\Migrate();
$command->setConfig(\MyNamespace\Config::fromArray(
    [
        'paths' => [
            'migrations' => APPLICATION_PATH . "/../db/migrations",
            'seeds' => APPLICATION_PATH . "/../db/seeds"
        ],
        'environments' => [
            'default_database' => 'production',
            'production' => [
                'name' => $db->get('dbname'),
                'adapter' => 'mysql',
                'host' => $db->get('host'),
                'port' => $db->get('port'),
                'user' => $db->get('username'),
                'pass' => $db->get('password'),
            ]
        ]
    ]
));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40016322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档