Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: skip resetDatabase when migrations are up-to-date #552

Open
wants to merge 3 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Test/DatabaseResetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +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 isDAMADoctrineTestBundleEnabled(): bool
public static function isDAMADoctrineTestBundleEnabled(bool $cached = false): bool
{
return \class_exists(StaticDriver::class) && StaticDriver::isKeepStaticConnections();
if($cached) {
return self::$isDAMADoctrineTestBundleEnabled;
}

self::$isDAMADoctrineTestBundleEnabled = \class_exists(StaticDriver::class) && StaticDriver::isKeepStaticConnections();
return self::$isDAMADoctrineTestBundleEnabled;
}

public static function resetDatabase(KernelInterface $kernel, bool $damaIsEnabled): void
Expand Down
59 changes: 59 additions & 0 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,6 +47,17 @@ public function __construct(private Application $application, private ManagerReg

public function resetDatabase(): void
{
if (DatabaseResetter::isDAMADoctrineTestBundleEnabled(true) && $this->isResetUsingMigrations()) {
try{
$this->runCommand($this->application, 'doctrine:migrations:up-to-date', ['--fail-on-unregistered' => true]);
$this->truncateAllTables();

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

$this->dropAndResetDatabase();
$this->createSchema();
}
Expand Down Expand Up @@ -171,4 +183,51 @@ 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);
$tables = $connection->getSchemaManager()->listTableNames();

if(count($tables) === 0) {
continue;
}

$databasePlatform = $connection->getDatabasePlatform()->getName();
$truncateStatement = match($databasePlatform) {
'postgresql' => 'TRUNCATE %s RESTART IDENTITY CASCADE;',
'mysql' => 'TRUNCATE %s;',
'sqlite' => 'DELETE FROM %s;',
// fallback to dropping and recreating schema
default => throw new \RuntimeException('Database platform not supported')
};

$statements = "";
foreach ($tables as $tableName)
{
if ($tableName === 'doctrine_migration_versions') {
continue;
}
$statements .= sprintf($truncateStatement, $tableName);
}

$disableForeignKeyChecks = match($databasePlatform) {
'mysql' => 'SET FOREIGN_KEY_CHECKS = 0; %s SET FOREIGN_KEY_CHECKS = 1;',
'sqlite' => 'PRAGMA foreign_keys = OFF; %s PRAGMA foreign_keys = ON;',
default => '%s',
};

try {
$connection->beginTransaction();
$connection->executeStatement(sprintf($disableForeignKeyChecks, $statements));
$connection->commit();
}catch(\Throwable $e) {
$connection->rollBack();
throw new \RuntimeException('Failed to reset database', 0, $e);
}

unset($connection);
}
}
}
Loading