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: 10 additions & 0 deletions src/Driver/PgSQL/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use function func_get_args;
use function implode;
use function pg_connect;
use function preg_match;
use function restore_error_handler;
use function set_error_handler;
use function sprintf;
Expand Down Expand Up @@ -64,9 +65,18 @@ private function constructConnectionString(
#[SensitiveParameter]
array $params
): string {
// pg_connect used by Doctrine DBAL does not support [...] notation,
// but requires the host address in plain form like `aa:bb:99...`
$matches = [];
if (isset($params['host']) && preg_match('/^\[(.+)\]$/', $params['host'], $matches) === 1) {
$params['hostaddr'] = $matches[1];
unset($params['host']);
}

$components = array_filter(
[
'host' => $params['host'] ?? null,
'hostaddr' => $params['hostaddr'] ?? null,
'port' => $params['port'] ?? null,
'dbname' => $params['dbname'] ?? 'postgres',
'user' => $params['user'] ?? null,
Expand Down
49 changes: 49 additions & 0 deletions tests/Driver/PgSQL/DriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Driver\PgSQL;

use Doctrine\DBAL\Driver as DriverInterface;
use Doctrine\DBAL\Driver\PgSQL\Driver;
use Doctrine\DBAL\Tests\Driver\AbstractPostgreSQLDriverTestCase;
use Doctrine\DBAL\Tests\TestUtil;

use function in_array;

class DriverTest extends AbstractPostgreSQLDriverTestCase
{
protected function setUp(): void
{
parent::setUp();

if (isset($GLOBALS['db_driver']) && $GLOBALS['db_driver'] === 'pgsql') {
return;
}

self::markTestSkipped('Test enabled only when using pgsql specific phpunit.xml');
}

/**
* Ensure we can handle URI notation for IPv6 addresses
*/
public function testConnectionIPv6(): void
{
if (! in_array($GLOBALS['db_host'], ['localhost', '127.0.0.1', '[::1]'], true)) {
// We cannot assume that every contributor runs the same setup as our CI
self::markTestSkipped('This test only works if there is a Postgres server listening on localhost.');
}

self::expectNotToPerformAssertions();

$params = TestUtil::getConnectionParams();
$params['host'] = '[::1]';

$this->driver->connect($params);
}

protected function createDriver(): DriverInterface
{
return new Driver();
}
}