Skip to content
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: 5 additions & 5 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,12 @@ public function compileDropAllTypes($types)
/**
* Compile the SQL needed to retrieve all table names.
*
* @param string|array $schema
* @param string|array $searchPath
* @return string
*/
public function compileGetAllTables($schema)
public function compileGetAllTables($searchPath)
{
return "select tablename from pg_catalog.pg_tables where schemaname in ('".implode("','", (array) $schema)."')";
return "select tablename from pg_catalog.pg_tables where schemaname in ('".implode("','", (array) $searchPath)."')";
}

/**
Expand All @@ -260,9 +260,9 @@ public function compileGetAllTables($schema)
* @param string|array $schema
* @return string
*/
public function compileGetAllViews($schema)
public function compileGetAllViews($searchPath)
{
return "select viewname from pg_catalog.pg_views where schemaname in ('".implode("','", (array) $schema)."')";
return "select viewname from pg_catalog.pg_views where schemaname in ('".implode("','", (array) $searchPath)."')";
}

/**
Expand Down
12 changes: 10 additions & 2 deletions src/Illuminate/Database/Schema/PostgresBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ public function dropAllTypes()
public function getAllTables()
{
return $this->connection->select(
$this->grammar->compileGetAllTables((array) $this->connection->getConfig('schema'))
$this->grammar->compileGetAllTables(
$this->parseSearchPath($this->connection->getConfig('search_path'))
)
);
}

Expand All @@ -119,7 +121,9 @@ public function getAllTables()
public function getAllViews()
{
return $this->connection->select(
$this->grammar->compileGetAllViews((array) $this->connection->getConfig('schema'))
$this->grammar->compileGetAllViews(
$this->parseSearchPath($this->connection->getConfig('search_path'))
)
);
}

Expand Down Expand Up @@ -209,6 +213,10 @@ protected function parseSearchPath($searchPath)

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

$schema = $schema === '$user'
? $this->connection->getConfig('username')
: $schema;
});

return $searchPath;
Expand Down
45 changes: 45 additions & 0 deletions tests/Database/DatabasePostgresBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,51 @@ public function testWhenDatabaseNotDefaultGetColumnListingWithFullyQualifiedRefe
$builder->getColumnListing('mydatabase.myapp.foo');
}

/**
* Ensure that when the search_path contains just one schema, only that
* schema is passed into the query that is executed to acquire the list
* of tables to be dropped.
*/
public function testDropAllTablesWithOneSchemaInSearchPath()
{
$connection = $this->getConnection();
$connection->shouldReceive('getConfig')->with('search_path')->andReturn('public');
$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');
$builder = $this->getBuilder($connection);

$builder->dropAllTables();
}

/**
* Ensure that when the search_path contains more than one schema, both
* schemas are passed into the query that is executed to acquire the list
* of tables to be dropped. Furthermore, ensure that the special '$user'
* variable is resolved to the username specified on the database connection
* in the process.
*/
public function testDropAllTablesWithMoreThanOneSchemaInSearchPath()
{
$connection = $this->getConnection();
$connection->shouldReceive('getConfig')->with('username')->andReturn('foouser');
$connection->shouldReceive('getConfig')->with('search_path')->andReturn('"$user", public');
$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'])->andReturn("select tablename from pg_catalog.pg_tables where schemaname in ('foouser','public')");
$connection->shouldReceive('select')->with("select tablename from pg_catalog.pg_tables where schemaname in ('foouser','public')")->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');
$builder = $this->getBuilder($connection);

$builder->dropAllTables();
}

protected function getConnection()
{
return m::mock(Connection::class);
Expand Down