Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix message on which migrations are going to be executed or migrated #962

Merged
merged 1 commit into from
Apr 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions lib/Doctrine/Migrations/DbalMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,6 @@ public function migrate(MigrationPlanList $migrationsPlan, MigratorConfiguration
return [];
}

$dryRun = $migratorConfiguration->isDryRun();
$this->logger->notice(
($dryRun ? 'Executing dry run of migration' : 'Migrating') . ' {direction} to {to}',
[
'direction' => $migrationsPlan->getDirection(),
'to' => (string) $migrationsPlan->getLast()->getVersion(),
]
);

$stopwatchEvent = $this->stopwatch->start('migrate');

$sql = $this->executeMigrations($migrationsPlan, $migratorConfiguration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use function array_map;
use function getcwd;
use function implode;
use function is_string;
use function is_writable;

Expand Down Expand Up @@ -142,6 +143,14 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
}

$this->getDependencyFactory()->getMetadataStorage()->ensureInitialized();

$this->getDependencyFactory()->getLogger()->notice(
'Executing' . ($migratorConfiguration->isDryRun() ? ' (dry-run)' : '') . ' {versions} {direction}',
[
'direction' => $plan->getDirection(),
'versions' => implode(', ', $versions),
]
);
$migrator->migrate($plan, $migratorConfiguration);

return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
}

$this->getDependencyFactory()->getMetadataStorage()->ensureInitialized();

$this->getDependencyFactory()->getLogger()->notice(
'Migrating' . ($migratorConfiguration->isDryRun() ? ' (dry-run)' : '') . ' {direction} to {to}',
[
'direction' => $plan->getDirection(),
'to' => (string) $version,
]
);

$migrator->migrate($plan, $migratorConfiguration);

return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,34 @@

use Doctrine\Migrations\AbstractMigration;
use Doctrine\Migrations\Configuration\Configuration;
use Doctrine\Migrations\Configuration\Connection\ExistingConnection;
use Doctrine\Migrations\Configuration\Migration\ExistingConfiguration;
use Doctrine\Migrations\DependencyFactory;
use Doctrine\Migrations\Metadata\MigrationPlan;
use Doctrine\Migrations\Metadata\MigrationPlanList;
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
use Doctrine\Migrations\Migrator;
use Doctrine\Migrations\MigratorConfiguration;
use Doctrine\Migrations\QueryWriter;
use Doctrine\Migrations\Tests\MigrationTestCase;
use Doctrine\Migrations\Tools\Console\Command\ExecuteCommand;
use Doctrine\Migrations\Version\Direction;
use Doctrine\Migrations\Version\MigrationPlanCalculator;
use Doctrine\Migrations\Version\Version;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Tester\CommandTester;
use function getcwd;
use function sys_get_temp_dir;
use function trim;

class ExecuteCommandTest extends TestCase
class ExecuteCommandTest extends MigrationTestCase
{
/** @var ExecuteCommand */
private $executeCommand;

/** @var MockObject|DependencyFactory */
/** @var DependencyFactory */
private $dependencyFactory;

/** @var CommandTester */
Expand Down Expand Up @@ -109,6 +112,7 @@ public function testExecute() : void
]);

self::assertSame(0, $this->executeCommandTester->getStatusCode());
self::assertSame('[notice] Executing 1 up', trim($this->executeCommandTester->getDisplay(true)));
}

public function testExecuteMultiple() : void
Expand Down Expand Up @@ -142,6 +146,7 @@ public function testExecuteMultiple() : void
]);

self::assertSame(0, $this->executeCommandTester->getStatusCode());
self::assertSame('[notice] Executing 1, 2 up', trim($this->executeCommandTester->getDisplay(true)));
}

public function testExecuteCancel() : void
Expand Down Expand Up @@ -169,16 +174,11 @@ public function testExecuteCancel() : void

