Skip to content

Commit ca8cb14

Browse files
committed
Drop support for silencing errors about DDL in transactions
I hesitated between this and removing the transaction helper entirely, but I think this solution will result in less support for us.
1 parent 53303eb commit ca8cb14

File tree

6 files changed

+42
-46
lines changed

6 files changed

+42
-46
lines changed

UPGRADE.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Upgrade to 4.0
2+
3+
It is no longer possible to relying on silencing to enable transactional
4+
migrations when the platform does not allow DDL inside transactions.
5+
16
# Upgrade to 3.1
27

38
- The "version" is the FQCN of the migration class (existing entries in the migrations table will be automatically updated).

lib/Doctrine/Migrations/DbalMigrator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ private function executeMigrations(
7373
$this->dispatcher->dispatchMigrationEvent(Events::onMigrationsMigrated, $migrationsPlan, $migratorConfiguration);
7474
} catch (Throwable $e) {
7575
if ($allOrNothing) {
76-
TransactionHelper::rollbackIfInTransaction($this->connection);
76+
TransactionHelper::rollback($this->connection);
7777
}
7878

7979
throw $e;
8080
}
8181

8282
if ($allOrNothing) {
83-
TransactionHelper::commitIfInTransaction($this->connection);
83+
TransactionHelper::commit($this->connection);
8484
}
8585

8686
return $sql;

lib/Doctrine/Migrations/Event/Listeners/AutoCommitListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function onMigrationsMigrated(MigrationsEventArgs $args): void
2626
return;
2727
}
2828

29-
TransactionHelper::commitIfInTransaction($conn);
29+
TransactionHelper::commit($conn);
3030
}
3131

3232
/** {@inheritdoc} */

lib/Doctrine/Migrations/Tools/TransactionHelper.php

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Doctrine\Migrations\Tools;
66

77
use Doctrine\DBAL\Connection;
8-
use Doctrine\Deprecations\Deprecation;
98
use LogicException;
109
use PDO;
1110

