AppMigration.php
2 years ago
AppMigrationTemplate.php
2 years ago
Cli.php
2 weeks ago
DbMigration.php
6 months ago
DbMigrationTemplate.php
2 years ago
Logger.php
2 years ago
Migrator.php
1 year ago
MigratorException.php
2 years ago
Repository.php
2 years ago
Runner.php
2 years ago
Store.php
6 months ago
index.php
3 years ago
Migrator.php
104 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Migrator; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | /** |
| 9 | * @phpstan-type MigrationDefinition array{name: string, level: string|null, status: string, started_at: string|null, completed_at: string|null, retries: int|null, error: string|null, unknown: bool} |
| 10 | */ |
| 11 | class Migrator { |
| 12 | const MIGRATION_STATUS_NEW = 'new'; |
| 13 | const MIGRATION_STATUS_STARTED = 'started'; |
| 14 | const MIGRATION_STATUS_COMPLETED = 'completed'; |
| 15 | const MIGRATION_STATUS_FAILED = 'failed'; |
| 16 | |
| 17 | /** @var Repository */ |
| 18 | private $repository; |
| 19 | |
| 20 | /** @var Runner */ |
| 21 | private $runner; |
| 22 | |
| 23 | /** @var Store */ |
| 24 | private $store; |
| 25 | |
| 26 | public function __construct( |
| 27 | Repository $repository, |
| 28 | Runner $runner, |
| 29 | Store $store |
| 30 | ) { |
| 31 | $this->repository = $repository; |
| 32 | $this->runner = $runner; |
| 33 | $this->store = $store; |
| 34 | } |
| 35 | |
| 36 | public function run(?Logger $logger = null): void { |
| 37 | $this->store->ensureMigrationsTable(); |
| 38 | $migrations = $this->getStatus(); |
| 39 | |
| 40 | if ($logger) { |
| 41 | $logger->logBefore($migrations); |
| 42 | } |
| 43 | |
| 44 | foreach ($migrations as $migration) { |
| 45 | if (!$migration['level'] || $migration['unknown'] || $migration['status'] === self::MIGRATION_STATUS_COMPLETED) { |
| 46 | continue; |
| 47 | } |
| 48 | |
| 49 | if ($logger) { |
| 50 | $logger->logMigrationStarted($migration); |
| 51 | } |
| 52 | |
| 53 | $this->runner->runMigration($migration['name'], $migration['level']); |
| 54 | |
| 55 | if ($logger) { |
| 56 | $logger->logMigrationCompleted($migration); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | if ($logger) { |
| 61 | $logger->logAfter(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Array with of migration status data. |
| 67 | * Ordering: |
| 68 | * 1. Db migrations ordered by filename |
| 69 | * 2. App migrations ordered by filename |
| 70 | * 3. Unknown migrations (saved in store but not in repository e.g., renamed or deleted) |
| 71 | * @return MigrationDefinition[] |
| 72 | */ |
| 73 | public function getStatus(): array { |
| 74 | $defined = $this->repository->loadAll(); |
| 75 | $definedMap = array_combine(array_column($defined, 'name'), $defined) ?: []; |
| 76 | $processed = $this->store->getAll(); |
| 77 | $processedMap = array_combine(array_column($processed, 'name'), $processed) ?: []; |
| 78 | $all = array_unique(array_merge(array_keys($definedMap), array_keys($processedMap))); |
| 79 | |
| 80 | $status = []; |
| 81 | foreach ($all as $name) { |
| 82 | $data = $processedMap[$name] ?? []; |
| 83 | $status[] = [ |
| 84 | 'name' => $name, |
| 85 | 'level' => $definedMap[$name]['level'] ?? null, |
| 86 | 'status' => $data ? $this->getMigrationStatus($data) : self::MIGRATION_STATUS_NEW, |
| 87 | 'started_at' => $data['started_at'] ?? null, |
| 88 | 'completed_at' => $data['completed_at'] ?? null, |
| 89 | 'retries' => isset($data['retries']) ? (int)$data['retries'] : null, |
| 90 | 'error' => $data && $data['error'] ? mb_strimwidth($data['error'], 0, 20, '…') : null, |
| 91 | 'unknown' => !isset($definedMap[$name]), |
| 92 | ]; |
| 93 | } |
| 94 | return $status; |
| 95 | } |
| 96 | |
| 97 | private function getMigrationStatus(array $data): string { |
| 98 | if (!isset($data['completed_at'])) { |
| 99 | return self::MIGRATION_STATUS_STARTED; |
| 100 | } |
| 101 | return isset($data['error']) ? self::MIGRATION_STATUS_FAILED : self::MIGRATION_STATUS_COMPLETED; |
| 102 | } |
| 103 | } |
| 104 |