File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change 1515
1616use CodeIgniter \CLI \BaseCommand ;
1717use CodeIgniter \CLI \CLI ;
18+ use CodeIgniter \Database \MigrationRunner ;
1819use 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 )) {
Original file line number Diff line number Diff 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 ' );
You can’t perform that action at this time.
0 commit comments