Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2f9e58c

Browse files
committedMar 21, 2024··
fix(PgSQL): Allow to pass IPv6 address in URI notation for postgres
Signed-off-by: Ferdinand Thiessen <[email protected]>
1 parent e5db00e commit 2f9e58c

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
 

‎src/Driver/PgSQL/Driver.php

+8
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,17 @@ private function constructConnectionString(
6464
#[SensitiveParameter]
6565
array $params
6666
): string {
67+
// pg_connect used by Doctrine DBAL does not support [...] notation but requires the host address in plain form like `aa:bb:99...`
68+
$matches = [];
69+
if (preg_match('/^\[(.+)\]$/', $params['host'] ?? '', $matches)) {
70+
$params['hostaddr'] = $matches[1];
71+
unset($params['host']);
72+
}
73+
6774
$components = array_filter(
6875
[
6976
'host' => $params['host'] ?? null,
77+
'hostaddr' => $params['hostaddr'] ?? null,
7078
'port' => $params['port'] ?? null,
7179
'dbname' => $params['dbname'] ?? 'postgres',
7280
'user' => $params['user'] ?? null,

‎tests/Driver/PgSQL/DriverTest.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Tests\Driver\PgSQL;
6+
7+
use Doctrine\DBAL\Driver as DriverInterface;
8+
use Doctrine\DBAL\Driver\Connection;
9+
use Doctrine\DBAL\Driver\PgSQL\Driver;
10+
use Doctrine\DBAL\Tests\Driver\AbstractPostgreSQLDriverTestCase;
11+
use Doctrine\DBAL\Tests\TestUtil;
12+
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
13+
14+
class DriverTest extends AbstractPostgreSQLDriverTestCase
15+
{
16+
protected function setUp(): void
17+
{
18+
parent::setUp();
19+
20+
if (isset($GLOBALS['db_driver']) && $GLOBALS['db_driver'] === 'pgsql') {
21+
return;
22+
}
23+
24+
self::markTestSkipped('Test enabled only when using pgsql specific phpunit.xml');
25+
}
26+
27+
/**
28+
* Ensure we can handle URI notation for IPv6 addresses
29+
*/
30+
#[RequiresPhpExtension('pgsql')]
31+
public function testConnectionIPv6(): void
32+
{
33+
$params = TestUtil::getConnectionParams();
34+
$params['host'] = '[::1]';
35+
36+
$connection = $this->connect((array)$params);
37+
38+
self::assertInstanceOf(Connection::class, $connection);
39+
}
40+
41+
protected function createDriver(): DriverInterface
42+
{
43+
return new Driver();
44+
}
45+
}

0 commit comments

Comments
 (0)
Please sign in to comment.