diff --git a/extend/catcher/command/MigrateRollbackCommand.php b/extend/catcher/command/MigrateRollbackCommand.php new file mode 100644 index 0000000..64e260e --- /dev/null +++ b/extend/catcher/command/MigrateRollbackCommand.php @@ -0,0 +1,118 @@ + + * @copyright By CatchAdmin + * @license https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt + */ + +namespace catcher\command; + +use catcher\CatchAdmin; +use Phinx\Db\Adapter\AdapterFactory; +use Phinx\Migration\MigrationInterface; +use think\console\Input; +use think\console\input\Argument; +use think\console\input\Option as InputOption; +use think\console\Output; +use think\facade\Console; +use think\migration\command\migrate\Rollback; +use think\migration\command\migrate\Run; + +class MigrateRollbackCommand extends Rollback +{ + protected $module; + + protected function configure() + { + $this->setName('catch-migrate:rollback') + ->setDescription('Rollback the last or to a specific migration') + ->addArgument('module', Argument::REQUIRED, 'migrate the module database') + ->addOption('--target', '-t', InputOption::VALUE_REQUIRED, 'The version number to rollback to') + ->addOption('--date', '-d', InputOption::VALUE_REQUIRED, 'The date to rollback to') + ->addOption('--force', '-f', InputOption::VALUE_NONE, 'Force rollback to ignore breakpoints') + ->setHelp(<<catch-migrate:rollback command reverts the last migration, or optionally up to a specific version + +php think catch-migrate:rollback +php think catch-migrate:rollback module -t 20111018185412 +php think catch-migrate:rollback module -d 20111018 +php think catch-migrate:rollback -v + +EOT + ); + } + + /** + * Rollback the migration. + * + * @param Input $input + * @param Output $output + * @return void + */ + protected function execute(Input $input, Output $output) + { + $this->module = $input->getArgument('module'); + $version = $input->getOption('target'); + $date = $input->getOption('date'); + $force = !!$input->getOption('force'); + + // rollback the specified environment + $start = microtime(true); + if (null !== $date) { + $this->rollbackToDateTime(new \DateTime($date), $force); + } else { + if (!$version) { + $migrations = glob(CatchAdmin::moduleMigrationsDirectory($this->module) . '*.php'); + foreach ($migrations as $migration) { + $version = explode('_', basename($migration))[0]; + $this->rollback($version, $force); + } + } else { + $this->rollback($version, $force); + } + } + $end = microtime(true); + $this->migrations = null; + $output->writeln(''); + $output->writeln('All Done. Took ' . sprintf('%.4fs', $end - $start) . ''); + } + + /** + * 获取 migration path + * + * @time 2019年12月03日 + * @return string + */ + protected function getPath(): string + { + return CatchAdmin::moduleMigrationsDirectory($this->module); + } + + /** + * + * @time 2020年01月21日 + * @param null $version + * @param bool $force + * @return void + */ + protected function rollback($version = null, $force = false) + { + $migrations = $this->getMigrations(); + $versionLog = $this->getVersionLog(); + $versions = array_keys($versionLog); + + if ($version) { + $this->executeMigration($migrations[$version], MigrationInterface::DOWN); + } else { + foreach ($migrations as $key => $migration) { + if (in_array($key, $versions)) { + $this->executeMigration($migration, MigrationInterface::DOWN); + } + } + } + } +}