Skip to content

Commit 0dabf8e

Browse files
[11.x] Fix unescaped table names issue of DatabaseTruncation trait by introducing Connection::withoutTablePrefix() method (#53842)
* add connection withoutTablePrefix * formatting
1 parent 0365480 commit 0dabf8e

File tree

4 files changed

+47
-15
lines changed

4 files changed

+47
-15
lines changed

src/Illuminate/Database/Connection.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,6 +1646,23 @@ public function withTablePrefix(Grammar $grammar)
16461646
return $grammar;
16471647
}
16481648

1649+
/**
1650+
* Execute the given callback without table prefix.
1651+
*
1652+
* @param \Closure $callback
1653+
* @return void
1654+
*/
1655+
public function withoutTablePrefix(Closure $callback): void
1656+
{
1657+
$tablePrefix = $this->getTablePrefix();
1658+
1659+
$this->setTablePrefix('');
1660+
1661+
$callback($this);
1662+
1663+
$this->setTablePrefix($tablePrefix);
1664+
}
1665+
16491666
/**
16501667
* Get the server version for the connection.
16511668
*

src/Illuminate/Foundation/Testing/DatabaseTruncation.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Illuminate\Contracts\Console\Kernel;
66
use Illuminate\Database\ConnectionInterface;
7-
use Illuminate\Database\Query\Expression;
87
use Illuminate\Database\Schema\PostgresBuilder;
98
use Illuminate\Foundation\Testing\Traits\CanConfigureMigrationCommands;
109
use Illuminate\Support\Collection;
@@ -99,13 +98,15 @@ function (Collection $tables) use ($connection, $name) {
9998
}
10099
)
101100
->each(function (array $table) use ($connection) {
102-
$table = $connection->table(
103-
new Expression($table['schema'] ? $table['schema'].'.'.$table['name'] : $table['name'])
104-
);
105-
106-
if ($table->exists()) {
107-
$table->truncate();
108-
}
101+
$connection->withoutTablePrefix(function ($connection) use ($table) {
102+
$table = $connection->table(
103+
$table['schema'] ? $table['schema'].'.'.$table['name'] : $table['name']
104+
);
105+
106+
if ($table->exists()) {
107+
$table->truncate();
108+
}
109+
});
109110
});
110111

111112
$connection->setEventDispatcher($dispatcher);

tests/Foundation/Testing/DatabaseTruncationTest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
use Illuminate\Config\Repository;
66
use Illuminate\Contracts\Events\Dispatcher;
77
use Illuminate\Database\Connection;
8-
use Illuminate\Database\Grammar;
9-
use Illuminate\Database\Query\Expression;
108
use Illuminate\Database\Schema\Builder;
119
use Illuminate\Database\Schema\PostgresBuilder;
1210
use Illuminate\Foundation\Testing\DatabaseTruncation;
@@ -189,18 +187,18 @@ private function arrangeConnection(
189187
$schema->shouldReceive('getSchemas')->once()->andReturn($schemas);
190188
}
191189

192-
$grammar = m::mock(Grammar::class);
193-
194190
$connection = m::mock(Connection::class);
195191
$connection->shouldReceive('getTablePrefix')->andReturn($prefix);
196192
$connection->shouldReceive('getEventDispatcher')->once()->andReturn($dispatcher = m::mock(Dispatcher::class));
197193
$connection->shouldReceive('unsetEventDispatcher')->once();
198194
$connection->shouldReceive('setEventDispatcher')->once()->with($dispatcher);
199195
$connection->shouldReceive('getSchemaBuilder')->once()->andReturn($schema);
196+
$connection->shouldReceive('withoutTablePrefix')->andReturnUsing(function ($callback) use ($connection) {
197+
$callback($connection);
198+
});
200199
$connection->shouldReceive('table')
201-
->with(m::type(Expression::class))
202-
->andReturnUsing(function (Expression $expression) use (&$actual, $grammar) {
203-
$actual[] = $expression->getValue($grammar);
200+
->andReturnUsing(function (string $tableName) use (&$actual) {
201+
$actual[] = $tableName;
204202

205203
$table = m::mock();
206204
$table->shouldReceive('exists')->andReturnTrue();

tests/Integration/Database/DatabaseConnectionsTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Database\Events\ConnectionEstablished;
99
use Illuminate\Database\SQLiteConnection;
1010
use Illuminate\Events\Dispatcher;
11+
use Illuminate\Support\Facades\DB;
1112
use RuntimeException;
1213

1314
class DatabaseConnectionsTest extends DatabaseTestCase
@@ -118,4 +119,19 @@ public function testEstablishingAConnectionWillDispatchAnEvent()
118119

119120
self::assertSame('my-phpunit-connection', $event->connectionName);
120121
}
122+
123+
public function testTablePrefix()
124+
{
125+
DB::setTablePrefix('prefix_');
126+
$this->assertSame('prefix_', DB::getTablePrefix());
127+
128+
DB::withoutTablePrefix(function ($connection) {
129+
$this->assertSame('', $connection->getTablePrefix());
130+
});
131+
132+
$this->assertSame('prefix_', DB::getTablePrefix());
133+
134+
DB::setTablePrefix('');
135+
$this->assertSame('', DB::getTablePrefix());
136+
}
121137
}

0 commit comments

Comments
 (0)