Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: migrate:rollback -b does not work due to TypeError #8958

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading