对于开发,我们有一个Symfony控制台命令,用于执行其他控制台命令,以便重建db,运行fixture等。
作为整个过程的一部分,我需要运行一些樱桃选择的学说迁移命令,但由于某种原因,我无法在同一个过程中运行多个执行命令。
要确认,我可以手动执行这些任务,并且可以在控制台执行中运行其中一个命令,然后在没有问题的情况下手动运行另一个命令。
$this->getApplication()->run(new ArrayInput(array(
'command' => 'doctrine:migrations:execute',
'version' => '20140310162336',
'--no-interaction' => true
)), $output);
$this->getApplication()->run(new ArrayInput(array(
'command' => 'doctrine:migrations:execute',
'version' => '20140310170437',
'--no-interaction' => true
)), $output);
返回的错误是:
[Doctrine\DBAL\Migrations\MigrationException]
Migration version 20140310162334 already registered with class Doctrine\DBAL\Migrations\Version
该版本是第一个存在的版本文件,可以确认一个版本不在migration_versions表中,也不需要在此场景中。建议将其加载到迁移对象中。
任何人都可以提供输入,如果我做错了,如果这可能是某个地方的错误。
使用dev-master运行Symfony 2.2。*和迁移包。
答案 0 :(得分:5)
我在symfony 2.6上遇到了同样的问题,Alexei Tenitski所描述的解决方案虽然看起来不合适,但却没有用。 这是对我有用的解决方案。
/**
* Loop thorugh the config and path config for migrations
* and execute migrations for each connection
*/
foreach (array_keys($this->migrationsConfig) as $configEm) {
if (
(empty($ems) || in_array($configEm, $ems))
&& !in_array($configEm, $ignoreEms)
) {
try {
// new instance of the command you want to run
// to force reload MigrationsConfig
$command = new MigrateSingleCommand($this->migrationsConfig);
$command->setApplication($this->getApplication());
$arguments = [
'command' => $commandString,
'--em' => $configEm,
];
$input = new ArrayInput($arguments);
$command->run($input, $output);
} catch (\Exception $e) {
$output->writeln(sprintf("<error>Error: %s</error>", $e->getMessage()));
}
}
}
如果使用$this->getApplication()->run()
,它将从$this->application->commands
获取命令,其中命令仅初始化一次,并且(初始化命令调用时),因此MigrationsConfig将在所有迭代中保持相同。 / p>
答案 1 :(得分:3)
问题是应用程序对每个调用使用相同的命令实例,并且Doctrine migrate命令不适用于此类环境。解决此问题的一种方法是克隆命令并直接使用其实例:
$commandName = 'doctrine:migrations:execute';
$prototypeCommand = $this->getApplication()->get($commandName);
// This is required to avoid merging of application definition for each cloned command
$prototypeCommand->mergeApplicationDefinition();
// Create a clone for a particular run
$command1 = clone $prototypeCommand;
// Run the command with specific params
$command1->run($input1, $output)
// Create another clone
$command2 = clone $prototypeCommand;
// Run the command with another set of params
$command2->run($input2, $output)
答案 2 :(得分:0)
我的猜测是因为你试图一次多次运行迁移命令。 您可能想尝试使用工作队列系统,甚至可能有一个捆绑工具。