FIX: Use DELETE FROM instead of TRUNCATE for clearTable

clearTable is mainly used for clearing data between tests. In this case,
there are very few or zero records, and DELETE FROM is quicker than
TRUNCATE, which works by deleting and recreating the table.

This materially speeds up test execution, at least on MySQL.
This commit is contained in:
Sam Minnee 2018-10-02 17:52:30 +13:00
parent c5201dc01a
commit ae9ab22a8f

View File

@ -362,4 +362,23 @@ class MySQLDatabase extends SS_Database {
public function random() {
return 'RAND()';
}
/**
* Clear all data in a given table
*
* @param string $table Name of table
*/
public function clearTable($table) {
$this->query("DELETE FROM \"$table\"");
// Check if resetting the auto-increment is needed
$autoIncrement = $this->preparedQuery(
'SELECT "AUTO_INCREMENT" FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?',
[ $this->getSelectedDatabase(), $table]
)->value();
if ($autoIncrement > 1) {
$this->query("ALTER TABLE \"$table\" AUTO_INCREMENT = 1");
}
}
}