Skip to content

[9.x] Fix Postgres driver not dropping all tables & views #41701

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

Merged
merged 2 commits into from
Mar 28, 2022
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
10 changes: 3 additions & 7 deletions src/Illuminate/Database/Concerns/ParsesSearchPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@ protected function parseSearchPath($searchPath)
$searchPath = $matches[0];
}

$searchPath ??= [];

array_walk($searchPath, static function (&$schema) {
$schema = trim($schema, '\'"');
});

return $searchPath;
return array_map(function ($schema) {
return trim($schema, '\'"');
}, $searchPath ?? []);
}
}
25 changes: 20 additions & 5 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public function compileDropIfExists(Blueprint $blueprint, Fluent $command)
*/
public function compileDropAllTables($tables)
{
return 'drop table "'.implode('","', $tables).'" cascade';
return 'drop table '.implode(',', $this->escapeNames($tables)).' cascade';
}

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

/**
Expand All @@ -294,7 +294,7 @@ public function compileDropAllViews($views)
*/
public function compileDropAllTypes($types)
{
return 'drop type "'.implode('","', $types).'" cascade';
return 'drop type '.implode(',', $this->escapeNames($types)).' cascade';
}

/**
Expand All @@ -305,7 +305,7 @@ public function compileDropAllTypes($types)
*/
public function compileGetAllTables($searchPath)
{
return "select tablename from pg_catalog.pg_tables where schemaname in ('".implode("','", (array) $searchPath)."')";
return "select tablename, concat('\"', schemaname, '\".\"', tablename, '\"') as qualifiedname from pg_catalog.pg_tables where schemaname in ('".implode("','", (array) $searchPath)."')";
}

/**
Expand All @@ -316,7 +316,7 @@ public function compileGetAllTables($searchPath)
*/
public function compileGetAllViews($searchPath)
{
return "select viewname from pg_catalog.pg_views where schemaname in ('".implode("','", (array) $searchPath)."')";
return "select viewname, concat('\"', schemaname, '\".\"', viewname, '\"') as qualifiedname from pg_catalog.pg_views where schemaname in ('".implode("','", (array) $searchPath)."')";
}

/**
Expand Down Expand Up @@ -486,6 +486,21 @@ public function compileComment(Blueprint $blueprint, Fluent $command)
);
}

/**
* Quote-escape the given tables, views, or types.
*
* @param array $names
* @return array
*/
public function escapeNames($names)
{
return array_map(static function ($name) {
return '"'.collect(explode('.', $name))
->map(fn ($segment) => trim($segment, '\'"'))
->implode('"."').'"';
}, $names);
}

/**
* Create the column definition for a char type.
*
Expand Down
22 changes: 9 additions & 13 deletions src/Illuminate/Database/Schema/PostgresBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ public function dropAllTables()
{
$tables = [];

$excludedTables = $this->connection->getConfig('dont_drop') ?? ['spatial_ref_sys'];
$excludedTables = $this->grammar->escapeNames(
$this->connection->getConfig('dont_drop') ?? ['spatial_ref_sys']
);

foreach ($this->getAllTables() as $row) {
$row = (array) $row;

$table = reset($row);

if (! in_array($table, $excludedTables)) {
$tables[] = $table;
if (empty(array_intersect($this->grammar->escapeNames($row), $excludedTables))) {
$tables[] = $row['qualifiedname'] ?? reset($row);
}
}

Expand All @@ -95,7 +95,7 @@ public function dropAllViews()
foreach ($this->getAllViews() as $row) {
$row = (array) $row;

$views[] = reset($row);
$views[] = $row['qualifiedname'] ?? reset($row);
}

if (empty($views)) {
Expand Down Expand Up @@ -239,14 +239,10 @@ protected function parseSchemaAndTable($reference)
*/
protected function parseSearchPath($searchPath)
{
$searchPath = $this->baseParseSearchPath($searchPath);

array_walk($searchPath, function (&$schema) {
$schema = $schema === '$user'
return array_map(function ($schema) {
return $schema === '$user'
? $this->connection->getConfig('username')
: $schema;
});

return $searchPath;
}, $this->baseParseSearchPath($searchPath));
}
}
30 changes: 18 additions & 12 deletions tests/Database/DatabasePostgresBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,12 @@ public function testDropAllTablesWhenSearchPathIsString()
$connection->shouldReceive('getConfig')->with('dont_drop')->andReturn(['foo']);
$grammar = m::mock(PostgresGrammar::class);
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
$grammar->shouldReceive('compileGetAllTables')->with(['public'])->andReturn("select tablename from pg_catalog.pg_tables where schemaname in ('public')");
$connection->shouldReceive('select')->with("select tablename from pg_catalog.pg_tables where schemaname in ('public')")->andReturn(['users']);
$grammar->shouldReceive('compileDropAllTables')->with(['users'])->andReturn('drop table "'.implode('","', ['users']).'" cascade');
$connection->shouldReceive('statement')->with('drop table "'.implode('","', ['users']).'" cascade');
$grammar->shouldReceive('compileGetAllTables')->with(['public'])->andReturn("select tablename, concat('\"', schemaname, '\".\"', tablename, '\"') as qualifiedname from pg_catalog.pg_tables where schemaname in ('public')");
$connection->shouldReceive('select')->with("select tablename, concat('\"', schemaname, '\".\"', tablename, '\"') as qualifiedname from pg_catalog.pg_tables where schemaname in ('public')")->andReturn([['tablename' => 'users', 'qualifiedname' => '"public"."users"']]);
$grammar->shouldReceive('escapeNames')->with(['foo'])->andReturn(['"foo"']);
$grammar->shouldReceive('escapeNames')->with(['tablename' => 'users', 'qualifiedname' => '"public"."users"'])->andReturn(['tablename' => '"users"', 'qualifiedname' => '"public"."users"']);
$grammar->shouldReceive('compileDropAllTables')->with(['"public"."users"'])->andReturn('drop table "public"."users" cascade');
$connection->shouldReceive('statement')->with('drop table "public"."users" cascade');
$builder = $this->getBuilder($connection);

