From 75aa9d1f4e589325fbee4d80aa7fdfb48eb12cc6 Mon Sep 17 00:00:00 2001 From: COil Date: Tue, 21 Sep 2021 22:10:52 +0200 Subject: [PATCH] validate schema command: allow to debug missing schema updates list --- .../Command/SchemaTool/UpdateCommand.php | 5 +- .../Console/Command/ValidateSchemaCommand.php | 8 +++ lib/Doctrine/ORM/Tools/SchemaValidator.php | 12 +++- .../Command/ValidateSchemaCommandTest.php | 68 +++++++++++++++++++ 4 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Tools/Console/Command/ValidateSchemaCommandTest.php diff --git a/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/UpdateCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/UpdateCommand.php index 6dffd12bbf..477f84c744 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/UpdateCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/UpdateCommand.php @@ -90,10 +90,7 @@ protected function executeSchemaCommand(InputInterface $input, OutputInterface $ if ($dumpSql) { $ui->text('The following SQL statements will be executed:'); $ui->newLine(); - - foreach ($sqls as $sql) { - $ui->text(sprintf(' %s;', $sql)); - } + $ui->listing($sqls); } if ($force) { diff --git a/lib/Doctrine/ORM/Tools/Console/Command/ValidateSchemaCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/ValidateSchemaCommand.php index b7f3dccae1..4fd7e7dbed 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/ValidateSchemaCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/ValidateSchemaCommand.php @@ -10,6 +10,7 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; +use function count; use function sprintf; /** @@ -76,6 +77,13 @@ protected function execute(InputInterface $input, OutputInterface $output) $ui->text('[SKIPPED] The database was not checked for synchronicity.'); } elseif (! $validator->schemaInSyncWithMetadata()) { $ui->error('The database schema is not in sync with the current mapping file.'); + + if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { + $sqls = $validator->getUpdateSchemaList(); + $ui->comment(sprintf('%d schema diff(s) detected:', count($sqls))); + $ui->listing($sqls); + } + $exit += 2; } else { $ui->success('The database schema is in sync with the mapping files.'); diff --git a/lib/Doctrine/ORM/Tools/SchemaValidator.php b/lib/Doctrine/ORM/Tools/SchemaValidator.php index db27180cdc..4bba3802db 100644 --- a/lib/Doctrine/ORM/Tools/SchemaValidator.php +++ b/lib/Doctrine/ORM/Tools/SchemaValidator.php @@ -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 array + */ + public function getUpdateSchemaList(): array { $schemaTool = new SchemaTool($this->em); $allMetadata = $this->em->getMetadataFactory()->getAllMetadata(); - return count($schemaTool->getUpdateSchemaSql($allMetadata, true)) === 0; + return $schemaTool->getUpdateSchemaSql($allMetadata, true); } } diff --git a/tests/Doctrine/Tests/ORM/Tools/Console/Command/ValidateSchemaCommandTest.php b/tests/Doctrine/Tests/ORM/Tools/Console/Command/ValidateSchemaCommandTest.php new file mode 100644 index 0000000000..1e805e2a3e --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Tools/Console/Command/ValidateSchemaCommandTest.php @@ -0,0 +1,68 @@ +add(new ValidateSchemaCommand(new SingleManagerProvider($this->_em))); + + $this->command = $application->find('orm:validate-schema'); + $this->tester = new CommandTester($this->command); + } + + public function testNotInSync(): void + { + $this->tester->execute( + [ + 'command' => $this->command->getName(), + ] + ); + + $display = $this->tester->getDisplay(); + + self::assertStringContainsString('The database schema is not in sync with the current mapping file', $display); + self::assertStringNotContainsString('CREATE TABLE cache_login', $display); + } + + public function testNotInSyncVerbose(): void + { + $this->tester->execute( + [ + 'command' => $this->command->getName(), + ], + [ + 'verbosity' => OutputInterface::VERBOSITY_VERBOSE, + ] + ); + + $display = $this->tester->getDisplay(); + self::assertStringContainsString('The database schema is not in sync with the current mapping file', $display); + self::assertStringContainsString('CREATE TABLE cache_login', $display); + } +}