Skip to content

Commit

Permalink
Fix schema quoting issue and update tests to verify behavior
Browse files Browse the repository at this point in the history
More specifically, fix issue whereby individual schema paths in an array were quoted twice.

Also, update arguments in tests to verify schema name parsing with vs. without quotes, for both string and array notations.
  • Loading branch information
cbj4074 committed Dec 4, 2020
1 parent 54c3fd3 commit 354784f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/Illuminate/Database/Connectors/PostgresConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ protected function configureTimezone($connection, array $config)
*/
protected function configureSearchPath($connection, $config)
{
if (isset($config['search_path']) || isset($config['schema'])) {
$searchPath = $this->formatSearchPath($config['search_path'] ?? $config['schema']);
if (isset($config['search_path'])) {
$searchPath = $this->formatSearchPath($config['search_path']);

$connection->prepare("set search_path to {$searchPath}")->execute();
}
Expand All @@ -109,15 +109,16 @@ protected function configureSearchPath($connection, $config)
protected function formatSearchPath($searchPath)
{
if (is_array($searchPath)) {
return '"'.implode('", "', $searchPath).'"';
$searchPath = '"'.implode('", "', $searchPath).'"';
}

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
8 changes: 4 additions & 4 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 @@ -120,7 +120,7 @@ public function testPostgresSearchPathArraySupported()
public function testPostgresSearchPathCommaSeparatedValueSupported()
{
$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('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']));
Expand All @@ -136,7 +136,7 @@ public function testPostgresSearchPathCommaSeparatedValueSupported()
public function testPostgresSearchPathVariablesSupported()
{
$dsn = 'pgsql:host=foo;dbname=bar';
$config = ['host' => 'foo', 'database' => 'bar', 'schema' => '$user, public, user', 'charset' => 'utf8'];
$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']));
Expand Down

0 comments on commit 354784f

Please sign in to comment.