Skip to content
Open
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
6 changes: 6 additions & 0 deletions src/AbstractMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ public function freeze(): void
$this->frozen = true;
}

public function reset(): void
{
$this->plannedSql = [];
$this->frozen = false;
}

protected function write(string $message): void
{
$this->logger->notice($message, ['migration' => $this]);
Expand Down
3 changes: 3 additions & 0 deletions src/Version/DbalExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ private function executeMigration(
$migration = $plan->getMigration();
$direction = $plan->getDirection();

// the same instance is reused when a process runs several migration commands
$migration->reset();

$result->setState(State::PRE);

$fromSchema = $this->getFromSchema($configuration);
Expand Down
11 changes: 11 additions & 0 deletions tests/AbstractMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ public function testThrowFrozenMigrationException(): void
$this->migration->exposedAddSql('SELECT 1', [1], [2]);
}

public function testResetClearsSqlAndUnfreezes(): void
{
$this->migration->exposedAddSql('SELECT 1');
$this->migration->freeze();

$this->migration->reset();
$this->migration->exposedAddSql('SELECT 2');

self::assertEquals([new Query('SELECT 2')], $this->migration->getSql());
}

public function testWarnIfOutputMessage(): void
{
$this->migration->warnIf(true, 'Warning was thrown');
Expand Down
20 changes: 20 additions & 0 deletions tests/Version/ExecutorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,26 @@ public function testExecuteDown(): void
], $this->getInterpolatedLogRecords($this->logger));
}

public function testExecuteSameMigrationUpThenDown(): void
{
$migratorConfiguration = new MigratorConfiguration();

$this->versionExecutor->execute(
new MigrationPlan($this->version, $this->migration, Direction::UP),
$migratorConfiguration,
);

$result = $this->versionExecutor->execute(
new MigrationPlan($this->version, $this->migration, Direction::DOWN),
$migratorConfiguration,
);

$queries = $result->getSql();
self::assertCount(2, $queries);
self::assertSame('SELECT 3', $queries[0]->getStatement());
self::assertSame('SELECT 4', $queries[1]->getStatement());
}

public function testExecuteDryRun(): void
{
$this->metadataStorage
Expand Down