Skip to content

Commit

Permalink
[test] upd snapshotter, use storage meta
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed Jun 11, 2018
1 parent 6d07c31 commit 18ea7f7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 49 deletions.
21 changes: 4 additions & 17 deletions Command/MakeCollectionsSnapshotsCommand.php
Expand Up @@ -49,26 +49,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$logger = new ConsoleLogger($output);

$logger->debug('Make snapshots of mongodb collections');
$logger->info('Make collection\s snapshots');

$snapshotter = new Snapshotter($this->client);
$processedCollections = [];
foreach ($this->yadm->getStorages() as $name => $storage) {
/** @var Storage $storage */

$collection = $storage->getCollection();

$collection->getCollectionName();

if (isset($processedCollections[$collection->getCollectionName()])) {
continue;
}

$snapshotter->make($collection, $logger);

$processedCollections[$collection->getCollectionName()] = true;
foreach ($this->yadm->getUniqueStorages() as $storage) {
$snapshotter->make($storage, $logger);
}

$logger->debug('Done');
$logger->info('Done');
}
}
37 changes: 20 additions & 17 deletions Snapshotter.php
@@ -1,8 +1,8 @@
<?php
namespace Makasim\Yadm\Bundle;

use Makasim\Yadm\Storage;
use MongoDB\Client;
use MongoDB\Collection;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

Expand All @@ -21,18 +21,18 @@ public function __construct(Client $client)
$this->client = $client;
}

/**
* @param Collection $collection
* @param LoggerInterface|null $logger
*/
public function make(Collection $collection, LoggerInterface $logger = null)
public function make(Storage $storage, LoggerInterface $logger = null)
{
$logger = $logger ?: new NullLogger();

$collection = $storage->getCollection();

$collectionName = $collection->getCollectionName();
$dbName = $collection->getDatabaseName();
$snapshotDbName = $dbName.'_snapshot';

$this->client->selectCollection($snapshotDbName, $collectionName)->drop();

$logger->debug(sprintf(
'Copy documents from <info>%s.%s</info> to <info>%s.%s</info>',
$dbName,
Expand All @@ -42,24 +42,28 @@ public function make(Collection $collection, LoggerInterface $logger = null)
));

$snapshotCollection = $this->client->selectCollection($snapshotDbName, $collectionName);
$snapshotCollection->drop();
foreach ($collection->find() as $document) {
$snapshotCollection->insertOne($document);
if ($documents = $collection->find()->toArray()) {
$snapshotCollection->insertMany($documents);
}
}

/**
* @param Collection $collection
* @param LoggerInterface|null $logger
*/
public function restore(Collection $collection, LoggerInterface $logger = null)
public function restore(Storage $storage, LoggerInterface $logger = null)
{
$logger = $logger ?: new NullLogger();

$collection = $storage->getCollection();

$collectionName = $collection->getCollectionName();
$dbName = $collection->getDatabaseName();
$snapshotDbName = $dbName.'_snapshot';

$collection->drop();

$this->client->selectDatabase($dbName)->createCollection($collectionName, $storage->getMeta()->getCreateCollectionOptions());
foreach ($storage->getMeta()->getIndexes() as $index) {
$collection->createIndex($index->getKey(), $index->getOptions());
}

$logger->debug(sprintf(
'Copy documents from <info>%s.%s</info> to <info>%s.%s</info>',
$dbName,
Expand All @@ -69,9 +73,8 @@ public function restore(Collection $collection, LoggerInterface $logger = null)
));

$snapshotCollection = $this->client->selectCollection($snapshotDbName, $collectionName);
$collection->drop();
foreach ($snapshotCollection->find() as $document) {
$collection->insertOne($document);
if ($documents = $snapshotCollection->find()->toArray()) {
$collection->insertMany($documents);
}
}
}
31 changes: 16 additions & 15 deletions Test/YadmExtension.php
Expand Up @@ -3,31 +3,32 @@

use Makasim\Yadm\Bundle\Snapshotter;
use Makasim\Yadm\Registry;
use Makasim\Yadm\Storage;
use MongoDB\Client;
use Symfony\Component\HttpKernel\Kernel;

trait YadmExtension
{
protected function restoreSnapshots()
protected function truncateStorages()
{
$snapshotter = new Snapshotter($this->getMongodbClient());

$processedCollections = [];
foreach ($this->getYadmRegistry()->getStorages() as $name => $storage) {
$collection = $storage->getCollection();

$collection->getCollectionName();

if (isset($processedCollections[$collection->getCollectionName()])) {
continue;
}

$snapshotter->restore($collection);
foreach ($this->getYadmRegistry()->getUniqueStorages() as $storage) {
$storage->getCollection()->drop();
}
}

$processedCollections[$collection->getCollectionName()] = true;
protected function restoreStorages()
{
foreach ($this->getYadmRegistry()->getUniqueStorages() as $storage) {
$this->restoreStorage($storage);
}
}

protected function restoreStorage(Storage $storage)
{
$snapshotter = new Snapshotter($this->getMongodbClient());
$snapshotter->restore($storage);
}

protected function getYadmRegistry(): Registry
{
return $this->getKernel()->getContainer()->get('yadm');
Expand Down

0 comments on commit 18ea7f7

Please sign in to comment.