forked from zenstruck/foundry
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(reset database): enable migration mode
- Loading branch information
Showing
21 changed files
with
263 additions
and
310 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,14 +5,14 @@ | |
namespace Zenstruck\Foundry\Mongo; | ||
|
||
use Symfony\Component\HttpKernel\KernelInterface; | ||
use Zenstruck\Foundry\Persistence\ResetDatabase\SchemaResetterInterface; | ||
use Zenstruck\Foundry\Persistence\ResetDatabase\SchemaResetter; | ||
use Zenstruck\Foundry\Persistence\SymfonyCommandRunner; | ||
|
||
/** | ||
* @internal | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
*/ | ||
final class MongoSchemaResetter implements SchemaResetterInterface | ||
final class MongoSchemaResetter implements SchemaResetter | ||
{ | ||
use SymfonyCommandRunner; | ||
|
||
|
@@ -29,23 +29,11 @@ public function resetSchema(KernelInterface $kernel): void | |
|
||
foreach ($this->managers as $manager) { | ||
try { | ||
self::runCommand( | ||
$application, | ||
'doctrine:mongodb:schema:drop', | ||
[ | ||
'--dm' => $manager, | ||
] | ||
); | ||
self::runCommand($application, 'doctrine:mongodb:schema:drop', ['--dm' => $manager]); | ||
} catch (\Exception) { | ||
} | ||
|
||
self::runCommand( | ||
$application, | ||
'doctrine:mongodb:schema:create', | ||
[ | ||
'--dm' => $manager, | ||
] | ||
); | ||
self::runCommand($application, 'doctrine:mongodb:schema:create', ['--dm' => $manager]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]> | ||
|
@@ -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); | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\ORM\ResetDatabase; | ||
|
||
use Doctrine\Bundle\DoctrineBundle\Registry; | ||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Platforms\PostgreSQLPlatform; | ||
use Doctrine\DBAL\Platforms\SQLitePlatform; | ||
use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
use Zenstruck\Foundry\Persistence\SymfonyCommandRunner; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
*/ | ||
abstract class AbstractOrmResetter | ||
{ | ||
use SymfonyCommandRunner; | ||
|
||
/** | ||
* @param list<string> $managers | ||
* @param list<string> $connections | ||
*/ | ||
public function __construct( | ||
private readonly Registry $registry, | ||
protected readonly array $managers, | ||
protected readonly array $connections, | ||
) { | ||
} | ||
|
||
protected function dropAndResetDatabase(Application $application): void | ||
{ | ||
foreach ($this->connections as $connectionName) { | ||
/** @var Connection $connection */ | ||
$connection = $this->registry->getConnection($connectionName); | ||
$databasePlatform = $connection->getDatabasePlatform(); | ||
|
||
if ($databasePlatform instanceof SQLitePlatform) { | ||
// we don't need to create the sqlite database - it's created when the schema is created | ||
continue; | ||
} | ||
|
||
if ($databasePlatform instanceof PostgreSQLPlatform) { | ||
// let's drop all connections to the database to be able to drop it | ||
self::runCommand( | ||
$application, | ||
'dbal:run-sql', | ||
[ | ||
'--connection' => $connectionName, | ||
'sql' => 'SELECT pid, pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = current_database() AND pid <> pg_backend_pid()', | ||
], | ||
canFail: true, | ||
); | ||
} | ||
|
||
self::runCommand( | ||
$application, | ||
'doctrine:database:drop', | ||
['--connection' => $connectionName, '--force' => true, '--if-exists' => true] | ||
); | ||
|
||
self::runCommand($application, 'doctrine:database:create', ['--connection' => $connectionName]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]> | ||
*/ | ||
final class DamaDatabaseResetter implements DatabaseResetterInterface | ||
final class DamaDatabaseResetter implements OrmDatabaseResetter | ||
{ | ||
public function __construct( | ||
private DatabaseResetterInterface $decorated, | ||
private OrmDatabaseResetter $decorated, | ||
) { | ||
} | ||
|
||
|
@@ -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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\ORM\ResetDatabase; | ||
|
||
use Doctrine\Bundle\DoctrineBundle\Registry; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
|
||
/** | ||
* @internal | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
*/ | ||
final class MigrateDatabaseResetter extends AbstractOrmResetter implements OrmDatabaseResetter | ||
{ | ||
/** | ||
* @param list<string> $configurations | ||
*/ | ||
public function __construct( | ||
private readonly array $configurations, | ||
Registry $registry, | ||
array $managers, | ||
array $connections, | ||
) | ||
{ | ||
parent::__construct($registry, $managers, $connections); | ||
} | ||
|
||
final public function resetSchema(KernelInterface $kernel): void | ||
{ | ||
$this->resetWithMigration($kernel); | ||
} | ||
|
||
public function resetDatabase(KernelInterface $kernel): void | ||
{ | ||
$this->resetWithMigration($kernel); | ||
} | ||
|
||
private function resetWithMigration(KernelInterface $kernel): void | ||
{ | ||
$application = self::application($kernel); | ||
|
||
$this->dropAndResetDatabase($application); | ||
|
||
if (!$this->configurations) { | ||
self::runCommand($application, 'doctrine:migrations:migrate'); | ||
|
||
return; | ||
} | ||
|
||
foreach ($this->configurations as $configuration) { | ||
self::runCommand($application, 'doctrine:migrations:migrate', ['--configuration' => $configuration]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,57 +4,9 @@ | |
|
||
namespace Zenstruck\Foundry\ORM\ResetDatabase; | ||
|
||
use Doctrine\Persistence\ManagerRegistry; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
use Zenstruck\Foundry\Persistence\ResetDatabase\DatabaseResetterInterface; | ||
use Zenstruck\Foundry\Persistence\SymfonyCommandRunner; | ||
use Zenstruck\Foundry\Persistence\ResetDatabase\DatabaseResetter; | ||
use Zenstruck\Foundry\Persistence\ResetDatabase\SchemaResetter; | ||
|
||
/** | ||
* @internal | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
*/ | ||
final class OrmDatabaseResetter implements DatabaseResetterInterface | ||
interface OrmDatabaseResetter extends DatabaseResetter, SchemaResetter | ||
{ | ||
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); | ||
|
||
$this->dropAndResetDatabase($application); | ||
$this->createSchema($application); | ||
} | ||
|
||
private function registry(): ManagerRegistry | ||
{ | ||
return $this->registry; | ||
} | ||
|
||
/** | ||
* @return list<string> | ||
*/ | ||
private function managers(): array | ||
{ | ||
return $this->managers; | ||
} | ||
|
||
/** | ||
* @return list<string> | ||
*/ | ||
private function connections(): array | ||
{ | ||
return $this->connections; | ||
} | ||
} |
Oops, something went wrong.