Wrap energy decr in transaction
This commit is contained in:
parent
42ab9eb8f4
commit
2b0322d962
|
@ -18,7 +18,7 @@ async def store(conn: Connection, click: Click) -> int:
|
||||||
async def delete_user_info(conn: Connection, user_id: int):
|
async def delete_user_info(conn: Connection, user_id: int):
|
||||||
async with conn.transaction():
|
async with conn.transaction():
|
||||||
await conn.execute('DELETE FROM clicks WHERE user_id=$1', user_id)
|
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:
|
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 def decr_energy(conn: Connection, user_id: int, amount: int) -> Tuple[int, int]:
|
||||||
|
with conn.transaction('serializable'):
|
||||||
new_energy, spent = await conn.fetchrow('''
|
new_energy, spent = await conn.fetchrow('''
|
||||||
WITH energy_cte AS (
|
WITH energy_cte AS (
|
||||||
SELECT energy, (CASE WHEN energy < $2 THEN energy ELSE $2 END) AS delta FROM users WHERE id=$1
|
SELECT energy, (CASE WHEN energy < $2 THEN energy ELSE $2 END) AS delta FROM users WHERE id=$1
|
||||||
|
|
Loading…
Reference in New Issue
Block a user