Skip to content

Commit

Permalink
[10.x] Fix issue with table prefix duplication in DatabaseTruncation …
Browse files Browse the repository at this point in the history
…trait (#48291)

* remove table prefix from DatabaseTruncation Trait to fix #48264

* formatting

* remove return type

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
mobidev86 and taylorotwell authored Sep 4, 2023
1 parent c02a064 commit cdf9dba
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/Illuminate/Foundation/Testing/DatabaseTruncation.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,28 @@ 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->withoutTablePrefix($connection, $table))->exists())
->each(fn ($table) => $connection->table($this->withoutTablePrefix($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 withoutTablePrefix(ConnectionInterface $connection, string $table)
{
$prefix = $connection->getTablePrefix();

return strpos($table, $prefix) === 0
? substr($table, strlen($prefix))
: $table;
}

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

0 comments on commit cdf9dba

Please sign in to comment.