$builder->dropAllTables();
Expand All @@ -255,10 +257,12 @@ public function testDropAllTablesWhenSearchPathIsStringOfMany()
$connection->shouldReceive('getConfig')->with('dont_drop')->andReturn(['foo']);
$grammar = m::mock(PostgresGrammar::class);
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
$grammar->shouldReceive('compileGetAllTables')->with(['foouser', 'public', 'foo_bar-Baz.Áüõß'])->andReturn("select tablename from pg_catalog.pg_tables where schemaname in ('foouser','public','foo_bar-Baz.Áüõß')");
$connection->shouldReceive('select')->with("select tablename from pg_catalog.pg_tables where schemaname in ('foouser','public','foo_bar-Baz.Áüõß')")->andReturn(['users', 'users']);
$grammar->shouldReceive('compileDropAllTables')->with(['users', 'users'])->andReturn('drop table "'.implode('","', ['users', 'users']).'" cascade');
$connection->shouldReceive('statement')->with('drop table "'.implode('","', ['users', 'users']).'" cascade');
$grammar->shouldReceive('compileGetAllTables')->with(['foouser', 'public', 'foo_bar-Baz.Áüõß'])->andReturn("select tablename, concat('\"', schemaname, '\".\"', tablename, '\"') as qualifiedname from pg_catalog.pg_tables where schemaname in ('foouser','public','foo_bar-Baz.Áüõß')");
$connection->shouldReceive('select')->with("select tablename, concat('\"', schemaname, '\".\"', tablename, '\"') as qualifiedname from pg_catalog.pg_tables where schemaname in ('foouser','public','foo_bar-Baz.Áüõß')")->andReturn([['tablename' => 'users', 'qualifiedname' => '"foouser"."users"']]);
$grammar->shouldReceive('escapeNames')->with(['foo'])->andReturn(['"foo"']);
$grammar->shouldReceive('escapeNames')->with(['tablename' => 'users', 'qualifiedname' => '"foouser"."users"'])->andReturn(['tablename' => '"users"', 'qualifiedname' => '"foouser"."users"']);
$grammar->shouldReceive('compileDropAllTables')->with(['"foouser"."users"'])->andReturn('drop table "foouser"."users" cascade');
$connection->shouldReceive('statement')->with('drop table "foouser"."users" cascade');
$builder = $this->getBuilder($connection);

