diff --git a/src/Command/BakeMigrationDiffCommand.php b/src/Command/BakeMigrationDiffCommand.php index 78b4a41f..6fc28f15 100644 --- a/src/Command/BakeMigrationDiffCommand.php +++ b/src/Command/BakeMigrationDiffCommand.php @@ -473,15 +473,7 @@ protected function bakeSnapshot($name, Arguments $args, ConsoleIo $io) $newArgs = []; $newArgs[] = $name; - if ($args->getOption('connection')) { - $newArgs[] = '-c'; - $newArgs[] = $args->getOption('connection'); - } - - if ($args->getOption('plugin')) { - $newArgs[] = '-p'; - $newArgs[] = $args->getOption('plugin'); - } + $newArgs = array_merge($newArgs, $this->parseOptions($args)); $exitCode = $this->executeCommand(BakeMigrationSnapshotCommand::class, $newArgs, $io); diff --git a/src/Command/BakeSimpleMigrationCommand.php b/src/Command/BakeSimpleMigrationCommand.php index 6708aa33..37f2dca8 100644 --- a/src/Command/BakeSimpleMigrationCommand.php +++ b/src/Command/BakeSimpleMigrationCommand.php @@ -40,7 +40,9 @@ abstract class BakeSimpleMigrationCommand extends SimpleBakeCommand * * @var string */ - public $pathFragment = 'config/Migrations/'; + public $pathFragment = 'config'; + + public const DEFAULT_MIGRATION_FOLDER = 'Migrations'; /** * @inheritDoc @@ -65,9 +67,10 @@ public function fileName($name): string */ public function getPath(Arguments $args): string { - $path = ROOT . DS . $this->pathFragment; + $migrationFolder = $this->pathFragment . DS . $args->getOption('source') . DS; + $path = ROOT . DS . $migrationFolder; if ($this->plugin) { - $path = $this->_pluginPath($this->plugin) . $this->pathFragment; + $path = $this->_pluginPath($this->plugin) . $migrationFolder; } return str_replace('/', DS, $path); @@ -188,6 +191,10 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar 'short' => 'f', 'boolean' => true, 'help' => 'Force overwriting existing file if a migration already exists with the same name.', + ])->addOption('source', [ + 'short' => 's', + 'default' => self::DEFAULT_MIGRATION_FOLDER, + 'help' => 'Name of the folder in which the migration should be saved.', ]); return $parser; diff --git a/src/Command/SnapshotTrait.php b/src/Command/SnapshotTrait.php index 5ba086e6..7d2fc2ff 100644 --- a/src/Command/SnapshotTrait.php +++ b/src/Command/SnapshotTrait.php @@ -59,15 +59,7 @@ protected function markSnapshotApplied($path, Arguments $args, ConsoleIo $io) $newArgs[] = $version; $newArgs[] = '-o'; - if ($args->getOption('connection')) { - $newArgs[] = '-c'; - $newArgs[] = $args->getOption('connection'); - } - - if ($args->getOption('plugin')) { - $newArgs[] = '-p'; - $newArgs[] = $args->getOption('plugin'); - } + $newArgs = array_merge($newArgs, $this->parseOptions($args)); $io->out('Marking the migration ' . $fileName . ' as migrated...'); $this->executeCommand(MigrationsMarkMigratedCommand::class, $newArgs, $io); @@ -83,8 +75,21 @@ protected function markSnapshotApplied($path, Arguments $args, ConsoleIo $io) */ protected function refreshDump(Arguments $args, ConsoleIo $io) { - $newArgs = []; + $newArgs = $this->parseOptions($args); + + $io->out('Creating a dump of the new database state...'); + $this->executeCommand(MigrationsDumpCommand::class, $newArgs, $io); + } + /** + * Will parse 'connection', 'plugin' and 'source' options into a new Array + * + * @param \Cake\Console\Arguments $args The command arguments. + * @return array Array containing the short for the option followed by its value + */ + protected function parseOptions(Arguments $args): array + { + $newArgs = []; if ($args->getOption('connection')) { $newArgs[] = '-c'; $newArgs[] = $args->getOption('connection'); @@ -95,7 +100,11 @@ protected function refreshDump(Arguments $args, ConsoleIo $io) $newArgs[] = $args->getOption('plugin'); } - $io->out('Creating a dump of the new database state...'); - $this->executeCommand(MigrationsDumpCommand::class, $newArgs, $io); + if ($args->getOption('source')) { + $newArgs[] = '-s'; + $newArgs[] = $args->getOption('source'); + } + + return $newArgs; } } diff --git a/tests/TestCase/Command/BakeMigrationDiffCommandTest.php b/tests/TestCase/Command/BakeMigrationDiffCommandTest.php index 93568c84..6bd5dd15 100644 --- a/tests/TestCase/Command/BakeMigrationDiffCommandTest.php +++ b/tests/TestCase/Command/BakeMigrationDiffCommandTest.php @@ -96,6 +96,28 @@ public function testEmptyHistoryNoMigrations() $this->assertExitCode(BaseCommand::CODE_ERROR); } + /** + * Tests baking a diff in a custom folder source + * + * @return void + */ + public function testBakeMigrationDiffInCustomFolder() + { + $customFolderName = 'CustomMigrationsFolder'; + $this->exec('bake migration_diff MigrationDiffForCustomFolder -c test -s ' . $customFolderName); + + $path = ROOT . DS . 'config' . DS . $customFolderName . DS; + $this->generatedFiles = glob($path . '*_MigrationDiffForCustomFolder.php'); + + $this->assertCount(1, $this->generatedFiles); + $this->assertFileExists($path . 'schema-dump-test.lock', 'Cannot test contents, file does not exist.'); + $this->generatedFiles[] = $path . 'schema-dump-test.lock'; + + $fileName = pathinfo($this->generatedFiles[0], PATHINFO_FILENAME); + $this->assertOutputContains('Marking the migration ' . $fileName . ' as migrated...'); + $this->assertOutputContains('Creating a dump of the new database state...'); + } + /** * Tests baking a diff *