Skip to content

Commit

Permalink
allow multiple migrations to be passed to the "execute" command
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed Dec 7, 2019
1 parent 3e4b0c8 commit 8b1d5cc
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 19 deletions.
13 changes: 9 additions & 4 deletions lib/Doctrine/Migrations/MigrationPlanCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,18 @@ public function __construct(MigrationRepository $migrationRepository, MetadataSt
$this->metadataStorage = $metadataStorage;
}

public function getPlanForExactVersion(Version $version, string $direction) : MigrationPlanList
/**
* @param Version[] $versions
*/
public function getPlanForVersions(array $versions, string $direction) : MigrationPlanList
{
$migration = $this->migrationRepository->getMigration($version);
$planItems = array_map(function (Version $version) use ($direction) : MigrationPlan {
$migration = $this->migrationRepository->getMigration($version);

$planItem = new MigrationPlan($migration->getVersion(), $migration->getMigration(), $direction);
return new MigrationPlan($migration->getVersion(), $migration->getMigration(), $direction);
}, $versions);

return new MigrationPlanList([$planItem], $direction);
return new MigrationPlanList($planItems, $direction);
}

public function getPlanUntilVersion(?Version $to = null) : MigrationPlanList
Expand Down
14 changes: 9 additions & 5 deletions lib/Doctrine/Migrations/Tools/Console/Command/ExecuteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use function array_map;
use function getcwd;
use function is_string;
use function is_writable;
Expand All @@ -30,8 +31,8 @@ protected function configure() : void
'Execute a single migration version up or down manually.'
)
->addArgument(
'version',
InputArgument::REQUIRED,
'versions',
InputArgument::REQUIRED|InputArgument::IS_ARRAY,
'The version to execute.',
null
)
Expand Down Expand Up @@ -94,14 +95,17 @@ protected function configure() : void

public function execute(InputInterface $input, OutputInterface $output) : ?int
{
$version = $input->getArgument('version');
$versions = $input->getArgument('versions');
$path = $input->getOption('write-sql');
$direction = $input->getOption('down') !== false
? Direction::DOWN
: Direction::UP;

$migrator = $this->getDependencyFactory()->getMigrator();
$plan = $this->getDependencyFactory()->getMigrationPlanCalculator()->getPlanForExactVersion(new Version($version), $direction);
$migrator = $this->getDependencyFactory()->getMigrator();
$planCalculator = $this->getDependencyFactory()->getMigrationPlanCalculator();
$plan = $planCalculator->getPlanForVersions(array_map(static function (string $version) : Version {
return new Version($version);
}, $versions), $direction);

$migratorConfigurationFactory = $this->getDependencyFactory()->getConsoleInputMigratorConfigurationFactory();
$migratorConfiguration = $migratorConfigurationFactory->getMigratorConfiguration($input);
Expand Down
16 changes: 14 additions & 2 deletions tests/Doctrine/Migrations/Tests/MigrationPlanCalculatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function setUp() : void
$this->migrationPlanCalculator = new MigrationPlanCalculator($this->migrationRepository, $this->metadataStorage);
}

public function testPlanForExactVersionWhenNoMigrations() : void
public function testPlanForVersionsWhenNoMigrations() : void
{
$m1 = new AvailableMigration(new Version('A'), $this->abstractMigration);
$m2 = new AvailableMigration(new Version('B'), $this->abstractMigration);
Expand All @@ -57,14 +57,26 @@ public function testPlanForExactVersionWhenNoMigrations() : void
return $migrationList->getMigration($version);
});

$plan = $this->migrationPlanCalculator->getPlanForExactVersion(new Version('C'), Direction::UP);
$plan = $this->migrationPlanCalculator->getPlanForVersions([new Version('C')], Direction::UP);

self::assertCount(1, $plan);
self::assertSame(Direction::UP, $plan->getDirection());
self::assertSame(Direction::UP, $plan->getFirst()->getDirection());
self::assertEquals(new Version('C'), $plan->getFirst()->getVersion());
}

public function testPlanForNoVersions() : void
{
$this->migrationRepository
->expects(self::never())
->method('getMigration');

$plan = $this->migrationPlanCalculator->getPlanForVersions([], Direction::UP);

self::assertCount(0, $plan);
self::assertSame(Direction::UP, $plan->getDirection());
}

/**
* @param string[] $expectedPlan
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class ExecuteCommandTest extends TestCase
/** @var MockObject */
private $queryWriter;

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

/**
* @param mixed $arg
*
Expand All @@ -62,7 +65,7 @@ public function testWriteSql($arg, string $path) : void
->with($path, 'down', ['A']);

$this->executeCommandTester->execute([
'version' => '1',
'versions' => ['1'],
'--down' => true,
'--write-sql' => $arg,
]);
Expand Down Expand Up @@ -97,7 +100,44 @@ public function testExecute() : void
});

$this->executeCommandTester->execute([
'version' => '1',
'versions' => ['1'],
'--down' => true,
]);

self::assertSame(0, $this->executeCommandTester->getStatusCode());
}

public function testExecuteMultiple() : void
{
$migration = $this->createMock(AbstractMigration::class);
$m1 = new AvailableMigration(new Version('1'), $migration);

$expectedMigrations = ['1', '2'];
$i = 0;
$this->migrationRepository
->expects(self::exactly(2))
->method('getMigration')
->willReturnCallback(static function (Version $version) use ($m1, $expectedMigrations, &$i) : AvailableMigration {
self::assertSame($expectedMigrations[$i++], (string) $version);

return $m1;
});

$this->executeCommand->expects(self::once())
->method('canExecute')
->willReturn(true);

$this->migrator
->expects(self::once())
->method('migrate')
->willReturnCallback(static function (MigrationPlanList $planList, MigratorConfiguration $configuration) {
self::assertFalse($configuration->isDryRun());

return ['A'];
});

$this->executeCommandTester->execute([
'versions' => ['1', '2'],
'--down' => true,
]);

Expand All @@ -120,7 +160,7 @@ public function testExecuteCancel() : void
});

$this->executeCommandTester->execute([
'version' => '1',
'versions' => ['1'],
'--down' => true,
]);

Expand All @@ -143,13 +183,13 @@ protected function setUp() : void
$migration = $this->createMock(AbstractMigration::class);
$m1 = new AvailableMigration(new Version('1'), $migration);

$migrationRepository = $this->createMock(MigrationRepository::class);
$migrationRepository
->expects(self::once())
$this->migrationRepository = $this->createMock(MigrationRepository::class);
$this->migrationRepository
->expects(self::atLeast(1))
->method('getMigration')
->willReturn($m1);

$planCalculator = new MigrationPlanCalculator($migrationRepository, $storage);
$planCalculator = new MigrationPlanCalculator($this->migrationRepository, $storage);

$configuration = new Configuration();
$configuration->setMetadataStorageConfiguration(new TableMetadataStorageConfiguration());
Expand All @@ -173,7 +213,7 @@ protected function setUp() : void

$this->dependencyFactory->expects(self::any())
->method('getMigrationRepository')
->willReturn($migrationRepository);
->willReturn($this->migrationRepository);

$this->executeCommand = $this->getMockBuilder(ExecuteCommand::class)
->setConstructorArgs([null, $this->dependencyFactory])
Expand Down

0 comments on commit 8b1d5cc

Please sign in to comment.