Skip to content

Commit

Permalink
Add possibility to remove all snapshots by aggregate type
Browse files Browse the repository at this point in the history
  • Loading branch information
prolic committed Mar 3, 2017
1 parent 87aa3a8 commit d28cbd6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
22 changes: 18 additions & 4 deletions src/MongoDbSnapshotStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,29 @@ public function save(Snapshot ...$snapshots): void
}
}

public function removeAll(string $aggregateType): void
{
$bucket = $this->client->selectDatabase($this->dbName)->selectGridFSBucket([
'bucketName' => $this->getGridFsName($aggregateType),
'readConcern' => $this->readConcern,
]);

$snapshots = $bucket->find([
'metadata.aggregate_type' => $aggregateType,
]);

foreach ($snapshots as $snapshot) {
$bucket->delete($snapshot->_id);
}
}

private function getGridFsName(string $aggregateType): string
{
if (isset($this->snapshotGridFsMap[$aggregateType])) {
$gridFsName = $this->snapshotGridFsMap[$aggregateType];
} else {
$gridFsName = $this->defaultSnapshotGridFsName;
return $gridFsName = $this->snapshotGridFsMap[$aggregateType];
}

return $gridFsName;
return $this->defaultSnapshotGridFsName;
}

/**
Expand Down
14 changes: 12 additions & 2 deletions tests/MongoDbSnapshotStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function it_saves_and_reads()
/**
* @test
*/
public function it_saves_multiple_snapshots()
public function it_saves_multiple_snapshots_and_removes_them()
{
$aggregateRoot = ['name' => 'Sascha'];
$aggregateType = 'user';
Expand All @@ -87,13 +87,23 @@ public function it_saves_multiple_snapshots()

$snapshot2 = new Snapshot($aggregateType, 'id2', $aggregateRoot, 2, $now);

$this->snapshotStore->save($snapshot1, $snapshot2);
$snapshot3 = new Snapshot('blog', 'id3', $aggregateRoot, 1, $now);

$this->snapshotStore->save($snapshot1, $snapshot2, $snapshot3);

$readSnapshot = $this->snapshotStore->get($aggregateType, 'id1');
$this->assertEquals($snapshot1, $readSnapshot);

$readSnapshot = $this->snapshotStore->get($aggregateType, 'id2');
$this->assertEquals($snapshot2, $readSnapshot);

$this->snapshotStore->removeAll($aggregateType);

$this->assertNull($this->snapshotStore->get($aggregateType, 'id1'));
$this->assertNull($this->snapshotStore->get($aggregateType, 'id2'));

$readSnapshot = $this->snapshotStore->get('blog', 'id3');
$this->assertEquals($snapshot3, $readSnapshot);
}

/**
Expand Down

0 comments on commit d28cbd6

Please sign in to comment.