Skip to content

Commit

Permalink
Merge pull request #618 from telepathPiddlingAccent/3.x-feat-608
Browse files Browse the repository at this point in the history
bake migration_diff now supports --source for multiple database configs
  • Loading branch information
othercorey authored Sep 22, 2023
2 parents 5fad3fe + 16548b1 commit 58446fd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 24 deletions.
10 changes: 1 addition & 9 deletions src/Command/BakeMigrationDiffCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
13 changes: 10 additions & 3 deletions src/Command/BakeSimpleMigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
33 changes: 21 additions & 12 deletions src/Command/SnapshotTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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');
Expand All @@ -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;
}
}
22 changes: 22 additions & 0 deletions tests/TestCase/Command/BakeMigrationDiffCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit 58446fd

Please sign in to comment.