Skip to content

Commit

Permalink
Merge pull request #1432 from VincentLanglet/freezeMigrations
Browse files Browse the repository at this point in the history
Throw an exception if addSql is used in post methods
  • Loading branch information
greg0ire authored Jun 7, 2024
2 parents 7780f8d + 44d34d6 commit a950494
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/AbstractMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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]);
Expand Down
15 changes: 15 additions & 0 deletions src/Exception/FrozenMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Exception;

use LogicException;

final class FrozenMigration extends LogicException implements MigrationException
{
public static function new(): self
{
return new self('The migration is frozen and cannot be edited anymore.');
}
}
2 changes: 2 additions & 0 deletions src/Version/DbalExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ private function executeMigration(
$this->addSql(new Query($sql));
}

$migration->freeze();

if (count($this->sql) !== 0) {
if (! $configuration->isDryRun()) {
$this->executeResult($configuration);
Expand Down
10 changes: 10 additions & 0 deletions tests/AbstractMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit a950494

Please sign in to comment.