Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mmic-bjohnson committed Jan 12, 2018
1 parent fcd2b9e commit 75bc57f
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 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 @@ -81,34 +81,42 @@ 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['searchpath']) || isset($config['schema'])) {
$searchPath = $this->formatSearchPath($config['searchpath'] ?? $config['schema']);

$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)) {
$schemas = array_map(function ($item) {
return '"'.$item.'"';
}, $searchPath);

$preparedSchemas = array_map(function ($item) {
return ":$item";
}, $schemas);

return implode(', ', array_combine($preparedSchemas, $schemas));
}

return '"'.$schema.'"';
return implode(', ', [":$searchPath" => '"'.$searchPath.'"']);
}

/**
Expand Down

0 comments on commit 75bc57f

Please sign in to comment.