Skip to content

Commit

Permalink
refactor(reset database): enable migration mode
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Aug 25, 2024
1 parent 0e9c81b commit d8ef4e7
Show file tree
Hide file tree
Showing 14 changed files with 213 additions and 208 deletions.
30 changes: 17 additions & 13 deletions config/orm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
use Zenstruck\Foundry\ORM\DoctrineOrmVersionGuesser;
use Zenstruck\Foundry\ORM\OrmV2PersistenceStrategy;
use Zenstruck\Foundry\ORM\OrmV3PersistenceStrategy;
use Zenstruck\Foundry\ORM\ResetDatabase\AbstractOrmResetter;
use Zenstruck\Foundry\ORM\ResetDatabase\DamaDatabaseResetter;
use Zenstruck\Foundry\ORM\ResetDatabase\DamaSchemaResetter;
use Zenstruck\Foundry\ORM\ResetDatabase\OrmDatabaseResetter;
use Zenstruck\Foundry\ORM\ResetDatabase\OrmMigrationDatabaseResetter;
use Zenstruck\Foundry\ORM\ResetDatabase\OrmSchemaResetter;

return static function (ContainerConfigurator $container): void {
Expand All @@ -19,36 +21,38 @@
abstract_arg('config'),
])
->tag('.foundry.persistence_strategy')
->set('.zenstruck_foundry.persistence.database_resetter.orm', OrmDatabaseResetter::class)

->set('.zenstruck_foundry.persistence.database_resetter.orm.abstract', AbstractOrmResetter::class)
->args([
service('doctrine'),
abstract_arg('managers'),
abstract_arg('connections'),
])
->abstract()

->set('.zenstruck_foundry.persistence.database_resetter.orm.schema', OrmDatabaseResetter::class)
->parent('.zenstruck_foundry.persistence.database_resetter.orm.abstract')
->tag('.foundry.persistence.database_resetter')
->tag('.foundry.persistence.schema_resetter')

->set('.zenstruck_foundry.persistence.database_resetter.orm.migrate', OrmMigrationDatabaseResetter::class)
->parent('.zenstruck_foundry.persistence.database_resetter.orm.abstract')
->tag('.foundry.persistence.database_resetter')
->set('.zenstruck_foundry.persistence.schema_resetter.orm', OrmSchemaResetter::class)
->args([
service('doctrine'),
abstract_arg('managers'),
abstract_arg('connections'),
])
->tag('.foundry.persistence.schema_resetter')
;

if (\class_exists(StaticDriver::class)) {
$container->services()
->set('.zenstruck_foundry.persistence.database_resetter.orm.dama', DamaDatabaseResetter::class)
->decorate('.zenstruck_foundry.persistence.database_resetter.orm')
->set('.zenstruck_foundry.persistence.database_resetter.orm.schema.dama', DamaDatabaseResetter::class)
->decorate('.zenstruck_foundry.persistence.database_resetter.orm.schema')
->args([
service('.inner'),
])
->tag('.foundry.persistence.database_resetter')
->set('.zenstruck_foundry.persistence.schema_resetter.orm.dama', DamaSchemaResetter::class)
->decorate('.zenstruck_foundry.persistence.schema_resetter.orm')
->set('.zenstruck_foundry.persistence.database_resetter.orm.migrate.dama', DamaDatabaseResetter::class)
->decorate('.zenstruck_foundry.persistence.database_resetter.orm.migrate')
->args([
service('.inner'),
])
->tag('.foundry.persistence.schema_resetter')
;
}
};
11 changes: 1 addition & 10 deletions src/ORM/AbstractORMPersistenceStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,10 @@

namespace Zenstruck\Foundry\ORM;

use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\MappingException as ORMMappingException;
use Doctrine\Persistence\Mapping\MappingException;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\HttpKernel\KernelInterface;
use Zenstruck\Foundry\Persistence\PersistenceManager;
use Zenstruck\Foundry\Persistence\PersistenceStrategy;
use Zenstruck\Foundry\Persistence\ResetDatabase\ResetDatabaseManager;

