From cecc2137b07d983a4c58c8a2cc245d7252cab376 Mon Sep 17 00:00:00 2001 From: mscherer Date: Tue, 6 Jan 2026 23:29:06 +0100 Subject: [PATCH] Fix phinx backend using incorrect RollbackCommand class When using the phinx backend with Bake installed, discoverPlugin() was finding both builtin and phinx commands. Since both RollbackCommand and MigrationsRollbackCommand have the same defaultName(), the builtin command was overwriting the phinx one due to alphabetical ordering. This fix explicitly registers only the phinx-specific commands instead of using discoverPlugin(), matching the approach used for builtin backend. Fixes #990 --- src/MigrationsPlugin.php | 11 ++--- tests/TestCase/MigrationsPluginTest.php | 53 +++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 tests/TestCase/MigrationsPluginTest.php diff --git a/src/MigrationsPlugin.php b/src/MigrationsPlugin.php index 0c677430b..e01cb4d99 100644 --- a/src/MigrationsPlugin.php +++ b/src/MigrationsPlugin.php @@ -131,15 +131,16 @@ public function console(CommandCollection $commands): CommandCollection return $commands; } + $classes = $this->migrationCommandsList; if (class_exists(SimpleBakeCommand::class)) { - $found = $commands->discoverPlugin($this->getName()); - - return $commands->addMany($found); + $classes[] = BakeMigrationCommand::class; + $classes[] = BakeMigrationDiffCommand::class; + $classes[] = BakeMigrationSnapshotCommand::class; + $classes[] = BakeSeedCommand::class; } $found = []; - // Convert to a method and use config to toggle command names. - foreach ($this->migrationCommandsList as $class) { + foreach ($classes as $class) { $name = $class::defaultName(); // If the short name has been used, use the full name. // This allows app commands to have name preference. diff --git a/tests/TestCase/MigrationsPluginTest.php b/tests/TestCase/MigrationsPluginTest.php new file mode 100644 index 000000000..1dcd8a02b --- /dev/null +++ b/tests/TestCase/MigrationsPluginTest.php @@ -0,0 +1,53 @@ +console($commands); + + $this->assertTrue($commands->has('migrations rollback')); + $this->assertSame(RollbackCommand::class, $commands->get('migrations rollback')); + } + + /** + * Test that phinx backend uses MigrationsRollbackCommand + * + * This is the reported bug in https://github.com/cakephp/migrations/issues/990 + */ + public function testConsolePhinxBackendUsesCorrectRollbackCommand(): void + { + Configure::write('Migrations.backend', 'phinx'); + + $plugin = new MigrationsPlugin(); + $commands = new CommandCollection(); + $commands = $plugin->console($commands); + + $this->assertTrue($commands->has('migrations rollback')); + // Bug: RollbackCommand is loaded instead of MigrationsRollbackCommand + $this->assertSame(MigrationsRollbackCommand::class, $commands->get('migrations rollback')); + } +}