Use Doctrine Migrations table to check for schema presence

This commit is contained in:
Yassine Guedidi 2024-01-24 23:09:47 +01:00
parent ef4964cf48
commit 1198b6432d
2 changed files with 10 additions and 6 deletions

View file

@ -109,6 +109,9 @@ services:
Doctrine\ORM\EntityManagerInterface:
alias: doctrine.orm.entity_manager
Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration:
alias: doctrine.migrations.storage.table_storage
Doctrine\Persistence\ManagerRegistry:
alias: doctrine

View file

@ -7,6 +7,7 @@ use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
use Doctrine\ORM\EntityManagerInterface;
use FOS\UserBundle\Event\UserEvent;
use FOS\UserBundle\FOSUserEvents;
@ -39,15 +40,17 @@ class InstallCommand extends Command
private EntityManagerInterface $entityManager;
private EventDispatcherInterface $dispatcher;
private UserManagerInterface $userManager;
private TableMetadataStorageConfiguration $tableMetadataStorageConfiguration;
private string $databaseDriver;
private array $defaultSettings;
private array $defaultIgnoreOriginInstanceRules;
public function __construct(EntityManagerInterface $entityManager, EventDispatcherInterface $dispatcher, UserManagerInterface $userManager, string $databaseDriver, array $defaultSettings, array $defaultIgnoreOriginInstanceRules)
public function __construct(EntityManagerInterface $entityManager, EventDispatcherInterface $dispatcher, UserManagerInterface $userManager, TableMetadataStorageConfiguration $tableMetadataStorageConfiguration, string $databaseDriver, array $defaultSettings, array $defaultIgnoreOriginInstanceRules)
{
$this->entityManager = $entityManager;
$this->dispatcher = $dispatcher;
$this->userManager = $userManager;
$this->tableMetadataStorageConfiguration = $tableMetadataStorageConfiguration;
$this->databaseDriver = $databaseDriver;
$this->defaultSettings = $defaultSettings;
$this->defaultIgnoreOriginInstanceRules = $defaultIgnoreOriginInstanceRules;
@ -415,14 +418,12 @@ class InstallCommand extends Command
/**
* Check if the schema is already created.
* If we found at least one table, it means the schema exists.
*
* @return bool
* We use the Doctrine Migrations table for the check.
*/
private function isSchemaPresent()
private function isSchemaPresent(): bool
{
$schemaManager = $this->entityManager->getConnection()->createSchemaManager();
return \count($schemaManager->listTableNames()) > 0 ? true : false;
return $schemaManager->tablesExist([$this->tableMetadataStorageConfiguration->getTableName()]);
}
}