Skip to content

[9.x] Address confusion between PostgreSQL concepts "schema" and "search_path" #35463

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 5 commits into from
Dec 4, 2020
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
28 changes: 15 additions & 13 deletions src/Illuminate/Database/Connectors/PostgresConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function connect(array $config)
// database. Setting this DB timezone is an optional configuration item.
$this->configureTimezone($connection, $config);

$this->configureSchema($connection, $config);
$this->configureSearchPath($connection, $config);

// Postgres allows an application_name to be set by the user and this name is
// used to when monitoring the application with pg_stat_activity. So we'll
Expand Down Expand Up @@ -85,38 +85,40 @@ protected function configureTimezone($connection, array $config)
}

/**
* Set the schema on the connection.
* Set the search_path on the connection.
*
* @param \PDO $connection
* @param array $config
* @return void
*/
protected function configureSchema($connection, $config)
protected function configureSearchPath($connection, $config)
{
if (isset($config['schema'])) {
$schema = $this->formatSchema($config['schema']);
if (isset($config['search_path'])) {
$searchPath = $this->formatSearchPath($config['search_path']);

$connection->prepare("set search_path to {$schema}")->execute();
$connection->prepare("set search_path to {$searchPath}")->execute();
}
}

/**
* Format the schema for the DSN.
* Format the search path for the DSN.
*
* @param array|string $schema
* @param array|string $searchPath
* @return string
*/
protected function formatSchema($schema)
protected function formatSearchPath($searchPath)
{
if (is_array($schema)) {
return '"'.implode('", "', $schema).'"';
if (is_array($searchPath)) {
$searchPath = '"'.implode('", "', $searchPath).'"';
}

return '"'.$schema.'"';
preg_match_all('/[a-zA-z0-9$]{1,}/i', $searchPath, $matches);

return '"'.implode('", "', $matches[0]).'"';
}

/**
* Set the schema on the connection.
* Set the application name on the connection.
*
* @param \PDO $connection
* @param array $config
Expand Down
6 changes: 3 additions & 3 deletions src/Illuminate/Database/Schema/PostgresBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ protected function parseSchemaAndTable($table)
{
$table = explode('.', $table);

if (is_array($schema = $this->connection->getConfig('schema'))) {
if (in_array($table[0], $schema)) {
if (is_array($searchPath = $this->connection->getConfig('search_path'))) {
if (in_array($table[0], $searchPath)) {
return [array_shift($table), implode('.', $table)];
}

$schema = head($schema);
$schema = head($searchPath);
}

return [$schema ?: 'public', implode('.', $table)];
Expand Down
36 changes: 34 additions & 2 deletions tests/Database/DatabaseConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function testPostgresConnectCallsCreateConnectionWithProperArguments()
public function testPostgresSearchPathIsSet()
{
$dsn = 'pgsql:host=foo;dbname=bar';
$config = ['host' => 'foo', 'database' => 'bar', 'schema' => 'public', 'charset' => 'utf8'];
$config = ['host' => 'foo', 'database' => 'bar', 'search_path' => 'public', 'charset' => 'utf8'];
$connector = $this->getMockBuilder(PostgresConnector::class)->onlyMethods(['createConnection', 'getOptions'])->getMock();
$connection = m::mock(stdClass::class);
$connector->expects($this->once())->method('getOptions')->with($this->equalTo($config))->willReturn(['options']);
Expand All @@ -104,7 +104,7 @@ public function testPostgresSearchPathIsSet()
public function testPostgresSearchPathArraySupported()
{
$dsn = 'pgsql:host=foo;dbname=bar';
$config = ['host' => 'foo', 'database' => 'bar', 'schema' => ['public', 'user'], 'charset' => 'utf8'];
$config = ['host' => 'foo', 'database' => 'bar', 'search_path' => ['public', '"user"'], 'charset' => 'utf8'];
$connector = $this->getMockBuilder(PostgresConnector::class)->onlyMethods(['createConnection', 'getOptions'])->getMock();
$connection = m::mock(stdClass::class);
$connector->expects($this->once())->method('getOptions')->with($this->equalTo($config))->willReturn(['options']);
Expand All @@ -117,6 +117,38 @@ public function testPostgresSearchPathArraySupported()
$this->assertSame($result, $connection);
}

public function testPostgresSearchPathCommaSeparatedValueSupported()
{
$dsn = 'pgsql:host=foo;dbname=bar';
$config = ['host' => 'foo', 'database' => 'bar', 'search_path' => 'public, "user"', 'charset' => 'utf8'];
$connector = $this->getMockBuilder('Illuminate\Database\Connectors\PostgresConnector')->setMethods(['createConnection', 'getOptions'])->getMock();
$connection = m::mock('stdClass');
$connector->expects($this->once())->method('getOptions')->with($this->equalTo($config))->will($this->returnValue(['options']));
$connector->expects($this->once())->method('createConnection')->with($this->equalTo($dsn), $this->equalTo($config), $this->equalTo(['options']))->will($this->returnValue($connection));
$connection->shouldReceive('prepare')->once()->with('set names \'utf8\'')->andReturn($connection);
$connection->shouldReceive('prepare')->once()->with('set search_path to "public", "user"')->andReturn($connection);
$connection->shouldReceive('execute')->twice();
$result = $connector->connect($config);

$this->assertSame($result, $connection);
}

public function testPostgresSearchPathVariablesSupported()
{
$dsn = 'pgsql:host=foo;dbname=bar';
$config = ['host' => 'foo', 'database' => 'bar', 'search_path' => '"$user", public, user', 'charset' => 'utf8'];
$connector = $this->getMockBuilder('Illuminate\Database\Connectors\PostgresConnector')->setMethods(['createConnection', 'getOptions'])->getMock();
$connection = m::mock('stdClass');
$connector->expects($this->once())->method('getOptions')->with($this->equalTo($config))->will($this->returnValue(['options']));
$connector->expects($this->once())->method('createConnection')->with($this->equalTo($dsn), $this->equalTo($config), $this->equalTo(['options']))->will($this->returnValue($connection));
$connection->shouldReceive('prepare')->once()->with('set names \'utf8\'')->andReturn($connection);
$connection->shouldReceive('prepare')->once()->with('set search_path to "$user", "public", "user"')->andReturn($connection);
$connection->shouldReceive('execute')->twice();
$result = $connector->connect($config);

$this->assertSame($result, $connection);
}

public function testPostgresApplicationNameIsSet()
{
$dsn = 'pgsql:host=foo;dbname=bar';
Expand Down