$builder->dropAllTables();
Expand All @@ -277,10 +281,12 @@ public function testDropAllTablesWhenSearchPathIsArrayOfMany()
$connection->shouldReceive('getConfig')->with('dont_drop')->andReturn(['foo']);
$grammar = m::mock(PostgresGrammar::class);
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
$grammar->shouldReceive('compileGetAllTables')->with(['foouser', 'dev', 'test', 'spaced schema'])->andReturn("select tablename from pg_catalog.pg_tables where schemaname in ('foouser','dev','test','spaced schema')");
$connection->shouldReceive('select')->with("select tablename from pg_catalog.pg_tables where schemaname in ('foouser','dev','test','spaced schema')")->andReturn(['users', 'users']);
$grammar->shouldReceive('compileDropAllTables')->with(['users', 'users'])->andReturn('drop table "'.implode('","', ['users', 'users']).'" cascade');
$connection->shouldReceive('statement')->with('drop table "'.implode('","', ['users', 'users']).'" cascade');
$grammar->shouldReceive('compileGetAllTables')->with(['foouser', 'dev', 'test', 'spaced schema'])->andReturn("select tablename, concat('\"', schemaname, '\".\"', tablename, '\"') as qualifiedname from pg_catalog.pg_tables where schemaname in ('foouser','dev','test','spaced schema')");
$connection->shouldReceive('select')->with("select tablename, concat('\"', schemaname, '\".\"', tablename, '\"') as qualifiedname from pg_catalog.pg_tables where schemaname in ('foouser','dev','test','spaced schema')")->andReturn([['tablename' => 'users', 'qualifiedname' => '"foouser"."users"']]);
$grammar->shouldReceive('escapeNames')->with(['foo'])->andReturn(['"foo"']);
$grammar->shouldReceive('escapeNames')->with(['tablename' => 'users', 'qualifiedname' => '"foouser"."users"'])->andReturn(['tablename' => '"users"', 'qualifiedname' => '"foouser"."users"']);
$grammar->shouldReceive('compileDropAllTables')->with(['"foouser"."users"'])->andReturn('drop table "foouser"."users" cascade');
$connection->shouldReceive('statement')->with('drop table "foouser"."users" cascade');
$builder = $this->getBuilder($connection);

$builder->dropAllTables();
Expand Down
118 changes: 118 additions & 0 deletions tests/Integration/Database/Postgres/PostgresSchemaBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Illuminate\Tests\Integration\Database\Postgres;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

/**
* @requires extension pdo_pgsql
* @requires OS Linux|Darwin
*/
class PostgresSchemaBuilderTest extends PostgresTestCase
{
protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);

$app['config']->set('database.connections.pgsql.search_path', 'public,private');
}

protected function defineDatabaseMigrations()
{
parent::defineDatabaseMigrations();

DB::statement('create schema if not exists private');
}

protected function destroyDatabaseMigrations()
{
DB::statement('drop table if exists public.table');
DB::statement('drop table if exists private.table');

DB::statement('drop view if exists public.foo');
DB::statement('drop view if exists private.foo');

DB::statement('drop schema private');

parent::destroyDatabaseMigrations();
}

public function testDropAllTablesOnAllSchemas()
{
Schema::create('public.table', function (Blueprint $table) {
$table->increments('id');
});
Schema::create('private.table', function (Blueprint $table) {
$table->increments('id');
});

Schema::dropAllTables();

$this->artisan('migrate:install');

$this->assertFalse(Schema::hasTable('public.table'));
$this->assertFalse(Schema::hasTable('private.table'));
}

public function testDropAllTablesUsesDontDropConfigOnAllSchemas()
{
$this->app['config']->set('database.connections.pgsql.dont_drop', ['spatial_ref_sys', 'table']);
DB::purge('pgsql');

Schema::create('public.table', function (Blueprint $table) {
$table->increments('id');
});
Schema::create('private.table', function (Blueprint $table) {
$table->increments('id');
});

Schema::dropAllTables();

$this->artisan('migrate:install');

$this->assertTrue(Schema::hasTable('public.table'));
$this->assertTrue(Schema::hasTable('private.table'));
}

public function testDropAllTablesUsesDontDropConfigOnOneSchema()
{
$this->app['config']->set('database.connections.pgsql.dont_drop', ['spatial_ref_sys', 'private.table']);
DB::purge('pgsql');

Schema::create('public.table', function (Blueprint $table) {
$table->increments('id');
});
Schema::create('private.table', function (Blueprint $table) {
$table->increments('id');
});

Schema::dropAllTables();

$this->artisan('migrate:install');

$this->assertFalse(Schema::hasTable('public.table'));
$this->assertTrue(Schema::hasTable('private.table'));
}

public function testDropAllViewsOnAllSchemas()
{
DB::statement('create view public.foo (id) as select 1');
DB::statement('create view private.foo (id) as select 1');

Schema::dropAllViews();

$this->assertFalse($this->hasView('public', 'foo'));
$this->assertFalse($this->hasView('private', 'foo'));
}

protected function hasView($schema, $table)
{
return DB::table('information_schema.views')
->where('table_catalog', $this->app['config']->get('database.connections.pgsql.database'))
->where('table_schema', $schema)
->where('table_name', $table)
->exists();
}
}