Add missing default value for short url visits count

This commit is contained in:
Alejandro Celaya 2024-03-26 09:24:55 +01:00
parent 054eb42613
commit 6fbb5a380d
2 changed files with 25 additions and 21 deletions

View File

@ -28,6 +28,7 @@ return static function (ClassMetadata $metadata, array $emConfig): void {
$builder->createField('count', Types::BIGINT)
->columnName('count')
->option('unsigned', true)
->option('default', 1)
->build();
$builder->createField('slotId', Types::INTEGER)

View File

@ -108,17 +108,20 @@ final readonly class ShortUrlVisitsCountPreFlushListener
// For engines without a specific UPSERT syntax, do a regular locked select followed by an insert or update
$qb = $conn->createQueryBuilder();
$qb->select('id')
->from('short_url_visits_counts')
->where($qb->expr()->and(
$qb->expr()->eq('short_url_id', ':short_url_id'),
$qb->expr()->eq('potential_bot', ':potential_bot'),
$qb->expr()->eq('slot_id', ':slot_id'),
))
->setParameter('short_url_id', $shortUrlId)
->setParameter('potential_bot', $potentialBot)
->setParameter('slot_id', $slotId)
->forUpdate()
->setMaxResults(1);
->from('short_url_visits_counts')
->where($qb->expr()->and(
$qb->expr()->eq('short_url_id', ':short_url_id'),
$qb->expr()->eq('potential_bot', ':potential_bot'),
$qb->expr()->eq('slot_id', ':slot_id'),
))
->setParameter('short_url_id', $shortUrlId)
->setParameter('potential_bot', $potentialBot)
->setParameter('slot_id', $slotId)
->setMaxResults(1);
if ($conn->getDatabasePlatform()::class === SQLServerPlatform::class) {
$qb->forUpdate();
}
$resultSet = $qb->executeQuery()->fetchOne();
$writeQb = ! $resultSet
@ -130,17 +133,17 @@ final readonly class ShortUrlVisitsCountPreFlushListener
'slot_id' => ':slot_id',
])
: $conn->createQueryBuilder()
->update('short_url_visits_counts')
->set('count', 'count + 1')
->where($qb->expr()->and(
$qb->expr()->eq('short_url_id', ':short_url_id'),
$qb->expr()->eq('potential_bot', ':potential_bot'),
$qb->expr()->eq('slot_id', ':slot_id'),
));
->update('short_url_visits_counts')
->set('count', 'count + 1')
->where($qb->expr()->and(
$qb->expr()->eq('short_url_id', ':short_url_id'),
$qb->expr()->eq('potential_bot', ':potential_bot'),
$qb->expr()->eq('slot_id', ':slot_id'),
));
$writeQb->setParameter('short_url_id', $shortUrlId)
->setParameter('potential_bot', $potentialBot)
->setParameter('slot_id', $slotId)
->executeStatement();
->setParameter('potential_bot', $potentialBot)
->setParameter('slot_id', $slotId)
->executeStatement();
}
}