fix db delete data issue
Tests / PHPUnit (push) Failing after 1m24s

This commit is contained in:
root
2026-07-13 01:43:32 -04:00
parent f3ac521d7c
commit 504c3bc9f9
12 changed files with 76 additions and 154 deletions
+27 -2
View File
@@ -15,6 +15,7 @@ class DBReset
{
$config = config('Database');
$dbConfig = $config->{self::DB_GROUP} ?? [];
self::assertSafeTestDatabase($config->default ?? [], $dbConfig);
try {
$db = Database::connect(self::DB_GROUP);
@@ -35,14 +36,38 @@ class DBReset
$db->disableForeignKeyChecks();
$tables = $db->listTables();
$tables = $db->resetDataCache()->listTables();
foreach ($tables as $table) {
if (!in_array($table, self::$excludeTables)) {
if (!in_array($table, self::$excludeTables, true) && $db->tableExists($table, false)) {
$db->query('TRUNCATE TABLE ' . $db->escapeIdentifiers($table));
}
}
$db->enableForeignKeyChecks();
}
private static function assertSafeTestDatabase(array $defaultConfig, array $testConfig): void
{
$defaultDatabase = (string) ($defaultConfig['database'] ?? '');
$testDatabase = (string) ($testConfig['database'] ?? '');
if ($testDatabase === '') {
throw new \RuntimeException('Refusing to reset database: tests database name is empty.');
}
if ($defaultDatabase !== '' && $testDatabase === $defaultDatabase) {
throw new \RuntimeException(sprintf(
'Refusing to reset database "%s": tests database matches the default application database.',
$testDatabase
));
}
if (! preg_match('/(^test_|_test$|^tests$|^alrahma_test$)/i', $testDatabase)) {
throw new \RuntimeException(sprintf(
'Refusing to reset database "%s": test database names must clearly identify a test database.',
$testDatabase
));
}
}
}