Skip to content

Commit 97aacef

Browse files
committed
fix compatibility with Doctrine DBAL 4
1 parent 5611ed4 commit 97aacef

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/ConnectionTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Doctrine\DBAL\Platforms\MySQL57Platform;
2222
use Doctrine\DBAL\Platforms\MySQLPlatform;
2323
use Doctrine\DBAL\Platforms\OraclePlatform;
24+
use Doctrine\DBAL\Platforms\SQLServer\SQL\Builder\SQLServerSelectSQLBuilder;
2425
use Doctrine\DBAL\Platforms\SQLServer2012Platform;
2526
use Doctrine\DBAL\Platforms\SQLServerPlatform;
2627
use Doctrine\DBAL\Query\QueryBuilder;
@@ -82,6 +83,9 @@ public function testGetWithNoPendingMessageWillReturnNull()
8283
$queryBuilder
8384
->method('getParameterTypes')
8485
->willReturn([]);
86+
$queryBuilder
87+
->method('getSQL')
88+
->willReturn('SELECT FOR UPDATE');
8589
$driverConnection->expects($this->once())
8690
->method('createQueryBuilder')
8791
->willReturn($queryBuilder);
@@ -120,7 +124,11 @@ private function getDBALConnectionMock()
120124
{
121125
$driverConnection = $this->createMock(DBALConnection::class);
122126
$platform = $this->createMock(AbstractPlatform::class);
123-
$platform->method('getWriteLockSQL')->willReturn('FOR UPDATE');
127+
128+
if (!method_exists(QueryBuilder::class, 'forUpdate')) {
129+
$platform->method('getWriteLockSQL')->willReturn('FOR UPDATE');
130+
}
131+
124132
$configuration = $this->createMock(\Doctrine\DBAL\Configuration::class);
125133
$driverConnection->method('getDatabasePlatform')->willReturn($platform);
126134
$driverConnection->method('getConfiguration')->willReturn($configuration);
@@ -381,7 +389,9 @@ public function testGeneratedSql(AbstractPlatform $platform, string $expectedSql
381389
$driverConnection
382390
->expects($this->once())
383391
->method('executeQuery')
384-
->with($expectedSql)
392+
->with($this->callback(function ($sql) use ($expectedSql) {
393+
return trim($expectedSql) === trim($sql);
394+
}))
385395
->willReturn($result)
386396
;
387397
$driverConnection->expects($this->once())->method('commit');
@@ -415,6 +425,12 @@ class_exists(SQLServerPlatform::class) && !class_exists(SQLServer2012Platform::c
415425
new OraclePlatform(),
416426
'SELECT w.id AS "id", w.body AS "body", w.headers AS "headers", w.queue_name AS "queue_name", w.created_at AS "created_at", w.available_at AS "available_at", w.delivered_at AS "delivered_at" FROM messenger_messages w WHERE w.id IN (SELECT m.id FROM messenger_messages m WHERE (m.queue_name = ?) AND (m.delivered_at is null OR m.delivered_at < ?) AND (m.available_at <= ?) ORDER BY available_at ASC FETCH NEXT 1 ROWS ONLY) FOR UPDATE',
417427
];
428+
} elseif (method_exists(QueryBuilder::class, 'forUpdate')) {
429+
// DBAL >= 3.8
430+
yield 'Oracle' => [
431+
new OraclePlatform(),
432+
'SELECT w.id AS "id", w.body AS "body", w.headers AS "headers", w.queue_name AS "queue_name", w.created_at AS "created_at", w.available_at AS "available_at", w.delivered_at AS "delivered_at" FROM messenger_messages w WHERE w.id IN (SELECT a.id FROM (SELECT m.id FROM messenger_messages m WHERE (m.queue_name = ?) AND (m.delivered_at is null OR m.delivered_at < ?) AND (m.available_at <= ?) ORDER BY available_at ASC) a WHERE ROWNUM <= 1 FOR UPDATE)',
433+
];
418434
} else {
419435
// DBAL < 4
420436
yield 'Oracle' => [

src/Symfony/Component/Messenger/Bridge/Doctrine/Transport/Connection.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\DBAL\Connection as DBALConnection;
1515
use Doctrine\DBAL\Driver\Exception as DriverException;
1616
use Doctrine\DBAL\Driver\Result as DriverResult;
17+
use Doctrine\DBAL\Exception;
1718
use Doctrine\DBAL\Exception as DBALException;
1819
use Doctrine\DBAL\Exception\TableNotFoundException;
1920
use Doctrine\DBAL\LockMode;
@@ -177,7 +178,16 @@ public function get(): ?array
177178

178179
// Append pessimistic write lock to FROM clause if db platform supports it
179180
$sql = $query->getSQL();
180-
if (preg_match('/FROM (.+) WHERE/', (string) $sql, $matches)) {
181+
182+
if (method_exists(QueryBuilder::class, 'forUpdate')) {
183+
$query->forUpdate();
184+
try {
185+
$sql = $query->getSQL();
186+
} catch (Exception $e) {
187+
}
188+
}
189+
190+
if (!method_exists(QueryBuilder::class, 'forUpdate') && preg_match('/FROM (.+) WHERE/', (string) $sql, $matches)) {
181191
$fromClause = $matches[1];
182192
$sql = str_replace(
183193
sprintf('FROM %s WHERE', $fromClause),
@@ -194,8 +204,12 @@ public function get(): ?array
194204
}
195205

196206
// use SELECT ... FOR UPDATE to lock table
207+
if (!method_exists(QueryBuilder::class, 'forUpdate')) {
208+
$sql .= ' '.$this->driverConnection->getDatabasePlatform()->getWriteLockSQL();
209+
}
210+
197211
$stmt = $this->executeQuery(
198-
$sql.' '.$this->driverConnection->getDatabasePlatform()->getWriteLockSQL(),
212+
$sql,
199213
$query->getParameters(),
200214
$query->getParameterTypes()
201215
);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy