Skip to content

Commit cdf9dba

Browse files
[10.x] Fix issue with table prefix duplication in DatabaseTruncation trait (#48291)
* remove table prefix from DatabaseTruncation Trait to fix #48264 * formatting * remove return type --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent c02a064 commit cdf9dba

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/Illuminate/Foundation/Testing/DatabaseTruncation.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,28 @@ protected function truncateTablesForConnection(ConnectionInterface $connection,
8585
fn ($tables) => $tables->intersect($this->tablesToTruncate),
8686
fn ($tables) => $tables->diff($this->exceptTables($name))
8787
)
88-
->filter(fn ($table) => $connection->table($table)->exists())
89-
->each(fn ($table) => $connection->table($table)->truncate());
88+
->filter(fn ($table) => $connection->table($this->withoutTablePrefix($connection, $table))->exists())
89+
->each(fn ($table) => $connection->table($this->withoutTablePrefix($connection, $table))->truncate());
9090

9191
$connection->setEventDispatcher($dispatcher);
9292
}
9393

94+
/**
95+
* Remove the table prefix from a table name, if it exists.
96+
*
97+
* @param \Illuminate\Database\ConnectionInterface $connection
98+
* @param string $table
99+
* @return string
100+
*/
101+
protected function withoutTablePrefix(ConnectionInterface $connection, string $table)
102+
{
103+
$prefix = $connection->getTablePrefix();
104+
105+
return strpos($table, $prefix) === 0
106+
? substr($table, strlen($prefix))
107+
: $table;
108+
}
109+
94110
/**
95111
* The database connections that should have their tables truncated.
96112
*

0 commit comments

Comments
 (0)