Skip to content

Commit 260855c

Browse files
[12.x] Fix dropping schema-qualified prefixed tables (#54834)
* fix dropping prefixed tables * add tests * fix tests
1 parent b7254f9 commit 260855c

File tree

4 files changed

+71
-9
lines changed

4 files changed

+71
-9
lines changed

src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ public function compileRenameIndex(Blueprint $blueprint, Fluent $command)
653653
*/
654654
public function compileDropAllTables($tables)
655655
{
656-
return 'drop table '.implode(', ', $this->wrapArray($tables));
656+
return 'drop table '.implode(', ', $this->escapeNames($tables));
657657
}
658658

659659
/**
@@ -664,7 +664,7 @@ public function compileDropAllTables($tables)
664664
*/
665665
public function compileDropAllViews($views)
666666
{
667-
return 'drop view '.implode(', ', $this->wrapArray($views));
667+
return 'drop view '.implode(', ', $this->escapeNames($views));
668668
}
669669

670670
/**
@@ -702,6 +702,20 @@ public function compileTableComment(Blueprint $blueprint, Fluent $command)
702702
);
703703
}
704704

705+
/**
706+
* Quote-escape the given tables, views, or types.
707+
*
708+
* @param array $names
709+
* @return array
710+
*/
711+
public function escapeNames($names)
712+
{
713+
return array_map(
714+
fn ($name) => (new Collection(explode('.', $name)))->map($this->wrapValue(...))->implode('.'),
715+
$names
716+
);
717+
}
718+
705719
/**
706720
* Create the column definition for a char type.
707721
*

src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -682,11 +682,10 @@ public function compileTableComment(Blueprint $blueprint, Fluent $command)
682682
*/
683683
public function escapeNames($names)
684684
{
685-
return array_map(static function ($name) {
686-
return '"'.(new Collection(explode('.', $name)))
687-
->map(fn ($segment) => trim($segment, '\'"'))
688-
->implode('"."').'"';
689-
}, $names);
685+
return array_map(
686+
fn ($name) => (new Collection(explode('.', $name)))->map($this->wrapValue(...))->implode('.'),
687+
$names
688+
);
690689
}
691690

692691
/**

tests/Database/DatabaseMySqlSchemaGrammarTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,6 +1490,22 @@ public function testDropAllViews()
14901490
$this->assertSame('drop view `alpha`, `beta`, `gamma`', $statement);
14911491
}
14921492

1493+
public function testDropAllTablesWithPrefixAndSchema()
1494+
{
1495+
$connection = $this->getConnection(prefix: 'prefix_');
1496+
$statement = $this->getGrammar($connection)->compileDropAllTables(['schema.alpha', 'schema.beta', 'schema.gamma']);
1497+
1498+
$this->assertSame('drop table `schema`.`alpha`, `schema`.`beta`, `schema`.`gamma`', $statement);
1499+
}
1500+
1501+
public function testDropAllViewsWithPrefixAndSchema()
1502+
{
1503+
$connection = $this->getConnection(prefix: 'prefix_');
1504+
$statement = $this->getGrammar($connection)->compileDropAllViews(['schema.alpha', 'schema.beta', 'schema.gamma']);
1505+
1506+
$this->assertSame('drop view `schema`.`alpha`, `schema`.`beta`, `schema`.`gamma`', $statement);
1507+
}
1508+
14931509
public function testGrammarsAreMacroable()
14941510
{
14951511
// compileReplace macro.

tests/Database/DatabasePostgresSchemaGrammarTest.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,38 @@ public function testDropAllTypesEscapesTableNames()
11101110
$this->assertSame('drop type "alpha", "beta", "gamma" cascade', $statement);
11111111
}
11121112

1113+
public function testDropAllTablesWithPrefixAndSchema()
1114+
{
1115+
$connection = $this->getConnection(prefix: 'prefix_');
1116+
$statement = $this->getGrammar($connection)->compileDropAllTables(['schema.alpha', 'schema.beta', 'schema.gamma']);
1117+
1118+
$this->assertSame('drop table "schema"."alpha", "schema"."beta", "schema"."gamma" cascade', $statement);
1119+
}
1120+
1121+
public function testDropAllViewsWithPrefixAndSchema()
1122+
{
1123+
$connection = $this->getConnection(prefix: 'prefix_');
1124+
$statement = $this->getGrammar($connection)->compileDropAllViews(['schema.alpha', 'schema.beta', 'schema.gamma']);
1125+
1126+
$this->assertSame('drop view "schema"."alpha", "schema"."beta", "schema"."gamma" cascade', $statement);
1127+
}
1128+
1129+
public function testDropAllTypesWithPrefixAndSchema()
1130+
{
1131+
$connection = $this->getConnection(prefix: 'prefix_');
1132+
$statement = $this->getGrammar($connection)->compileDropAllTypes(['schema.alpha', 'schema.beta', 'schema.gamma']);
1133+
1134+
$this->assertSame('drop type "schema"."alpha", "schema"."beta", "schema"."gamma" cascade', $statement);
1135+
}
1136+
1137+
public function testDropAllDomainsWithPrefixAndSchema()
1138+
{
1139+
$connection = $this->getConnection(prefix: 'prefix_');
1140+
$statement = $this->getGrammar($connection)->compileDropAllDomains(['schema.alpha', 'schema.beta', 'schema.gamma']);
1141+
1142+
$this->assertSame('drop domain "schema"."alpha", "schema"."beta", "schema"."gamma" cascade', $statement);
1143+
}
1144+
11131145
public function testCompileColumns()
11141146
{
11151147
$connection = $this->getConnection();
@@ -1122,10 +1154,11 @@ public function testCompileColumns()
11221154

11231155
protected function getConnection(
11241156
?PostgresGrammar $grammar = null,
1125-
?PostgresBuilder $builder = null
1157+
?PostgresBuilder $builder = null,
1158+
string $prefix = ''
11261159
) {
11271160
$connection = m::mock(Connection::class)
1128-
->shouldReceive('getTablePrefix')->andReturn('')
1161+
->shouldReceive('getTablePrefix')->andReturn($prefix)
11291162
->shouldReceive('getConfig')->with('prefix_indexes')->andReturn(null)
11301163
->getMock();
11311164

0 commit comments

Comments
 (0)