|
|
@@ -1,4 +1,5 @@
|
|
|
<?php
|
|
|
+
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
namespace App\Service\Doctrine\SchemaValidation;
|
|
|
@@ -8,25 +9,24 @@ use App\Service\Utils\EntityUtils;
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo;
|
|
|
use Doctrine\ORM\Mapping\MappingException;
|
|
|
-use RuntimeException;
|
|
|
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
|
|
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
|
|
|
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
|
|
|
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
|
|
|
|
|
/**
|
|
|
- * Validation du schéma Doctrine par comparaison aux entités en production sur la V1
|
|
|
+ * Validation du schéma Doctrine par comparaison aux entités en production sur la V1.
|
|
|
*
|
|
|
* — À supprimer lorsque la migration sera achevée —
|
|
|
*/
|
|
|
class SchemaValidationService
|
|
|
{
|
|
|
public function __construct(
|
|
|
- private readonly EntityManagerInterface $entityManager,
|
|
|
+ private readonly EntityManagerInterface $entityManager,
|
|
|
private readonly ApiLegacyRequestService $apiLegacyRequestService,
|
|
|
- private readonly EntityUtils $entityUtils
|
|
|
- )
|
|
|
- {}
|
|
|
+ private readonly EntityUtils $entityUtils,
|
|
|
+ ) {
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* Compare the V2 doctrine schema to the one in V1, and return a list of differences,
|
|
|
@@ -34,7 +34,8 @@ class SchemaValidationService
|
|
|
*
|
|
|
* [<entity> → Difference | [<field> → Difference]]
|
|
|
*
|
|
|
- * @return array<string, Difference | array<Difference>>
|
|
|
+ * @return array<string, Difference|array<Difference>>
|
|
|
+ *
|
|
|
* @throws ClientExceptionInterface
|
|
|
* @throws MappingException
|
|
|
* @throws RedirectionExceptionInterface
|
|
|
@@ -50,9 +51,10 @@ class SchemaValidationService
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Retrieve the V2 schema
|
|
|
+ * Retrieve the V2 schema.
|
|
|
+ *
|
|
|
+ * @return array<string, array<string|array<string|int>>>
|
|
|
*
|
|
|
- * @return array<string, array<string | array<string|int>>>
|
|
|
* @throws MappingException
|
|
|
*/
|
|
|
protected function getV2Schema(): array
|
|
|
@@ -78,9 +80,10 @@ class SchemaValidationService
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Retrieve the V1 schema
|
|
|
+ * Retrieve the V1 schema.
|
|
|
+ *
|
|
|
+ * @return array<string, array<string|array<string|int>>>
|
|
|
*
|
|
|
- * @return array<string, array<string | array<string|int>>>
|
|
|
* @throws ClientExceptionInterface
|
|
|
* @throws RedirectionExceptionInterface
|
|
|
* @throws ServerExceptionInterface
|
|
|
@@ -94,11 +97,12 @@ class SchemaValidationService
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Get a list of differences between V1 and V2 doctrine schemas
|
|
|
+ * Get a list of differences between V1 and V2 doctrine schemas.
|
|
|
+ *
|
|
|
+ * @param array<string, array<string|array<string|int>>> $schemaV1
|
|
|
+ * @param array<string, array<string|array<string|int>>> $schemaV2
|
|
|
*
|
|
|
- * @param array<string, array<string | array<string|int>>> $schemaV1
|
|
|
- * @param array<string, array<string| array<string|int>>> $schemaV2
|
|
|
- * @return array<string, Difference | array<Difference>>
|
|
|
+ * @return array<string, Difference|array<Difference>>
|
|
|
*/
|
|
|
protected function getDiff(array $schemaV1, array $schemaV2, ?DiffTypeEnum $filter = null): array
|
|
|
{
|
|
|
@@ -106,7 +110,6 @@ class SchemaValidationService
|
|
|
];
|
|
|
|
|
|
foreach ($schemaV1 as $entity => $fields) {
|
|
|
-
|
|
|
if (!$this->isEntityInSchema($schemaV2, $entity)) {
|
|
|
// L'entité n'existe pas en V2
|
|
|
if (!$filter || $filter === DiffTypeEnum::MISSING_ENTITY) {
|
|
|
@@ -121,13 +124,13 @@ class SchemaValidationService
|
|
|
|
|
|
foreach ($fields as $field => $fieldTypeV1) {
|
|
|
if (
|
|
|
- !$this->isPropertyInSchema($schemaV2, $entity, $field) &&
|
|
|
- $this->isRelationField($schemaV1, $entity, $field) &&
|
|
|
- $this->isPropertyInSchema($schemaV2, $entity, $field .'s')
|
|
|
+ !$this->isPropertyInSchema($schemaV2, $entity, $field)
|
|
|
+ && $this->isRelationField($schemaV1, $entity, $field)
|
|
|
+ && $this->isPropertyInSchema($schemaV2, $entity, $field.'s')
|
|
|
) {
|
|
|
// Le champ existe en V2, mais il a été mis au pluriel, par exemple : $contactPoint devenu $contactPoints
|
|
|
// Pour éviter les faux positifs, on renomme le champ dans le schéma V1
|
|
|
- $schemaV1[$entity][$field .'s'] = $fieldTypeV1;
|
|
|
+ $schemaV1[$entity][$field.'s'] = $fieldTypeV1;
|
|
|
unset($schemaV1[$entity][$field]);
|
|
|
$fields = $schemaV1[$entity];
|
|
|
}
|
|
|
@@ -136,13 +139,12 @@ class SchemaValidationService
|
|
|
$diff[$entity] = [];
|
|
|
|
|
|
foreach ($fields as $field => $fieldTypeV1) {
|
|
|
-
|
|
|
if (!$this->isPropertyInSchema($schemaV2, $entity, $field)) {
|
|
|
// Le champ n'existe pas en V2
|
|
|
if ($this->isRelationField($schemaV1, $entity, $field)) {
|
|
|
$diff[$entity][$field] = new Difference(
|
|
|
DiffTypeEnum::MISSING_RELATION,
|
|
|
- "Relation " . $this->getRelationTypeLabel($fieldTypeV1) . " `$field` is missing in V2",
|
|
|
+ 'Relation '.$this->getRelationTypeLabel($fieldTypeV1)." `$field` is missing in V2",
|
|
|
$fieldTypeV1,
|
|
|
);
|
|
|
} else {
|
|
|
@@ -199,11 +201,9 @@ class SchemaValidationService
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Returns true if the given entity name exists in the doctrine schema
|
|
|
+ * Returns true if the given entity name exists in the doctrine schema.
|
|
|
*
|
|
|
- * @param array<string, array<string | array<string|int>>> $schema
|
|
|
- * @param string $entity
|
|
|
- * @return bool
|
|
|
+ * @param array<string, array<string|array<string|int>>> $schema
|
|
|
*/
|
|
|
protected function isEntityInSchema(array $schema, string $entity): bool
|
|
|
{
|
|
|
@@ -213,10 +213,7 @@ class SchemaValidationService
|
|
|
/**
|
|
|
* Returns true if the given property name exists in the doctrine schema under this entity.
|
|
|
*
|
|
|
- * @param array<string, array<string | array<string|int>>> $schema
|
|
|
- * @param string $entity
|
|
|
- * @param string $property
|
|
|
- * @return bool
|
|
|
+ * @param array<string, array<string|array<string|int>>> $schema
|
|
|
*/
|
|
|
protected function isPropertyInSchema(array $schema, string $entity, string $property): bool
|
|
|
{
|
|
|
@@ -226,10 +223,7 @@ class SchemaValidationService
|
|
|
/**
|
|
|
* Is the given field a relation field.
|
|
|
*
|
|
|
- * @param array<string, array<string | array<string|int>>> $schema
|
|
|
- * @param string $entity
|
|
|
- * @param string $relation
|
|
|
- * @return bool
|
|
|
+ * @param array<string, array<string|array<string|int>>> $schema
|
|
|
*/
|
|
|
protected function isRelationField(array $schema, string $entity, string $relation): bool
|
|
|
{
|
|
|
@@ -242,9 +236,8 @@ class SchemaValidationService
|
|
|
*
|
|
|
* @param array<string, string> $relationReference
|
|
|
* @param array<string, string> $relationCompared
|
|
|
- * @return Difference|null
|
|
|
*/
|
|
|
- protected function getRelationDiff(array $relationReference, array $relationCompared): Difference|null
|
|
|
+ protected function getRelationDiff(array $relationReference, array $relationCompared): ?Difference
|
|
|
{
|
|
|
if ($relationReference['type'] !== $relationCompared['type']) {
|
|
|
return new Difference(
|
|
|
@@ -259,7 +252,7 @@ class SchemaValidationService
|
|
|
) {
|
|
|
return new Difference(
|
|
|
DiffTypeEnum::DIFFERENT_RELATION_CONFIGURATION,
|
|
|
- "Relation configuration is different (targetEntity)",
|
|
|
+ 'Relation configuration is different (targetEntity)',
|
|
|
$relationReference
|
|
|
);
|
|
|
}
|
|
|
@@ -269,7 +262,7 @@ class SchemaValidationService
|
|
|
) {
|
|
|
return new Difference(
|
|
|
DiffTypeEnum::DIFFERENT_RELATION_CONFIGURATION,
|
|
|
- "Relation configuration is different (mappedBy)",
|
|
|
+ 'Relation configuration is different (mappedBy)',
|
|
|
$relationReference
|
|
|
);
|
|
|
}
|
|
|
@@ -281,7 +274,6 @@ class SchemaValidationService
|
|
|
* Get the name of a relation from a ClassMetadataInfo integer constant.
|
|
|
*
|
|
|
* @param array<string, string|int> $relation
|
|
|
- * @return string
|
|
|
*/
|
|
|
protected function getRelationTypeLabel(array $relation): string
|
|
|
{
|