mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Fixed ApiKeyRepository for MS and Postgres
This commit is contained in:
parent
eed7b6e565
commit
1966367caf
@ -14,13 +14,16 @@ class ApiKeyRepository extends EntitySpecificationRepository implements ApiKeyRe
|
|||||||
{
|
{
|
||||||
$em = $this->getEntityManager();
|
$em = $this->getEntityManager();
|
||||||
$em->wrapInTransaction(function () use ($apiKey, $em): void {
|
$em->wrapInTransaction(function () use ($apiKey, $em): void {
|
||||||
$amountOfApiKeys = $em->createQueryBuilder()->select('COUNT(a.id)')
|
// Ideally this would be a SELECT COUNT(...), but MsSQL and Postgres do not allow locking on aggregates
|
||||||
->from(ApiKey::class, 'a')
|
// Because of that we check if at least one result exists
|
||||||
->getQuery()
|
$firstResult = $em->createQueryBuilder()->select('a.id')
|
||||||
->setLockMode(LockMode::PESSIMISTIC_WRITE)
|
->from(ApiKey::class, 'a')
|
||||||
->getSingleScalarResult();
|
->setMaxResults(1)
|
||||||
|
->getQuery()
|
||||||
|
->setLockMode(LockMode::PESSIMISTIC_WRITE)
|
||||||
|
->getOneOrNullResult();
|
||||||
|
|
||||||
if ($amountOfApiKeys === 0) {
|
if ($firstResult === null) {
|
||||||
$em->persist(ApiKey::fromKey($apiKey));
|
$em->persist(ApiKey::fromKey($apiKey));
|
||||||
$em->flush();
|
$em->flush();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user