Skip to content

Commit 6046a57

Browse files
authored
make "use_savepoints = true" no-op with DBAL 4 (#1773)
1 parent 0a230d3 commit 6046a57

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

src/DependencyInjection/DoctrineExtension.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,15 @@ protected function loadDbalConnection($name, array $connection, ContainerBuilder
300300
$def->setClass($options['wrapperClass']);
301301
}
302302

303-
if (! empty($connection['use_savepoints'])) {
304-
$def->addMethodCall('setNestTransactionsWithSavepoints', [$connection['use_savepoints']]);
303+
if (isset($connection['use_savepoints'])) {
304+
// DBAL >= 4 always has savepoints enabled. So we only need to call "setNestTransactionsWithSavepoints" for DBAL < 4
305+
if (method_exists(Connection::class, 'getEventManager')) {
306+
if ($connection['use_savepoints']) {
307+
$def->addMethodCall('setNestTransactionsWithSavepoints', [$connection['use_savepoints']]);
308+
}
309+
} elseif (! $connection['use_savepoints']) {
310+
throw new LogicException('The "use_savepoints" option can only be set to "true" and should ideally not be set when using DBAL >= 4');
311+
}
305312
}
306313

307314
$container->setDefinition(

tests/DependencyInjection/AbstractDoctrineExtensionTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ public function testDbalLoadSinglePrimaryReplicaConnection(): void
240240

241241
public function testDbalLoadSavepointsForNestedTransactions(): void
242242
{
243+
if (!method_exists(Connection::class, 'getEventManager')) {
244+
self::markTestSkipped('This test requires DBAL < 4');
245+
}
246+
243247
$container = $this->loadContainer('dbal_savepoints');
244248

245249
$calls = $container->getDefinition('doctrine.dbal.savepoints_connection')->getMethodCalls();

tests/DependencyInjection/DoctrineExtensionTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -554,10 +554,14 @@ public function testUseSavePointsAddMethodCallToAddSavepointsToTheConnection():
554554
],
555555
], $container);
556556

557+
$isUsingDBAL3 = method_exists(Connection::class, 'getEventManager');
558+
557559
$calls = $container->getDefinition('doctrine.dbal.default_connection')->getMethodCalls();
558-
$this->assertCount(1, $calls);
559-
$this->assertEquals('setNestTransactionsWithSavepoints', $calls[0][0]);
560-
$this->assertTrue($calls[0][1][0]);
560+
$this->assertCount((int) $isUsingDBAL3, $calls);
561+
if ($isUsingDBAL3) {
562+
$this->assertEquals('setNestTransactionsWithSavepoints', $calls[0][0]);
563+
$this->assertTrue($calls[0][1][0]);
564+
}
561565
}
562566

563567
public function testAutoGenerateProxyClasses(): void

0 commit comments

Comments
 (0)