Skip to content

Commit 3af1330

Browse files
committed
aiopg vs asyncpg
1 parent 7178538 commit 3af1330

File tree

34 files changed

+161
-154
lines changed

34 files changed

+161
-154
lines changed

packages/pytest-simcore/src/pytest_simcore/helpers/webserver_workspaces.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from simcore_postgres_database.models.workspaces_access_rights import (
66
workspaces_access_rights,
77
)
8-
from simcore_service_webserver.db.plugin import get_database_engine
8+
from simcore_service_webserver.db.plugin import get_aiopg_engine
99
from sqlalchemy.dialects.postgresql import insert as pg_insert
1010

1111

@@ -18,7 +18,7 @@ async def update_or_insert_workspace_group(
1818
write: bool,
1919
delete: bool,
2020
) -> None:
21-
async with get_database_engine(app).acquire() as conn:
21+
async with get_aiopg_engine(app).acquire() as conn:
2222
insert_stmt = pg_insert(workspaces_access_rights).values(
2323
workspace_id=workspace_id,
2424
gid=group_id,

services/web/server/src/simcore_service_webserver/db/plugin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ def setup_db(app: web.Application):
3030

3131

3232
# aiopg helpers
33-
get_database_engine = _aiopg.get_database_engine
34-
get_engine_state = _aiopg.get_engine_state
33+
get_aiopg_engine = _aiopg.get_database_engine
34+
get_aiopg_engine_state = _aiopg.get_engine_state
3535
is_service_responsive = _aiopg.is_service_responsive
3636
is_service_enabled = _aiopg.is_service_enabled
3737

3838
# asyncpg helpers
39-
get_async_engine = _asyncpg.get_async_engine
39+
get_asyncpg_engine = _asyncpg.get_async_engine

services/web/server/src/simcore_service_webserver/diagnostics/_handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def _get_client_session_info():
116116
async def _check_pg():
117117
check.services["postgres"] = {
118118
"healthy": await plugin.is_service_responsive(request.app),
119-
"pool": plugin.get_engine_state(request.app),
119+
"pool": plugin.get_aiopg_engine_state(request.app),
120120
}
121121

122122
async def _check_storage():

services/web/server/src/simcore_service_webserver/director_v2/_handlers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
from .._constants import RQ_PRODUCT_KEY
2929
from .._meta import API_VTAG as VTAG
30-
from ..db.plugin import get_database_engine
30+
from ..db.plugin import get_aiopg_engine
3131
from ..login.decorators import login_required
3232
from ..products import api as products_api
3333
from ..security.decorators import permission_required
@@ -91,7 +91,7 @@ async def start_computation(request: web.Request) -> web.Response:
9191
X_SIMCORE_USER_AGENT, UNDEFINED_DEFAULT_SIMCORE_USER_AGENT_VALUE
9292
)
9393

