Skip to content

Commit

Permalink
fix(orm): reset database instead of dropping the schema when using mi…
Browse files Browse the repository at this point in the history
…grations (#615)
  • Loading branch information
vincentchalamon committed Jun 10, 2024
1 parent 1b970c6 commit c2cbcbc
Showing 1 changed file with 36 additions and 26 deletions.
62 changes: 36 additions & 26 deletions src/ORM/AbstractORMPersistenceStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,36 @@ final public function resetDatabase(KernelInterface $kernel): void
{
$application = self::application($kernel);

$this->dropAndResetDatabase($application);
$this->createSchema($application);
}

final public function resetSchema(KernelInterface $kernel): void
{
if (PersistenceManager::isDAMADoctrineTestBundleEnabled()) {
// not required as the DAMADoctrineTestBundle wraps each test in a transaction
return;
}

$application = self::application($kernel);

$this->dropSchema($application);
$this->createSchema($application);
}

final public function managedNamespaces(): array
{
$namespaces = [];

foreach ($this->objectManagers() as $objectManager) {
$namespaces[] = $objectManager->getConfiguration()->getEntityNamespaces();
}

return \array_values(\array_merge(...$namespaces));
}

private function dropAndResetDatabase(Application $application): void
{
foreach ($this->connections() as $connection) {
$databasePlatform = $this->registry->getConnection($connection)->getDatabasePlatform(); // @phpstan-ignore-line

Expand Down Expand Up @@ -118,32 +148,6 @@ final public function resetDatabase(KernelInterface $kernel): void
]);
self::runCommand($application, 'doctrine:database:create', ['--connection' => $connection]);
}

$this->createSchema($application);
}

final public function resetSchema(KernelInterface $kernel): void
{
if (PersistenceManager::isDAMADoctrineTestBundleEnabled()) {
// not required as the DAMADoctrineTestBundle wraps each test in a transaction
return;
}

$application = self::application($kernel);

$this->dropSchema($application);
$this->createSchema($application);
}

final public function managedNamespaces(): array
{
$namespaces = [];

foreach ($this->objectManagers() as $objectManager) {
$namespaces[] = $objectManager->getConfiguration()->getEntityNamespaces();
}

return \array_values(\array_merge(...$namespaces));
}

private function createSchema(Application $application): void
Expand All @@ -166,6 +170,12 @@ private function createSchema(Application $application): void

private function dropSchema(Application $application): void
{
if (self::RESET_MODE_MIGRATE === $this->config['reset']['mode']) {
$this->dropAndResetDatabase($application);

return;
}

foreach ($this->managers() as $manager) {
self::runCommand($application, 'doctrine:schema:drop', [
'--em' => $manager,
Expand Down

0 comments on commit c2cbcbc

Please sign in to comment.