Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Fix issue with table prefix duplication in DatabaseTruncation trait #48291

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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