Skip to content

Commit

Permalink
fix(PgSQL): Allow to pass IPv6 address in URI notation for postgres
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Mar 21, 2024
1 parent d9109cd commit 9159548
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Driver/PgSQL/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,17 @@ 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 (preg_match('/^\[(.+)\]$/', $params['host'] ?? '', $matches)) {
$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
45 changes: 45 additions & 0 deletions tests/Driver/PgSQL/DriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Driver\PgSQL;

use Doctrine\DBAL\Driver as DriverInterface;
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\PgSQL\Driver;
use Doctrine\DBAL\Tests\Driver\AbstractPostgreSQLDriverTestCase;
use Doctrine\DBAL\Tests\TestUtil;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;

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
*/
#[RequiresPhpExtension('pgsql')]
public function testConnectionIPv6(): void
{
$params = TestUtil::getConnectionParams();
$params['host'] = '[::1]';

$connection = $this->connect((array)$params);

self::assertInstanceOf(Connection::class, $connection);
}

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

0 comments on commit 9159548

Please sign in to comment.