我目前面临着学说和我的多个数据库(db_name)的迁移问题
我在同一个 MYSQL 实例中有多个数据库,一个用于公司,另一个用于每个客户端(实体)。
每个数据库都是在 postPersit 操作中创建的(我正在运行覆盖数据库的自定义命令:create ofdoctrine 以使用客户端的 uuid 创建数据库,这是有效的)。
我想对迁移执行相同的操作,但遇到错误“找不到配置”,当我放置常规配置时,它找不到所需的内容。
我创建了一个 WrapperConnection 并添加了选择数据库的可能性。我在运行 API 平台请求之前使用此包装器(在 ApiNormalizer 内)。
我的代码...
#[AsCommand(name: 'custom:migration:migrate')]
final class MigrationCommand extends DoctrineCommand
{
use EntityManagerTrait;
use ParameterBagTrait;
protected function configure(): void
{
$this
->setAliases(['migrate'])
->setDescription(
'Execute a migration to a specified version or the latest available version.',
)
->addArgument(
'name',
InputArgument::REQUIRED,
'The database name'
)
;
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$databaseName = $input->getArgument('name');
if (null === $databaseName) {
// return Command::FAILURE;
}
/** @var MultiDbConnectionWrapper */
$connection = $this->em->getConnection();
$connection->selectDatabase($databaseName);
// Doctrine code is below.
$migrateCmd = new DoctrineMigrateCommand($this->getDependencyFactory());
$migrateInput = new ArrayInput([
'--configuration' => $this->parameterBag->get('kernel.project_dir').'/config/packages/doctrine.yaml',
]);
$migrateCmd->run($migrateInput, $output);
}
}
我尝试直接复制/粘贴类的代码来编辑它......我失败了......
我想在动态模式上使用迁移,但我对原则代码和迁移很陌生。 你能帮我吗?
如果我知道配置是如何工作的,我可以使用我认为良好的模式创建迁移文件的副本并运行它......你觉得怎么样?
抱歉我的英语近似。
我找到了。
解决方案在这里
<?php
namespace App\Command\Doctrine;
use App\Service\Trait\EntityManagerTrait;
use App\Doctrine\MultiDbConnectionWrapper;
use App\Service\Trait\ParameterBagTrait;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\Migrations\Tools\Console\Command\MigrateCommand;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\ArrayInput;
use Doctrine\Migrations\Exception\NoMigrationsFoundWithCriteria;
use Doctrine\Migrations\Exception\NoMigrationsToExecute;
use Doctrine\Migrations\Exception\UnknownMigrationVersion;
use Doctrine\Migrations\Metadata\ExecutedMigrationsList;
use Doctrine\Migrations\Tools\Console\ConsoleInputMigratorConfigurationFactory;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Input\InputOption;
use Doctrine\Migrations\Tools\Console\Command\DoctrineCommand;
use Doctrine\Migrations\DependencyFactory;
use Doctrine\Migrations\Tools\Console\Command\MigrateCommand as DoctrineMigrateCommand;
use Symfony\Component\Yaml\Yaml;
// the name of the command is what users type after "php bin/console"
#[AsCommand(name: 'alx:migration:migrate')]
final class MigrationCommand extends DoctrineCommand
{
use EntityManagerTrait;
use ParameterBagTrait;
protected function configure(): void
{
$this
->setAliases(['migrate'])
->setDescription(
'Execute a migration to a specified version or the latest available version.',
)
->addArgument(
'name',
InputArgument::REQUIRED,
'The database name'
)
;
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$databaseName = $input->getArgument('name');
if (null === $databaseName) {
// return Command::FAILURE;
}
// These two file are generated for temporary use.
$dbFilePath = $this->getTmpDatabaseFile($databaseName);
$migrationFilePath = $this->getTmpDatabaseFile();
$migrateCmd = new DoctrineMigrateCommand();
$migrateCmd->setName('doctrine:migrations:migrate');
$migrateInput = new ArrayInput([
'--db-configuration' => $dbFilePath,
'--configuration' => $migrationFilePath
]);
$migrateCmd->run($migrateInput, $output);
return 0;
}
}
数据库文件
<?php
return [
'url' => 'mysql://USER:PWD@database:3306/myDatabaseName?serverVersion=10.5.15-MariaDB&charset=utf8mb4'
];
迁移文件(yaml)
migrations_paths:
# namespace is arbitrary but should be different from App\Migrations
# as migrations classes should NOT be autoloaded
'DoctrineMigrations': '/app/migrations'
table_storage:
table_name: 'doctrine_migration_versions'
version_column_name: 'version'
version_column_length: '191'