Skip to content

Commit

Permalink
PostgresBuilder fixes for renamed `config('database.connections.pgsql…
Browse files Browse the repository at this point in the history
….search_path')` (#41215)

1. `PostgresBuilder::parseSchemaAndTable()`
   * Needs a fallback to <= 8.x `config('database.connections.pgsql.schema')`
     when 9.x renamed `config('database.connections.pgsql.search_path')`
     is missing.
   * Remove duplicate `$user` variable `config('database.connections.pgsql.username')`
     replacement handling already done by `parseSearchPath()`.
2. `PostgresBuilder::getAllTables()` + `getAllViews()` + `parseSchemaAndTable()`
   Apply the `parseSearchPath()` fixes applied to PostgresConnector:
   laravel/framework#41088
3. `DatabasePostgresBuilderTest`
   Add more test cases and use terse method names instead of extensive
   comments to concisely communicate each case.
  • Loading branch information
derekmd authored Feb 24, 2022
1 parent 2676dcd commit 6f24977
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 38 deletions.
29 changes: 29 additions & 0 deletions Concerns/ParsesSearchPath.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Illuminate\Database\Concerns;

trait ParsesSearchPath
{
/**
* Parse the Postgres "search_path" configuration value into an array.
*
* @param string|array|null $searchPath
* @return array
*/
protected function parseSearchPath($searchPath)
{
if (is_string($searchPath)) {
preg_match_all('/[^\s,"\']+/', $searchPath, $matches);

$searchPath = $matches[0];
}

$searchPath ??= [];

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

return $searchPath;
}
}
26 changes: 3 additions & 23 deletions Connectors/PostgresConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Illuminate\Database\Connectors;

use Illuminate\Database\Concerns\ParsesSearchPath;
use PDO;

class PostgresConnector extends Connector implements ConnectorInterface
{
use ParsesSearchPath;

/**
* The default PDO connection options.
*
Expand Down Expand Up @@ -118,29 +121,6 @@ protected function configureSearchPath($connection, $config)
}
}

/**
* Parse the "search_path" configuration value into an array.
*
* @param string|array $searchPath
* @return array
*/
protected function parseSearchPath($searchPath)
{
if (is_string($searchPath)) {
preg_match_all('/[^\s,"\']+/', $searchPath, $matches);

$searchPath = $matches[0];
}

$searchPath ??= [];

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

return $searchPath;
}

/**
* Format the search path for the DSN.
*
Expand Down
26 changes: 11 additions & 15 deletions Schema/PostgresBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

namespace Illuminate\Database\Schema;

use Illuminate\Database\Concerns\ParsesSearchPath;

class PostgresBuilder extends Builder
{
use ParsesSearchPath {
parseSearchPath as baseParseSearchPath;
}

/**
* Create a database in the schema.
*
Expand Down Expand Up @@ -197,7 +203,7 @@ public function getColumnListing($table)
protected function parseSchemaAndTable($reference)
{
$searchPath = $this->parseSearchPath(
$this->connection->getConfig('search_path') ?: 'public'
$this->connection->getConfig('search_path') ?: $this->connection->getConfig('schema') ?: 'public'
);

$parts = explode('.', $reference);
Expand All @@ -215,9 +221,7 @@ protected function parseSchemaAndTable($reference)
// We will use the default schema unless the schema has been specified in the
// query. If the schema has been specified in the query then we can use it
// instead of a default schema configured in the connection search path.
$schema = $searchPath[0] === '$user'
? $this->connection->getConfig('username')
: $searchPath[0];
$schema = $searchPath[0];

if (count($parts) === 2) {
$schema = $parts[0];
Expand All @@ -228,24 +232,16 @@ protected function parseSchemaAndTable($reference)
}

/**
* Parse the "search_path" value into an array.
* Parse the "search_path" configuration value into an array.
*
* @param string|array $searchPath
* @param string|array|null $searchPath
* @return array
*/
protected function parseSearchPath($searchPath)
{
if (is_string($searchPath)) {
preg_match_all('/[a-zA-z0-9$]{1,}/i', $searchPath, $matches);

$searchPath = $matches[0];
}

$searchPath ??= [];
$searchPath = $this->baseParseSearchPath($searchPath);

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

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

0 comments on commit 6f24977

Please sign in to comment.