我正在使用Phinx在多台服务器上执行跨越1000多个应用程序的迁移。每个应用程序都应该执行相同的迁移。
为了做到这一点,在中央服务器上有一个app实例,它知道执行引导过程所需的所有信任和其他信息(这是基于applicationId的)。
这个中心实例(我们称之为adminapp)通过STDIN执行命令并接收applicationIds,然后执行一个引导应用程序并运行迁移命令的循环。
<?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建议:
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?
发布于 2016-10-13 09:45:10
最后,我扩展了Phinx类\Phinx\Config\Config并创建了fromArray方法。
$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'),
]
]
]
));https://stackoverflow.com/questions/40016322
复制相似问题