Skip to content

Commit

Permalink
Actualize option --all and --limit (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Oct 16, 2023
1 parent 59f8546 commit 5535122
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 72 deletions.
35 changes: 19 additions & 16 deletions src/Command/DownCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
/**
* Reverts the specified number of latest migrations.
*
* For example,
* For example:
*
* ```
* yii migrate:down # revert the last migration
* yii migrate:down 3 # revert the last 3 migrations
* yii migrate:down all # revert all migrations
* ```shell
* ./yii migrate:down # revert the last migration
* ./yii migrate:down --limit=3 # revert the last 3 migrations
* ./yii migrate:down --all # revert all migrations
* ```
*/
#[AsCommand('migrate:down', 'Reverts the specified number of latest migrations.')]
Expand All @@ -48,7 +48,7 @@ public function __construct(
protected function configure(): void
{
$this
->addOption('limit', 'l', InputOption::VALUE_OPTIONAL, 'Number of migrations to revert.', 1)
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Number of migrations to revert.', 1)
->addOption('all', 'a', InputOption::VALUE_NONE, 'Revert all migrations.');
}

Expand All @@ -61,14 +61,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$this->migrationService->before(self::getDefaultName() ?? '');

$limit = null;
if (!$input->getOption('all')) {
$limit = (int)$input->getOption('limit');
if ($limit <= 0) {
$io->error('The limit argument must be greater than 0.');
$this->migrationService->databaseConnection();
return Command::INVALID;
}
$limit = !$input->getOption('all')
? (int)$input->getOption('limit')
: null;

if ($limit !== null && $limit <= 0) {
$io->error('The limit option must be greater than 0.');
$this->migrationService->databaseConnection();

Check warning on line 70 in src/Command/DownCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ $limit = !$input->getOption('all') ? (int) $input->getOption('limit') : null; if ($limit !== null && $limit <= 0) { $io->error('The limit option must be greater than 0.'); - $this->migrationService->databaseConnection(); + return Command::INVALID; } $migrations = $this->migrator->getHistory($limit);

return Command::INVALID;
}

$migrations = $this->migrator->getHistory($limit);
Expand All @@ -83,8 +84,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$migrations = array_keys($migrations);

$n = count($migrations);
$migrationWord = $n === 1 ? 'migration' : 'migrations';

$output->writeln(
"<fg=yellow>Total $n " . ($n === 1 ? 'migration' : 'migrations') . " to be reverted:</>\n"
"<fg=yellow>Total $n $migrationWord to be reverted:</>\n"
);

foreach ($migrations as $migration) {
Expand All @@ -95,7 +98,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$helper = $this->getHelper('question');

$question = new ConfirmationQuestion(
"\n<fg=cyan>Revert the above " . ($n === 1 ? 'migration y/n: ' : 'migrations y/n: '),
"\n<fg=cyan>Revert the above $migrationWord y/n: ",
true
);

Expand Down
31 changes: 17 additions & 14 deletions src/Command/HistoryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
/**
* Displays the migration history.
*
* This command will show the list of migrations that have been applied
* so far. For example,
* This command will show the list of migrations that have been applied so far.
*
* ```
* yii migrate:history # last 10 migrations
* yii migrate:history 5 # last 5 migrations
* yii migrate:history all # whole history
* For example:
*
* ```shell
* ./yii migrate:history # last 10 migrations
* ./yii migrate:history --limit=5 # last 5 migrations
* ./yii migrate:history --all # whole history
* ```
*/
#[AsCommand('migrate:history', 'Displays the migration history.')]
Expand All @@ -40,7 +41,9 @@ public function __construct(

protected function configure(): void
{
$this->addOption('limit', 'l', InputOption::VALUE_OPTIONAL, 'Maximum number of migrations to display.', null);
$this
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Number of migrations to display.', 10)
->addOption('all', 'a', InputOption::VALUE_NONE, 'All migrations.');
}

protected function execute(InputInterface $input, OutputInterface $output): int
Expand All @@ -51,10 +54,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$this->migrationService->before(self::getDefaultName() ?? '');

$limit = filter_var($input->getOption('limit'), FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
$limit = !$input->getOption('all')
? (int)$input->getOption('limit')
: null;

if ($limit < 0) {
$io->error('The step argument must be greater than 0.');
if ($limit !== null && $limit <= 0) {
$io->error('The limit option must be greater than 0.');
$this->migrationService->databaseConnection();

return Command::INVALID;
Expand All @@ -70,12 +75,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$n = count($migrations);

if ($limit > 0) {
if ($limit === $n) {
$io->section("Last $n applied " . ($n === 1 ? 'migration' : 'migrations') . ':');
} else {
$io->section(
"Total $n " . ($n === 1 ? 'migration has' : 'migrations have') . ' been applied before:'
);
$io->section("Total $n " . ($n === 1 ? 'migration has' : 'migrations have') . ' been applied before:');
}

foreach ($migrations as $version => $time) {
Expand Down
22 changes: 14 additions & 8 deletions src/Command/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
* Displays not yet applied migrations.
*
* This command will show the new migrations that have not been applied yet.
* For example,
*
* For example:
*
* ```shell
* ./yii migrate:new # first 10 new migrations
* ./yii migrate:new 5 # first 5 new migrations
* ./yii migrate:new all # all new migrations
* ./yii migrate:new --limit=5 # first 5 new migrations
* ./yii migrate:new --all # all new migrations
* ./yii migrate:new --path=@vendor/yiisoft/rbac-db/migrations # new migrations from the directory
* ./yii migrate:new --namespace=Yiisoft\\Rbac\\Db\\Migrations # new migrations from the namespace
*
Expand All @@ -43,7 +44,8 @@ public function __construct(private MigrationService $migrationService)
protected function configure(): void
{
$this
->addOption('limit', 'l', InputOption::VALUE_OPTIONAL, 'Number of migrations to display.', '10')
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Number of migrations to display.', 10)
->addOption('all', 'a', InputOption::VALUE_NONE, 'All new migrations.')
->addOption('path', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Path to migrations to display.')
->addOption('namespace', 'ns', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Namespace of migrations to display.');
}
Expand All @@ -66,10 +68,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$this->migrationService->before(self::getDefaultName() ?? '');

$limit = (int) $input->getOption('limit');
$limit = !$input->getOption('all')
? (int)$input->getOption('limit')
: null;

if ($limit < 0) {
$io->error('The step argument must be greater than 0.');
if ($limit !== null && $limit <= 0) {
$io->error('The limit option must be greater than 0.');
$this->migrationService->databaseConnection();

return Command::INVALID;
Expand All @@ -88,7 +92,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$n = count($migrations);
$migrationWord = $n === 1 ? 'migration' : 'migrations';

if ($limit && $n > $limit) {
if ($limit !== null && $n > $limit) {
$migrations = array_slice($migrations, 0, $limit);

$io->warning("Showing $limit out of $n new $migrationWord:\n");
} else {
$io->section("Found $n new $migrationWord:");
Expand Down
24 changes: 14 additions & 10 deletions src/Command/RedoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@
/**
* Redoes the last few migrations.
*
* This command will first revert the specified migrations, and then apply
* them again. For example,
* This command will first revert the specified migrations, and then apply them again.
*
* ```
* yii migrate:redo # redo the last applied migration
* yii migrate:redo 3 # redo last 3 applied migrations
* yii migrate:redo all # redo all migrations
* For example:
*
* ```shell
* ./yii migrate:redo # redo the last applied migration
* ./yii migrate:redo --limit=3 # redo last 3 applied migrations
* ./yii migrate:redo --all # redo all migrations
* ```
*/
#[AsCommand('migrate:redo', 'Redoes the last few migrations.')]
Expand All @@ -52,7 +53,8 @@ public function __construct(
protected function configure(): void
{
$this
->addOption('limit', 'l', InputOption::VALUE_OPTIONAL, 'Number of migrations to redo.', null);
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Number of migrations to redo.', 1)
->addOption('all', 'a', InputOption::VALUE_NONE, 'All migrations.');
}

protected function execute(InputInterface $input, OutputInterface $output): int
Expand All @@ -65,10 +67,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$this->migrationService->before(self::getDefaultName() ?? '');

$limit = filter_var($input->getOption('limit'), FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
$limit = !$input->getOption('all')
? (int)$input->getOption('limit')
: null;

if ($limit < 0) {
$io->error('The step argument must be greater than 0.');
if ($limit !== null && $limit <= 0) {
$io->error('The limit option must be greater than 0.');
$this->migrationService->databaseConnection();

return Command::INVALID;
Expand Down
38 changes: 20 additions & 18 deletions src/Command/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
* ./yii migrate:up --namespace=Yiisoft\\Rbac\\Db\\Migrations # apply new migrations from the namespace
*
* # apply new migrations from multiple directories and namespaces
* ./yii migrate:new --path=@vendor/yiisoft/rbac-db/migrations --path=@vendor/yiisoft/cache-db/migrations
* ./yii migrate:new --namespace=Yiisoft\\Rbac\\Db\\Migrations --namespace=Yiisoft\\Cache\\Db\\Migrations
* ./yii migrate:up --path=@vendor/yiisoft/rbac-db/migrations --path=@vendor/yiisoft/cache-db/migrations
* ./yii migrate:up --namespace=Yiisoft\\Rbac\\Db\\Migrations --namespace=Yiisoft\\Cache\\Db\\Migrations
* ```
*/
#[AsCommand('migrate:up', 'Applies new migrations.')]
Expand All @@ -52,7 +52,7 @@ public function __construct(
protected function configure(): void
{
$this
->addOption('limit', 'l', InputOption::VALUE_OPTIONAL, 'Number of migrations to apply.', '0')
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Number of migrations to apply.')
->addOption('path', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Path to migrations to apply.')
->addOption('namespace', 'ns', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Namespace of migrations to apply.');
}
Expand All @@ -79,7 +79,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::INVALID;
}

$limit = (int) $input->getOption('limit');
$limit = $input->getOption('limit');

if ($limit !== null) {
$limit = (int)$limit;

if ($limit <= 0) {
$io->error('The limit option must be greater than 0.');
$this->migrationService->databaseConnection();

return Command::INVALID;
}
}

/** @psalm-var class-string[] $migrations */
$migrations = $this->migrationService->getNewMigrations();
Expand All @@ -92,24 +103,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::SUCCESS;
}

$total = count($migrations);
$n = count($migrations);
$migrationWord = $n === 1 ? 'migration' : 'migrations';

if ($limit > 0) {
if ($limit !== null && $n > $limit) {

Check warning on line 109 in src/Command/UpdateCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8-ubuntu-latest

Escaped Mutant for Mutator "GreaterThan": --- Original +++ New @@ @@ } $n = count($migrations); $migrationWord = $n === 1 ? 'migration' : 'migrations'; - if ($limit !== null && $n > $limit) { + if ($limit !== null && $n >= $limit) { $migrations = array_slice($migrations, 0, $limit); $output->writeln("<fg=yellow>Total {$limit} out of {$n} new {$migrationWord} to be applied:</>\n"); } else {
$migrations = array_slice($migrations, 0, $limit);
}

$n = count($migrations);

if ($n === $total) {
$output->writeln(
"<fg=yellow>Total $n new " . ($n === 1 ? 'migration' : 'migrations') . ' to be ' .
"applied:</>\n"
);
$output->writeln("<fg=yellow>Total $limit out of $n new $migrationWord to be applied:</>\n");
} else {
$output->writeln(
"<fg=yellow>Total $n out of $total new " . ($total === 1 ? 'migration' : 'migrations') .
" to be applied:</>\n"
);
$output->writeln("<fg=yellow>Total $n new $migrationWord to be applied:</>\n");
}

foreach ($migrations as $migration) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Common/Command/AbstractDownCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public function testIncorrectLimit(int $limit): void
$output = $command->getDisplay(true);

$this->assertSame(Command::INVALID, $exitCode);
$this->assertStringContainsString('The limit argument must be greater than 0.', $output);
$this->assertStringContainsString('The limit option must be greater than 0.', $output);
}

public function createCommand(ContainerInterface $container): CommandTester
Expand Down
30 changes: 29 additions & 1 deletion tests/Common/Command/AbstractHistoryCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function testIncorrectLimit(): void
$output = $command->getDisplay(true);

$this->assertSame(Command::INVALID, $exitCode);
$this->assertStringContainsString('The step argument must be greater than 0.', $output);
$this->assertStringContainsString('The limit option must be greater than 0.', $output);
}

public function testWithoutNewMigrations(): void
Expand All @@ -109,6 +109,34 @@ public function testWithoutNewMigrations(): void
$this->assertStringContainsString('[WARNING] No migration has been done before.', $output);
}

public function testOptionAll(): void
{
MigrationHelper::useMigrationsNamespace($this->container);

MigrationHelper::createAndApplyMigration(
$this->container,
'Create_Post',
'table',
'post',
['name:string(50)'],
);
MigrationHelper::createAndApplyMigration(
$this->container,
'Create_User',
'table',
'user',
['name:string(50)'],
);

$command = $this->createCommand($this->container);

$exitCode = $command->execute(['--all' => true]);
$output = $command->getDisplay(true);

$this->assertSame(Command::SUCCESS, $exitCode);
$this->assertStringContainsString('Total 2 migrations have been applied before:', $output);
}

public function createCommand(ContainerInterface $container): CommandTester
{
return CommandHelper::getCommandTester($container, HistoryCommand::class);
Expand Down
Loading

0 comments on commit 5535122

Please sign in to comment.