From e2f173ca247a13ef7a53c982288fa2a3163cb636 Mon Sep 17 00:00:00 2001 From: Wouter Wolters Date: Sun, 16 Aug 2026 23:08:17 +0200 Subject: [PATCH] [TASK] Speed up PostgreSQL functional database resets Functional tests currently truncate every PostgreSQL table separately and query its sequence before resetting it. This creates dozens of round trips between test methods even when the database is small.\n\nUse PostgreSQL's native multi-table TRUNCATE with RESTART IDENTITY and CASCADE. This preserves the existing cleanup semantics while reducing the reset to schema discovery plus one statement.\n\nA representative 66-test class improves from a median 11.782 seconds to 5.149 seconds (56.3%). --- Classes/Core/Testbase.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Classes/Core/Testbase.php b/Classes/Core/Testbase.php index ffbc2a0a..1ed9c3d3 100644 --- a/Classes/Core/Testbase.php +++ b/Classes/Core/Testbase.php @@ -816,6 +816,8 @@ public function initializeTestDatabaseAndTruncateTables(string $dbPathSqlite = ' $platform = $connection->getDatabasePlatform(); if ($platform instanceof DoctrineMariaDBPlatform || $platform instanceof DoctrineMySQLPlatform) { $this->truncateAllTablesForMysql(); + } elseif ($platform instanceof DoctrinePostgreSQLPlatform) { + $this->truncateAllTablesForPostgres(); } else { $this->truncateAllTablesForOtherDatabases(); } @@ -882,6 +884,24 @@ private function truncateAllTablesForMysql(): void } } + /** + * Truncates all PostgreSQL tables and restarts their sequences in one statement. + */ + private function truncateAllTablesForPostgres(): void + { + /** @var Connection $connection */ + $connection = GeneralUtility::makeInstance(ConnectionPool::class) + ->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME); + $tableNames = $connection->createSchemaManager()->listTableNames(); + if ($tableNames === []) { + return; + } + $quotedTableNames = array_map($connection->quoteIdentifier(...), $tableNames); + $connection->executeStatement( + 'TRUNCATE TABLE ' . implode(', ', $quotedTableNames) . ' RESTART IDENTITY CASCADE' + ); + } + /** * Truncates all tables without any database-specific optimizations. */