Skip to content

Commit

Permalink
Fix truncate table for MySQL 8
Browse files Browse the repository at this point in the history
  • Loading branch information
odan committed Feb 4, 2022
1 parent 94b283a commit dc758be
Showing 1 changed file with 16 additions and 23 deletions.
39 changes: 16 additions & 23 deletions src/Traits/DatabaseTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,12 @@ protected function createTables(): void
return;
}

$this->unsetStatsExpiry();
$this->dropTables();
$this->importSchema();

define('DB_TEST_TRAIT_INIT', 1);
}

/**
* Workaround for MySQL 8: update_time not working.
*
* https://bugs.mysql.com/bug.php?id=95407
*
* @return void
*/
private function unsetStatsExpiry()
{
$expiry = $this->getDatabaseVariable('information_schema_stats_expiry');
$version = (string)$this->getDatabaseVariable('version');

if ($expiry !== null && version_compare($version, '8.0.0', '>=')) {
$this->getConnection()->exec('SET information_schema_stats_expiry=0;');
}
}

/**
* Get database variable.
*
Expand Down Expand Up @@ -196,13 +178,24 @@ protected function truncateTables(): void

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

// Truncate only changed tables
$statement = $this->createQueryStatement(
'SELECT TABLE_NAME
$expiry = $this->getDatabaseVariable('information_schema_stats_expiry');
if ($expiry === null) {
// MariaDB: Truncate only changed tables
$statement = $this->createQueryStatement(
'SELECT TABLE_NAME
FROM information_schema.tables
WHERE table_schema = database()
AND update_time IS NOT NULL'
);
AND (update_time IS NOT NULL OR auto_increment IS NOT NULL)'
);
} else {
// MySQL: Truncate all tables
// Workaround for MySQL 8: update_time not working.
// Even SET information_schema_stats_expiry=0; has no affect anymore.
// https://bugs.mysql.com/bug.php?id=95407
$statement = $this->createQueryStatement(
'SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema = database()'
);
}

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

Expand Down

0 comments on commit dc758be

Please sign in to comment.