ソースを参照

add a basic version of the getDiff method

Olivier Massot 1 年間 前
コミット
dc7cd741f9
1 ファイル変更38 行追加14 行削除
  1. 38 14
      src/Service/Doctrine/SchemaValidationService.php

+ 38 - 14
src/Service/Doctrine/SchemaValidationService.php

@@ -3,9 +3,7 @@ declare(strict_types=1);
 
 namespace App\Service\Doctrine;
 
-use App\Entity\Access\Access;
 use App\Service\ApiLegacy\ApiLegacyRequestService;
-use App\Service\Rest\ApiRequestService;
 use Doctrine\ORM\EntityManagerInterface;
 use Doctrine\ORM\Mapping\MappingException;
 
@@ -18,13 +16,18 @@ use Doctrine\ORM\Mapping\MappingException;
 class SchemaValidationService
 {
     public function __construct(
-        private EntityManagerInterface $entityManager, private readonly ApiRequestService $apiRequestService, private readonly ApiLegacyRequestService $apiLegacyRequestService,
+        private EntityManagerInterface $entityManager,
+        private readonly ApiLegacyRequestService $apiLegacyRequestService,
     )
     {}
 
     public function validateSchema() {
         $schemaV1 = $this->getV1Schema();
         $schemaV2 = $this->getV2Schema();
+
+        $diff = $this->getDiff($schemaV1, $schemaV2);
+
+        dd($diff);
     }
 
     /**
@@ -39,14 +42,14 @@ class SchemaValidationService
         $schema = [];
 
         foreach ($metadata as $entityMetadata) {
-            $schema[$entityMetadata->getName()] = [];
+            $schema[$entityMetadata->getTableName()] = [];
 
             foreach ($entityMetadata->getFieldNames() as $field) {
-                $schema[$entityMetadata->getName()][$field] = $entityMetadata->getTypeOfField($field);
+                $schema[$entityMetadata->getTableName()][$field] = $entityMetadata->getTypeOfField($field);
             }
 
             foreach ($entityMetadata->getAssociationNames() as $association) {
-                $schema[$entityMetadata->getName()][$association] = $entityMetadata->getAssociationMapping($association);
+                $schema[$entityMetadata->getTableName()][$association] = $entityMetadata->getAssociationMapping($association);
             }
         }
 
@@ -55,23 +58,44 @@ class SchemaValidationService
 
     /**
      * Retrieve the V1 schema
-     * @return void
      */
-    protected function getV1Schema() {
+    protected function getV1Schema(): array {
         $response = $this->apiLegacyRequestService->get('/_internal/doctrine/schema');
 
-        $schema = json_decode($response->getContent(), true);
-
-        dd($schema);
-
-        return $schema;
+        return json_decode($response->getContent(), true);
     }
 
     /**
      * Get a list of differences between V1 and V2 doctrine schemas
      * @return void
      */
-    protected function getDiff() {
+    protected function getDiff(array $schemaV1, array $schemaV2): array {
+        $diff = [
+            'missing-entity' => [],
+            'entities' => []
+        ];
+
+        foreach ($schemaV1 as $entity => $fields) {
+            if (!isset($schemaV2[$entity])) {
+                $diff['missing-entity'][] = $entity;
+                continue;
+            }
+
+            $diff['entities'][$entity] = [];
+
+            foreach ($fields as $field => $type) {
+                if (!isset($schemaV2[$entity][$field])) {
+                    $diff['entities'][$entity][$field] = 'missing';
+                    continue;
+                }
+
+                if ($schemaV2[$entity][$field] !== $type) {
+                    $diff['entities'][$entity][$field] = 'type-diff';
+                    continue;
+                }
+            }
+        }
 
+        return $diff;
     }
 }