github-actions[bot]
Automated Deploy: 0d5b759d8eca219a25b3ab717925e73a66bbf4a4
2756f0f
Raw History Blame Contribute Delete
1.09 kB
"""Subscription rows, keyed by the store's stable transaction id."""
from sqlalchemy import select
from sqlalchemy.dialects.postgresql import insert
from app.common.base_repository import BaseRepository
from app.modules.billing.models import Subscription
class SubscriptionRepository(BaseRepository[Subscription]):
model = Subscription
async def lock_by_original_id(self, original_transaction_id: str) -> Subscription | None:
result = await self.session.execute(select(Subscription).where(
Subscription.original_transaction_id == original_transaction_id).with_for_update())
return result.scalars().first()
async def insert_if_absent(self, **values) -> bool:
"""Inserts unless the original_transaction_id exists (concurrent posts are safe). Returns True if inserted."""
result = await self.session.execute(
insert(Subscription).values(**values)
.on_conflict_do_nothing(index_elements=[Subscription.original_transaction_id])
.returning(Subscription.id))
return result.scalar() is not None