From edd85e5d99695ebac4e4bfa3c7c464088cf7211a Mon Sep 17 00:00:00 2001 From: Manuel Aranda Date: Mon, 15 Sep 2025 18:37:04 +0200 Subject: [PATCH] refs #2812. Fixed bug in delete repository. Added constraints --- migrations/Version20250915162242.php | 33 +++++++++++++++++++ migrations/Version20250915162434.php | 33 +++++++++++++++++++ migrations/Version20250915162508.php | 33 +++++++++++++++++++ src/Entity/Client.php | 1 + src/Entity/GitRepository.php | 1 + src/Entity/ImageRepository.php | 2 +- src/Entity/NetworkSettings.php | 1 + .../Processor/ImageRepositoryProcessor.php | 21 +++++++++--- 8 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 migrations/Version20250915162242.php create mode 100644 migrations/Version20250915162434.php create mode 100644 migrations/Version20250915162508.php diff --git a/migrations/Version20250915162242.php b/migrations/Version20250915162242.php new file mode 100644 index 0000000..8631eca --- /dev/null +++ b/migrations/Version20250915162242.php @@ -0,0 +1,33 @@ +addSql('ALTER TABLE git_repository DROP FOREIGN KEY FK_C2B3204A50C9D4F7'); + $this->addSql('ALTER TABLE git_repository ADD CONSTRAINT FK_C2B3204A50C9D4F7 FOREIGN KEY (repository_id) REFERENCES image_repository (id) ON DELETE CASCADE'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE git_repository DROP FOREIGN KEY FK_C2B3204A50C9D4F7'); + $this->addSql('ALTER TABLE git_repository ADD CONSTRAINT FK_C2B3204A50C9D4F7 FOREIGN KEY (repository_id) REFERENCES image_repository (id)'); + } +} diff --git a/migrations/Version20250915162434.php b/migrations/Version20250915162434.php new file mode 100644 index 0000000..89b5ecb --- /dev/null +++ b/migrations/Version20250915162434.php @@ -0,0 +1,33 @@ +addSql('ALTER TABLE network_settings DROP FOREIGN KEY FK_48869B5450C9D4F7'); + $this->addSql('ALTER TABLE network_settings ADD CONSTRAINT FK_48869B5450C9D4F7 FOREIGN KEY (repository_id) REFERENCES image_repository (id) ON DELETE SET NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE network_settings DROP FOREIGN KEY FK_48869B5450C9D4F7'); + $this->addSql('ALTER TABLE network_settings ADD CONSTRAINT FK_48869B5450C9D4F7 FOREIGN KEY (repository_id) REFERENCES image_repository (id)'); + } +} diff --git a/migrations/Version20250915162508.php b/migrations/Version20250915162508.php new file mode 100644 index 0000000..f4a1d00 --- /dev/null +++ b/migrations/Version20250915162508.php @@ -0,0 +1,33 @@ +addSql('ALTER TABLE client DROP FOREIGN KEY FK_C744045550C9D4F7'); + $this->addSql('ALTER TABLE client ADD CONSTRAINT FK_C744045550C9D4F7 FOREIGN KEY (repository_id) REFERENCES image_repository (id) ON DELETE SET NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE client DROP FOREIGN KEY FK_C744045550C9D4F7'); + $this->addSql('ALTER TABLE client ADD CONSTRAINT FK_C744045550C9D4F7 FOREIGN KEY (repository_id) REFERENCES image_repository (id)'); + } +} diff --git a/src/Entity/Client.php b/src/Entity/Client.php index b28f14f..0ada71c 100644 --- a/src/Entity/Client.php +++ b/src/Entity/Client.php @@ -68,6 +68,7 @@ class Client extends AbstractEntity private ?PxeTemplate $template = null; #[ORM\ManyToOne()] + #[ORM\JoinColumn(onDelete: 'SET NULL')] private ?ImageRepository $repository = null; #[ORM\ManyToOne(inversedBy: 'clients')] diff --git a/src/Entity/GitRepository.php b/src/Entity/GitRepository.php index de54128..591a656 100644 --- a/src/Entity/GitRepository.php +++ b/src/Entity/GitRepository.php @@ -17,6 +17,7 @@ class GitRepository extends AbstractEntity private ?string $description = null; #[ORM\ManyToOne(inversedBy: 'gitRepositories')] + #[ORM\JoinColumn(onDelete: 'CASCADE')] private ?ImageRepository $repository = null; public function __construct() diff --git a/src/Entity/ImageRepository.php b/src/Entity/ImageRepository.php index 8052bf4..64bc645 100644 --- a/src/Entity/ImageRepository.php +++ b/src/Entity/ImageRepository.php @@ -28,7 +28,7 @@ class ImageRepository extends AbstractEntity /** * @var Collection */ - #[ORM\OneToMany(mappedBy: 'repository', targetEntity: ImageImageRepository::class)] + #[ORM\OneToMany(mappedBy: 'repository', targetEntity: ImageImageRepository::class, cascade: ['remove'], orphanRemoval: true)] private Collection $imageImageRepositories; #[ORM\Column(length: 255, nullable: true)] diff --git a/src/Entity/NetworkSettings.php b/src/Entity/NetworkSettings.php index 7d58e0f..8267783 100644 --- a/src/Entity/NetworkSettings.php +++ b/src/Entity/NetworkSettings.php @@ -67,6 +67,7 @@ class NetworkSettings extends AbstractEntity private ?HardwareProfile $hardwareProfile = null; #[ORM\ManyToOne] + #[ORM\JoinColumn(onDelete: 'SET NULL')] private ?ImageRepository $repository = null; #[ORM\ManyToOne] diff --git a/src/State/Processor/ImageRepositoryProcessor.php b/src/State/Processor/ImageRepositoryProcessor.php index 082d093..9a7b625 100644 --- a/src/State/Processor/ImageRepositoryProcessor.php +++ b/src/State/Processor/ImageRepositoryProcessor.php @@ -12,13 +12,17 @@ use ApiPlatform\Validator\ValidatorInterface; use App\Dto\Input\ImageRepositoryInput; use App\Dto\Output\ImageRepositoryOutput; use App\Repository\ImageRepositoryRepository; +use Doctrine\ORM\EntityManagerInterface; +use App\Entity\Client; readonly class ImageRepositoryProcessor implements ProcessorInterface { public function __construct( private ImageRepositoryRepository $imageRepository, - private ValidatorInterface $validator - ) + private ValidatorInterface $validator, + private EntityManagerInterface $entityManager + + ) { } @@ -60,8 +64,17 @@ readonly class ImageRepositoryProcessor implements ProcessorInterface private function processDelete($data, Operation $operation, array $uriVariables = [], array $context = []): null { - $user = $this->imageRepository->findOneByUuid($uriVariables['uuid']); - $this->imageRepository->delete($user); + $repository = $this->imageRepository->findOneByUuid($uriVariables['uuid']); + + $clients = $this->entityManager->getRepository(Client::class)->findBy(['repository' => $repository]); + + foreach ($clients as $client) { + $client->setRepository($client->getOrganizationalUnit()?->getNetworkSettings()?->getRepository()); + $this->entityManager->persist($client); + $this->entityManager->flush(); + } + + $this->imageRepository->delete($repository); return null; }