mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Create DB logic to delete orphan visits
This commit is contained in:
@@ -19,4 +19,13 @@ class VisitDeleterRepository extends EntitySpecificationRepository implements Vi
|
||||
|
||||
return $qb->getQuery()->execute();
|
||||
}
|
||||
|
||||
public function deleteOrphanVisits(): int
|
||||
{
|
||||
$qb = $this->getEntityManager()->createQueryBuilder();
|
||||
$qb->delete(Visit::class, 'v')
|
||||
->where($qb->expr()->isNull('v.shortUrl'));
|
||||
|
||||
return $qb->getQuery()->execute();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,4 +9,6 @@ use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
||||
interface VisitDeleterRepositoryInterface
|
||||
{
|
||||
public function deleteShortUrlVisits(ShortUrl $shortUrl): int;
|
||||
|
||||
public function deleteOrphanVisits(): int;
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ class VisitDeleterRepositoryTest extends DatabaseTestCase
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function deletesExpectedVisits(): void
|
||||
public function deletesExpectedShortUrlVisits(): void
|
||||
{
|
||||
$shortUrl1 = ShortUrl::withLongUrl('https://foo.com');
|
||||
$this->getEntityManager()->persist($shortUrl1);
|
||||
@@ -59,4 +59,21 @@ class VisitDeleterRepositoryTest extends DatabaseTestCase
|
||||
self::assertEquals(1, $this->repo->deleteShortUrlVisits($shortUrl3));
|
||||
self::assertEquals(0, $this->repo->deleteShortUrlVisits($shortUrl3));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function deletesExpectedOrphanVisits(): void
|
||||
{
|
||||
$visitor = Visitor::emptyInstance();
|
||||
$this->getEntityManager()->persist(Visit::forBasePath($visitor));
|
||||
$this->getEntityManager()->persist(Visit::forInvalidShortUrl($visitor));
|
||||
$this->getEntityManager()->persist(Visit::forRegularNotFound($visitor));
|
||||
$this->getEntityManager()->persist(Visit::forBasePath($visitor));
|
||||
$this->getEntityManager()->persist(Visit::forInvalidShortUrl($visitor));
|
||||
$this->getEntityManager()->persist(Visit::forRegularNotFound($visitor));
|
||||
|
||||
$this->getEntityManager()->flush();
|
||||
|
||||
self::assertEquals(6, $this->repo->deleteOrphanVisits());
|
||||
self::assertEquals(0, $this->repo->deleteOrphanVisits());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user