From 2f9e58c924dedb3bea8ce8d13d7446bb23a0d21f Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Thu, 21 Mar 2024 18:52:34 +0100 Subject: [PATCH] fix(PgSQL): Allow to pass IPv6 address in URI notation for postgres Signed-off-by: Ferdinand Thiessen --- src/Driver/PgSQL/Driver.php | 8 ++++++ tests/Driver/PgSQL/DriverTest.php | 45 +++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 tests/Driver/PgSQL/DriverTest.php diff --git a/src/Driver/PgSQL/Driver.php b/src/Driver/PgSQL/Driver.php index 6377499a760..a388fdd9a0c 100644 --- a/src/Driver/PgSQL/Driver.php +++ b/src/Driver/PgSQL/Driver.php @@ -64,9 +64,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, diff --git a/tests/Driver/PgSQL/DriverTest.php b/tests/Driver/PgSQL/DriverTest.php new file mode 100644 index 00000000000..817e7a44ef2 --- /dev/null +++ b/tests/Driver/PgSQL/DriverTest.php @@ -0,0 +1,45 @@ +connect((array)$params); + + self::assertInstanceOf(Connection::class, $connection); + } + + protected function createDriver(): DriverInterface + { + return new Driver(); + } +}