94-
async with get_database_engine(request.app).acquire() as conn:
94+
async with get_aiopg_engine(request.app).acquire() as conn:
9595
group_properties = (
9696
await GroupExtraPropertiesRepo.get_aggregated_properties_for_user(
9797
conn, user_id=req_ctx.user_id, product_name=req_ctx.product_name

services/web/server/src/simcore_service_webserver/folders/_folders_db.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from sqlalchemy.orm import aliased
2323
from sqlalchemy.sql import asc, desc, select
2424

25-
from ..db.plugin import get_database_engine
25+
from ..db.plugin import get_aiopg_engine
2626
from .errors import FolderAccessForbiddenError, FolderNotFoundError
2727

2828
_logger = logging.getLogger(__name__)
@@ -54,7 +54,7 @@ async def create(
5454
user_id is not None and workspace_id is not None
5555
), "Both user_id and workspace_id cannot be provided at the same time. Please provide only one."
5656

57-
async with get_database_engine(app).acquire() as conn:
57+
async with get_aiopg_engine(app).acquire() as conn:
5858
result = await conn.execute(
5959
folders_v2.insert()
6060
.values(
@@ -117,7 +117,7 @@ async def list_(
117117
list_query = base_query.order_by(desc(getattr(folders_v2.c, order_by.field)))
118118
list_query = list_query.offset(offset).limit(limit)
119119

120-
async with get_database_engine(app).acquire() as conn:
120+
async with get_aiopg_engine(app).acquire() as conn:
121121
count_result = await conn.execute(count_query)
122122
total_count = await count_result.scalar()
123123

@@ -142,7 +142,7 @@ async def get(
142142
)
143143
)
144144

145-
async with get_database_engine(app).acquire() as conn:
145+
async with get_aiopg_engine(app).acquire() as conn:
146146
result = await conn.execute(query)
147147
row = await result.first()
148148
if row is None:
@@ -178,7 +178,7 @@ async def get_for_user_or_workspace(
178178
else:
179179
query = query.where(folders_v2.c.workspace_id == workspace_id)
180180

181-
async with get_database_engine(app).acquire() as conn:
181+
async with get_aiopg_engine(app).acquire() as conn:
182182
result = await conn.execute(query)
183183
row = await result.first()
184184
if row is None:
@@ -196,7 +196,7 @@ async def update(
196196
parent_folder_id: FolderID | None,
197197
product_name: ProductName,
198198
) -> FolderDB:
199-
async with get_database_engine(app).acquire() as conn:
199+
async with get_aiopg_engine(app).acquire() as conn:
200200
result = await conn.execute(
201201
folders_v2.update()
202202
.values(
@@ -222,7 +222,7 @@ async def delete_recursively(
222222
folder_id: FolderID,
223223
product_name: ProductName,
224224
) -> None:
225-
async with get_database_engine(app).acquire() as conn, conn.begin():
225+
async with get_aiopg_engine(app).acquire() as conn, conn.begin():
226226
# Step 1: Define the base case for the recursive CTE
227227
base_query = select(
228228
folders_v2.c.folder_id, folders_v2.c.parent_folder_id
@@ -276,7 +276,7 @@ async def get_projects_recursively_only_if_user_is_owner(
276276
or the `users_to_groups` table for private workspace projects.
277277
"""
278278

279-
async with get_database_engine(app).acquire() as conn, conn.begin():
279+
async with get_aiopg_engine(app).acquire() as conn, conn.begin():
280280
# Step 1: Define the base case for the recursive CTE
281281
base_query = select(
282282
folders_v2.c.folder_id, folders_v2.c.parent_folder_id

services/web/server/src/simcore_service_webserver/groups/_classifiers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
)
2727
from simcore_postgres_database.models.classifiers import group_classifiers
2828

29-
from ..db.plugin import get_database_engine
29+
from ..db.plugin import get_aiopg_engine
3030
from ..scicrunch.db import ResearchResourceRepository
3131
from ..scicrunch.service_client import SciCrunch
3232

@@ -75,7 +75,7 @@ class Classifiers(BaseModel):
7575

7676
class GroupClassifierRepository:
7777
def __init__(self, app: web.Application):
78-
self.engine = get_database_engine(app)
78+
self.engine = get_aiopg_engine(app)
7979

8080
async def _get_bundle(self, gid: int) -> RowProxy | None:
8181
async with self.engine.acquire() as conn:

services/web/server/src/simcore_service_webserver/groups/api.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from models_library.groups import Group
77
from models_library.users import GroupID, UserID
88

9-
from ..db.plugin import get_database_engine
9+
from ..db.plugin import get_aiopg_engine
1010
from ..users.api import get_user
1111
from . import _db
1212
from ._utils import AccessRightsDict
@@ -21,15 +21,15 @@ async def list_user_groups_with_read_access(
2121
"""
2222
# NOTE: Careful! It seems we are filtering out groups, such as Product Groups,
2323
# because they do not have read access. I believe this was done because the frontend did not want to display them.
24-
async with get_database_engine(app).acquire() as conn:
24+
async with get_aiopg_engine(app).acquire() as conn:
2525
return await _db.get_all_user_groups_with_read_access(conn, user_id=user_id)
2626

2727

2828
async def list_all_user_groups(app: web.Application, user_id: UserID) -> list[Group]:
2929
"""
3030
Return all user groups
3131
"""
32-
async with get_database_engine(app).acquire() as conn:
32+
async with get_aiopg_engine(app).acquire() as conn:
3333
groups_db = await _db.get_all_user_groups(conn, user_id=user_id)
3434

3535
return [Group.construct(**group.dict()) for group in groups_db]
@@ -44,7 +44,7 @@ async def get_user_group(
4444
raises GroupNotFoundError
4545
raises UserInsufficientRightsError
4646
"""
47-
async with get_database_engine(app).acquire() as conn:
47+
async with get_aiopg_engine(app).acquire() as conn:
4848
return await _db.get_user_group(conn, user_id=user_id, gid=gid)
4949

5050

@@ -55,7 +55,7 @@ async def get_product_group_for_user(
5555
Returns product's group if user belongs to it, otherwise it
5656
raises GroupNotFoundError
5757
"""
58-
async with get_database_engine(app).acquire() as conn:
58+
async with get_aiopg_engine(app).acquire() as conn:
5959
return await _db.get_product_group_for_user(
6060
conn, user_id=user_id, product_gid=product_gid
6161
)
@@ -64,7 +64,7 @@ async def get_product_group_for_user(
6464
async def create_user_group(
6565
app: web.Application, user_id: UserID, new_group: dict
6666
) -> dict[str, Any]:
67-
async with get_database_engine(app).acquire() as conn:
67+
async with get_aiopg_engine(app).acquire() as conn:
6868
return await _db.create_user_group(conn, user_id=user_id, new_group=new_group)
6969

7070

@@ -74,7 +74,7 @@ async def update_user_group(
7474
gid: GroupID,
7575
new_group_values: dict[str, str],
7676
) -> dict[str, str]:
77-
async with get_database_engine(app).acquire() as conn:
77+
async with get_aiopg_engine(app).acquire() as conn:
7878
return await _db.update_user_group(
7979
conn, user_id=user_id, gid=gid, new_group_values=new_group_values
8080
)
@@ -83,28 +83,28 @@ async def update_user_group(
8383
async def delete_user_group(
8484
app: web.Application, user_id: UserID, gid: GroupID
8585
) -> None:
86-
async with get_database_engine(app).acquire() as conn:
86+
async with get_aiopg_engine(app).acquire() as conn:
8787
return await _db.delete_user_group(conn, user_id=user_id, gid=gid)
8888

8989

9090
async def list_users_in_group(
9191
app: web.Application, user_id: UserID, gid: GroupID
9292
) -> list[dict[str, str]]:
93-
async with get_database_engine(app).acquire() as conn:
93+
async with get_aiopg_engine(app).acquire() as conn:
9494
return await _db.list_users_in_group(conn, user_id=user_id, gid=gid)
9595

9696

9797
async def auto_add_user_to_groups(app: web.Application, user_id: UserID) -> None:
9898
user: dict = await get_user(app, user_id)
9999

100-
async with get_database_engine(app).acquire() as conn:
100+
async with get_aiopg_engine(app).acquire() as conn:
101101
return await _db.auto_add_user_to_groups(conn, user=user)
102102

103103

104104
async def auto_add_user_to_product_group(
105105
app: web.Application, user_id: UserID, product_name: str
106106
) -> GroupID:
107-
async with get_database_engine(app).acquire() as conn:
107+
async with get_aiopg_engine(app).acquire() as conn:
108108
return await _db.auto_add_user_to_product_group(
109109
conn, user_id=user_id, product_name=product_name
110110
)
@@ -113,7 +113,7 @@ async def auto_add_user_to_product_group(
113113
async def is_user_by_email_in_group(
114114
app: web.Application, user_email: LowerCaseEmailStr, group_id: GroupID
115115
) -> bool:
116-
async with get_database_engine(app).acquire() as conn:
116+
async with get_aiopg_engine(app).acquire() as conn:
117117
return await _db.is_user_by_email_in_group(
118118
conn,
119119
email=user_email,
@@ -141,7 +141,7 @@ async def add_user_in_group(
141141
msg = "Invalid method call, missing user id or user email"
142142
raise GroupsError(msg)
143143

144-
async with get_database_engine(app).acquire() as conn:
144+
async with get_aiopg_engine(app).acquire() as conn:
145145
if new_user_email:
146146
user: RowProxy = await _db.get_user_from_email(conn, new_user_email)
147147
new_user_id = user["id"]
@@ -162,7 +162,7 @@ async def add_user_in_group(
162162
async def get_user_in_group(
163163
app: web.Application, user_id: UserID, gid: GroupID, the_user_id_in_group: int
164164
) -> dict[str, str]:
165-
async with get_database_engine(app).acquire() as conn:
165+
async with get_aiopg_engine(app).acquire() as conn:
166166
return await _db.get_user_in_group(
167167
conn, user_id=user_id, gid=gid, the_user_id_in_group=the_user_id_in_group
168168
)
@@ -175,7 +175,7 @@ async def update_user_in_group(
175175
the_user_id_in_group: int,
176176
new_values_for_user_in_group: dict,
177177
) -> dict[str, str]:
178-
async with get_database_engine(app).acquire() as conn:
178+
async with get_aiopg_engine(app).acquire() as conn:
179179
return await _db.update_user_in_group(
180180
conn,
181181
user_id=user_id,
@@ -188,14 +188,14 @@ async def update_user_in_group(
188188
async def delete_user_in_group(
189189
app: web.Application, user_id: UserID, gid: GroupID, the_user_id_in_group: int
190190
) -> None:
191-
async with get_database_engine(app).acquire() as conn:
191+
async with get_aiopg_engine(app).acquire() as conn:
192192
return await _db.delete_user_in_group(
193193
conn, user_id=user_id, gid=gid, the_user_id_in_group=the_user_id_in_group
194194
)
195195

196196

197197
async def get_group_from_gid(app: web.Application, gid: GroupID) -> Group | None:
198-
async with get_database_engine(app).acquire() as conn:
198+
async with get_aiopg_engine(app).acquire() as conn:
199199
group_db = await _db.get_group_from_gid(conn, gid=gid)
200200

201201
if group_db:

services/web/server/src/simcore_service_webserver/login/_auth_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from simcore_postgres_database.models.users import UserStatus
77
from simcore_postgres_database.utils_users import UsersRepo
88

9-
from ..db.plugin import get_database_engine
9+
from ..db.plugin import get_aiopg_engine
1010
from ..groups.api import is_user_by_email_in_group
1111
from ..products.api import Product
1212
from ..security.api import check_password, encrypt_password
@@ -30,7 +30,7 @@ async def create_user(
3030
expires_at: datetime | None,
3131
) -> dict[str, Any]:
3232

33-
async with get_database_engine(app).acquire() as conn:
33+
async with get_aiopg_engine(app).acquire() as conn:
3434
user = await UsersRepo.new_user(
3535
conn,
3636
email=email,

services/web/server/src/simcore_service_webserver/login/handlers_change.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from servicelib.mimetype_constants import MIMETYPE_APPLICATION_JSON
99
from servicelib.request_keys import RQT_USERID_KEY
1010
from simcore_postgres_database.utils_users import UsersRepo
11-
from simcore_service_webserver.db.plugin import get_database_engine
11+
from simcore_service_webserver.db.plugin import get_aiopg_engine
1212

1313
from .._meta import API_VTAG
1414
from ..products.api import Product, get_current_product
@@ -149,7 +149,7 @@ async def submit_request_to_change_email(request: web.Request):
149149
if user["email"] == request_body.email:
150150
return flash_response("Email changed")
151151

152-
async with get_database_engine(request.app).acquire() as conn:
152+
async with get_aiopg_engine(request.app).acquire() as conn:
153153
if await UsersRepo.is_email_used(conn, email=request_body.email):
154154
raise web.HTTPUnprocessableEntity(reason="This email cannot be used")
155155

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pydantic import BaseModel, PositiveInt
1010
from simcore_postgres_database.utils_payments_autorecharge import AutoRechargeStmts
1111

12-
from ..db.plugin import get_database_engine
12+
from ..db.plugin import get_aiopg_engine
1313
from .errors import InvalidPaymentMethodError
1414

1515
_logger = logging.getLogger(__name__)
@@ -34,7 +34,7 @@ async def get_wallet_autorecharge(
3434
*,
3535
wallet_id: WalletID,
3636
) -> PaymentsAutorechargeDB | None:
37-
async with get_database_engine(app).acquire() as conn:
37+
async with get_aiopg_engine(app).acquire() as conn:
3838
stmt = AutoRechargeStmts.get_wallet_autorecharge(wallet_id)
3939
result = await conn.execute(stmt)
4040
row = await result.first()
@@ -53,7 +53,7 @@ async def replace_wallet_autorecharge(
5353
InvalidPaymentMethodError: if `new` includes some invalid 'primary_payment_method_id'
5454
5555
"""
56-
async with get_database_engine(app).acquire() as conn:
56+
async with get_aiopg_engine(app).acquire() as conn:
5757
stmt = AutoRechargeStmts.is_valid_payment_method(
5858
user_id=user_id,
5959
wallet_id=new.wallet_id,

0 commit comments

Comments
 (0)