Skip to content

Commit

Permalink
Merge pull request #6 from makasim/makasim-patch-1
Browse files Browse the repository at this point in the history
Update SchemaUpdateCommand.php
  • Loading branch information
makasim authored Dec 29, 2018
2 parents c437164 + d4e2dc2 commit e2995d0
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions Command/SchemaUpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Makasim\Yadm\Storage;
use MongoDB\Client;
use MongoDB\Database;
use MongoDB\Driver\Exception\CommandException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -46,6 +47,8 @@ protected function configure()
->setName(static::$defaultName)
->setDescription('Update database schema.')
->addOption('drop', null, InputOption::VALUE_NONE, 'Setup command loads fixtures if option set.')
->addOption('no-create-collections', null, InputOption::VALUE_NONE, 'Skip create collections steps.')
->addOption('ignore-duplicate-key-exception', null, InputOption::VALUE_NONE, 'Ingores duplicate keys exception.')
;
}

Expand All @@ -64,9 +67,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->dropDatabase($output);
}

$this->setupCreateCollections($output);
if (false == $input->getOption('no-create-collections')) {
$this->setupCreateCollections($output);
}

$this->setupLockIndexes($output);
$this->setupModelIndexes($output);
$this->setupModelIndexes($output, $input->getOption('ignore-duplicate-key-exception'));
}

private function setupCreateCollections(OutputInterface $output)
Expand Down Expand Up @@ -99,15 +105,21 @@ private function setupLockIndexes(OutputInterface $output)
}
}

private function setupModelIndexes(OutputInterface $output)
private function setupModelIndexes(OutputInterface $output, bool $ingoreDuplicateKeyException)
{
$output->writeln('Creating indexes');

foreach ($this->yadm->getUniqueStorages() as $storage) {
$collection = $storage->getCollection();
if ($indexes = $storage->getMeta()->getIndexes()) {
foreach ($indexes as $index) {
$collection->createIndex($index->getKey(), $index->getOptions());
try {
$collection->createIndex($index->getKey(), $index->getOptions());
} catch (CommandException $e) {
if ($ingoreDuplicateKeyException) {
$output->writeln('EXCEPTION - '.$e->getMessage());
}
}
}

$output->writeln("\t> ".$collection->getCollectionName(), OutputInterface::VERBOSITY_DEBUG);
Expand All @@ -122,4 +134,4 @@ private function dropDatabase(OutputInterface $output)

$this->database->drop();
}
}
}

0 comments on commit e2995d0

Please sign in to comment.