Skip to content

Commit 24a7673

Browse files
authored
Merge pull request #8958 from kenjis/fix-migrate-rollback-batch
fix: `migrate:rollback -b` does not work due to TypeError
2 parents 7aced76 + 3cfb940 commit 24a7673

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

system/Commands/Database/MigrateRollback.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use CodeIgniter\CLI\BaseCommand;
1717
use CodeIgniter\CLI\CLI;
18+
use CodeIgniter\Database\MigrationRunner;
1819
use Throwable;
1920

2021
/**
@@ -78,10 +79,23 @@ public function run(array $params)
7879
// @codeCoverageIgnoreEnd
7980
}
8081

82+
/** @var MigrationRunner $runner */
8183
$runner = service('migrations');
8284

8385
try {
8486
$batch = $params['b'] ?? CLI::getOption('b') ?? $runner->getLastBatch() - 1;
87+
88+
if (is_string($batch)) {
89+
if (! ctype_digit($batch)) {
90+
CLI::error('Invalid batch number: ' . $batch, 'light_gray', 'red');
91+
CLI::newLine();
92+
93+
return EXIT_ERROR;
94+
}
95+
96+
$batch = (int) $batch;
97+
}
98+
8599
CLI::write(lang('Migrations.rollingBack') . ' ' . $batch, 'yellow');
86100

87101
if (! $runner->regress($batch)) {

tests/system/Commands/DatabaseCommandsTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,24 @@ public function testMigrate(): void
5353
$this->assertStringContainsString('Migrations complete.', $this->getBuffer());
5454
}
5555

56+
public function testMigrateRollbackValidBatchNumber(): void
57+
{
58+
command('migrate --all');
59+
$this->clearBuffer();
60+
61+
command('migrate:rollback -b 1');
62+
$this->assertStringContainsString('Done rolling back migrations.', $this->getBuffer());
63+
}
64+
65+
public function testMigrateRollbackInvalidBatchNumber(): void
66+
{
67+
command('migrate --all');
68+
$this->clearBuffer();
69+
70+
command('migrate:rollback -b x');
71+
$this->assertStringContainsString('Invalid batch number: x', $this->getBuffer());
72+
}
73+
5674
public function testMigrateRollback(): void
5775
{
5876
command('migrate --all -g tests');

0 commit comments

Comments
 (0)