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 22, 2024
1 parent e5db00e commit 2360331
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
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 (preg_match('/^\[(.+)\]$/', $params['host'] ?? '', $matches) === 1) {
$params['hostaddr'] = $matches[1];
unset($params['host']);

Check warning on line 73 in src/Driver/PgSQL/Driver.php

View check run for this annotation

Codecov / codecov/patch

src/Driver/PgSQL/Driver.php#L70-L73

Added lines #L70 - L73 were not covered by tests
}

$components = array_filter(
[
'host' => $params['host'] ?? null,
'hostaddr' => $params['hostaddr'] ?? null,

Check warning on line 79 in src/Driver/PgSQL/Driver.php

View check run for this annotation

Codecov / codecov/patch

src/Driver/PgSQL/Driver.php#L79

Added line #L79 was not covered by tests
'port' => $params['port'] ?? null,
'dbname' => $params['dbname'] ?? 'postgres',
'user' => $params['user'] ?? null,
Expand Down
42 changes: 42 additions & 0 deletions tests/Driver/PgSQL/DriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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;

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
{
self::expectNotToPerformAssertions();

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

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

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

0 comments on commit 2360331

Please sign in to comment.