*/ class UpdateDb extends \Asika\SimpleConsole\Console { /** * @var Logger */ protected $logger; /** * @var \ByJG\DbMigration\Migration */ protected $migration; protected $helpOptions = ['h', 'help', '?']; public function __construct( Logger $logger, \ByJG\DbMigration\Migration $migration, ?array $argv = null ) { parent::__construct($argv); $this->logger = $logger; $this->migration = $migration; } protected function getHelp() { $help = <<] [-h|--help|-?] [-v] Description Update database schema Options Optional target version number, default is the latest version. Do not use this parameter if you're not sure what you're doing, it will result in data loss! -h|--help|-? Show help information -v Show more debug information. HELP; return $help; } protected function doExecute() { if (count($this->args) == 0) { $this->out($this->getHelp()); return 0; } if (count($this->args) > 2) { throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments'); } $currentVersion = $this->migration->getCurrentVersion()['version']; $this->out('Database schema currently in version ' . $currentVersion); if (count($this->args) == 1) { $this->out('Updating database schema to latest version...'); $this->migration->up(); $this->out('Database schema migrated to version ' . $this->migration->getCurrentVersion()['version']); return 0; } $target = $this->getArgument(1); if ($target > $currentVersion) { $this->out('Updating database schema to version ' . $target); $this->migration->up($target); $this->out('Database schema migrated up to version ' . $this->migration->getCurrentVersion()['version']); return 0; } if ($target < $currentVersion) { $this->out('Downgrading database schema to version ' . $target); $this->migration->down($target); $this->out('Database schema migrated down to version ' . $this->migration->getCurrentVersion()['version']); return 0; } $this->out('Target version equal to current version, exiting.'); return 0; } }