diff --git a/src/classes/Controllers/Console/UpdateDb.php b/src/classes/Controllers/Console/UpdateDb.php index d3ba7a3..9ac7c76 100644 --- a/src/classes/Controllers/Console/UpdateDb.php +++ b/src/classes/Controllers/Console/UpdateDb.php @@ -37,12 +37,14 @@ class UpdateDb extends \Asika\SimpleConsole\Console $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; @@ -56,16 +58,38 @@ HELP; return 0; } - if (count($this->args) > 1) { + if (count($this->args) > 2) { throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments'); } - $this->out('Updating database schema to latest version...'); + $currentVersion = $this->migration->getCurrentVersion()['version']; - $this->migration->up(); + $this->out('Database schema currently in version ' . $currentVersion); - $this->out('Database schema migrated to version ' . $this->migration->getCurrentVersion()['version']); + 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; } }