新增rollback和create migration 命令

This commit is contained in:
wuyanwen 2020-01-21 17:00:17 +08:00
parent ef545b8732
commit 2c732555b0
4 changed files with 261 additions and 55 deletions

View File

@ -18,7 +18,7 @@ class InstallCommand extends Command
protected function configure()
{
$this->setName('catch:install')
// ->addArgument('module', Argument::REQUIRED, 'module name')
->addOption('reinstall', '-r',Option::VALUE_NONE, 'reinstall back')
->setDescription('install project');
}
@ -31,17 +31,23 @@ class InstallCommand extends Command
*/
protected function execute(Input $input, Output $output)
{
$this->detectionEnvironment();
if ($input->getOption('reinstall')) {
$this->reInstall();
$this->project();
} else {
$this->firstStep();
$this->detectionEnvironment();
$this->secondStep();
$this->firstStep();
$this->thirdStep();
$this->secondStep();
$this->finished();
$this->thirdStep();
$this->project();
$this->finished();
$this->project();
}
}
/**
@ -94,6 +100,7 @@ class InstallCommand extends Command
$this->output->info('🎉 environment checking finished');
}
/**
* 安装第一步
*
@ -160,19 +167,41 @@ class InstallCommand extends Command
'connections' => $connections,
], 'database');
foreach (CatchAdmin::getModulesDirectory() as $directory) {
$moduleInfo = CatchAdmin::getModuleInfo($directory);
if (is_dir(CatchAdmin::moduleMigrationsDirectory($moduleInfo['alias']))) {
$output = Console::call('catch-migrate:run', [$moduleInfo['alias']]);
$this->output->info(sprintf('module [%s] migrations %s', $moduleInfo['alias'], $output->fetch()));
$seedOut = Console::call('catch-seed:run', [$moduleInfo['alias']]);
$this->output->info(sprintf('module [%s] seeds %s', $moduleInfo['alias'], $seedOut->fetch()));
}
}
$this->migrateAndSeeds();
}
}
/**
* 生成表结构
*
* @time 2020年01月20日
* @return void
*/
protected function migrateAndSeeds(): void
{
foreach (CatchAdmin::getModulesDirectory() as $directory) {
$moduleInfo = CatchAdmin::getModuleInfo($directory);
if (is_dir(CatchAdmin::moduleMigrationsDirectory($moduleInfo['alias']))) {
$output = Console::call('catch-migrate:run', [$moduleInfo['alias']]);
$this->output->info(sprintf('module [%s] migrations %s', $moduleInfo['alias'], $output->fetch()));
$seedOut = Console::call('catch-seed:run', [$moduleInfo['alias']]);
$this->output->info(sprintf('module [%s] seeds %s', $moduleInfo['alias'], $seedOut->fetch()));
}
}
}
protected function migrateRollback()
{
foreach (CatchAdmin::getModulesDirectory() as $directory) {
$moduleInfo = CatchAdmin::getModuleInfo($directory);
if (is_dir(CatchAdmin::moduleMigrationsDirectory($moduleInfo['alias']))) {
$rollbackOut = Console::call('catch-migrate:rollback', [$moduleInfo['alias'], '-f']);
// $this->output->info(sprintf('module [%s] [%s] rollback %s', $moduleInfo['alias'], basename($migration), $rollbackOut->fetch()));
}
}
}
/**
* 安装第四步
*
@ -267,41 +296,6 @@ class InstallCommand extends Command
return file_exists(root_path() . '.env') ? root_path() . '.env' : '';
}
/**
* 检测根目录
*
* @time 2019年11月28日
* @return bool
*/
protected function checkRootDatabase(): bool
{
$databasePath = root_path('database');
if (!is_dir($databasePath)) {
if (!mkdir($databasePath, 0777, true) && !is_dir($databasePath)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $databasePath));
}
}
$migrationPath = $databasePath . DIRECTORY_SEPARATOR . 'migrations' . DIRECTORY_SEPARATOR;
$seedPath = $databasePath . DIRECTORY_SEPARATOR . 'seeds' . DIRECTORY_SEPARATOR;
if (!is_dir($migrationPath)) {
if (!mkdir($migrationPath, 0777, true) && !is_dir($migrationPath)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $migrationPath));
}
}
if (!is_dir($seedPath)) {
if (!mkdir($seedPath, 0777, true) && !is_dir($seedPath)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $seedPath));
}
}
return true;
}
protected function project()
{
@ -323,4 +317,16 @@ class InstallCommand extends Command
', $year));
}
protected function reInstall(): void
{
$ask = strtolower($this->output->ask($this->input,'reset project? (Y/N)'));
if ($ask === 'y' || $ask === 'yes' ) {
$this->migrateRollback();
$this->migrateAndSeeds();
}
}
}

