Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw an exception if addSql is used in post methods #1432

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading