Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(PgSQL): Allow to pass IPv6 address in URI notation for postgres #6344

Open
wants to merge 1 commit into
base: 3.8.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 @@
#[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();
}
}