Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Commit

Permalink
Fix #23. Pending migrations count is now executed against the diff of…
Browse files Browse the repository at this point in the history
… versions instead of against separate repository and storage counts, to avoid scenarios where the repository and storage are out of sync but still have the same number of items.
  • Loading branch information
gsomoza committed Oct 9, 2015
1 parent 1672648 commit b35d977
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/CommandBus/Config/StatusHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ public function handle(StatusMessage $message)
'Nothing has been migrated yet.';
$output->writeln($currentMessage);

$pendingCount = $availableMigrations->count() - $migratedVersions->count();
$diff = array_diff($availableMigrations->toArray(), $migratedVersions->toArray());

$pendingCount = count($diff);

if ($pendingCount > 0) {
$diff = array_diff($availableMigrations->toArray(), $migratedVersions->toArray());
$executable = defined('MIGRATIONS_EXECUTABLE') ? MIGRATIONS_EXECUTABLE . ' ' : '';
$output->writeln([
"Your database is out-of-date by $pendingCount versions, and can be migrated.",
Expand Down
35 changes: 31 additions & 4 deletions test/CommandBus/Config/StatusHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,14 @@ public function splitDiffProvider()

/**
* testHandle
*
* @param LinkedVersions $available
* @param MigratedVersions $migrated
* @param $pendingCount
*
* @dataProvider handleProvider
*/
public function testHandle(LinkedVersions $available, MigratedVersions $migrated)
public function testHandle(LinkedVersions $available, MigratedVersions $migrated, $pendingCount)
{
$this->repository->shouldReceive('fetchAll')->once()->andReturn($available);
$this->storage->shouldReceive('fetchAll')->once()->andReturn($migrated);
Expand All @@ -197,7 +200,6 @@ public function testHandle(LinkedVersions $available, MigratedVersions $migrated
'/[Nn]othing has been migrated/' :
'/[Cc]urrent version.*?' . $migrated->last()->getId() . '.*?$/';
$this->output->shouldReceive('writeln')->once()->with($currentMsg);
$pendingCount = $available->count() - $migrated->count();
if ($pendingCount > 0) {
$this->output->shouldReceive('writeln')->with(m::on(function($messages) use ($pendingCount) {
return preg_match("/out\\-of\\-date.*?by $pendingCount versions/", $messages[0])
Expand Down Expand Up @@ -231,9 +233,12 @@ public function testHandle(LinkedVersions $available, MigratedVersions $migrated
*/
public function handleProvider()
{
// Calculate combinations of different repository and storage states.
// All test-cases here should assume sequential execution of migrations, so that we can easily calculate
// the number of pending migrations with the foreach loop below (see comment below).
$repVersions = [
[],
Version::fromArray(range(1,10))
Version::fromArray(range(1,10)),
];
$repositories = [];
foreach ($repVersions as $versions) {
Expand All @@ -249,7 +254,29 @@ public function handleProvider()
$this->linkVersions($versions, true);
$storages[] = new MigratedVersions($versions);
}
return $this->combinations([$repositories, $storages]);

$combinations = $this->combinations([$repositories, $storages]);

// calculate pending number of migrations and set that as the third parameter. See note in function header.
foreach($combinations as &$combination) { // NB: addressing by reference!
$pending = $combination[0]->count() - $combination[1]->count();
$combination[] = $pending;
}

// Additional "special" use-cases below. Pending count (third parameter) should be set manually.

// Test case for https://github.com/baleen/cli/issues/23
$repositoryVersions23 = [new V(1), new V(3)];
$this->linkVersions($repositoryVersions23);
$storageVersions23 = [new V(1), new V(2)];
$this->linkVersions($storageVersions23, true);
$combinations[] = [
new LinkedVersions($repositoryVersions23),
new MigratedVersions($storageVersions23),
1 // one migration pending: v3
];

return $combinations;
}

/**
Expand Down

0 comments on commit b35d977

Please sign in to comment.