From c9ce9a90fdcb38789e5963d47749d9b512218df0 Mon Sep 17 00:00:00 2001 From: Ferdinand Kuhl Date: Tue, 29 Jun 2021 14:23:25 +0200 Subject: [PATCH] Added a command to clean up old and obsolete sequence entries --- Classes/Command/SequenceCommandController.php | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/Classes/Command/SequenceCommandController.php b/Classes/Command/SequenceCommandController.php index 47e388b..d91a6aa 100644 --- a/Classes/Command/SequenceCommandController.php +++ b/Classes/Command/SequenceCommandController.php @@ -4,7 +4,9 @@ declare(strict_types=1); namespace DigiComp\Sequence\Command; +use DigiComp\Sequence\Domain\Model\Insert; use DigiComp\Sequence\Service\SequenceGenerator; +use Doctrine\ORM\EntityManagerInterface; use Neos\Flow\Annotations as Flow; use Neos\Flow\Cli\CommandController; @@ -21,6 +23,12 @@ class SequenceCommandController extends CommandController */ protected $sequenceGenerator; + /** + * @Flow\Inject + * @var EntityManagerInterface + */ + protected $entityManager; + /** * Sets minimum number for sequence generator * @@ -32,5 +40,28 @@ class SequenceCommandController extends CommandController $this->sequenceGenerator->advanceTo($to, $type); } - // TODO: make clean up job to delete all but the biggest number to save resources + /** + * @param string[] $typesToClean + */ + public function cleanSequenceInsertsCommand(array $typesToClean = []) + { + $cleanArray = []; + if (empty($typesToClean)) { + $results = $this->entityManager + ->createQuery('SELECT i.type, MAX(i.number) max_number FROM ' . Insert::class . ' i GROUP BY i.type') + ->getScalarResult(); + foreach ($results as $result) { + $cleanArray[$result['type']] = (int) $result['max_number']; + } + } else { + foreach ($typesToClean as $typeToClean) { + $cleanArray[$typeToClean] = $this->sequenceGenerator->getLastNumberFor($typeToClean); + } + } + foreach ($cleanArray as $typeToClean => $number) { + $this->entityManager + ->createQuery('DELETE FROM ' . Insert::class . ' i WHERE i.type = ?0 AND i.number < ?1') + ->execute([$typeToClean, $number]); + } + } }