Added a command to clean up old and obsolete sequence entries

This commit is contained in:
Ferdinand Kuhl 2021-06-29 14:23:25 +02:00
parent 8d2c150141
commit c9ce9a90fd

View file

@ -4,7 +4,9 @@ declare(strict_types=1);
namespace DigiComp\Sequence\Command; namespace DigiComp\Sequence\Command;
use DigiComp\Sequence\Domain\Model\Insert;
use DigiComp\Sequence\Service\SequenceGenerator; use DigiComp\Sequence\Service\SequenceGenerator;
use Doctrine\ORM\EntityManagerInterface;
use Neos\Flow\Annotations as Flow; use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController; use Neos\Flow\Cli\CommandController;
@ -21,6 +23,12 @@ class SequenceCommandController extends CommandController
*/ */
protected $sequenceGenerator; protected $sequenceGenerator;
/**
* @Flow\Inject
* @var EntityManagerInterface
*/
protected $entityManager;
/** /**
* Sets minimum number for sequence generator * Sets minimum number for sequence generator
* *
@ -32,5 +40,28 @@ class SequenceCommandController extends CommandController
$this->sequenceGenerator->advanceTo($to, $type); $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]);
}
}
} }