Skip to content

Commit

Permalink
fix: ensure all tables are empty when no migrations are needed
Browse files Browse the repository at this point in the history
  • Loading branch information
haase-fabian committed Feb 1, 2024
1 parent 7d5ad3b commit 3583d4a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
13 changes: 7 additions & 6 deletions src/Test/DatabaseResetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,21 @@
final class DatabaseResetter
{
private static bool $hasBeenReset = false;
private static bool $isDAMADoctrineTestBundleEnabled = false;

public static function hasBeenReset(): bool
{
return self::$hasBeenReset;
}

public static function isDAMADoctrineTestBundleAvailable(): bool
public static function isDAMADoctrineTestBundleEnabled(bool $cached = false): bool
{
return \class_exists(StaticDriver::class);
}
if($cached) {
return self::$isDAMADoctrineTestBundleEnabled;
}

public static function isDAMADoctrineTestBundleEnabled(): bool
{
return \class_exists(StaticDriver::class) && StaticDriver::isKeepStaticConnections();
self::$isDAMADoctrineTestBundleEnabled = \class_exists(StaticDriver::class) && StaticDriver::isKeepStaticConnections();
return self::$isDAMADoctrineTestBundleEnabled;
}

public static function resetDatabase(KernelInterface $kernel, bool $damaIsEnabled): void
Expand Down
42 changes: 39 additions & 3 deletions src/Test/ORMDatabaseResetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Zenstruck\Foundry\Test;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\Persistence\ManagerRegistry;
Expand Down Expand Up @@ -46,12 +47,14 @@ public function __construct(private Application $application, private ManagerReg

public function resetDatabase(): void
{
if (DatabaseResetter::isDAMADoctrineTestBundleAvailable() && $this->isResetUsingMigrations()) {
if (DatabaseResetter::isDAMADoctrineTestBundleEnabled(true) && $this->isResetUsingMigrations()) {
try{
$this->runCommand($this->application, 'doctrine:migrations:up-to-date', ['--fail-on-unregistered' => true]);
// not required as the database schema is already up-to-date
$this->truncateAllTables();

// not required as the database schema is already up-to-date and all tables were truncated
return;
}catch(\RuntimeException $e){
}catch(\Throwable $e){
}
}

Expand Down Expand Up @@ -180,4 +183,37 @@ private function isResetUsingMigrations(): bool

return self::RESET_MODE_MIGRATE === $this->resetMode;
}


private function truncateAllTables(): void {
foreach ($this->connectionsToReset() as $connectionName) {
/** @var Connection $connection */
$connection = $this->registry->getConnection($connectionName);
$databasePlatform = $connection->getDatabasePlatform();
$truncateStatement = $databasePlatform->getTruncateTableSQL("%s");

switch($databasePlatform->getName()){
case 'postgresql': $connection->executeStatement('SET CONSTRAINTS ALL DEFERRED;'); break;
case 'mysql': $connection->executeStatement('SET FOREIGN_KEY_CHECKS = 0;'); break;
}

$tables = $connection->getSchemaManager()->listTableNames();
$connection->beginTransaction();
foreach ($tables as $tableName)
{
if ($tableName === 'doctrine_migration_versions') {
continue;
}
$connection->executeStatement(sprintf($truncateStatement, $tableName));
}
$connection->commit();

switch($databasePlatform->getName()){
case 'postgresql': $connection->executeStatement('SET CONSTRAINTS ALL IMMEDIATE;'); break;
case 'mysql': $connection->executeStatement('SET FOREIGN_KEY_CHECKS = 1;'); break;
}

unset($connection);
}
}
}

0 comments on commit 3583d4a

Please sign in to comment.