From d9b36533a26c27a05075ef3abaebc12046672bbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 19 Sep 2024 12:31:46 +0200 Subject: [PATCH] Fix the query to find staled entity workflows Add a test to check that the query still works --- .../Workflow/EntityWorkflowRepository.php | 13 ++++-- .../EntityWorkflowRepositoryTest.php | 46 +++++++++++++++++++ 2 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Tests/Repository/EntityWorkflowRepositoryTest.php diff --git a/src/Bundle/ChillMainBundle/Repository/Workflow/EntityWorkflowRepository.php b/src/Bundle/ChillMainBundle/Repository/Workflow/EntityWorkflowRepository.php index 62282fbe9..13c900ffb 100644 --- a/src/Bundle/ChillMainBundle/Repository/Workflow/EntityWorkflowRepository.php +++ b/src/Bundle/ChillMainBundle/Repository/Workflow/EntityWorkflowRepository.php @@ -13,6 +13,7 @@ namespace Chill\MainBundle\Repository\Workflow; use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\Workflow\EntityWorkflow; +use Chill\MainBundle\Entity\Workflow\EntityWorkflowStep; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; use Doctrine\ORM\QueryBuilder; @@ -211,17 +212,19 @@ class EntityWorkflowRepository implements ObjectRepository $qb->select('sw.id') // only the workflow which are not finalized - ->where('NOT EXISTS (SELECT 1 FROM chill_main_entity_workflow_step ews WHERE ews.isFinal = TRUE AND ews.entityWorkflow = sw.id)') + ->where(sprintf('NOT EXISTS (SELECT 1 FROM %s ews WHERE ews.isFinal = TRUE AND ews.entityWorkflow = sw.id)', EntityWorkflowStep::class)) ->andWhere( $qb->expr()->orX( - // only the workflow where all the last transition is older than transitionAt - ':olderThanDate > ALL (SELECT ews.transitionAt FROM chill_main_entity_workflow_step ews WHERE ews.transitionAt IS NOT NULL AND ews.entityWorkflow = sw.id)', + // only the workflow where all the transitions are older than transitionAt + sprintf(':olderThanDate > ALL (SELECT ews2.transitionAt FROM %s ews2 WHERE ews2.transitionAt IS NOT NULL AND ews2.entityWorkflow = sw)', EntityWorkflowStep::class), // or the workflow which have only the initial step, with no transition - '1 = (SELECT COUNT(ews.id) FROM chill_main_entity_workflow_step ews WHERE ews.step = :initial AND ews.transitionAt IS NULL AND ews.createdAt < :olderThanDate AND ews.entityWorkflow = sw.id)', + sprintf('1 = (SELECT COUNT(ews3.id) FROM %s ews3 WHERE ews3.currentStep = :initial AND ews3.transitionAt IS NULL AND ews3.entityWorkflow = sw)', EntityWorkflowStep::class), ) ) ->andWhere('sw.createdAt < :olderThanDate') - ->setParameter('olderThanDate', $olderThanDate); + ->setParameter('olderThanDate', $olderThanDate) + ->setParameter('initial', 'initial') + ; return $qb->getQuery()->getResult(); } diff --git a/src/Bundle/ChillMainBundle/Tests/Repository/EntityWorkflowRepositoryTest.php b/src/Bundle/ChillMainBundle/Tests/Repository/EntityWorkflowRepositoryTest.php new file mode 100644 index 000000000..4efb8196e --- /dev/null +++ b/src/Bundle/ChillMainBundle/Tests/Repository/EntityWorkflowRepositoryTest.php @@ -0,0 +1,46 @@ +em = static::getContainer()->get(EntityManagerInterface::class); + } + + /** + * This test only check that the query is ok. + * + * The semantic of the query is not checked. + */ + public function testFindWorkflowsWithoutFinalStepAndOlderThanCheckQueryIsOk(): void + { + $repository = new EntityWorkflowRepository($this->em); + + $actual = $repository->findWorkflowsWithoutFinalStepAndOlderThan(new \DateTimeImmutable('10 years ago')); + + self::assertIsArray($actual, 'check that the query is successful'); + } +}