Skip to content

Commit

Permalink
validate schema command: allow to debug missing schema updates list
Browse files Browse the repository at this point in the history
  • Loading branch information
COil authored and greg0ire committed Sep 19, 2021
1 parent 2d42d78 commit 6d0f9a8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lib/Doctrine/ORM/Tools/Console/Command/ValidateSchemaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

use function count;
use function sprintf;

/**
Expand Down Expand Up @@ -76,6 +77,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
$ui->text('<comment>[SKIPPED] The database was not checked for synchronicity.</comment>');
} elseif (! $validator->schemaInSyncWithMetadata()) {
$ui->error('The database schema is not in sync with the current mapping file.');

if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
$updates = $validator->getUpdateSchemaList();

$rows = [];
foreach ($updates as $idx => $sql) {
$rows[] = [(int) $idx + 1, $sql];
}

$ui->comment(sprintf('<info>%d</info> schema diff(s) detected:', count($updates)));
$ui->table(['', 'SQL'], $rows);
}

$exit += 2;
} else {
$ui->success('The database schema is in sync with the mapping files.');
Expand Down
12 changes: 11 additions & 1 deletion lib/Doctrine/ORM/Tools/SchemaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,21 @@ public function validateClass(ClassMetadataInfo $class)
* @return bool
*/
public function schemaInSyncWithMetadata()
{
return count($this->getUpdateSchemaList()) === 0;
}

/**
* Returns the list of missing Database Schema updates.
*
* @return string[]
*/
public function getUpdateSchemaList()
{
$schemaTool = new SchemaTool($this->em);

$allMetadata = $this->em->getMetadataFactory()->getAllMetadata();

return count($schemaTool->getUpdateSchemaSql($allMetadata, true)) === 0;
return $schemaTool->getUpdateSchemaSql($allMetadata, true);
}
}

0 comments on commit 6d0f9a8

Please sign in to comment.