From 2a1f986e13c44e8e39a3fd1a92873a8073e5a307 Mon Sep 17 00:00:00 2001 From: Tim van Gompel Date: Wed, 8 Mar 2023 19:22:24 +0100 Subject: [PATCH 1/2] feat: add support for Postgres connect_via_database port definition --- src/Illuminate/Database/Connectors/PostgresConnector.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Connectors/PostgresConnector.php b/src/Illuminate/Database/Connectors/PostgresConnector.php index c54163f9b89f..2d68a205a87c 100755 --- a/src/Illuminate/Database/Connectors/PostgresConnector.php +++ b/src/Illuminate/Database/Connectors/PostgresConnector.php @@ -167,13 +167,14 @@ protected function getDsn(array $config) // name than the database used for "information_schema" queries. This is // typically the case if using "pgbouncer" type software when pooling. $database = $connect_via_database ?? $database; + $port = $connect_via_port ?? $port ?? null; $dsn = "pgsql:{$host}dbname='{$database}'"; // If a port was specified, we will add it to this Postgres DSN connections // format. Once we have done that we are ready to return this connection // string back out for usage, as this has been fully constructed here. - if (isset($config['port'])) { + if ($port !== null) { $dsn .= ";port={$port}"; } From cbc60df9af00bc008ffc514868c7e2f79cd99849 Mon Sep 17 00:00:00 2001 From: Tim van Gompel Date: Wed, 8 Mar 2023 19:29:55 +0100 Subject: [PATCH 2/2] test: add pgsql connect_via_port test --- tests/Database/DatabaseConnectorTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/Database/DatabaseConnectorTest.php b/tests/Database/DatabaseConnectorTest.php index 24f6bc326842..79ee6d0606c4 100755 --- a/tests/Database/DatabaseConnectorTest.php +++ b/tests/Database/DatabaseConnectorTest.php @@ -232,6 +232,22 @@ public function testPostgresApplicationUseAlternativeDatabaseName() $this->assertSame($result, $connection); } + public function testPostgresApplicationUseAlternativeDatabaseNameAndPort() + { + $dsn = 'pgsql:dbname=\'baz\';port=2345'; + $config = ['database' => 'bar', 'connect_via_database' => 'baz', 'port' => 5432, 'connect_via_port' => 2345]; + $connector = $this->getMockBuilder(PostgresConnector::class)->onlyMethods(['createConnection', 'getOptions'])->getMock(); + $connection = m::mock(stdClass::class); + $connector->expects($this->once())->method('getOptions')->with($this->equalTo($config))->willReturn(['options']); + $connector->expects($this->once())->method('createConnection')->with($this->equalTo($dsn), $this->equalTo($config), $this->equalTo(['options']))->willReturn($connection); + $statement = m::mock(PDOStatement::class); + $connection->shouldReceive('prepare')->zeroOrMoreTimes()->andReturn($statement); + $statement->shouldReceive('execute')->zeroOrMoreTimes(); + $result = $connector->connect($config); + + $this->assertSame($result, $connection); + } + public function testPostgresConnectorReadsIsolationLevelFromConfig() { $dsn = 'pgsql:host=foo;dbname=\'bar\';port=111';