Implement full migration running logic

[MAILPOET-4466]
This commit is contained in:
Jan Jakes
2022-10-27 16:20:29 +02:00
committed by Aschepikov
parent d199e0b77e
commit 036d0a29ae
3 changed files with 146 additions and 1 deletions

View File

@ -11,17 +11,43 @@ class Migrator {
/** @var Repository */
private $repository;
/** @var Runner */
private $runner;
/** @var Store */
private $store;
public function __construct(
Repository $repository,
Runner $runner,
Store $store
) {
$this->repository = $repository;
$this->runner = $runner;
$this->store = $store;
}
public function run(): void {
$this->store->ensureMigrationsTable();
$migrations = $this->getStatus();
// do not try to run migrations if any are running or failed
foreach ($migrations as $migration) {
if ($migration['status'] === self::MIGRATION_STATUS_STARTED) {
throw MigratorException::runningMigrationsExist();
}
if ($migration['status'] === self::MIGRATION_STATUS_FAILED) {
throw MigratorException::failedMigrationsExist();
}
}
foreach ($migrations as $migration) {
if ($migration['status'] === self::MIGRATION_STATUS_NEW) {
$this->runner->runMigration($migration['name']);
}
}
}
/** @return array{name: string, status: string, started_at: string|null, completed_at: string|null, error: string|null}[] */
public function getStatus(): array {
$defined = $this->repository->loadAll();