Skip to content

Commit

Permalink
make "use_savepoints = true" no-op with DBAL 4
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaicher committed Mar 17, 2024
1 parent 0a230d3 commit 933b543
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/DependencyInjection/DoctrineExtension.php
Expand Up @@ -300,8 +300,13 @@ protected function loadDbalConnection($name, array $connection, ContainerBuilder
$def->setClass($options['wrapperClass']);
}

if (! empty($connection['use_savepoints'])) {
$def->addMethodCall('setNestTransactionsWithSavepoints', [$connection['use_savepoints']]);
if (isset($connection['use_savepoints'])) {
// DBAL >= 4 always has savepoints enabled. So we only need to call "setNestTransactionsWithSavepoints" for DBAL < 4
if (method_exists(Connection::class, 'getEventManager') && $connection['use_savepoints']) {
$def->addMethodCall('setNestTransactionsWithSavepoints', [$connection['use_savepoints']]);
} elseif (! $connection['use_savepoints']) {
throw new LogicException('The "use_savepoints" option can only be set to "true" when using DBAL >= 4');
}
}

$container->setDefinition(
Expand Down
4 changes: 4 additions & 0 deletions tests/DependencyInjection/AbstractDoctrineExtensionTest.php
Expand Up @@ -240,6 +240,10 @@ public function testDbalLoadSinglePrimaryReplicaConnection(): void

public function testDbalLoadSavepointsForNestedTransactions(): void
{
if (!method_exists(Connection::class, 'getEventManager')) {
self::markTestSkipped('This test requires DBAL < 4');
}

$container = $this->loadContainer('dbal_savepoints');

$calls = $container->getDefinition('doctrine.dbal.savepoints_connection')->getMethodCalls();
Expand Down
10 changes: 7 additions & 3 deletions tests/DependencyInjection/DoctrineExtensionTest.php
Expand Up @@ -554,10 +554,14 @@ public function testUseSavePointsAddMethodCallToAddSavepointsToTheConnection():
],
], $container);

$isUsingDBAL3 = method_exists(Connection::class, 'getEventManager');

$calls = $container->getDefinition('doctrine.dbal.default_connection')->getMethodCalls();
$this->assertCount(1, $calls);
$this->assertEquals('setNestTransactionsWithSavepoints', $calls[0][0]);
$this->assertTrue($calls[0][1][0]);
$this->assertCount((int) $isUsingDBAL3, $calls);
if ($isUsingDBAL3) {
$this->assertEquals('setNestTransactionsWithSavepoints', $calls[0][0]);
$this->assertTrue($calls[0][1][0]);
}
}

public function testAutoGenerateProxyClasses(): void
Expand Down

0 comments on commit 933b543

Please sign in to comment.