protected function setUp() : void
{
$this->dependencyFactory = $this->getMockBuilder(DependencyFactory::class)
->disableOriginalConstructor()
->setMethodsExcept(['getConsoleInputMigratorConfigurationFactory'])
->getMock();

$this->migrator = $this->createMock(Migrator::class);
$connection = $this->getSqliteConnection();

$this->migrator = $this->createMock(Migrator::class);
$this->queryWriter = $this->createMock(QueryWriter::class);

$migration = $this->createMock(AbstractMigration::class);
$migration = $this->createMock(AbstractMigration::class);

$p1 = new MigrationPlan(new Version('1'), $migration, Direction::UP);
$pl = new MigrationPlanList([$p1], Direction::UP);
Expand All @@ -193,21 +193,11 @@ protected function setUp() : void
$configuration->setMetadataStorageConfiguration(new TableMetadataStorageConfiguration());
$configuration->addMigrationsDirectory('DoctrineMigrations', sys_get_temp_dir());

$this->dependencyFactory->expects(self::any())
->method('getConfiguration')
->willReturn($configuration);

$this->dependencyFactory->expects(self::once())
->method('getMigrator')
->willReturn($this->migrator);

$this->dependencyFactory->expects(self::once())
->method('getMigrationPlanCalculator')
->willReturn($this->planCalculator);
$this->dependencyFactory = DependencyFactory::fromConnection(new ExistingConfiguration($configuration), new ExistingConnection($connection));

$this->dependencyFactory->expects(self::any())
->method('getQueryWriter')
->willReturn($this->queryWriter);
$this->dependencyFactory->setService(Migrator::class, $this->migrator);
$this->dependencyFactory->setService(MigrationPlanCalculator::class, $this->planCalculator);
$this->dependencyFactory->setService(QueryWriter::class, $this->queryWriter);

$this->executeCommand = new ExecuteCommand($this->dependencyFactory);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Doctrine\Migrations\DependencyFactory;
use Doctrine\Migrations\Metadata\MigrationPlanList;
use Doctrine\Migrations\Metadata\Storage\MetadataStorage;
use Doctrine\Migrations\MigrationRepository;
use Doctrine\Migrations\Migrator;
use Doctrine\Migrations\MigratorConfiguration;
use Doctrine\Migrations\QueryWriter;
Expand All @@ -28,6 +29,7 @@
use function getcwd;
use function strpos;
use function sys_get_temp_dir;
use function trim;

class MigrateCommandTest extends MigrationTestCase
{
Expand All @@ -52,6 +54,9 @@ class MigrateCommandTest extends MigrationTestCase
/** @var MockObject|QuestionHelper */
private $questions;

/** @var MigrationRepository */
private $migrationRepository;

public function testExecuteEmptyMigrationPlanCausesException() : void
{
$result = new ExecutionResult(new Version('A'));
Expand Down Expand Up @@ -206,6 +211,40 @@ public function testExecuteMigrate() : void
$this->migrateCommandTester->execute([]);

self::assertSame(0, $this->migrateCommandTester->getStatusCode());
self::assertSame('[notice] Migrating up to A', trim($this->migrateCommandTester->getDisplay(true)));
}

public function testExecuteMigrateDown() : void
{
$migration = $this->createMock(AbstractMigration::class);
Helper::registerMigrationInstance($this->migrationRepository, new Version('B'), $migration);

$result = new ExecutionResult(new Version('A'));
$this->storage->complete($result);

$result = new ExecutionResult(new Version('B'));
$this->storage->complete($result);

$migrator = $this->createMock(DbalMigrator::class);
$this->dependencyFactory->setService(Migrator::class, $migrator);

$this->questions->expects(self::once())
->method('ask')
->willReturn(true);

$migrator->expects(self::once())
->method('migrate')
->willReturnCallback(static function (MigrationPlanList $planList, MigratorConfiguration $configuration) : array {
self::assertCount(1, $planList);
self::assertEquals(new Version('B'), $planList->getFirst()->getVersion());

return ['A'];
});

$this->migrateCommandTester->execute(['version' => 'prev']);

self::assertSame(0, $this->migrateCommandTester->getStatusCode());
self::assertSame('[notice] Migrating down to A', trim($this->migrateCommandTester->getDisplay(true)));
}

public function testExecuteMigrateAllOrNothing() : void
Expand Down Expand Up @@ -298,8 +337,8 @@ protected function setUp() : void

$migration = $this->createMock(AbstractMigration::class);

$migrationRepository = $this->dependencyFactory->getMigrationRepository();
Helper::registerMigrationInstance($migrationRepository, new Version('A'), $migration);
$this->migrationRepository = $this->dependencyFactory->getMigrationRepository();
Helper::registerMigrationInstance($this->migrationRepository, new Version('A'), $migration);

$this->migrateCommand = new MigrateCommand($this->dependencyFactory);

Expand Down