Skip to content

Commit

Permalink
Merge pull request #1519 from malarzm/gh1271
Browse files Browse the repository at this point in the history
Add command to validate mapping
  • Loading branch information
malarzm committed Dec 27, 2016
2 parents 2f1eb74 + e663600 commit 23b3b50
Showing 1 changed file with 53 additions and 0 deletions.
@@ -0,0 +1,53 @@
<?php

namespace Doctrine\ODM\MongoDB\Tools\Console\Command\Schema;

use Doctrine\Common\Cache\VoidCache;
use Doctrine\ODM\MongoDB\DocumentManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ValidateCommand extends Command
{
/**
* @see \Symfony\Component\Console\Command\Command
*/
protected function configure()
{
$this
->setName('odm:schema:validate')
->setDescription('Validates if document mapping stays the same after serializing into cache.')
->setDefinition(array())
->setHelp(<<<EOT
Validates if document mapping stays the same after serializing into cache.
EOT
);
}

/**
* @see \Symfony\Component\Console\Command\Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
/** @var DocumentManager $dm */
$dm = $this->getHelper('documentManager')->getDocumentManager();
$metadataFactory = $dm->getMetadataFactory();
$metadataFactory->setCacheDriver(new VoidCache());

$errors = 0;
foreach ($metadataFactory->getAllMetadata() as $meta) {
if ($meta != unserialize(serialize($meta))) {
++$errors;
$output->writeln(sprintf("%s has mapping issues.", $meta->name));
}
}
if ($errors) {
$output->writeln(sprintf('<error>%d document(s) have mapping issues.</error>'));
} else {
$output->writeln('All documents are OK!');
}

return $errors ? 255 : 0;
}
}

0 comments on commit 23b3b50

Please sign in to comment.