Skip to content

Commit

Permalink
FIX Clear table logic for MySQL 8
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Aug 5, 2024
1 parent 1ac3428 commit adc0533
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/ORM/Connect/MySQLDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,35 @@ public function random()
*/
public function clearTable($table)
{
$this->query("TRUNCATE TABLE \"$table\"");
// Not simply using "TRUNCATE TABLE \"$table\"" because DELETE is a lot quicker
// than TRUNCATE which is very relevant during unit testing. Using TRUNCATE will lead to an
// approximately 50% increase it the total time of running unit tests.
$self = $this;
$fn = function () use ($self, $table) {
$this->query("DELETE FROM \"$table\"");
// Check if resetting the auto-increment is needed
// Note that ALTER TABLE is an expensive operation so only do it if necessary
$autoIncrement = $self->preparedQuery(
'SELECT "AUTO_INCREMENT" FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?',
[ $this->getSelectedDatabase(), $table]
)->value();
if ($autoIncrement > 1) {
$self->query("ALTER TABLE \"$table\" AUTO_INCREMENT = 1");
}
};
if ($this->supportsTransactions()) {
// Wrap in a transaction because MySQL will use always use `max(ID) + 1` as a
// minimum value for auto-increment
// In MySQL 8, there were issues where the auto-increment was sometimes being reset to 2,
// presumbably due to some sort of timing issue
$this->withTransaction($fn);
} else {
$fn();
}
}

private function clearTableInner($table)
{

}
}

0 comments on commit adc0533

Please sign in to comment.