Skip to content

Commit

Permalink
Merge pull request #827 from doctrine/display-descriptions-while-migr…
Browse files Browse the repository at this point in the history
…ating

Display descriptions while migrating
  • Loading branch information
jwage committed Jun 6, 2019
2 parents 635a85a + acc550c commit ebe6f89
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 43 deletions.
22 changes: 17 additions & 5 deletions lib/Doctrine/Migrations/Version/Executor.php
Expand Up @@ -197,11 +197,7 @@ private function executeMigration(

$migration->{'pre' . ucfirst($direction)}($fromSchema);

if ($direction === Direction::UP) {
$this->outputWriter->write("\n" . sprintf(' <info>++</info> migrating <comment>%s</comment>', $version->getVersion()) . "\n");
} else {
$this->outputWriter->write("\n" . sprintf(' <info>--</info> reverting <comment>%s</comment>', $version->getVersion()) . "\n");
}
$this->outputWriter->write("\n" . $this->getMigrationHeader($version, $migration, $direction) . "\n");

$version->setState(State::EXEC);

Expand Down Expand Up @@ -274,6 +270,22 @@ private function executeMigration(
return $versionExecutionResult;
}

private function getMigrationHeader(Version $version, AbstractMigration $migration, string $direction) : string
{
$versionInfo = $version->getVersion();
$description = $migration->getDescription();

if ($description !== '') {
$versionInfo .= ' (' . $description . ')';
}

if ($direction === Direction::UP) {
return sprintf(' <info>++</info> migrating <comment>%s</comment>', $versionInfo);
}

return sprintf(' <info>--</info> reverting <comment>%s</comment>', $versionInfo);
}

private function skipMigration(
SkipMigration $e,
Version $version,
Expand Down
112 changes: 74 additions & 38 deletions tests/Doctrine/Migrations/Tests/Version/ExecutorTest.php
Expand Up @@ -62,29 +62,6 @@ public function testAddSql() : void

public function testExecuteUp() : void
{
$platform = $this->createMock(AbstractPlatform::class);

$this->connection->expects(self::once())
->method('getDatabasePlatform')
->willReturn($platform);

$stopwatchEvent = $this->createMock(StopwatchEvent::class);

$this->stopwatch->expects(self::any())
->method('start')
->willReturn($stopwatchEvent);

$stopwatchEvent->expects(self::any())
->method('stop');

$stopwatchEvent->expects(self::any())
->method('getDuration')
->willReturn(100);

$stopwatchEvent->expects(self::any())
->method('getMemory')
->willReturn(100);

$this->outputWriter->expects(self::at(0))
->method('write')
->with("\n <info>++</info> migrating <comment>001</comment>\n");
Expand Down Expand Up @@ -130,25 +107,28 @@ public function testExecuteUp() : void
self::assertFalse($this->migration->postDownExecuted);
}

public function testExecuteDown() : void
/**
* @test
*/
public function executeUpShouldAppendDescriptionWhenItIsNotEmpty() : void
{
$stopwatchEvent = $this->createMock(StopwatchEvent::class);

$this->stopwatch->expects(self::any())
->method('start')
->willReturn($stopwatchEvent);

$stopwatchEvent->expects(self::any())
->method('stop');
$this->outputWriter->expects(self::at(0))
->method('write')
->with("\n <info>++</info> migrating <comment>001 (testing)</comment>\n");

$stopwatchEvent->expects(self::any())
->method('getDuration')
->willReturn(100);
$migratorConfiguration = (new MigratorConfiguration())
->setTimeAllQueries(true);

$stopwatchEvent->expects(self::any())
->method('getMemory')
->willReturn(100);
$this->versionExecutor->execute(
$this->version,
new VersionExecutorTestMigration($this->version, 'testing'),
Direction::UP,
$migratorConfiguration
);
}

public function testExecuteDown() : void
{
$this->outputWriter->expects(self::at(0))
->method('write')
->with("\n <info>--</info> reverting <comment>001</comment>\n");
Expand Down Expand Up @@ -194,6 +174,26 @@ public function testExecuteDown() : void
self::assertTrue($this->migration->postDownExecuted);
}

/**
* @test
*/
public function executeDownShouldAppendDescriptionWhenItIsNotEmpty() : void
{
$this->outputWriter->expects(self::at(0))
->method('write')
->with("\n <info>--</info> reverting <comment>001 (testing)</comment>\n");

$migratorConfiguration = (new MigratorConfiguration())
->setTimeAllQueries(true);

$this->versionExecutor->execute(
$this->version,
new VersionExecutorTestMigration($this->version, 'testing'),
Direction::DOWN,
$migratorConfiguration
);
}

protected function setUp() : void
{
$this->configuration = $this->createMock(Configuration::class);
Expand All @@ -216,6 +216,10 @@ protected function setUp() : void
->method('getConnection')
->willReturn($this->connection);

$this->connection->expects(self::any())
->method('getDatabasePlatform')
->willReturn($this->createMock(AbstractPlatform::class));

$this->version = new Version(
$this->configuration,
'001',
Expand All @@ -224,6 +228,23 @@ protected function setUp() : void
);

$this->migration = new VersionExecutorTestMigration($this->version);

$stopwatchEvent = $this->createMock(StopwatchEvent::class);

$this->stopwatch->expects(self::any())
->method('start')
->willReturn($stopwatchEvent);

$stopwatchEvent->expects(self::any())
->method('stop');

$stopwatchEvent->expects(self::any())
->method('getDuration')
->willReturn(100);

$stopwatchEvent->expects(self::any())
->method('getMemory')
->willReturn(100);
}
}

Expand All @@ -241,6 +262,21 @@ class VersionExecutorTestMigration extends AbstractMigration
/** @var bool */
public $postDownExecuted = false;

/** @var string */
private $description;

public function __construct(Version $version, string $description = '')
{
parent::__construct($version);

$this->description = $description;
}

public function getDescription() : string
{
return $this->description;
}

public function preUp(Schema $fromSchema) : void
{
$this->preUpExecuted = true;
Expand Down

0 comments on commit ebe6f89

Please sign in to comment.