Skip to content

Commit

Permalink
Merge pull request #978 from goetas/already-latest-no-error
Browse files Browse the repository at this point in the history
Display more informative messages for migration aliases
  • Loading branch information
goetas committed May 23, 2020
2 parents ebd2551 + 5712f4c commit af6a764
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 65 deletions.
48 changes: 22 additions & 26 deletions lib/Doctrine/Migrations/Tools/Console/Command/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\Migrations\Tools\Console\Command;

use Doctrine\Migrations\Exception\NoMigrationsFoundWithCriteria;
use Doctrine\Migrations\Exception\NoMigrationsToExecute;
use Doctrine\Migrations\Exception\UnknownMigrationVersion;
use Doctrine\Migrations\Metadata\ExecutedMigrationsList;
use Symfony\Component\Console\Formatter\OutputFormatter;
Expand All @@ -14,10 +15,11 @@
use Symfony\Component\Console\Output\OutputInterface;
use function count;
use function getcwd;
use function in_array;
use function is_string;
use function is_writable;
use function sprintf;
use function substr;
use function strpos;

/**
* The MigrateCommand class is responsible for executing a migration from the current version to another
Expand Down Expand Up @@ -134,10 +136,8 @@ protected function execute(InputInterface $input, OutputInterface $output) : int

try {
$version = $this->getDependencyFactory()->getVersionAliasResolver()->resolveVersionAlias($versionAlias);
} catch (UnknownMigrationVersion|NoMigrationsFoundWithCriteria $e) {
$this->getVersionNameFromAlias($versionAlias);

return 1;
} catch (UnknownMigrationVersion|NoMigrationsToExecute|NoMigrationsFoundWithCriteria $e) {
return $this->errorForAlias($versionAlias, $allowNoMigration);
}

$planCalculator = $this->getDependencyFactory()->getMigrationPlanCalculator();
Expand All @@ -150,16 +150,8 @@ protected function execute(InputInterface $input, OutputInterface $output) : int

$plan = $planCalculator->getPlanUntilVersion($version);

if (count($plan) === 0 && ! $allowNoMigration) {
$this->io->warning('Could not find any migrations to execute.');

return 1;
}

if (count($plan) === 0) {
$this->getVersionNameFromAlias($versionAlias);

return 0;
return $this->errorForAlias($versionAlias, $allowNoMigration);
}

$migrator = $this->getDependencyFactory()->getMigrator();
Expand Down Expand Up @@ -227,29 +219,33 @@ private function checkExecutedUnavailableMigrations(
return true;
}

private function getVersionNameFromAlias(string $versionAlias) : void
private function errorForAlias(string $versionAlias, bool $allowNoMigration) : int
{
if ($versionAlias === 'first') {
$this->io->error('Already at first version.');
if (in_array($versionAlias, ['first', 'next', 'latest'], true) || strpos($versionAlias, 'current') === 0) {
$version = $this->getDependencyFactory()->getVersionAliasResolver()->resolveVersionAlias('current');

return;
}
$message = sprintf(
'The version "%s" couldn\'t be reached, you are at version "%s"',
$versionAlias,
(string) $version
);

if ($versionAlias === 'next' || $versionAlias === 'latest') {
$this->io->error('Already at latest version.');
if ($allowNoMigration) {
$this->io->warning($message);

return;
}
return 0;
}

if (substr($versionAlias, 0, 7) === 'current') {
$this->io->error('The delta couldn\'t be reached.');
$this->io->error($message);

return;
return 1;
}

$this->io->error(sprintf(
'Unknown version: %s',
OutputFormatter::escape($versionAlias)
));

return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,6 @@ private function getFormattedVersionAlias(string $alias, ExecutedMigrationsList
}
}

if ($alias === 'latest' && $version!== null && $executedMigrations->hasMigration($version)) {
return 'Already at latest version';
}

// Before first version "virtual" version number
if ((string) $version === '0') {
return '<comment>0</comment>';
Expand Down
7 changes: 6 additions & 1 deletion lib/Doctrine/Migrations/Version/DefaultAliasResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public function __construct(
* - latest: The latest available version.
*
* If an existing version number is specified, it is returned verbatimly.
*
* @throws NoMigrationsToExecute
* @throws UnknownMigrationVersion
* @throws NoMigrationsFoundWithCriteria
*/
public function resolveVersionAlias(string $alias) : Version
{
Expand All @@ -62,6 +66,7 @@ public function resolveVersionAlias(string $alias) : Version

switch ($alias) {
case self::ALIAS_FIRST:
case '0':
return new Version('0');
case self::ALIAS_CURRENT:
try {
Expand Down Expand Up @@ -93,7 +98,7 @@ public function resolveVersionAlias(string $alias) : Version
try {
return $availableMigrations->getLast()->getVersion();
} catch (NoMigrationsFoundWithCriteria $e) {
throw NoMigrationsToExecute::new($e);
return $this->resolveVersionAlias(self::ALIAS_CURRENT);
}

// no break because of return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Tester\CommandTester;
use function getcwd;
use function sprintf;
use function strpos;
use function trim;

Expand Down Expand Up @@ -71,7 +72,7 @@ class MigrateCommandTest extends MigrationTestCase
/** @var TableMetadataStorageConfiguration */
private $metadataConfiguration;

public function testExecuteEmptyMigrationPlanCausesException() : void
public function testTargetUnknownVersion() : void
{
$result = new ExecutionResult(new Version('A'));
$this->storage->complete($result);
Expand All @@ -81,53 +82,55 @@ public function testExecuteEmptyMigrationPlanCausesException() : void
['interactive' => false]
);

self::assertTrue(strpos($this->migrateCommandTester->getDisplay(true), 'Could not find any migrations to execute') !== false);
self::assertStringContainsString('[ERROR] Unknown version: A', $this->migrateCommandTester->getDisplay(true));
self::assertSame(1, $this->migrateCommandTester->getStatusCode());
}

public function testExecuteAlreadyAtFirstVersion() : void
/**
* @return array<array<bool|string|null>>
*/
public function getTargetAliases() : array
{
$this->migrateCommandTester->execute(
[
'version' => 'first',
'--allow-no-migration' => true,
],
['interactive' => false]
);

self::assertTrue(strpos($this->migrateCommandTester->getDisplay(true), 'Already at first version.') !== false);
self::assertSame(0, $this->migrateCommandTester->getStatusCode());
return [
['latest', true, 'A'],
['latest', false, 'A'],
['first', true, null],
['first', false, null],
['next', true, 'A'],
['next', false, 'A'],
['current+1', false, 'A'],
['current+1', true, 'A'],
];
}

public function testExecuteAlreadyAtLatestVersion() : void
/**
* @dataProvider getTargetAliases
*/
public function testExecuteAtVersion(string $targetAlias, bool $allowNoMigration, ?string $executedMigration) : void
{
$result = new ExecutionResult(new Version('A'));
$this->storage->complete($result);
if ($executedMigration !== null) {
$result = new ExecutionResult(new Version($executedMigration));
$this->storage->complete($result);
}

$this->migrateCommandTester->execute(
[
'version' => 'latest',
'--allow-no-migration' => true,
'version' => $targetAlias,
'--allow-no-migration' => $allowNoMigration,
],
['interactive' => false]
);

self::assertTrue(strpos($this->migrateCommandTester->getDisplay(true), 'Already at latest version.') !== false);
self::assertSame(0, $this->migrateCommandTester->getStatusCode());
}

public function testExecuteTheDeltaCouldNotBeReached() : void
{
$result = new ExecutionResult(new Version('A'));
$this->storage->complete($result);

$this->migrateCommandTester->execute(
['version' => 'current+1'],
['interactive' => false]
self::assertStringContainsString(
trim($this->migrateCommandTester->getDisplay(true)),
sprintf(
'[%s] The version "%s" couldn\'t be reached, you are at version "%s"',
($allowNoMigration ? 'WARNING' : 'ERROR'),
$targetAlias,
($executedMigration ?? '0')
)
);

self::assertTrue(strpos($this->migrateCommandTester->getDisplay(true), 'The delta couldn\'t be reached.') !== false);
self::assertSame(1, $this->migrateCommandTester->getStatusCode());
self::assertSame($allowNoMigration ? 0 : 1, $this->migrateCommandTester->getStatusCode());
}

public function testExecuteUnknownVersion() : void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function testExecute() : void
'| Versions | Previous | 1230 |',
'| | Current | 1233 |',
'| | Next | Already at latest version |',
'| | Latest | |',
'| | Latest | 1233 |',
'|----------------------------------------------------------------------------------------------------------------------|',
'| Migrations | Executed | 2 |',
'| | Executed Unavailable | 2 |',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public function getAliases() : array
['current-1', 'A'],
['current+1', 'C'],
['B', 'B'],
['0', '0'],
['X', null, UnknownMigrationVersion::class],
];
}
Expand Down

0 comments on commit af6a764

Please sign in to comment.