@@ -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
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user