Skip to content

Commit 9fad259

Browse files
committed
Change migration folder resolving and allow override
1 parent e69af07 commit 9fad259

File tree

2 files changed

+129
-78
lines changed

2 files changed

+129
-78
lines changed

Neos.Flow/Classes/Command/DoctrineCommandController.php

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -283,22 +283,24 @@ public function dqlCommand(int $depth = 3, string $hydrationMode = 'array', int
283283
* available, executed and pending migrations.
284284
*
285285
* @param boolean $showMigrations Output a list of all migrations and their status
286+
* @param string|null $migrationFolder Provide alternative platform folder name (as in "Mysql"), otherwise configured connection is used.
286287
* @return void
287-
* @throws StopCommandException
288288
* @throws DBALException
289+
* @throws StopCommandException
289290
* @see neos.flow:doctrine:migrate
290291
* @see neos.flow:doctrine:migrationexecute
291292
* @see neos.flow:doctrine:migrationgenerate
292293
* @see neos.flow:doctrine:migrationversion
293294
*/
294-
public function migrationStatusCommand(bool $showMigrations = false): void
295+
public function migrationStatusCommand(bool $showMigrations = false, ?string $migrationFolder = null): void
295296
{
296297
if (!$this->isDatabaseConfigured()) {
297298
$this->outputLine('Doctrine migration status not available, the driver and host backend options are not set in /Configuration/Settings.yaml.');
298299
$this->quit(1);
299300
}
300301

301-
$this->outputLine($this->doctrineService->getFormattedMigrationStatus($showMigrations));
302+
$this->maybeOutputMigrationFolderWarning($migrationFolder);
303+
$this->outputLine($this->doctrineService->getFormattedMigrationStatus($showMigrations, $migrationFolder));
302304
}
303305

304306
/**
@@ -311,14 +313,15 @@ public function migrationStatusCommand(bool $showMigrations = false): void
311313
* @param string|null $output A file to write SQL to, instead of executing it
312314
* @param boolean $dryRun Whether to do a dry run or not
313315
* @param boolean $quiet If set, only the executed migration versions will be output, one per line
316+
* @param string|null $migrationFolder Provide alternative platform folder name (as in "Mysql"), otherwise configured connection is used.
314317
* @return void
315318
* @throws StopCommandException
316319
* @see neos.flow:doctrine:migrationstatus
317320
* @see neos.flow:doctrine:migrationexecute
318321
* @see neos.flow:doctrine:migrationgenerate
319322
* @see neos.flow:doctrine:migrationversion
320323
*/
321-
public function migrateCommand(string $version = 'latest', string $output = null, bool $dryRun = false, bool $quiet = false): void
324+
public function migrateCommand(string $version = 'latest', string $output = null, bool $dryRun = false, bool $quiet = false, ?string $migrationFolder = null): void
322325
{
323326
if (!$this->isDatabaseConfigured()) {
324327
$this->outputLine('Doctrine migration not possible, the driver and host backend options are not set in /Configuration/Settings.yaml.');
@@ -329,8 +332,9 @@ public function migrateCommand(string $version = 'latest', string $output = null
329332
$this->outputLine(sprintf('The path "%s" is not writeable!', dirname($output)));
330333
}
331334

335+
$this->maybeOutputMigrationFolderWarning($migrationFolder);
332336
try {
333-
$result = $this->doctrineService->executeMigrations($this->normalizeVersion($version), $output, $dryRun, $quiet);
337+
$result = $this->doctrineService->executeMigrations($this->normalizeVersion($version), $output, $dryRun, $quiet, $migrationFolder);
334338
if ($result !== '' && $quiet === false) {
335339
$this->outputLine($result);
336340
}
@@ -358,14 +362,15 @@ protected function emitAfterDatabaseMigration(): void
358362
* @param string $direction Whether to execute the migration up (default) or down
359363
* @param string|null $output A file to write SQL to, instead of executing it
360364
* @param boolean $dryRun Whether to do a dry run or not
365+
* @param string|null $migrationFolder Provide alternative platform folder name (as in "Mysql"), otherwise configured connection is used.
361366
* @return void
362367
* @throws StopCommandException
363368
* @see neos.flow:doctrine:migrate
364369
* @see neos.flow:doctrine:migrationstatus
365370
* @see neos.flow:doctrine:migrationgenerate
366371
* @see neos.flow:doctrine:migrationversion
367372
*/
368-
public function migrationExecuteCommand(string $version, string $direction = 'up', string $output = null, bool $dryRun = false): void
373+
public function migrationExecuteCommand(string $version, string $direction = 'up', string $output = null, bool $dryRun = false, ?string $migrationFolder = null): void
369374
{
370375
if (!$this->isDatabaseConfigured()) {
371376
$this->outputLine('Doctrine migration not possible, the driver and host backend options are not set in /Configuration/Settings.yaml.');
@@ -376,8 +381,9 @@ public function migrationExecuteCommand(string $version, string $direction = 'up
376381
$this->outputLine(sprintf('The path "%s" is not writeable!', dirname($output)));
377382
}
378383

384+
$this->maybeOutputMigrationFolderWarning($migrationFolder);
379385
try {
380-
$this->outputLine($this->doctrineService->executeMigration($this->normalizeVersion($version), $direction, $output, $dryRun));
386+
$this->outputLine($this->doctrineService->executeMigration($this->normalizeVersion($version), $direction, $output, $dryRun, $migrationFolder));
381387
} catch (\Exception $exception) {
382388
$this->handleException($exception);
383389
}
@@ -392,15 +398,17 @@ public function migrationExecuteCommand(string $version, string $direction = 'up
392398
* @param string $version The migration to execute
393399
* @param boolean $add The migration to mark as migrated
394400
* @param boolean $delete The migration to mark as not migrated
401+
* @param string|null $migrationFolder Provide alternative platform folder name (as in "Mysql"), otherwise configured connection is used.
395402
* @return void
396-
* @throws StopCommandException
397403
* @throws DBALException
404+
* @throws FilesException
405+
* @throws StopCommandException
398406
* @see neos.flow:doctrine:migrate
399407
* @see neos.flow:doctrine:migrationstatus
400408
* @see neos.flow:doctrine:migrationexecute
401409
* @see neos.flow:doctrine:migrationgenerate
402410
*/
403-
public function migrationVersionCommand(string $version, bool $add = false, bool $delete = false): void
411+
public function migrationVersionCommand(string $version, bool $add = false, bool $delete = false, ?string $migrationFolder = null): void
404412
{
405413
if (!$this->isDatabaseConfigured()) {
406414
$this->outputLine('Doctrine migration not possible, the driver and host backend options are not set in /Configuration/Settings.yaml.');
@@ -411,8 +419,9 @@ public function migrationVersionCommand(string $version, bool $add = false, bool
411419
throw new \InvalidArgumentException('You must specify whether you want to --add or --delete the specified version.');
412420
}
413421

422+
$this->maybeOutputMigrationFolderWarning($migrationFolder);
414423
try {
415-
$this->doctrineService->markAsMigrated($this->normalizeVersion($version), $add ?: false);
424+
$this->doctrineService->markAsMigrated($this->normalizeVersion($version), $add ?: false, $migrationFolder);
416425
} catch (MigrationException $exception) {
417426
$this->outputLine($exception->getMessage());
418427
$this->quit(1);
@@ -441,6 +450,7 @@ public function migrationVersionCommand(string $version, bool $add = false, bool
441450
* @param boolean $diffAgainstCurrent Whether to base the migration on the current schema structure
442451
* @param string|null $filterExpression Only include tables/sequences matching the filter expression regexp
443452
* @param boolean $force Generate migrations even if there are migrations left to execute
453+
* @param string|null $migrationFolder Provide alternative platform folder name (as in "Mysql"), otherwise configured connection is used.
444454
* @return void
445455
* @throws DBALException
446456
* @throws StopCommandException
@@ -450,14 +460,14 @@ public function migrationVersionCommand(string $version, bool $add = false, bool
450460
* @see neos.flow:doctrine:migrationexecute
451461
* @see neos.flow:doctrine:migrationversion
452462
*/
453-
public function migrationGenerateCommand(bool $diffAgainstCurrent = true, string $filterExpression = null, bool $force = false): void
463+
public function migrationGenerateCommand(bool $diffAgainstCurrent = true, string $filterExpression = null, bool $force = false, ?string $migrationFolder = null): void
454464
{
455465
if (!$this->isDatabaseConfigured()) {
456466
$this->outputLine('Doctrine migration generation has been SKIPPED, the driver and host backend options are not set in /Configuration/Settings.yaml.');
457467
$this->quit(1);
458468
}
459469

460-
$migrationStatus = $this->doctrineService->getMigrationStatus();
470+
$migrationStatus = $this->doctrineService->getMigrationStatus($migrationFolder);
461471
if ($force === false && $migrationStatus['new'] !== 0) {
462472
$this->outputLine('There are %d new migrations available. To avoid duplication those should be executed via `doctrine:migrate` before creating additional migrations.', [$migrationStatus['new']]);
463473
$this->quit(1);
@@ -475,7 +485,9 @@ public function migrationGenerateCommand(bool $diffAgainstCurrent = true, string
475485
}
476486
}
477487

478-
[$status, $migrationClassPathAndFilename] = $this->doctrineService->generateMigration($diffAgainstCurrent, $filterExpression);
488+
$this->maybeOutputMigrationFolderWarning($migrationFolder);
489+
490+
[$status, $migrationClassPathAndFilename] = $this->doctrineService->generateMigration($diffAgainstCurrent, $filterExpression, $migrationFolder);
479491

480492
$this->outputLine('<info>%s</info>', [$status]);
481493
$this->outputLine();
@@ -496,18 +508,20 @@ public function migrationGenerateCommand(bool $diffAgainstCurrent = true, string
496508
$selectedPackage = $this->output->select('Do you want to move the migration to one of these packages?', $choices, $choices[0]);
497509
$this->outputLine();
498510

511+
$migrationPlatformFolderPart = $migrationFolder ?? $this->doctrineService->getMigrationFolderName();
512+
499513
if ($selectedPackage !== $choices[0]) {
500514
$selectedPackage = $packages[$selectedPackage];
501515
/** @var Package $selectedPackage */
502-
$targetPathAndFilename = Files::concatenatePaths([$selectedPackage->getPackagePath(), 'Migrations', $this->doctrineService->getDatabasePlatformName(), basename($migrationClassPathAndFilename)]);
516+
$targetPathAndFilename = Files::concatenatePaths([$selectedPackage->getPackagePath(), 'Migrations', $migrationPlatformFolderPart, basename($migrationClassPathAndFilename)]);
503517
Files::createDirectoryRecursively(dirname($targetPathAndFilename));
504518
rename($migrationClassPathAndFilename, $targetPathAndFilename);
505519
$this->outputLine('The migration was moved to: <comment>%s</comment>', [substr($targetPathAndFilename, strlen(FLOW_PATH_ROOT))]);
506520
$this->outputLine();
507521
$this->outputLine('Next Steps:');
508522
} else {
509523
$this->outputLine('Next Steps:');
510-
$this->outputLine(sprintf('- Move <comment>%s</comment> to YourPackage/<comment>Migrations/%s/</comment>', $migrationClassPathAndFilename, $this->doctrineService->getDatabasePlatformName()));
524+
$this->outputLine(sprintf('- Move <comment>%s</comment> to YourPackage/<comment>Migrations/%s/</comment>', $migrationClassPathAndFilename, $migrationPlatformFolderPart));
511525
}
512526
$this->outputLine('- Review and adjust the generated migration.');
513527
$this->outputLine('- (optional) execute the migration using <comment>%s doctrine:migrate</comment>', [$this->getFlowInvocationString()]);
@@ -554,4 +568,11 @@ private function normalizeVersion(string $version): string
554568
}
555569
return sprintf('Neos\Flow\Persistence\Doctrine\Migrations\Version%s', $version);
556570
}
571+
572+
private function maybeOutputMigrationFolderWarning(?string $migrationFolder = null)
573+
{
574+
if ($migrationFolder !== null) {
575+
$this->outputLine('<comment>Migration folder override is in effect, migration files are not searched in folder matching the configured connection but in ...Migrations/%s/</comment>', [$migrationFolder]);
576+
}
577+
}
557578
}

0 commit comments

Comments
 (0)