From 44d34d635ea05a72a7332df02a70087865624e80 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Thu, 23 May 2024 10:15:20 +0200 Subject: [PATCH] Freeze migrations before executing sql --- src/AbstractMigration.php | 12 ++++++++++++ src/Exception/FrozenMigration.php | 15 +++++++++++++++ src/Version/DbalExecutor.php | 2 ++ tests/AbstractMigrationTest.php | 10 ++++++++++ 4 files changed, 39 insertions(+) create mode 100644 src/Exception/FrozenMigration.php diff --git a/src/AbstractMigration.php b/src/AbstractMigration.php index 098eb620a..e13045c88 100644 --- a/src/AbstractMigration.php +++ b/src/AbstractMigration.php @@ -10,6 +10,7 @@ use Doctrine\DBAL\Schema\AbstractSchemaManager; use Doctrine\DBAL\Schema\Schema; use Doctrine\Migrations\Exception\AbortMigration; +use Doctrine\Migrations\Exception\FrozenMigration; use Doctrine\Migrations\Exception\IrreversibleMigration; use Doctrine\Migrations\Exception\MigrationException; use Doctrine\Migrations\Exception\SkipMigration; @@ -36,6 +37,8 @@ abstract class AbstractMigration /** @var Query[] */ private array $plannedSql = []; + private bool $frozen = false; + public function __construct(Connection $connection, private readonly LoggerInterface $logger) { $this->connection = $connection; @@ -125,6 +128,10 @@ protected function addSql( array $params = [], array $types = [], ): void { + if ($this->frozen) { + throw FrozenMigration::new(); + } + $this->plannedSql[] = new Query($sql, $params, $types); } @@ -134,6 +141,11 @@ public function getSql(): array return $this->plannedSql; } + public function freeze(): void + { + $this->frozen = true; + } + protected function write(string $message): void { $this->logger->notice($message, ['migration' => $this]); diff --git a/src/Exception/FrozenMigration.php b/src/Exception/FrozenMigration.php new file mode 100644 index 000000000..45c0c1a91 --- /dev/null +++ b/src/Exception/FrozenMigration.php @@ -0,0 +1,15 @@ +addSql(new Query($sql)); } + $migration->freeze(); + if (count($this->sql) !== 0) { if (! $configuration->isDryRun()) { $this->executeResult($configuration); diff --git a/tests/AbstractMigrationTest.php b/tests/AbstractMigrationTest.php index 027511f76..24d952b58 100644 --- a/tests/AbstractMigrationTest.php +++ b/tests/AbstractMigrationTest.php @@ -6,6 +6,7 @@ use Doctrine\DBAL\Schema\Schema; use Doctrine\Migrations\Exception\AbortMigration; +use Doctrine\Migrations\Exception\FrozenMigration; use Doctrine\Migrations\Exception\IrreversibleMigration; use Doctrine\Migrations\Exception\SkipMigration; use Doctrine\Migrations\Query\Query; @@ -47,6 +48,15 @@ public function testAddSql(): void self::assertEquals([new Query('SELECT 1', [1], [2])], $this->migration->getSql()); } + public function testThrowFrozenMigrationException(): void + { + $this->expectException(FrozenMigration::class); + $this->expectExceptionMessage('The migration is frozen and cannot be edited anymore.'); + + $this->migration->freeze(); + $this->migration->exposedAddSql('SELECT 1', [1], [2]); + } + public function testWarnIfOutputMessage(): void { $this->migration->warnIf(true, 'Warning was thrown');