Skip to content

Commit 55b000e

Browse files
committed
fix compatibility with Doctrine DBAL 4
1 parent 5611ed4 commit 55b000e

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ public function testGetWithNoPendingMessageWillReturnNull()
8282
$queryBuilder
8383
->method('getParameterTypes')
8484
->willReturn([]);
85+
$queryBuilder
86+
->method('getSQL')
87+
->willReturn('SELECT FOR UPDATE');
8588
$driverConnection->expects($this->once())
8689
->method('createQueryBuilder')
8790
->willReturn($queryBuilder);
@@ -120,7 +123,11 @@ private function getDBALConnectionMock()
120123
{
121124
$driverConnection = $this->createMock(DBALConnection::class);
122125
$platform = $this->createMock(AbstractPlatform::class);
123-
$platform->method('getWriteLockSQL')->willReturn('FOR UPDATE');
126+
127+
if (!method_exists(QueryBuilder::class, 'forUpdate')) {
128+
$platform->method('getWriteLockSQL')->willReturn('FOR UPDATE');
129+
}
130+
124131
$configuration = $this->createMock(\Doctrine\DBAL\Configuration::class);
125132
$driverConnection->method('getDatabasePlatform')->willReturn($platform);
126133
$driverConnection->method('getConfiguration')->willReturn($configuration);
@@ -381,7 +388,9 @@ public function testGeneratedSql(AbstractPlatform $platform, string $expectedSql
381388
$driverConnection
382389
->expects($this->once())
383390
->method('executeQuery')
384-
->with($expectedSql)
391+
->with($this->callback(function ($sql) use ($expectedSql) {
392+
return trim($expectedSql) === trim($sql);
393+
}))
385394
->willReturn($result)
386395
;
387396
$driverConnection->expects($this->once())->method('commit');
@@ -415,6 +424,12 @@ class_exists(SQLServerPlatform::class) && !class_exists(SQLServer2012Platform::c
415424
new OraclePlatform(),
416425
'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',
417426
];
427+
} elseif (method_exists(QueryBuilder::class, 'forUpdate')) {
428+
// DBAL >= 3.8
429+
yield 'Oracle' => [
430+
new OraclePlatform(),
431+
'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)',
432+
];
418433
} else {
419434
// DBAL < 4
420435
yield 'Oracle' => [

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,14 @@ public function get(): ?array
177177

178178
// Append pessimistic write lock to FROM clause if db platform supports it
179179
$sql = $query->getSQL();
180-
if (preg_match('/FROM (.+) WHERE/', (string) $sql, $matches)) {
180+
181+
if (method_exists(QueryBuilder::class, 'forUpdate')) {
182+
$query->forUpdate();
183+
try {
184+
$sql = $query->getSQL();
185+
} catch (DBALException $e) {
186+
}
187+
} elseif (preg_match('/FROM (.+) WHERE/', (string) $sql, $matches)) {
181188
$fromClause = $matches[1];
182189
$sql = str_replace(
183190
sprintf('FROM %s WHERE', $fromClause),
@@ -194,8 +201,12 @@ public function get(): ?array
194201
}
195202

196203
// use SELECT ... FOR UPDATE to lock table
204+
if (!method_exists(QueryBuilder::class, 'forUpdate')) {
205+
$sql .= ' '.$this->driverConnection->getDatabasePlatform()->getWriteLockSQL();
206+
}
207+
197208
$stmt = $this->executeQuery(
198-
$sql.' '.$this->driverConnection->getDatabasePlatform()->getWriteLockSQL(),
209+
$sql,
199210
$query->getParameters(),
200211
$query->getParameterTypes()
201212
);

0 commit comments

Comments
 (0)