From c5201dc01a69e9a0b9c84ad120104f9aeba7e86e Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Wed, 3 Oct 2018 11:36:11 +1300 Subject: [PATCH 1/2] FIX: Allow DataObjectTest to be run by itself --- tests/model/DataObjectTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/model/DataObjectTest.php b/tests/model/DataObjectTest.php index 58da7cc57..e4fdb953e 100644 --- a/tests/model/DataObjectTest.php +++ b/tests/model/DataObjectTest.php @@ -29,6 +29,7 @@ class DataObjectTest extends SapphireTest { 'DataObjectTest_Play', 'DataObjectTest_Ploy', 'DataObjectTest_Bogey', + 'DataObjectTest_Sortable', 'ManyManyListTest_Product', 'ManyManyListTest_Category', ); From ae9ab22a8ff1b48c90f7dfe2899c09efaa65b161 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Tue, 2 Oct 2018 17:52:30 +1300 Subject: [PATCH 2/2] 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. --- model/connect/MySQLDatabase.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/model/connect/MySQLDatabase.php b/model/connect/MySQLDatabase.php index f0628e4d5..92d86b513 100644 --- a/model/connect/MySQLDatabase.php +++ b/model/connect/MySQLDatabase.php @@ -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"); + } + } }