Skip to content

Commit

Permalink
Merge pull request #8958 from kenjis/fix-migrate-rollback-batch
Browse files Browse the repository at this point in the history
fix: `migrate:rollback -b` does not work due to TypeError
  • Loading branch information
kenjis authored Jun 17, 2024
2 parents 7aced76 + 3cfb940 commit 24a7673
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
14 changes: 14 additions & 0 deletions system/Commands/Database/MigrateRollback.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use CodeIgniter\Database\MigrationRunner;
use Throwable;

/**
Expand Down Expand Up @@ -78,10 +79,23 @@ public function run(array $params)
// @codeCoverageIgnoreEnd
}

/** @var MigrationRunner $runner */
$runner = service('migrations');

try {
$batch = $params['b'] ?? CLI::getOption('b') ?? $runner->getLastBatch() - 1;

if (is_string($batch)) {
if (! ctype_digit($batch)) {
CLI::error('Invalid batch number: ' . $batch, 'light_gray', 'red');
CLI::newLine();

return EXIT_ERROR;
}

$batch = (int) $batch;
}

CLI::write(lang('Migrations.rollingBack') . ' ' . $batch, 'yellow');

if (! $runner->regress($batch)) {
Expand Down
18 changes: 18 additions & 0 deletions tests/system/Commands/DatabaseCommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ public function testMigrate(): void
$this->assertStringContainsString('Migrations complete.', $this->getBuffer());
}

public function testMigrateRollbackValidBatchNumber(): void
{
command('migrate --all');
$this->clearBuffer();

command('migrate:rollback -b 1');
$this->assertStringContainsString('Done rolling back migrations.', $this->getBuffer());
}

public function testMigrateRollbackInvalidBatchNumber(): void
{
command('migrate --all');
$this->clearBuffer();

command('migrate:rollback -b x');
$this->assertStringContainsString('Invalid batch number: x', $this->getBuffer());
}

public function testMigrateRollback(): void
{
command('migrate --all -g tests');
Expand Down

0 comments on commit 24a7673

Please sign in to comment.