From 1ae327f40b50a3f663baf74f80225c6db3b639b5 Mon Sep 17 00:00:00 2001 From: Aleksey Tupichenkov Date: Thu, 18 Sep 2025 11:04:39 +0300 Subject: [PATCH] fix: quote schema special chars --- src/Doctrine/SchemaConnection.php | 2 ++ tests/Doctrine/SchemaConnectionTest.php | 34 ++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Doctrine/SchemaConnection.php b/src/Doctrine/SchemaConnection.php index 56315e0..b19d14d 100644 --- a/src/Doctrine/SchemaConnection.php +++ b/src/Doctrine/SchemaConnection.php @@ -50,6 +50,8 @@ private function ensurePostgreSql(): void private function applySearchPath(string $schema): void { if ($this->_conn !== null) { + $schema = $this->getDatabasePlatform()->quoteIdentifier($schema); + $this->_conn->exec('SET search_path TO ' . $schema); } } diff --git a/tests/Doctrine/SchemaConnectionTest.php b/tests/Doctrine/SchemaConnectionTest.php index 741128c..e114ce7 100644 --- a/tests/Doctrine/SchemaConnectionTest.php +++ b/tests/Doctrine/SchemaConnectionTest.php @@ -23,7 +23,7 @@ public function testConnectSetsSearchPath(): void $driverConnection->expects($this->once()) ->method('exec') - ->with('SET search_path TO test_schema'); + ->with('SET search_path TO "test_schema"'); $driver = $this->createMock(Driver::class); @@ -49,6 +49,38 @@ public function testConnectSetsSearchPath(): void self::assertTrue($result); } + public function testConnectSetsSearchPathWithSpecialChars(): void + { + $driverConnection = $this->createMock(DriverConnection::class); + + $driverConnection->expects($this->once()) + ->method('exec') + ->with('SET search_path TO "test-schema/foo"'); + + $driver = $this->createMock(Driver::class); + + $driver->method('connect')->willReturn($driverConnection); + + $platform = new PostgreSQLPlatform(); + $connection = $this->getMockBuilder(SchemaConnection::class) + ->setConstructorArgs([[], $driver, new Configuration(), new EventManager()]) + ->onlyMethods(['getDatabasePlatform', 'fetchOne']) + ->getMock(); + + $connection->method('getDatabasePlatform')->willReturn($platform); + $connection->method('fetchOne')->willReturn(true); + + $resolver = new BaggageSchemaResolver(); + + $resolver->setSchema('test-schema/foo'); + + SchemaConnection::setSchemaResolver($resolver); + + $result = $connection->connect(); + + self::assertTrue($result); + } + public function testConnectSkipsWhenNoSchema(): void { $driverConnection = $this->createMock(DriverConnection::class);