From 0cc72c91ada58d6927dab6e93bfe785b623f3e7a Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Wed, 3 Oct 2018 13:24:06 +1300 Subject: [PATCH] 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. Cherry-pick of SS3 ae9ab22a8ff1b48c90f7dfe2899c09efaa65b161 --- src/ORM/Connect/MySQLDatabase.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/ORM/Connect/MySQLDatabase.php b/src/ORM/Connect/MySQLDatabase.php index 49f6dd62d..7a518ee70 100644 --- a/src/ORM/Connect/MySQLDatabase.php +++ b/src/ORM/Connect/MySQLDatabase.php @@ -523,4 +523,24 @@ class MySQLDatabase extends Database { 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"); + } + } }