@@ -16,43 +15,35 @@
1615
*/
1716
final class TransactionHelper
1817
{
19-
public static function commitIfInTransaction(Connection $connection): void
18+
public static function commit(Connection $connection): void
2019
{
2120
if (! self::inTransaction($connection)) {
22-
Deprecation::trigger(
23-
'doctrine/migrations',
24-
'https://github.com/doctrine/migrations/issues/1169',
25-
<<<'DEPRECATION'
21+
throw new LogicException(
22+
<<<'EXCEPTION'
2623
Context: trying to commit a transaction
27-
Problem: the transaction is already committed, relying on silencing is deprecated.
24+
Problem: the transaction is already committed.
2825
Solution: override `AbstractMigration::isTransactional()` so that it returns false.
2926
Automate that by setting `transactional` to false in the configuration.
3027
More details at https://www.doctrine-project.org/projects/doctrine-migrations/en/3.2/explanation/implicit-commits.html
31-
DEPRECATION
28+
EXCEPTION
3229
);
33-
34-
return;
3530
}
3631

3732
$connection->commit();
3833
}
3934

40-
public static function rollbackIfInTransaction(Connection $connection): void
35+
public static function rollback(Connection $connection): void
4136
{
4237
if (! self::inTransaction($connection)) {
43-
Deprecation::trigger(
44-
'doctrine/migrations',
45-
'https://github.com/doctrine/migrations/issues/1169',
46-
<<<'DEPRECATION'
38+
throw new LogicException(
39+
<<<'EXCEPTION'
4740
Context: trying to rollback a transaction
48-
Problem: the transaction is already rolled back, relying on silencing is deprecated.
41+
Problem: the transaction is already rolled back.
4942
Solution: override `AbstractMigration::isTransactional()` so that it returns false.
5043
Automate that by setting `transactional` to false in the configuration.
5144
More details at https://www.doctrine-project.org/projects/doctrine-migrations/en/3.2/explanation/implicit-commits.html
52-
DEPRECATION
45+
EXCEPTION
5346
);
54-
55-
return;
5647
}
5748

5849
$connection->rollBack();

lib/Doctrine/Migrations/Version/DbalExecutor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ private function executeMigration(
211211
}
212212

213213
if ($migration->isTransactional()) {
214-
TransactionHelper::commitIfInTransaction($this->connection);
214+
TransactionHelper::commit($this->connection);
215215
}
216216

217217
$plan->markAsExecuted($result);
@@ -252,7 +252,7 @@ private function migrationEnd(Throwable $e, MigrationPlan $plan, ExecutionResult
252252
$migration = $plan->getMigration();
253253
if ($migration->isTransactional()) {
254254
//only rollback transaction if in transactional mode
255-
TransactionHelper::rollbackIfInTransaction($this->connection);
255+
TransactionHelper::rollback($this->connection);
256256
}
257257

258258
$plan->markAsExecuted($result);

tests/Doctrine/Migrations/Tests/Tools/TransactionHelperTest.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@
55
namespace Doctrine\Migrations\Tests\Tools;
66

77
use Doctrine\DBAL\Connection;
8-
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
98
use Doctrine\Migrations\Tools\TransactionHelper;
9+
use LogicException;
1010
use PDO;
1111
use PHPUnit\Framework\TestCase;
1212

1313
final class TransactionHelperTest extends TestCase
1414
{
15-
use VerifyDeprecations;
16-
17-
public function testItTriggersADeprecationWhenUseful(): void
15+
public function testItThrowsAnExceptionWhenAttemptingToCommitWhileNotInsideATransaction(): void
1816
{
1917
$connection = $this->createStub(Connection::class);
2018
$wrappedConnection = $this->createStub(PDO::class);
@@ -23,18 +21,27 @@ public function testItTriggersADeprecationWhenUseful(): void
2321

2422
$wrappedConnection->method('inTransaction')->willReturn(false);
2523

26-
$this->expectDeprecationWithIdentifier(
27-
'https://github.com/doctrine/migrations/issues/1169'
28-
);
29-
TransactionHelper::commitIfInTransaction($connection);
24+
$this->expectException(LogicException::class);
25+
TransactionHelper::commit($connection);
26+
}
27+
28+
public function testItThrowsAnExceptionWhenAttemptingToRollbackWhileNotInsideATransaction(): void
29+
{
30+
$connection = $this->createStub(Connection::class);
31+
$wrappedConnection = $this->createStub(PDO::class);
3032

31-
$this->expectDeprecationWithIdentifier(
32-
'https://github.com/doctrine/migrations/issues/1169'
33-
);
34-
TransactionHelper::rollbackIfInTransaction($connection);
33+
$connection->method('getNativeConnection')->willReturn($wrappedConnection);
34+
35+
$wrappedConnection->method('inTransaction')->willReturn(false);
36+
37+
$this->expectException(LogicException::class);
38+
TransactionHelper::rollback($connection);
3539
}
3640

37-
public function testItDoesNotTriggerADeprecationWhenUseless(): void
41+
/**
42+
* @doesNotPerformAssertions
43+
*/
44+
public function testItDoesNotThrowAnExceptionWhenUseless(): void
3845
{
3946
$connection = $this->createStub(Connection::class);
4047
$wrappedConnection = $this->createStub(PDO::class);
@@ -43,14 +50,7 @@ public function testItDoesNotTriggerADeprecationWhenUseless(): void
4350

4451
$wrappedConnection->method('inTransaction')->willReturn(true);
4552

46-
$this->expectNoDeprecationWithIdentifier(
47-
'https://github.com/doctrine/migrations/issues/1169'
48-
);
49-
TransactionHelper::commitIfInTransaction($connection);
50-
51-
$this->expectNoDeprecationWithIdentifier(
52-
'https://github.com/doctrine/migrations/issues/1169'
53-
);
54-
TransactionHelper::rollbackIfInTransaction($connection);
53+
TransactionHelper::commit($connection);
54+
TransactionHelper::rollback($connection);
5555
}
5656
}

0 commit comments

Comments
 (0)