Skip to content

Commit

Permalink
Apply workaround to maintain expected behavior
Browse files Browse the repository at this point in the history
This test case addition simulates what happens when the
`--all-or-nothing` option is indicated, but no explicit
value is passed.

We'll set a default option, which is retrieved in case the option
has not been provided, allowing us to override the value in case the
option is different than the default value.
  • Loading branch information
agustingomes authored and greg0ire committed Jan 11, 2023
1 parent 2c6ee5f commit 4da00c5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ protected function configure(): void
'all-or-nothing',
null,
InputOption::VALUE_OPTIONAL,
'Wrap the entire migration in a transaction.'
'Wrap the entire migration in a transaction.',
'notprovided'
)
->setHelp(<<<EOT
The <info>%command.name%</info> command executes a migration to a specified version or the latest available version:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,26 @@ 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') ? $input->getOption('all-or-nothing') : null;
$allOrNothing = (bool) ($allOrNothing ?? $this->configuration->isAllOrNothing());
$allOrNothing = $this->determineAllOrNothingValueFrom($input) ?? $this->configuration->isAllOrNothing();

return (new MigratorConfiguration())
->setDryRun($dryRun)
->setTimeAllQueries($timeAllQueries)
->setAllOrNothing($allOrNothing);
}

private function determineAllOrNothingValueFrom(InputInterface $input): ?bool
{
$allOrNothingOption = null;

if ($input->hasOption('all-or-nothing')) {
$allOrNothingOption = $input->getOption('all-or-nothing');
}

if ($allOrNothingOption === 'notprovided') {
return null;
}

return (bool) ($allOrNothingOption ?? true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public function testExecuteMigrateDown(): void
}

/**
* @psalm-param array<string, bool> $input
* @psalm-param array<string, bool|int|string|null> $input
*
* @dataProvider allOrNothing
*/
Expand Down Expand Up @@ -390,13 +390,22 @@ public function testExecuteMigrateAllOrNothing(bool $default, array $input, bool
}

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

yield [false, ['--all-or-nothing' => true], true];
yield [false, ['--all-or-nothing' => 1], true];
yield [false, ['--all-or-nothing' => '1'], true];
yield [false, ['--all-or-nothing' => null], true];

yield [true, ['--all-or-nothing' => false], false];
yield [true, ['--all-or-nothing' => 0], false];
yield [true, ['--all-or-nothing' => '0'], false];

yield [true, [], true];
yield [false, [], false];
Expand Down

0 comments on commit 4da00c5

Please sign in to comment.