Wrap energy decr in transaction
All checks were successful
CI/CD Pipeline / lint-sast (push) Successful in 2m47s
CI/CD Pipeline / deploy (push) Successful in 1m21s

This commit is contained in:
Michail Kostochka 2025-06-09 13:56:18 +03:00
parent 42ab9eb8f4
commit 0e4dfc890a

View File

@ -18,7 +18,7 @@ async def store(conn: Connection, click: Click) -> int:
async def delete_user_info(conn: Connection, user_id: int):
async with conn.transaction():
await conn.execute('DELETE FROM clicks WHERE user_id=$1', user_id)
await conn.execute('DELTE FROM users WHERE id=$1', user_id)
await conn.execute('DELETE FROM users WHERE id=$1', user_id)
async def get_period_sum(conn: Connection, user_id: int, period: int) -> decimal.Decimal:
@ -65,6 +65,7 @@ async def get_energy(conn: Connection, user_id: int) -> int:
async def decr_energy(conn: Connection, user_id: int, amount: int) -> Tuple[int, int]:
async with conn.transaction(isolation='serializable'):
new_energy, spent = await conn.fetchrow('''
WITH energy_cte AS (
SELECT energy, (CASE WHEN energy < $2 THEN energy ELSE $2 END) AS delta FROM users WHERE id=$1