Add optional version parameter to console updatedb

This commit is contained in:
Hypolite Petovan 2022-05-01 09:28:13 -04:00
parent cd809f7646
commit b0142f6ab2

View file

@ -37,12 +37,14 @@ class UpdateDb extends \Asika\SimpleConsole\Console
$help = <<<HELP
console updatedb - Update database schema
Usage
bin/console updatedb [-h|--help|-?] [-v]
bin/console updatedb [<version>] [-h|--help|-?] [-v]
Description
Update database schema
Options
<version> 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');
}
$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;
}
}