Browse Source

schema validate command: add the csv option

Olivier Massot 1 year ago
parent
commit
ebdc4ba2ac
3 changed files with 104 additions and 95 deletions
  1. 0 25
      rector.php
  2. 104 0
      src/Commands/Doctrine/SchemaValidateCommand.php
  3. 0 70
      src/Service/Cron/Job/SchemaValidation.php

+ 0 - 25
rector.php

@@ -1,25 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
-use Rector\Config\RectorConfig;
-use \Rector\Symfony\Set\SymfonySetList;
-
-return static function (RectorConfig $rectorConfig): void {
-    $rectorConfig->paths([
-        __DIR__ . '/config',
-        __DIR__ . '/public',
-        __DIR__ . '/src',
-    ]);
-
-    // register a single rule
-    $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);
-    $rectorConfig->symfonyContainerXml(__DIR__ . '/var/cache/docker/App_KernelDockerDebugContainer.xml');
-
-    $rectorConfig->sets([
-        \Rector\Symfony\Set\SymfonyLevelSetList::UP_TO_SYMFONY_60,
-        SymfonySetList::SYMFONY_CODE_QUALITY,
-        SymfonySetList::SYMFONY_CONSTRUCTOR_INJECTION,
-    ]);
-};

+ 104 - 0
src/Commands/Doctrine/SchemaValidateCommand.php

@@ -0,0 +1,104 @@
+<?php
+
+/** @noinspection PhpUnused */
+
+namespace App\Commands\Doctrine;
+
+use App\Service\Doctrine\SchemaValidation\Difference;
+use App\Service\Doctrine\SchemaValidation\DiffTypeEnum;
+use App\Service\Doctrine\SchemaValidation\SchemaValidationService;
+use Doctrine\ORM\Tools\SchemaTool;
+use JetBrains\PhpStorm\Pure;
+use Symfony\Component\Console\Attribute\AsCommand;
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Style\SymfonyStyle;
+
+/**
+ * Overrides the default doctrine:schema:update command.
+ */
+#[AsCommand(
+    name: 'ot:schema:validate',
+    description: 'Compare le schema doctrine de la V2 à celui de la V1'
+)]
+class SchemaValidateCommand extends Command
+{
+    #[Pure]
+    public function __construct(
+        private readonly SchemaValidationService $schemaValidationService,
+    ) {
+        parent::__construct();
+    }
+
+    /**
+     * Configures the command.
+     */
+    protected function configure(): void
+    {
+        $this->addOption(
+            'filter',
+            null,
+            InputOption::VALUE_OPTIONAL,
+            "Filter the type of difference to display (ex: 'MISSING_PROPERTY')."
+        );
+        $this->addOption(
+            'csv',
+            null,
+            InputOption::VALUE_NONE,
+            "Print the result in CSV format."
+        );
+    }
+
+    protected function execute(InputInterface $input, OutputInterface $output): int
+    {
+        $filter = $input->getOption('filter') ? DiffTypeEnum::from($input->getOption('filter')) : null;
+
+        $diff = $this->schemaValidationService->validateSchema($filter);
+
+        foreach ($diff as $entity => $value) {
+            if (empty($value)) {
+                continue;
+            }
+
+            if ($input->getOption('csv')) {
+                $this->printCsv($output, $entity, $value);
+            } else {
+                $this->printVerbose($output, $entity, $value);
+            }
+        }
+
+        if ($diff) {
+            $output->writeln(count($diff) . " differences found");
+        } else {
+            $output->writeln("No difference found");
+        }
+
+        return 0;
+    }
+
+    protected function printVerbose(OutputInterface $output, string $entity, Difference | array $differences): void {
+        $output->writeln($entity);
+
+        if (!is_array($differences)) {
+            $output->writeln($differences->getType()->value . " : " . $differences->getMessage());
+        } else {
+            foreach ($differences as $field => $difference) {
+                $output->writeln(" * " . $field . " - " . $difference->getType()->value . " : " . $difference->getMessage());
+            }
+        }
+
+        $output->writeln("\n");
+    }
+
+    protected function printCsv(OutputInterface $output, string $entity, Difference | array $differences): void {
+        if (!is_array($differences)) {
+            $output->writeln(implode(';', [$entity, '', $differences->getType()->value]));
+        } else {
+            foreach ($differences as $field => $difference) {
+                $output->writeln(implode(';', [$entity, $field, $difference->getType()->value]));
+            }
+        }
+    }
+}

+ 0 - 70
src/Service/Cron/Job/SchemaValidation.php

@@ -1,70 +0,0 @@
-<?php
-
-namespace App\Service\Cron\Job;
-
-use App\Service\Cron\BaseCronJob;
-use App\Service\Doctrine\SchemaValidation\SchemaValidationService;
-use JetBrains\PhpStorm\Pure;
-
-/**
- * Cronjob de validation du schéma Doctrine par comparaison aux entités en production sur la V1
- * En cas de différences, un mail est envoyé à l'exploitation.
- *
- * -- A supprimer lorsque la migration sera achevée --
- *
- * >>> ot:cron run schema-validation --preview
- * >>> ot:cron run schema-validation
- */
-class SchemaValidation extends BaseCronJob
-{
-    #[Pure]
-    public function __construct(
-        private readonly SchemaValidationService $schemaValidationService,
-    ) {
-        parent::__construct();
-    }
-
-    /**
-     * Preview the result of the execution, without actually deleting anything.
-     *
-     * @throws \Exception
-     */
-    public function preview(): void
-    {
-        $this->validateAndPrint();
-    }
-
-    /**
-     * Proceed to the deletion of the files and the purge of the DB.
-     *
-     * @throws \Exception
-     */
-    public function execute(): void
-    {
-        $this->validateAndPrint();
-    }
-
-    protected function validateAndPrint() {
-        $diff = $this->schemaValidationService->validateSchema();
-
-        foreach ($diff as $entity => $value) {
-            $this->ui->print($entity);
-
-            if (!is_array($value)) {
-                $this->ui->print($value->getType()->value . " : " . $value->getMessage());
-            } else {
-                foreach ($value as $field => $difference) {
-                    $this->ui->print(" * " . $field . " - " . $difference->getType()->value . " : " . $difference->getMessage());
-                }
-            }
-
-            $this->ui->print("\n");
-        }
-
-        if ($diff) {
-            $this->ui->print(count($diff) . " differences found");
-        } else {
-            $this->ui->print("No difference found");
-        }
-    }
-}