Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1519 from malarzm/gh1271
Add command to validate mapping
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
lib/Doctrine/ODM/MongoDB/Tools/Console/Command/Schema/ValidateCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |