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
Cli.php
167 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Migrator; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use WP_CLI; |
| 9 | |
| 10 | class Cli { |
| 11 | /** @var Migrator */ |
| 12 | private $migrator; |
| 13 | |
| 14 | /** @var Repository */ |
| 15 | private $repository; |
| 16 | |
| 17 | /** @var Store */ |
| 18 | private $store; |
| 19 | |
| 20 | public function __construct( |
| 21 | Migrator $migrator, |
| 22 | Repository $repository, |
| 23 | Store $store |
| 24 | ) { |
| 25 | $this->migrator = $migrator; |
| 26 | $this->repository = $repository; |
| 27 | $this->store = $store; |
| 28 | } |
| 29 | |
| 30 | public function initialize(): void { |
| 31 | if (!class_exists(WP_CLI::class)) { |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | WP_CLI::add_command('mailpoet migrations run', [$this, 'run'], [ |
| 36 | 'shortdesc' => 'Runs MailPoet database migrations', |
| 37 | ]); |
| 38 | |
| 39 | WP_CLI::add_command('mailpoet migrations status', [$this, 'status'], [ |
| 40 | 'shortdesc' => 'Shows status of MailPoet database migrations', |
| 41 | ]); |
| 42 | |
| 43 | // Deprecated colon-named aliases kept for backwards compatibility. Remove in a future release (STOMAIL-8177). |
| 44 | $this->registerDeprecatedAlias('mailpoet:migrations:run', 'mailpoet migrations run', [$this, 'run']); |
| 45 | $this->registerDeprecatedAlias('mailpoet:migrations:status', 'mailpoet migrations status', [$this, 'status']); |
| 46 | } |
| 47 | |
| 48 | private function registerDeprecatedAlias(string $oldName, string $newName, callable $handler): void { |
| 49 | WP_CLI::add_command($oldName, function () use ($oldName, $newName, $handler): void { |
| 50 | WP_CLI::warning(sprintf('`wp %s` is deprecated and will be removed in a future release. Use `wp %s` instead.', $oldName, $newName)); |
| 51 | $handler(); |
| 52 | }, [ |
| 53 | 'shortdesc' => sprintf('Deprecated. Use `wp %s` instead', $newName), |
| 54 | ]); |
| 55 | } |
| 56 | |
| 57 | public function run(): void { |
| 58 | $this->printHeader(); |
| 59 | $this->migrator->run(new class($this) implements Logger { |
| 60 | /** @var Cli */ |
| 61 | private $cli; |
| 62 | |
| 63 | /** @var float */ |
| 64 | private $started; |
| 65 | |
| 66 | /** @var float */ |
| 67 | private $migrationStarted; |
| 68 | |
| 69 | /** @var int */ |
| 70 | private $migrationsCount = 0; |
| 71 | |
| 72 | public function __construct( |
| 73 | Cli $cli |
| 74 | ) { |
| 75 | $this->cli = $cli; |
| 76 | } |
| 77 | |
| 78 | public function logBefore(array $status): void { |
| 79 | WP_CLI::log("STATUS:\n"); |
| 80 | $this->cli->printStats($status); |
| 81 | |
| 82 | $new = array_values( |
| 83 | array_filter($status, function (array $migration): bool { |
| 84 | return $migration['status'] === Migrator::MIGRATION_STATUS_NEW; |
| 85 | }) |
| 86 | ); |
| 87 | |
| 88 | if (count($new) === 0) { |
| 89 | WP_CLI::success('No new migrations to run.'); |
| 90 | } else { |
| 91 | WP_CLI::log("RUNNING MIGRATIONS:\n"); |
| 92 | } |
| 93 | $this->started = microtime(true); |
| 94 | } |
| 95 | |
| 96 | public function logMigrationStarted(array $migration): void { |
| 97 | WP_CLI::out(sprintf(' %s... ', $migration['name'])); |
| 98 | $this->migrationStarted = microtime(true); |
| 99 | } |
| 100 | |
| 101 | public function logMigrationCompleted(array $migration): void { |
| 102 | $this->migrationsCount += 1; |
| 103 | $seconds = microtime(true) - $this->migrationStarted; |
| 104 | WP_CLI::out(sprintf("completed in %.0Fs ✔\n", $seconds)); |
| 105 | } |
| 106 | |
| 107 | public function logAfter(): void { |
| 108 | if ($this->migrationsCount > 0) { |
| 109 | $seconds = microtime(true) - $this->started; |
| 110 | WP_CLI::log(''); |
| 111 | WP_CLI::success(sprintf("Completed %d new migrations in %.0Fs.", $this->migrationsCount, $seconds)); |
| 112 | } |
| 113 | } |
| 114 | }); |
| 115 | } |
| 116 | |
| 117 | public function status(): void { |
| 118 | $this->printHeader(); |
| 119 | $status = $this->migrator->getStatus(); |
| 120 | if (!$status) { |
| 121 | WP_CLI::warning("No migrations found.\n"); |
| 122 | } else { |
| 123 | WP_CLI::log("STATUS:\n"); |
| 124 | $this->printStats($status); |
| 125 | |
| 126 | WP_CLI::log("MIGRATIONS:\n"); |
| 127 | $table = array_map(function (array $data): array { |
| 128 | $data['name'] .= $data['unknown'] ? ' (unknown)' : ''; |
| 129 | unset($data['unknown']); |
| 130 | return array_map(function ($field) { |
| 131 | return $field === null ? '' : $field; |
| 132 | }, $data); |
| 133 | }, $status); |
| 134 | WP_CLI\Utils\format_items('table', $table, array_keys($table[0])); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | public function printHeader(): void { |
| 139 | WP_CLI::log('MAILPOET DATABASE MIGRATIONS'); |
| 140 | WP_CLI::log("============================\n"); |
| 141 | } |
| 142 | |
| 143 | public function printStats(array $status): void { |
| 144 | $stats = [ |
| 145 | Migrator::MIGRATION_STATUS_NEW => 0, |
| 146 | Migrator::MIGRATION_STATUS_COMPLETED => 0, |
| 147 | Migrator::MIGRATION_STATUS_STARTED => 0, |
| 148 | Migrator::MIGRATION_STATUS_FAILED => 0, |
| 149 | ]; |
| 150 | foreach ($status as $migration) { |
| 151 | $stats[$migration['status']] += 1; |
| 152 | } |
| 153 | |
| 154 | $defined = count($this->repository->loadAll()); |
| 155 | $processed = array_sum($stats) - $stats[Migrator::MIGRATION_STATUS_NEW]; |
| 156 | |
| 157 | WP_CLI::log(sprintf('Defined: %4d (in %s)', $defined, realpath($this->repository->getMigrationsDir()))); |
| 158 | WP_CLI::log(sprintf('Processed: %4d (in database table \'%s\')', $processed, $this->store->getMigrationsTable())); |
| 159 | WP_CLI::log(''); |
| 160 | WP_CLI::log(sprintf('New: %4d (not run yet)', $stats[Migrator::MIGRATION_STATUS_NEW])); |
| 161 | WP_CLI::log(sprintf('Completed: %4d (successfully executed)', $stats[Migrator::MIGRATION_STATUS_COMPLETED])); |
| 162 | WP_CLI::log(sprintf('Started: %4d (still running, or never completed)', $stats[Migrator::MIGRATION_STATUS_STARTED])); |
| 163 | WP_CLI::log(sprintf('Failed: %4d (an error occurred)', $stats[Migrator::MIGRATION_STATUS_FAILED])); |
| 164 | WP_CLI::log(''); |
| 165 | } |
| 166 | } |
| 167 |