Skip to content

Commit 1dc3725

Browse files
committed
🐛 Fix: update database connection handling in autorecharge functions to use asyncpg engine
1 parent 2e5d5a0 commit 1dc3725

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

services/web/server/src/simcore_service_webserver/payments/_autorecharge_db.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
from models_library.wallets import WalletID
99
from pydantic import BaseModel, ConfigDict, PositiveInt
1010
from simcore_postgres_database.utils_payments_autorecharge import AutoRechargeStmts
11+
from simcore_postgres_database.utils_repos import (
12+
pass_or_acquire_connection,
13+
transaction_context,
14+
)
15+
from sqlalchemy.ext.asyncio import AsyncConnection
1116

12-
from ..db.plugin import get_database_engine_legacy
17+
from ..db.plugin import get_asyncpg_engine
1318
from .errors import InvalidPaymentMethodError
1419

1520
_logger = logging.getLogger(__name__)
@@ -29,18 +34,20 @@ class PaymentsAutorechargeGetDB(BaseModel):
2934

3035
async def get_wallet_autorecharge(
3136
app: web.Application,
37+
connection: AsyncConnection | None = None,
3238
*,
3339
wallet_id: WalletID,
3440
) -> PaymentsAutorechargeGetDB | None:
35-
async with get_database_engine_legacy(app).acquire() as conn:
41+
async with pass_or_acquire_connection(get_asyncpg_engine(app), connection) as conn:
3642
stmt = AutoRechargeStmts.get_wallet_autorecharge(wallet_id)
3743
result = await conn.execute(stmt)
38-
row = await result.first()
44+
row = result.one_or_none()
3945
return PaymentsAutorechargeGetDB.model_validate(row) if row else None
4046

4147

4248
async def replace_wallet_autorecharge(
4349
app: web.Application,
50+
connection: AsyncConnection | None = None,
4451
*,
4552
user_id: UserID,
4653
wallet_id: WalletID,
@@ -51,7 +58,7 @@ async def replace_wallet_autorecharge(
5158
InvalidPaymentMethodError: if `new` includes some invalid 'primary_payment_method_id'
5259
5360
"""
54-
async with get_database_engine_legacy(app).acquire() as conn:
61+
async with transaction_context(get_asyncpg_engine(app), connection) as conn:
5562
stmt = AutoRechargeStmts.is_valid_payment_method(
5663
user_id=user_id,
5764
wallet_id=new.wallet_id,
@@ -71,6 +78,5 @@ async def replace_wallet_autorecharge(
7178
monthly_limit_in_usd=new.monthly_limit_in_usd,
7279
)
7380
result = await conn.execute(stmt)
74-
row = await result.first()
75-
assert row # nosec
81+
row = result.one()
7682
return PaymentsAutorechargeGetDB.model_validate(row)

0 commit comments

Comments
 (0)