Skip to content

Commit dc758be

Browse files
committed
Fix truncate table for MySQL 8
1 parent 94b283a commit dc758be

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

src/Traits/DatabaseTestTrait.php

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,30 +63,12 @@ protected function createTables(): void
6363
return;
6464
}
6565

66-
$this->unsetStatsExpiry();
6766
$this->dropTables();
6867
$this->importSchema();
6968

7069
define('DB_TEST_TRAIT_INIT', 1);
7170
}
7271

73-
/**
74-
* Workaround for MySQL 8: update_time not working.
75-
*
76-
* https://bugs.mysql.com/bug.php?id=95407
77-
*
78-
* @return void
79-
*/
80-
private function unsetStatsExpiry()
81-
{
82-
$expiry = $this->getDatabaseVariable('information_schema_stats_expiry');
83-
$version = (string)$this->getDatabaseVariable('version');
84-
85-
if ($expiry !== null && version_compare($version, '8.0.0', '>=')) {
86-
$this->getConnection()->exec('SET information_schema_stats_expiry=0;');
87-
}
88-
}
89-
9072
/**
9173
* Get database variable.
9274
*
@@ -196,13 +178,24 @@ protected function truncateTables(): void
196178

197179
$pdo->exec('SET unique_checks=0; SET foreign_key_checks=0;');
198180

199-
// Truncate only changed tables
200-
$statement = $this->createQueryStatement(
201-
'SELECT TABLE_NAME
181+
$expiry = $this->getDatabaseVariable('information_schema_stats_expiry');
182+
if ($expiry === null) {
183+
// MariaDB: Truncate only changed tables
184+
$statement = $this->createQueryStatement(
185+
'SELECT TABLE_NAME
202186
FROM information_schema.tables
203187
WHERE table_schema = database()
204-
AND update_time IS NOT NULL'
205-
);
188+
AND (update_time IS NOT NULL OR auto_increment IS NOT NULL)'
189+
);
190+
} else {
191+
// MySQL: Truncate all tables
192+
// Workaround for MySQL 8: update_time not working.
193+
// Even SET information_schema_stats_expiry=0; has no affect anymore.
194+
// https://bugs.mysql.com/bug.php?id=95407
195+
$statement = $this->createQueryStatement(
196+
'SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema = database()'
197+
);
198+
}
206199

207200
$rows = (array)$statement->fetchAll(PDO::FETCH_ASSOC);
208201

0 commit comments

Comments
 (0)