-
-
Notifications
You must be signed in to change notification settings - Fork 85
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
25 changed files
with
308 additions
and
341 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
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\Mongo; | ||
|
||
use Zenstruck\Foundry\Persistence\ResetDatabase\BeforeEachTestResetter; | ||
|
||
interface MongoResetter extends BeforeEachTestResetter | ||
{ | ||
} |
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
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,67 @@ | ||
<?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 <nikophil@gmail.com> | ||
* @internal | ||
*/ | ||
abstract class BaseOrmResetter | ||
{ | ||
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, | ||
) { | ||
} | ||
|
||
final 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
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 <nikophil@gmail.com> | ||
*/ | ||
final class MigrateDatabaseResetter extends BaseOrmResetter implements OrmResetter | ||
{ | ||
/** | ||
* @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 resetBeforeEachTest(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]); | ||
} | ||
} | ||
} |
Oops, something went wrong.