Skip to content

Commit

Permalink
Merge pull request #1161 from grachevko/all-or-nothing
Browse files Browse the repository at this point in the history
Bugfix: all-or-nothing from configuration must not be ignored
  • Loading branch information
greg0ire committed Jun 10, 2021
2 parents c925cd5 + 6cf7769 commit 8d0c586
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/continuous-integration.yml
Expand Up @@ -50,6 +50,8 @@ jobs:
coverage: "pcov"
extensions: "pdo_sqlite"
ini-values: "zend.assertions=1"
# Remove this line when a fix for is available https://github.com/box-project/box/issues/555
tools: "composer:v2.0.14"

- name: "Download box"
run: "./download-box.sh"
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/Migrations/Provider/OrmSchemaProvider.php
Expand Up @@ -35,7 +35,7 @@ public function __construct(EntityManagerInterface $em)
*/
public function createSchema(): Schema
{
/** @var array<int, ClassMetadata> $metadata */
/** @var array<int, ClassMetadata<object>> $metadata */
$metadata = $this->entityManager->getMetadataFactory()->getAllMetadata();

if (count($metadata) === 0) {
Expand Down
Expand Up @@ -73,8 +73,7 @@ protected function configure(): void
'all-or-nothing',
null,
InputOption::VALUE_OPTIONAL,
'Wrap the entire migration in a transaction.',
false
'Wrap the entire migration in a transaction.'
)
->setHelp(<<<EOT
The <info>%command.name%</info> command executes a migration to a specified version or the latest available version:
Expand Down
Expand Up @@ -22,7 +22,8 @@ public function getMigratorConfiguration(InputInterface $input): MigratorConfigu
{
$timeAllQueries = $input->hasOption('query-time') ? (bool) $input->getOption('query-time') : false;
$dryRun = $input->hasOption('dry-run') ? (bool) $input->getOption('dry-run') : false;
$allOrNothing = $input->hasOption('all-or-nothing') ? (bool) $input->getOption('all-or-nothing') : $this->configuration->isAllOrNothing();
$allOrNothing = $input->hasOption('all-or-nothing') ? $input->getOption('all-or-nothing') : null;
$allOrNothing = (bool) ($allOrNothing ?? $this->configuration->isAllOrNothing());

return (new MigratorConfiguration())
->setDryRun($dryRun)
Expand Down
Expand Up @@ -12,7 +12,7 @@
class ClassMetadataFactory extends BaseMetadataFactoryAlias
{
/**
* @return ClassMetadata[]
* @psalm-return list<ClassMetadata<object>>
*/
public function getAllMetadata(): array
{
Expand Down
Expand Up @@ -31,6 +31,7 @@
use Doctrine\Migrations\Version\ExecutionResult;
use Doctrine\Migrations\Version\MigrationFactory;
use Doctrine\Migrations\Version\Version;
use Generator;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\QuestionHelper;
Expand Down Expand Up @@ -371,28 +372,46 @@ public function testExecuteMigrateDown(): void
self::assertStringContainsString('[notice] Migrating down to A', trim($this->migrateCommandTester->getDisplay(true)));
}

public function testExecuteMigrateAllOrNothing(): void
/**
* @dataProvider allOrNothing
* @psalm-param array<string, bool> $input
*/
public function testExecuteMigrateAllOrNothing(bool $default, array $input, bool $expected): void
{
$migrator = $this->createMock(DbalMigrator::class);
$this->dependencyFactory->setService(Migrator::class, $migrator);
$this->configuration->setAllOrNothing($default);

$migrator->expects(self::once())
->method('migrate')
->willReturnCallback(static function (MigrationPlanList $planList, MigratorConfiguration $configuration): array {
self::assertTrue($configuration->isAllOrNothing());
->willReturnCallback(static function (MigrationPlanList $planList, MigratorConfiguration $configuration) use ($expected): array {
self::assertSame($expected, $configuration->isAllOrNothing());
self::assertCount(1, $planList);

return ['A'];
});

$this->migrateCommandTester->execute(
['--all-or-nothing' => true],
$input,
['interactive' => false]
);

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

/**
* @psalm-return Generator<array{bool, array<string, bool>, bool}>
*/
public function allOrNothing(): Generator
{
yield [false, ['--all-or-nothing' => false], false];
yield [false, ['--all-or-nothing' => true], true];
yield [true, ['--all-or-nothing' => false], false];

yield [true, [], true];
yield [false, [], false];
}

public function testExecuteMigrateCancelExecutedUnavailableMigrations(): void
{
$result = new ExecutionResult(new Version('345'));
Expand Down

0 comments on commit 8d0c586

Please sign in to comment.