Fix the query to find staled entity workflows

Add a test to check that the query still works
This commit is contained in:
Julien Fastré 2024-09-19 12:31:46 +02:00
parent 3697aee584
commit d9b36533a2
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
2 changed files with 54 additions and 5 deletions

View File

@ -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();
}

View File

@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace ChillMainBundle\Tests\Repository;
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* @internal
*
* @coversNothing
*/
class EntityWorkflowRepositoryTest extends KernelTestCase
{
private EntityManagerInterface $em;
protected function setUp(): void
{
self::bootKernel();
$this->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');
}
}