From c434c2036e5dfdf731275c11743fe5cb6edf3752 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 --- docs/en/reference/tools.rst | 5 ++ .../Command/SchemaTool/UpdateCommand.php | 5 +- .../Console/Command/ValidateSchemaCommand.php | 8 ++ lib/Doctrine/ORM/Tools/SchemaValidator.php | 12 ++- .../Command/ValidateSchemaCommandTest.php | 73 +++++++++++++++++++ 5 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Tools/Console/Command/ValidateSchemaCommandTest.php diff --git a/docs/en/reference/tools.rst b/docs/en/reference/tools.rst index ad60e5317f..f42b35ac0d 100644 --- a/docs/en/reference/tools.rst +++ b/docs/en/reference/tools.rst @@ -393,6 +393,11 @@ You can either use the Doctrine Command Line Tool: doctrine orm:validate-schema +If the validation fails, you can change the verbosity level to +check the detected errors: + + doctrine orm:validate-schema -v + Or you can trigger the validation manually: .. code-block:: 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 93ff4ad9ec..36cb89b819 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..aa29938c03 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Tools/Console/Command/ValidateSchemaCommandTest.php @@ -0,0 +1,73 @@ +_em->getConnection()->getDatabasePlatform() instanceof SqlitePlatform) { + self::markTestSkipped('Only with sqlite'); + } + + $application = new Application(); + $application->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('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('cache_login', $display); + } +}