Skip to content

Commit 5535122

Browse files
authored
Actualize option --all and --limit (#212)
1 parent 59f8546 commit 5535122

10 files changed

+187
-72
lines changed

src/Command/DownCommand.php

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
/**
2424
* Reverts the specified number of latest migrations.
2525
*
26-
* For example,
26+
* For example:
2727
*
28-
* ```
29-
* yii migrate:down # revert the last migration
30-
* yii migrate:down 3 # revert the last 3 migrations
31-
* yii migrate:down all # revert all migrations
28+
* ```shell
29+
* ./yii migrate:down # revert the last migration
30+
* ./yii migrate:down --limit=3 # revert the last 3 migrations
31+
* ./yii migrate:down --all # revert all migrations
3232
* ```
3333
*/
3434
#[AsCommand('migrate:down', 'Reverts the specified number of latest migrations.')]
@@ -48,7 +48,7 @@ public function __construct(
4848
protected function configure(): void
4949
{
5050
$this
51-
->addOption('limit', 'l', InputOption::VALUE_OPTIONAL, 'Number of migrations to revert.', 1)
51+
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Number of migrations to revert.', 1)
5252
->addOption('all', 'a', InputOption::VALUE_NONE, 'Revert all migrations.');
5353
}
5454

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

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

64-
$limit = null;
65-
if (!$input->getOption('all')) {
66-
$limit = (int)$input->getOption('limit');
67-
if ($limit <= 0) {
68-
$io->error('The limit argument must be greater than 0.');
69-
$this->migrationService->databaseConnection();
70-
return Command::INVALID;
71-
}
64+
$limit = !$input->getOption('all')
65+
? (int)$input->getOption('limit')
66+
: null;
67+
68+
if ($limit !== null && $limit <= 0) {
69+
$io->error('The limit option must be greater than 0.');
70+
$this->migrationService->databaseConnection();
71+
72+
return Command::INVALID;
7273
}
7374

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

8586
$n = count($migrations);
87+
$migrationWord = $n === 1 ? 'migration' : 'migrations';
88+
8689
$output->writeln(
87-
"<fg=yellow>Total $n " . ($n === 1 ? 'migration' : 'migrations') . " to be reverted:</>\n"
90+
"<fg=yellow>Total $n $migrationWord to be reverted:</>\n"
8891
);
8992

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

97100
$question = new ConfirmationQuestion(
98-
"\n<fg=cyan>Revert the above " . ($n === 1 ? 'migration y/n: ' : 'migrations y/n: '),
101+
"\n<fg=cyan>Revert the above $migrationWord y/n: ",
99102
true
100103
);
101104

src/Command/HistoryCommand.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
/**
2020
* Displays the migration history.
2121
*
22-
* This command will show the list of migrations that have been applied
23-
* so far. For example,
22+
* This command will show the list of migrations that have been applied so far.
2423
*
25-
* ```
26-
* yii migrate:history # last 10 migrations
27-
* yii migrate:history 5 # last 5 migrations
28-
* yii migrate:history all # whole history
24+
* For example:
25+
*
26+
* ```shell
27+
* ./yii migrate:history # last 10 migrations
28+
* ./yii migrate:history --limit=5 # last 5 migrations
29+
* ./yii migrate:history --all # whole history
2930
* ```
3031
*/
3132
#[AsCommand('migrate:history', 'Displays the migration history.')]
@@ -40,7 +41,9 @@ public function __construct(
4041

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

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

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

54-
$limit = filter_var($input->getOption('limit'), FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
57+
$limit = !$input->getOption('all')
58+
? (int)$input->getOption('limit')
59+
: null;
5560

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

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

7176
$n = count($migrations);
7277

73-
if ($limit > 0) {
78+
if ($limit === $n) {
7479
$io->section("Last $n applied " . ($n === 1 ? 'migration' : 'migrations') . ':');
7580
} else {
76-
$io->section(
77-
"Total $n " . ($n === 1 ? 'migration has' : 'migrations have') . ' been applied before:'
78-
);
81+
$io->section("Total $n " . ($n === 1 ? 'migration has' : 'migrations have') . ' been applied before:');
7982
}
8083

8184
foreach ($migrations as $version => $time) {

src/Command/NewCommand.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
* Displays not yet applied migrations.
1919
*
2020
* This command will show the new migrations that have not been applied yet.
21-
* For example,
21+
*
22+
* For example:
2223
*
2324
* ```shell
2425
* ./yii migrate:new # first 10 new migrations
25-
* ./yii migrate:new 5 # first 5 new migrations
26-
* ./yii migrate:new all # all new migrations
26+
* ./yii migrate:new --limit=5 # first 5 new migrations
27+
* ./yii migrate:new --all # all new migrations
2728
* ./yii migrate:new --path=@vendor/yiisoft/rbac-db/migrations # new migrations from the directory
2829
* ./yii migrate:new --namespace=Yiisoft\\Rbac\\Db\\Migrations # new migrations from the namespace
2930
*
@@ -43,7 +44,8 @@ public function __construct(private MigrationService $migrationService)
4344
protected function configure(): void
4445
{
4546
$this
46-
->addOption('limit', 'l', InputOption::VALUE_OPTIONAL, 'Number of migrations to display.', '10')
47+
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Number of migrations to display.', 10)
48+
->addOption('all', 'a', InputOption::VALUE_NONE, 'All new migrations.')
4749
->addOption('path', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Path to migrations to display.')
4850
->addOption('namespace', 'ns', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Namespace of migrations to display.');
4951
}
@@ -66,10 +68,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6668

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

69-
$limit = (int) $input->getOption('limit');
71+
$limit = !$input->getOption('all')
72+
? (int)$input->getOption('limit')
73+
: null;
7074

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

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

91-
if ($limit && $n > $limit) {
95+
if ($limit !== null && $n > $limit) {
96+
$migrations = array_slice($migrations, 0, $limit);
97+
9298
$io->warning("Showing $limit out of $n new $migrationWord:\n");
9399
} else {
94100
$io->section("Found $n new $migrationWord:");

src/Command/RedoCommand.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@
2525
/**
2626
* Redoes the last few migrations.
2727
*
28-
* This command will first revert the specified migrations, and then apply
29-
* them again. For example,
28+
* This command will first revert the specified migrations, and then apply them again.
3029
*
31-
* ```
32-
* yii migrate:redo # redo the last applied migration
33-
* yii migrate:redo 3 # redo last 3 applied migrations
34-
* yii migrate:redo all # redo all migrations
30+
* For example:
31+
*
32+
* ```shell
33+
* ./yii migrate:redo # redo the last applied migration
34+
* ./yii migrate:redo --limit=3 # redo last 3 applied migrations
35+
* ./yii migrate:redo --all # redo all migrations
3536
* ```
3637
*/
3738
#[AsCommand('migrate:redo', 'Redoes the last few migrations.')]
@@ -52,7 +53,8 @@ public function __construct(
5253
protected function configure(): void
5354
{
5455
$this
55-
->addOption('limit', 'l', InputOption::VALUE_OPTIONAL, 'Number of migrations to redo.', null);
56+
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Number of migrations to redo.', 1)
57+
->addOption('all', 'a', InputOption::VALUE_NONE, 'All migrations.');
5658
}
5759

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

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

68-
$limit = filter_var($input->getOption('limit'), FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
70+
$limit = !$input->getOption('all')
71+
? (int)$input->getOption('limit')
72+
: null;
6973

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

7478
return Command::INVALID;

src/Command/UpdateCommand.php

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
* ./yii migrate:up --namespace=Yiisoft\\Rbac\\Db\\Migrations # apply new migrations from the namespace
3434
*
3535
* # apply new migrations from multiple directories and namespaces
36-
* ./yii migrate:new --path=@vendor/yiisoft/rbac-db/migrations --path=@vendor/yiisoft/cache-db/migrations
37-
* ./yii migrate:new --namespace=Yiisoft\\Rbac\\Db\\Migrations --namespace=Yiisoft\\Cache\\Db\\Migrations
36+
* ./yii migrate:up --path=@vendor/yiisoft/rbac-db/migrations --path=@vendor/yiisoft/cache-db/migrations
37+
* ./yii migrate:up --namespace=Yiisoft\\Rbac\\Db\\Migrations --namespace=Yiisoft\\Cache\\Db\\Migrations
3838
* ```
3939
*/
4040
#[AsCommand('migrate:up', 'Applies new migrations.')]
@@ -52,7 +52,7 @@ public function __construct(
5252
protected function configure(): void
5353
{
5454
$this
55-
->addOption('limit', 'l', InputOption::VALUE_OPTIONAL, 'Number of migrations to apply.', '0')
55+
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Number of migrations to apply.')
5656
->addOption('path', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Path to migrations to apply.')
5757
->addOption('namespace', 'ns', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Namespace of migrations to apply.');
5858
}
@@ -79,7 +79,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7979
return Command::INVALID;
8080
}
8181

82-
$limit = (int) $input->getOption('limit');
82+
$limit = $input->getOption('limit');
83+
84+
if ($limit !== null) {
85+
$limit = (int)$limit;
86+
87+
if ($limit <= 0) {
88+
$io->error('The limit option must be greater than 0.');
89+
$this->migrationService->databaseConnection();
90+
91+
return Command::INVALID;
92+
}
93+
}
8394

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

95-
$total = count($migrations);
106+
$n = count($migrations);
107+
$migrationWord = $n === 1 ? 'migration' : 'migrations';
96108

97-
if ($limit > 0) {
109+
if ($limit !== null && $n > $limit) {
98110
$migrations = array_slice($migrations, 0, $limit);
99-
}
100-
101-
$n = count($migrations);
102111

103-
if ($n === $total) {
104-
$output->writeln(
105-
"<fg=yellow>Total $n new " . ($n === 1 ? 'migration' : 'migrations') . ' to be ' .
106-
"applied:</>\n"
107-
);
112+
$output->writeln("<fg=yellow>Total $limit out of $n new $migrationWord to be applied:</>\n");
108113
} else {
109-
$output->writeln(
110-
"<fg=yellow>Total $n out of $total new " . ($total === 1 ? 'migration' : 'migrations') .
111-
" to be applied:</>\n"
112-
);
114+
$output->writeln("<fg=yellow>Total $n new $migrationWord to be applied:</>\n");
113115
}
114116

115117
foreach ($migrations as $migration) {

tests/Common/Command/AbstractDownCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public function testIncorrectLimit(int $limit): void
270270
$output = $command->getDisplay(true);
271271

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

276276
public function createCommand(ContainerInterface $container): CommandTester

tests/Common/Command/AbstractHistoryCommandTest.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function testIncorrectLimit(): void
9393
$output = $command->getDisplay(true);
9494

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

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

112+
public function testOptionAll(): void
113+
{
114+
MigrationHelper::useMigrationsNamespace($this->container);
115+
116+
MigrationHelper::createAndApplyMigration(
117+
$this->container,
118+
'Create_Post',
119+
'table',
120+
'post',
121+
['name:string(50)'],
122+
);
123+
MigrationHelper::createAndApplyMigration(
124+
$this->container,
125+
'Create_User',
126+
'table',
127+
'user',
128+
['name:string(50)'],
129+
);
130+
131+
$command = $this->createCommand($this->container);
132+
133+
$exitCode = $command->execute(['--all' => true]);
134+
$output = $command->getDisplay(true);
135+
136+
$this->assertSame(Command::SUCCESS, $exitCode);
137+
$this->assertStringContainsString('Total 2 migrations have been applied before:', $output);
138+
}
139+
112140
public function createCommand(ContainerInterface $container): CommandTester
113141
{
114142
return CommandHelper::getCommandTester($container, HistoryCommand::class);

0 commit comments

Comments
 (0)