Skip to content

Commit 58446fd

Browse files
authored
Merge pull request #618 from telepathPiddlingAccent/3.x-feat-608
bake migration_diff now supports --source for multiple database configs
2 parents 5fad3fe + 16548b1 commit 58446fd

4 files changed

Lines changed: 54 additions & 24 deletions

File tree

src/Command/BakeMigrationDiffCommand.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -473,15 +473,7 @@ protected function bakeSnapshot($name, Arguments $args, ConsoleIo $io)
473473
$newArgs = [];
474474
$newArgs[] = $name;
475475

476-
if ($args->getOption('connection')) {
477-
$newArgs[] = '-c';
478-
$newArgs[] = $args->getOption('connection');
479-
}
480-
481-
if ($args->getOption('plugin')) {
482-
$newArgs[] = '-p';
483-
$newArgs[] = $args->getOption('plugin');
484-
}
476+
$newArgs = array_merge($newArgs, $this->parseOptions($args));
485477

486478
$exitCode = $this->executeCommand(BakeMigrationSnapshotCommand::class, $newArgs, $io);
487479

src/Command/BakeSimpleMigrationCommand.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ abstract class BakeSimpleMigrationCommand extends SimpleBakeCommand
4040
*
4141
* @var string
4242
*/
43-
public $pathFragment = 'config/Migrations/';
43+
public $pathFragment = 'config';
44+
45+
public const DEFAULT_MIGRATION_FOLDER = 'Migrations';
4446

4547
/**
4648
* @inheritDoc
@@ -65,9 +67,10 @@ public function fileName($name): string
6567
*/
6668
public function getPath(Arguments $args): string
6769
{
68-
$path = ROOT . DS . $this->pathFragment;
70+
$migrationFolder = $this->pathFragment . DS . $args->getOption('source') . DS;
71+
$path = ROOT . DS . $migrationFolder;
6972
if ($this->plugin) {
70-
$path = $this->_pluginPath($this->plugin) . $this->pathFragment;
73+
$path = $this->_pluginPath($this->plugin) . $migrationFolder;
7174
}
7275

7376
return str_replace('/', DS, $path);
@@ -188,6 +191,10 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar
188191
'short' => 'f',
189192
'boolean' => true,
190193
'help' => 'Force overwriting existing file if a migration already exists with the same name.',
194+
])->addOption('source', [
195+
'short' => 's',
196+
'default' => self::DEFAULT_MIGRATION_FOLDER,
197+
'help' => 'Name of the folder in which the migration should be saved.',
191198
]);
192199

193200
return $parser;

src/Command/SnapshotTrait.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,7 @@ protected function markSnapshotApplied($path, Arguments $args, ConsoleIo $io)
5959
$newArgs[] = $version;
6060
$newArgs[] = '-o';
6161

62-
if ($args->getOption('connection')) {
63-
$newArgs[] = '-c';
64-
$newArgs[] = $args->getOption('connection');
65-
}
66-
67-
if ($args->getOption('plugin')) {
68-
$newArgs[] = '-p';
69-
$newArgs[] = $args->getOption('plugin');
70-
}
62+
$newArgs = array_merge($newArgs, $this->parseOptions($args));
7163

7264
$io->out('Marking the migration ' . $fileName . ' as migrated...');
7365
$this->executeCommand(MigrationsMarkMigratedCommand::class, $newArgs, $io);
@@ -83,8 +75,21 @@ protected function markSnapshotApplied($path, Arguments $args, ConsoleIo $io)
8375
*/
8476
protected function refreshDump(Arguments $args, ConsoleIo $io)
8577
{
86-
$newArgs = [];
78+
$newArgs = $this->parseOptions($args);
79+
80+
$io->out('Creating a dump of the new database state...');
81+
$this->executeCommand(MigrationsDumpCommand::class, $newArgs, $io);
82+
}
8783

84+
/**
85+
* Will parse 'connection', 'plugin' and 'source' options into a new Array
86+
*
87+
* @param \Cake\Console\Arguments $args The command arguments.
88+
* @return array Array containing the short for the option followed by its value
89+
*/
90+
protected function parseOptions(Arguments $args): array
91+
{
92+
$newArgs = [];
8893
if ($args->getOption('connection')) {
8994
$newArgs[] = '-c';
9095
$newArgs[] = $args->getOption('connection');
@@ -95,7 +100,11 @@ protected function refreshDump(Arguments $args, ConsoleIo $io)
95100
$newArgs[] = $args->getOption('plugin');
96101
}
97102

98-
$io->out('Creating a dump of the new database state...');
99-
$this->executeCommand(MigrationsDumpCommand::class, $newArgs, $io);
103+
if ($args->getOption('source')) {
104+
$newArgs[] = '-s';
105+
$newArgs[] = $args->getOption('source');
106+
}
107+
108+
return $newArgs;
100109
}
101110
}

tests/TestCase/Command/BakeMigrationDiffCommandTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,28 @@ public function testEmptyHistoryNoMigrations()
9696
$this->assertExitCode(BaseCommand::CODE_ERROR);
9797
}
9898

99+
/**
100+
* Tests baking a diff in a custom folder source
101+
*
102+
* @return void
103+
*/
104+
public function testBakeMigrationDiffInCustomFolder()
105+
{
106+
$customFolderName = 'CustomMigrationsFolder';
107+
$this->exec('bake migration_diff MigrationDiffForCustomFolder -c test -s ' . $customFolderName);
108+
109+
$path = ROOT . DS . 'config' . DS . $customFolderName . DS;
110+
$this->generatedFiles = glob($path . '*_MigrationDiffForCustomFolder.php');
111+
112+
$this->assertCount(1, $this->generatedFiles);
113+
$this->assertFileExists($path . 'schema-dump-test.lock', 'Cannot test contents, file does not exist.');
114+
$this->generatedFiles[] = $path . 'schema-dump-test.lock';
115+
116+
$fileName = pathinfo($this->generatedFiles[0], PATHINFO_FILENAME);
117+
$this->assertOutputContains('Marking the migration ' . $fileName . ' as migrated...');
118+
$this->assertOutputContains('Creating a dump of the new database state...');
119+
}
120+
99121
/**
100122
* Tests baking a diff
101123
*

0 commit comments

Comments
 (0)