Skip to content

Commit e78d372

Browse files
committed
feat: Rollback Migration on Error
1 parent 1d33b43 commit e78d372

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

webfiori/framework/cli/commands/RunMigrationsCommand.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,19 @@ private function getDBConnection(?MigrationsRunner $runner = null) {
232232
return CLIUtils::getConnectionName($this);
233233
}
234234
}
235+
public function getNext(MigrationsRunner $runner) : ?AbstractMigration {
236+
foreach ($runner->getMigrations() as $m) {
237+
if ($runner->isApplied($m->getName())) {
238+
continue;
239+
} else {
240+
return $m;
241+
}
242+
}
243+
return null;
244+
}
235245
private function applyNext(MigrationsRunner $runner, &$listOfApplied) : bool {
246+
$toBeApplied = $this->getNext($runner);
247+
236248
try {
237249
//$this->println("Executing migration...");
238250
$applied = $runner->applyOne();
@@ -248,6 +260,15 @@ private function applyNext(MigrationsRunner $runner, &$listOfApplied) : bool {
248260
$this->error('Failed to execute migration due to following:');
249261
$this->println($ex->getMessage().' (Line '.$ex->getLine().')');
250262
$this->warning('Execution stopped.');
263+
$this->warning('Rolling back changes...');
264+
265+
try {
266+
$toBeApplied->down($runner);
267+
} catch (Throwable $exc) {
268+
$this->error('Failed to rollback due to following:');
269+
$this->println($ex->getMessage().' (Line '.$ex->getLine().')');
270+
}
271+
251272
return false;
252273
}
253274
}

0 commit comments

Comments
 (0)