当我尝试在 Symfony dockerized 应用程序上运行
doctrine:schema:update --complete --dump-sql
时,输出如下所示:
ALTER TABLE offer DROP FOREIGN KEY FK_29D6873EC1EA42F3;
DROP TABLE doctrine_migration_versions;
我希望表migration_versions不应该被删除! 我正在使用
mariadb:10.9.4 mysql
将
schema_filter
添加到您的doctrine.yaml
doctrine:
dbal:
url: '%env(DATABASE_URL)%'
schema_filter: "~^(?!doctrine_migration_versions$)~"
并尝试在不使用 --complete 选项的情况下启动命令
您可以在我的旧答案之一上阅读对此的更好解释: Symfony 5 - schema_filter 的原则不起作用
你也可以尝试这个方法:
use Doctrine\Migrations\DependencyFactory;
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
class RemoveMigrationTableFromSchemaListener
{
private ?TableMetadataStorageConfiguration $configuration;
public function __construct(
private readonly DependencyFactory $dependencyFactory,
) {
$configuration = $this->dependencyFactory->getConfiguration()->getMetadataStorageConfiguration();
$this->configuration = $configuration instanceof TableMetadataStorageConfiguration ? $configuration : null;
}
public function postGenerateSchema(GenerateSchemaEventArgs $args): void
{
if (!$this->configuration) {
return;
}
$schema = $args->getSchema();
$table = $schema->createTable($this->configuration->getTableName());
$table->addColumn(
$this->configuration->getVersionColumnName(),
'string',
['notnull' => true, 'length' => $this->configuration->getVersionColumnLength()],
);
$table->addColumn($this->configuration->getExecutedAtColumnName(), 'datetime', ['notnull' => false]);
$table->addColumn($this->configuration->getExecutionTimeColumnName(), 'integer', ['notnull' => false]);
$table->setPrimaryKey([$this->configuration->getVersionColumnName()]);
}
}
服务中.yml
App\Doctrine\EventListener\RemoveMigrationTableFromSchemaListener:
arguments:
$dependencyFactory: '@doctrine.migrations.dependency_factory'
tags:
- { name: doctrine.event_listener, event: postGenerateSchema }
此代码从
doctrine_migration_versions
命令中排除 doctrine schema update