/**
* @author Kevin Bond <kevinbond@gmail.com>
Expand All @@ -32,9 +26,6 @@
*/
abstract class AbstractORMPersistenceStrategy extends PersistenceStrategy
{
public const RESET_MODE_SCHEMA = 'schema';
public const RESET_MODE_MIGRATE = 'migrate';

final public function contains(object $object): bool
{
$em = $this->objectManagerFor($object::class);
Expand All @@ -53,7 +44,7 @@ final public function hasChanges(object $object): bool
// cannot use UOW::recomputeSingleEntityChangeSet() here as it wrongly computes embedded objects as changed
$em->getUnitOfWork()->computeChangeSet($em->getClassMetadata($object::class), $object);

return (bool) $em->getUnitOfWork()->getEntityChangeSet($object);
return (bool)$em->getUnitOfWork()->getEntityChangeSet($object);
}

final public function truncate(string $class): void
Expand Down
49 changes: 49 additions & 0 deletions src/ORM/ResetDatabase/AbstractOrmResetter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Zenstruck\Foundry\ORM\ResetDatabase;

use Doctrine\Bundle\DoctrineBundle\Registry;
use Zenstruck\Foundry\Persistence\SymfonyCommandRunner;

/**
* @author Nicolas PHILIPPE <nikophil@gmail.com>
*/
abstract class AbstractOrmResetter
{
use OrmDatabaseResetterTrait;
use SymfonyCommandRunner;

/**
* @param list<string> $managers
* @param list<string> $connections
*/
public function __construct(
private Registry $registry,
private array $managers,
private array $connections,
) {
}

protected function registry(): Registry
{
return $this->registry;
}

/**
* @return list<string>
*/
protected function managers(): array
{
return $this->managers;
}

/**
* @return list<string>
*/
protected function connections(): array
{
return $this->connections;
}
}
15 changes: 12 additions & 3 deletions src/ORM/ResetDatabase/DamaDatabaseResetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@
use Symfony\Component\HttpKernel\KernelInterface;
use Zenstruck\Foundry\Configuration;
use Zenstruck\Foundry\Persistence\PersistenceManager;
use Zenstruck\Foundry\Persistence\ResetDatabase\DatabaseResetterInterface;
use Zenstruck\Foundry\Persistence\ResetDatabase\ResetDatabaseManager;

/**
* @internal
* @author Nicolas PHILIPPE <nikophil@gmail.com>
*/
final class DamaDatabaseResetter implements DatabaseResetterInterface
final class DamaDatabaseResetter implements OrmDatabaseResetterInterface
{
public function __construct(
private DatabaseResetterInterface $decorated,
private OrmDatabaseResetterInterface $decorated,
) {
}

Expand Down Expand Up @@ -49,4 +48,14 @@ public function resetDatabase(KernelInterface $kernel): void
// re-enable static connections
StaticDriver::setKeepStaticConnections(true);
}

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

$this->decorated->resetSchema($kernel);
}
}
31 changes: 0 additions & 31 deletions src/ORM/ResetDatabase/DamaSchemaResetter.php

This file was deleted.

52 changes: 22 additions & 30 deletions src/ORM/ResetDatabase/OrmDatabaseResetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,15 @@

namespace Zenstruck\Foundry\ORM\ResetDatabase;

use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\HttpKernel\KernelInterface;
use Zenstruck\Foundry\Persistence\ResetDatabase\DatabaseResetterInterface;
use Zenstruck\Foundry\Persistence\SymfonyCommandRunner;

/**
* @internal
* @author Nicolas PHILIPPE <nikophil@gmail.com>
*/
final class OrmDatabaseResetter implements DatabaseResetterInterface
final class OrmDatabaseResetter extends AbstractOrmResetter implements OrmDatabaseResetterInterface
{
use OrmDatabaseResetterTrait;
use SymfonyCommandRunner;

/**
* @param list<string> $managers
* @param list<string> $connections
*/
public function __construct(
private ManagerRegistry $registry,
private array $managers,
private array $connections,
) {
}

final public function resetDatabase(KernelInterface $kernel): void
{
$application = self::application($kernel);
Expand All @@ -37,24 +21,32 @@ final public function resetDatabase(KernelInterface $kernel): void
$this->createSchema($application);
}

private function registry(): ManagerRegistry
final public function resetSchema(KernelInterface $kernel): void
{
return $this->registry;
$application = self::application($kernel);

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

/**
* @return list<string>
*/
private function managers(): array
private function createSchema(Application $application): void
{
return $this->managers;
foreach ($this->managers() as $manager) {
self::runCommand($application, 'doctrine:schema:update', [
'--em' => $manager,
'--force' => true,
]);
}
}

/**
* @return list<string>
*/
private function connections(): array
private function dropSchema(Application $application): void
{
return $this->connections;
foreach ($this->managers() as $manager) {
self::runCommand($application, 'doctrine:schema:drop', [
'--em' => $manager,
'--force' => true,
'--full-database' => true,
]);
}
}
}
12 changes: 12 additions & 0 deletions src/ORM/ResetDatabase/OrmDatabaseResetterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Zenstruck\Foundry\ORM\ResetDatabase;

use Zenstruck\Foundry\Persistence\ResetDatabase\DatabaseResetterInterface;
use Zenstruck\Foundry\Persistence\ResetDatabase\SchemaResetterInterface;

interface OrmDatabaseResetterInterface extends DatabaseResetterInterface, SchemaResetterInterface
{
}
Loading

0 comments on commit d8ef4e7

Please sign in to comment.