Skip to content

Commit

Permalink
Merge pull request #12 from venyii/fix-validation-lock
Browse files Browse the repository at this point in the history
Prevent locking database if validation error occurred in version command
  • Loading branch information
gruberro authored Nov 20, 2017
2 parents bb2de92 + 02227c6 commit e7a4b90
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/Console/Command/VersionsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O
$migrations = $this->getMigrations($directories);
$db = $this->connect($input->getOption('server'), $input->getArgument('database'));

$this->acquireLock($db);

$add = $input->getOption('add');
$delete = $input->getOption('delete');
$all = $input->getOption('all');
Expand All @@ -108,6 +106,8 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O
throw new Console\Exception\RuntimeException("Specify --all or a single migration id");
}

$this->acquireLock($db);

try {
if ($all) {
$id = null;
Expand Down
30 changes: 30 additions & 0 deletions tests/Console/Command/VersionsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,36 @@ public function testExecuteWithInvalidMigrationDirectory()
);
}

public function testExecuteWithValidationErrorDoesNotLockDatabase()
{
$application = new Application();
$application->add(new MongoDbMigrations\Console\Command\VersionsCommand());

$command = $application->find('php-mongodb-migrations:version');
$commandTester = new CommandTester($command);
$hasValidationException = false;

try {
$commandTester->execute(
[
'command' => $command->getName(),
'database' => $this->getTestDatabaseName(),
]
);
} catch (\RuntimeException $e) {
$hasValidationException = true;
$this->assertSame('Specify --all or a single migration id', $e->getMessage());
}

$this->assertTrue($hasValidationException, 'Expected a validation exception to be thrown');

$databaseMigrationsLockCollection = $this->getTestDatabase()->selectCollection('DATABASE_MIGRATIONS_LOCK');
$this->assertNull(
$databaseMigrationsLockCollection->findOne(['locked' => ['$exists' => true]]),
'The database lock must not be acquired, even if the command fails!'
);
}

public function testExecuteASingleIdOnly()
{
$application = new Application();
Expand Down

0 comments on commit e7a4b90

Please sign in to comment.