Skip to content

Commit

Permalink
Freeze migrations before executing sql
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed May 23, 2024
1 parent 4f507d7 commit 2f88c94
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/Doctrine/Migrations/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,11 @@ protected function addSql(
array $params = [],
array $types = [],
): void {
if ($this->frozen) {

Check failure on line 131 in lib/Doctrine/Migrations/AbstractMigration.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Expected 1 line after "if", found 2.
throw FrozenMigration::new();

Check warning on line 132 in lib/Doctrine/Migrations/AbstractMigration.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/Migrations/AbstractMigration.php#L132

Added line #L132 was not covered by tests
}

Check failure on line 134 in lib/Doctrine/Migrations/AbstractMigration.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Functions must not contain multiple empty lines in a row; found 2 empty lines

$this->plannedSql[] = new Query($sql, $params, $types);
}

Expand All @@ -134,6 +142,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 lib/Doctrine/Migrations/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

Check warning on line 11 in lib/Doctrine/Migrations/Exception/FrozenMigration.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/Migrations/Exception/FrozenMigration.php#L11

Added line #L11 was not covered by tests
{
return new self('The migration is frozen and cannot be edited anymore.');

Check warning on line 13 in lib/Doctrine/Migrations/Exception/FrozenMigration.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/Migrations/Exception/FrozenMigration.php#L13

Added line #L13 was not covered by tests
}
}
2 changes: 2 additions & 0 deletions lib/Doctrine/Migrations/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

0 comments on commit 2f88c94

Please sign in to comment.