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

[12.x] Fix dropping schema-qualified prefixed tables #54834

Merged
Merged
Show file tree
Hide file tree
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
18 changes: 16 additions & 2 deletions src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ public function compileRenameIndex(Blueprint $blueprint, Fluent $command)
*/
public function compileDropAllTables($tables)
{
return 'drop table '.implode(', ', $this->wrapArray($tables));
return 'drop table '.implode(', ', $this->escapeNames($tables));
}

/**
Expand All @@ -664,7 +664,7 @@ public function compileDropAllTables($tables)
*/
public function compileDropAllViews($views)
{
return 'drop view '.implode(', ', $this->wrapArray($views));
return 'drop view '.implode(', ', $this->escapeNames($views));
}

/**
Expand Down Expand Up @@ -702,6 +702,20 @@ public function compileTableComment(Blueprint $blueprint, Fluent $command)
);
}

/**
* Quote-escape the given tables, views, or types.
*
* @param array $names
* @return array
*/
public function escapeNames($names)
{
return array_map(
fn ($name) => (new Collection(explode('.', $name)))->map($this->wrapValue(...))->implode('.'),
$names
);
}

/**
* Create the column definition for a char type.
*
Expand Down
9 changes: 4 additions & 5 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -682,11 +682,10 @@ public function compileTableComment(Blueprint $blueprint, Fluent $command)
*/
public function escapeNames($names)
{
return array_map(static function ($name) {
return '"'.(new Collection(explode('.', $name)))
->map(fn ($segment) => trim($segment, '\'"'))
->implode('"."').'"';
}, $names);
return array_map(
fn ($name) => (new Collection(explode('.', $name)))->map($this->wrapValue(...))->implode('.'),
$names
);
}

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/Database/DatabaseMySqlSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1490,6 +1490,22 @@ public function testDropAllViews()
$this->assertSame('drop view `alpha`, `beta`, `gamma`', $statement);
}

public function testDropAllTablesWithPrefixAndSchema()
{
$connection = $this->getConnection(prefix: 'prefix_');
$statement = $this->getGrammar($connection)->compileDropAllTables(['schema.alpha', 'schema.beta', 'schema.gamma']);

$this->assertSame('drop table `schema`.`alpha`, `schema`.`beta`, `schema`.`gamma`', $statement);
}

public function testDropAllViewsWithPrefixAndSchema()
{
$connection = $this->getConnection(prefix: 'prefix_');
$statement = $this->getGrammar($connection)->compileDropAllViews(['schema.alpha', 'schema.beta', 'schema.gamma']);

$this->assertSame('drop view `schema`.`alpha`, `schema`.`beta`, `schema`.`gamma`', $statement);
}

public function testGrammarsAreMacroable()
{
// compileReplace macro.
Expand Down
37 changes: 35 additions & 2 deletions tests/Database/DatabasePostgresSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,38 @@ public function testDropAllTypesEscapesTableNames()
$this->assertSame('drop type "alpha", "beta", "gamma" cascade', $statement);
}

public function testDropAllTablesWithPrefixAndSchema()
{
$connection = $this->getConnection(prefix: 'prefix_');
$statement = $this->getGrammar($connection)->compileDropAllTables(['schema.alpha', 'schema.beta', 'schema.gamma']);

$this->assertSame('drop table "schema"."alpha", "schema"."beta", "schema"."gamma" cascade', $statement);
}

public function testDropAllViewsWithPrefixAndSchema()
{
$connection = $this->getConnection(prefix: 'prefix_');
$statement = $this->getGrammar($connection)->compileDropAllViews(['schema.alpha', 'schema.beta', 'schema.gamma']);

$this->assertSame('drop view "schema"."alpha", "schema"."beta", "schema"."gamma" cascade', $statement);
}

public function testDropAllTypesWithPrefixAndSchema()
{
$connection = $this->getConnection(prefix: 'prefix_');
$statement = $this->getGrammar($connection)->compileDropAllTypes(['schema.alpha', 'schema.beta', 'schema.gamma']);

$this->assertSame('drop type "schema"."alpha", "schema"."beta", "schema"."gamma" cascade', $statement);
}

public function testDropAllDomainsWithPrefixAndSchema()
{
$connection = $this->getConnection(prefix: 'prefix_');
$statement = $this->getGrammar($connection)->compileDropAllDomains(['schema.alpha', 'schema.beta', 'schema.gamma']);

$this->assertSame('drop domain "schema"."alpha", "schema"."beta", "schema"."gamma" cascade', $statement);
}

public function testCompileColumns()
{
$connection = $this->getConnection();
Expand All @@ -1122,10 +1154,11 @@ public function testCompileColumns()

protected function getConnection(
?PostgresGrammar $grammar = null,
?PostgresBuilder $builder = null
?PostgresBuilder $builder = null,
string $prefix = ''
) {
$connection = m::mock(Connection::class)
->shouldReceive('getTablePrefix')->andReturn('')
->shouldReceive('getTablePrefix')->andReturn($prefix)
->shouldReceive('getConfig')->with('prefix_indexes')->andReturn(null)
->getMock();

Expand Down
Loading