Add missing COALESCE when summing visits counts

This commit is contained in:
Alejandro Celaya 2024-03-27 09:15:21 +01:00
parent 3d7b1ca799
commit 10e941cea6
4 changed files with 14 additions and 15 deletions

View File

@ -14,6 +14,11 @@ final readonly class Ordering
{
}
public static function none(): self
{
return new self();
}
/**
* @param array{string|null, string|null} $props
*/
@ -23,11 +28,6 @@ final readonly class Ordering
return new self($field, $dir ?? self::DEFAULT_DIR);
}
public static function none(): self
{
return new self(null, self::DEFAULT_DIR);
}
public static function fromFieldAsc(string $field): self
{
return new self($field, self::ASC_DIR);

View File

@ -30,7 +30,7 @@ class ShortUrlListRepository extends EntitySpecificationRepository implements Sh
{
$buildVisitsSubQuery = function (string $alias, bool $excludingBots): string {
$vqb = $this->getEntityManager()->createQueryBuilder();
$vqb->select('SUM(' . $alias . '.count)')
$vqb->select('COALESCE(SUM(' . $alias . '.count), 0)')
->from(ShortUrlVisitsCount::class, $alias)
->where($vqb->expr()->eq($alias . '.shortUrl', 's'));
@ -50,7 +50,7 @@ class ShortUrlListRepository extends EntitySpecificationRepository implements Sh
->setMaxResults($filtering->limit)
->setFirstResult($filtering->offset)
// This param is used in one of the sub-queries, but needs to set in the parent query
->setParameter('potentialBot', 0);
->setParameter('potentialBot', false);
$this->processOrderByForList($qb, $filtering);

View File

@ -128,7 +128,7 @@ final class ShortUrlVisitsCountTracker
$qb->expr()->eq('slot_id', ':slot_id'),
))
->setParameter('short_url_id', $shortUrlId)
->setParameter('potential_bot', $potentialBot)
->setParameter('potential_bot', $potentialBot ? '1' : '0')
->setParameter('slot_id', $slotId)
->setMaxResults(1);
@ -155,7 +155,7 @@ final class ShortUrlVisitsCountTracker
));
$writeQb->setParameter('short_url_id', $shortUrlId)
->setParameter('potential_bot', $potentialBot)
->setParameter('potential_bot', $potentialBot ? '1' : '0')
->setParameter('slot_id', $slotId)
->executeStatement();
}

View File

@ -115,12 +115,11 @@ class ShortUrlListRepositoryTest extends DatabaseTestCase
self::assertCount(3, $result);
self::assertSame($bar, $result[0]->shortUrl);
// FIXME Check why this assertion fails
// $result = $this->repo->findList(new ShortUrlsListFiltering(
// orderBy: Ordering::fromFieldDesc(OrderableField::NON_BOT_VISITS->value),
// ));
// self::assertCount(3, $result);
// self::assertSame($foo2, $result[0]->shortUrl);
$result = $this->repo->findList(new ShortUrlsListFiltering(
orderBy: Ordering::fromFieldDesc(OrderableField::NON_BOT_VISITS->value),
));
self::assertCount(3, $result);
self::assertSame($foo2, $result[0]->shortUrl);
$result = $this->repo->findList(new ShortUrlsListFiltering(
dateRange: DateRange::until(Chronos::now()->subDays(2)),