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 94c6187
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
11 changes: 9 additions & 2 deletions src/DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,15 @@ 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')) {
if ($connection['use_savepoints']) {
$def->addMethodCall('setNestTransactionsWithSavepoints', [$connection['use_savepoints']]);

Check warning on line 307 in src/DependencyInjection/DoctrineExtension.php

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/DoctrineExtension.php#L306-L307

Added lines #L306 - L307 were not covered by tests
}
} elseif (! $connection['use_savepoints']) {
throw new LogicException('The "use_savepoints" option can only be set to "true" when using DBAL >= 4');

Check warning on line 310 in src/DependencyInjection/DoctrineExtension.php

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/DoctrineExtension.php#L310

Added line #L310 was not covered by tests
}
}

$container->setDefinition(
Expand Down
4 changes: 4 additions & 0 deletions tests/DependencyInjection/AbstractDoctrineExtensionTest.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 94c6187

Please sign in to comment.