浏览代码

add the schema validation service and cron job

Olivier Massot 1 年之前
父节点
当前提交
a4d7c7fe8e
共有 2 个文件被更改,包括 126 次插入0 次删除
  1. 49 0
      src/Service/Cron/Job/SchemaValidation.php
  2. 77 0
      src/Service/Doctrine/SchemaValidationService.php

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

@@ -0,0 +1,49 @@
+<?php
+
+namespace App\Service\Cron\Job;
+
+use App\Service\Cron\BaseCronJob;
+use App\Service\Doctrine\SchemaValidationService;
+use App\Service\Utils\DatesUtils;
+use Doctrine\DBAL\Connection;
+use Doctrine\DBAL\DBALException;
+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->schemaValidationService->validateSchema();
+    }
+
+    /**
+     * Proceed to the deletion of the files and the purge of the DB.
+     *
+     * @throws \Exception
+     */
+    public function execute(): void
+    {
+        $this->schemaValidationService->validateSchema();
+    }
+}

+ 77 - 0
src/Service/Doctrine/SchemaValidationService.php

@@ -0,0 +1,77 @@
+<?php
+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;
+
+/**
+ * Validation du schéma Doctrine par comparaison aux entités en production sur la V1
+ *
+ * -- A supprimer lorsque la migration sera achevée --
+ *
+ */
+class SchemaValidationService
+{
+    public function __construct(
+        private EntityManagerInterface $entityManager, private readonly ApiRequestService $apiRequestService, private readonly ApiLegacyRequestService $apiLegacyRequestService,
+    )
+    {}
+
+    public function validateSchema() {
+        $schemaV1 = $this->getV1Schema();
+        $schemaV2 = $this->getV2Schema();
+    }
+
+    /**
+     * Retrieve the V2 schema
+     *
+     * @return array<string, array>
+     * @throws MappingException
+     */
+    protected function getV2Schema(): array
+    {
+        $metadata = $this->entityManager->getMetadataFactory()->getAllMetadata();
+        $schema = [];
+
+        foreach ($metadata as $entityMetadata) {
+            $schema[$entityMetadata->getName()] = [];
+
+            foreach ($entityMetadata->getFieldNames() as $field) {
+                $schema[$entityMetadata->getName()][$field] = $entityMetadata->getTypeOfField($field);
+            }
+
+            foreach ($entityMetadata->getAssociationNames() as $association) {
+                $schema[$entityMetadata->getName()][$association] = $entityMetadata->getAssociationMapping($association);
+            }
+        }
+
+        return $schema;
+    }
+
+    /**
+     * Retrieve the V1 schema
+     * @return void
+     */
+    protected function getV1Schema() {
+        $response = $this->apiLegacyRequestService->get('/_internal/doctrine/schema');
+
+        $schema = json_decode($response->getContent(), true);
+
+        dd($schema);
+
+        return $schema;
+    }
+
+    /**
+     * Get a list of differences between V1 and V2 doctrine schemas
+     * @return void
+     */
+    protected function getDiff() {
+
+    }
+}