Skip to content

Commit

Permalink
remove table prefix from DatabaseTruncation Trait to fix laravel#48264
Browse files Browse the repository at this point in the history
  • Loading branch information
mobidev86 committed Sep 4, 2023
1 parent 03564b1 commit 6d0bd67
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/Illuminate/Foundation/Testing/DatabaseTruncation.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,34 @@ protected function truncateTablesForConnection(ConnectionInterface $connection,
fn ($tables) => $tables->intersect($this->tablesToTruncate),
fn ($tables) => $tables->diff($this->exceptTables($name))
)
->filter(fn ($table) => $connection->table($table)->exists())
->each(fn ($table) => $connection->table($table)->truncate());
->filter(fn ($table) => $connection->table($this->removeTablePrefix($connection, $table))->exists())
->each(fn ($table) => $connection->table($this->removeTablePrefix($connection, $table))->truncate());

$connection->setEventDispatcher($dispatcher);
}

/**
* Remove the table prefix from a table name, if it exists.
*
* @param \Illuminate\Database\ConnectionInterface $connection
* @param string $table
* @return string
*/
protected function removeTablePrefix(ConnectionInterface $connection, string $table): string
{
// Retrieve the table prefix associated with the connection
$prefix = $connection->getTablePrefix();

// Check if the table name contains the prefix
if (strpos($table, $prefix) === 0) {
// If the prefix is found, remove it from the table name
return substr($table, strlen($prefix));
}

// If no prefix is found, return the original table name
return $table;
}

/**
* The database connections that should have their tables truncated.
*
Expand Down

0 comments on commit 6d0bd67

Please sign in to comment.