我正在尝试将doctrine/migrations集成到TP 5框架中以进行单元测试,因此我需要使用doctrine-migrations提供的API来运行迁移,而不是使用CLI。然而,我在使用SQLite引擎时遇到了这个错误:元数据存储未初始化,请运行sync-metadata-storage命令来解决此问题。
这是我的准则
$connectionLoader = new ConfigurationFile('./migrations-db.php');
$configurationLoader = new ConfigurationFileWithFallback('./migrations.php');
$dependencyFactory = DependencyFactory::fromConnection($configurationLoader, $connectionLoader);
$version = $dependencyFactory->getVersionAliasResolver()->resolveVersionAlias('latest');
$planCalculator = $dependencyFactory->getMigrationPlanCalculator();
$plan = $planCalculator->getPlanUntilVersion($version);
$migrator = $dependencyFactory->getMigrator();
$migratorConfigurationFactory = $dependencyFactory->getConsoleInputMigratorConfigurationFactory();
$migratorConfiguration = $migratorConfigurationFactory->getMigratorConfiguration(new ArrayInput([]));
$sql = $migrator->migrate($plan, $migratorConfiguration);
这里是我的migrations-db.php
<?php
// return [
// 'driver' => 'pdo_mysql',
// 'host' => '127.0.0.1',
// 'port' => 13306,
// 'password' => 'zhihui',
// 'dbname' => 'migration',
// 'user' => 'root',
// ];
return [
'driver' => 'pdo_sqlite',
'dbname' => ':memory:',
];
正如你所看到的,当驱动程序是'mysql_pdo'时,一切都很好。
然而,奇怪的是,无论我选择哪种驱动程序,在CLI中运行doctrine-migrations:migrate都没有任何问题。
1条答案
按热度按时间sxissh061#
我已经解决了这个问题,并且找到了一种在
Doctrine\Migrations\Tools\Console\Command\SyncMetadataCommand::execute
方法中调用sync-metadata-storage的方法。所以我只需要加上这行
$dependencyFactory->getMetadataStorage()->ensureInitialized();