View File

@ -8,3 +8,99 @@
* @copyright By CatchAdmin
* @license https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt
*/
namespace catcher\command;
use catcher\CatchAdmin;
use Phinx\Util\Util;
use think\console\Input;
use think\console\input\Argument as InputArgument;
use think\console\Output;
use think\migration\command\migrate\Create;
use think\migration\Creator;
class MigrateCreateCommand extends Create
{
/*
*
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('catch-migrate:create')
->setDescription('Create a new migration')
->addArgument('module', InputArgument::REQUIRED, 'the module where you create')
->addArgument('name', InputArgument::REQUIRED, 'What is the name of the migration?')
->setHelp(sprintf('%sCreates a new database migration%s', PHP_EOL, PHP_EOL));
}
/**
* Create the new migration.
*
* @param Input $input
* @param Output $output
* @return void
* @throws InvalidArgumentException
* @throws RuntimeException
*/
protected function execute(Input $input, Output $output)
{
$module = $input->getArgument('module');
$className = $input->getArgument('name');
$path = $this->create($module, $className);
$output->writeln('<info>created</info> .' . str_replace(getcwd(), '', realpath($path)));
}
/**
*
* @time 2020年01月21日
* @param $module
* @param $className
* @return string
*/
protected function create($module, $className): string
{
$path = CatchAdmin::moduleMigrationsDirectory($module);
if (!Util::isValidPhinxClassName($className)) {
throw new InvalidArgumentException(sprintf('The migration class name "%s" is invalid. Please use CamelCase format.', $className));
}
if (!Util::isUniqueMigrationClassName($className, $path)) {
throw new InvalidArgumentException(sprintf('The migration class name "%s" already exists', $className));
}
// Compute the file path
$fileName = Util::mapClassNameToFileName($className);
$filePath = $path . DIRECTORY_SEPARATOR . $fileName;
if (is_file($filePath)) {
throw new InvalidArgumentException(sprintf('The file "%s" already exists', $filePath));
}
// Verify that the template creation class (or the aliased class) exists and that it implements the required interface.
$aliasedClassName = null;
// Load the alternative template if it is defined.
$contents = file_get_contents($this->getTemplate());
// inject the class names appropriate to this migration
$contents = strtr($contents, [
'MigratorClass' => $className,
]);
if (false === file_put_contents($filePath, $contents)) {
throw new RuntimeException(sprintf('The file "%s" could not be written to', $path));
}
return $filePath;
}
protected function getTemplate()
{
return __DIR__ . '/stubs/migrate.stub';
}
}

View File

@ -8,3 +8,107 @@
* @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(<<<EOT
The <info>catch-migrate:rollback</info> command reverts the last migration, or optionally up to a specific version
<info>php think catch-migrate:rollback</info>
<info>php think catch-migrate:rollback module -t 20111018185412</info>
<info>php think catch-migrate:rollback module -d 20111018</info>
<info>php think catch-migrate:rollback -v</info>
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('<comment>All Done. Took ' . sprintf('%.4fs', $end - $start) . '</comment>');
}
/**
* 获取 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);
foreach ($migrations as $key => $migration) {
if (in_array($key, $versions)) {
$this->executeMigration($migration, MigrationInterface::DOWN);
}
}
}
}

View File

@ -23,8 +23,8 @@ class MigrateRunCommand extends Run
The <info>migrate:run</info> command runs all available migrations, optionally up to a specific version
<info>php think catch-migrate:run module</info>
<info>php think catch-migrate:run -t 20110103081132</info>
<info>php think catch-migrate:run -d 20110103</info>
<info>php think catch-migrate:run module -t 20110103081132</info>
<info>php think catch-migrate:run module -d 20110103</info>
<info>php think catch-migrate:run -v</info>
EOT
@ -63,4 +63,4 @@ EOT
{
return CatchAdmin::moduleMigrationsDirectory($this->module);
}
}
}