From febacfc570564281c565d2d224995a00bcb2371b Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Thu, 27 Feb 2025 22:32:05 +0330 Subject: [PATCH 1/3] fix dropping prefixed tables --- .../Database/Schema/Grammars/MySqlGrammar.php | 18 ++++++++++++++++-- .../Schema/Grammars/PostgresGrammar.php | 9 ++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php b/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php index 38fd418141b2..693fc78a3659 100755 --- a/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php @@ -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)); } /** @@ -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)); } /** @@ -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. * diff --git a/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php b/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php index 2a49cd1d1c23..1eae481a8df9 100755 --- a/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php @@ -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 + ); } /** From bcbf68468f8ddd698a81cb6c19249b097408b617 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Thu, 27 Feb 2025 22:32:11 +0330 Subject: [PATCH 2/3] add tests --- .../DatabaseMySqlSchemaGrammarTest.php | 16 ++++++++ .../DatabasePostgresSchemaGrammarTest.php | 37 ++++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/tests/Database/DatabaseMySqlSchemaGrammarTest.php b/tests/Database/DatabaseMySqlSchemaGrammarTest.php index a00aff0472b9..09082dab62df 100755 --- a/tests/Database/DatabaseMySqlSchemaGrammarTest.php +++ b/tests/Database/DatabaseMySqlSchemaGrammarTest.php @@ -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. diff --git a/tests/Database/DatabasePostgresSchemaGrammarTest.php b/tests/Database/DatabasePostgresSchemaGrammarTest.php index 36e26f9885b8..4bd9a11e02b9 100755 --- a/tests/Database/DatabasePostgresSchemaGrammarTest.php +++ b/tests/Database/DatabasePostgresSchemaGrammarTest.php @@ -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 type "schema"."alpha", "schema"."beta", "schema"."gamma" cascade', $statement); + } + public function testCompileColumns() { $connection = $this->getConnection(); @@ -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(); From 1c712d83e43bed466d0e833747371c814056a8ba Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Thu, 27 Feb 2025 22:40:49 +0330 Subject: [PATCH 3/3] fix tests --- tests/Database/DatabasePostgresSchemaGrammarTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Database/DatabasePostgresSchemaGrammarTest.php b/tests/Database/DatabasePostgresSchemaGrammarTest.php index 4bd9a11e02b9..220cdf9c5fc9 100755 --- a/tests/Database/DatabasePostgresSchemaGrammarTest.php +++ b/tests/Database/DatabasePostgresSchemaGrammarTest.php @@ -1139,7 +1139,7 @@ public function testDropAllDomainsWithPrefixAndSchema() $connection = $this->getConnection(prefix: 'prefix_'); $statement = $this->getGrammar($connection)->compileDropAllDomains(['schema.alpha', 'schema.beta', 'schema.gamma']); - $this->assertSame('drop type "schema"."alpha", "schema"."beta", "schema"."gamma" cascade', $statement); + $this->assertSame('drop domain "schema"."alpha", "schema"."beta", "schema"."gamma" cascade', $statement); } public function testCompileColumns()