From 99ddddb671799bd12b4db42fcd2d50f3920c93ba Mon Sep 17 00:00:00 2001 From: Hugo Posnic Date: Thu, 18 Jul 2024 17:05:58 +0200 Subject: [PATCH 1/2] Introduce level info (orm/database) in deletion settings In output and in graph (by colors) --- src/Command/AnalyseCommand.php | 63 ++++++++++++++++++---------------- src/Enum/DeletionType.php | 12 +++++++ src/Enum/Level.php | 11 ++++++ 3 files changed, 57 insertions(+), 29 deletions(-) create mode 100644 src/Enum/DeletionType.php create mode 100644 src/Enum/Level.php diff --git a/src/Command/AnalyseCommand.php b/src/Command/AnalyseCommand.php index cf50181..ce0375b 100644 --- a/src/Command/AnalyseCommand.php +++ b/src/Command/AnalyseCommand.php @@ -6,6 +6,8 @@ use Doctrine\ORM\EntityManagerInterface; use DoctrineRelationsAnalyserBundle\Enum\AnalysisMode; +use DoctrineRelationsAnalyserBundle\Enum\DeletionType; +use DoctrineRelationsAnalyserBundle\Enum\Level; use Graphp\Graph\Graph; use Graphp\GraphViz\GraphViz; use Symfony\Component\Console\Attribute\AsCommand; @@ -81,16 +83,28 @@ protected function execute(InputInterface $input, OutputInterface $output): int $deletions = []; if (isset($association['orphanRemoval']) && $association['orphanRemoval']) { - $deletions['orphanRemoval'] = true; + $deletions[] = [ + 'type' => DeletionType::ORPHAN_REMOVAL, + 'level' => Level::ORM, + 'value' => 'true', + ]; } if (isset($association['cascade']) && in_array('remove', $association['cascade'], true)) { - $deletions['cascade'] = true; + $deletions[] = [ + 'type' => DeletionType::CASCADE, + 'level' => Level::ORM, + 'value' => '["remove"]', + ]; } if (isset($association['joinColumns']) && !empty($association['joinColumns'])) { if (isset($association['joinColumns'][0]['onDelete']) && !empty($association['joinColumns'][0]['onDelete'])) { - $deletions['onDelete'] = $association['joinColumns'][0]['onDelete']; + $deletions[] = [ + 'type' => DeletionType::ON_DELETE, + 'level' => Level::DATABASE, + 'value' => $association['joinColumns'][0]['onDelete'], + ]; } } @@ -162,14 +176,9 @@ private function outputRelationships(array $relationships, SymfonyStyle $io, Ana if (!empty($relation['deletions'])) { $io->text('Deletions properties:'); - if (isset($relation['deletions']['onDelete'])) { - $io->text("- onDelete: {$relation['deletions']['onDelete']}"); - } - if (isset($relation['deletions']['orphanRemoval'])) { - $io->text('- orphanRemoval: true'); - } - if (isset($relation['deletions']['cascade'])) { - $io->text("- cascade: ['remove']"); + foreach ($relation['deletions'] as $deletion) { + $level = Level::ORM === $deletion['level'] ? 'ORM' : 'Database'; + $io->text("- {$deletion['type']->value}: {$deletion['value']} ($level level)"); } } } @@ -201,25 +210,21 @@ private function generateGraph(array $relationships, string $outputPath, Analysi $edge = $graph->createEdgeDirected($nodes[$entity], $nodes[$targetEntity]); $edge->setAttribute('graphviz.label', $this->getRelationType($relation['type'])); } elseif (AnalysisMode::DELETIONS === $mode) { - foreach ($relation['deletions'] as $key => $value) { - $invertArrow = false; - if ('onDelete' === $key) { - $label = "onDelete: {$value}"; - $invertArrow = true; - } elseif ('orphanRemoval' === $key) { - $label = 'orphanRemoval: true'; - } elseif ('cascade' === $key) { - $label = 'cascade: "remove"'; + foreach ($relation['deletions'] as $deletion) { + $invertArrow = DeletionType::ON_DELETE === $deletion['type'] ? true : false; + if ($invertArrow) { + // Arrow points from parent (entity) to child (targetEntity) + $edge = $graph->createEdgeDirected($nodes[$targetEntity], $nodes[$entity]); + } else { + // Arrow points from child (targetEntity) to parent (entity) + $edge = $graph->createEdgeDirected($nodes[$entity], $nodes[$targetEntity]); } - if (isset($label)) { - if ($invertArrow) { - // Arrow points from parent (entity) to child (targetEntity) - $edge = $graph->createEdgeDirected($nodes[$targetEntity], $nodes[$entity]); - } else { - // Arrow points from child (targetEntity) to parent (entity) - $edge = $graph->createEdgeDirected($nodes[$entity], $nodes[$targetEntity]); - } - $edge->setAttribute('graphviz.label', $label); + $label = "{$deletion['type']->value}: {$deletion['value']}"; + $edge->setAttribute('graphviz.label', $label); + if (Level::ORM === $deletion['level']) { + $edge->setAttribute('graphviz.color', 'blue'); + } else { + $edge->setAttribute('graphviz.color', 'red'); } } } diff --git a/src/Enum/DeletionType.php b/src/Enum/DeletionType.php new file mode 100644 index 0000000..c26f3d5 --- /dev/null +++ b/src/Enum/DeletionType.php @@ -0,0 +1,12 @@ + Date: Thu, 18 Jul 2024 17:08:47 +0200 Subject: [PATCH 2/2] Bump version to 0.2.0 --- CHANGELOG.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f02757f..6af50e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,15 +7,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.2.0] - 2024-07-18 + +### Added + +- Introduce level info (orm/database) in deletion settings ## [0.1.1] - 2024-07-18 ### Fixed -- Add minimum-stability and prefer-stable for graphp packages. +- Add minimum-stability and prefer-stable for graphp packages ## [0.1.0] - 2024-07-18 ### Added -- Initial version. \ No newline at end of file +- Initial version \ No newline at end of file