Skip to content

Commit 97b1780

Browse files
committed
migration
1 parent 25205d9 commit 97b1780

File tree

4 files changed

+33
-25
lines changed

4 files changed

+33
-25
lines changed

packages/postgres-database/src/simcore_postgres_database/utils_groups_extra_properties.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import datetime
22
import logging
33
import warnings
4+
from collections.abc import Callable
45
from dataclasses import dataclass, fields
5-
from typing import Any, Callable
6+
from typing import Any
67

78
import sqlalchemy as sa
89
from aiopg.sa.connection import SAConnection
@@ -22,12 +23,10 @@
2223
)
2324

2425

25-
class GroupExtraPropertiesError(Exception):
26-
...
26+
class GroupExtraPropertiesError(Exception): ...
2727

2828

29-
class GroupExtraPropertiesNotFoundError(GroupExtraPropertiesError):
30-
...
29+
class GroupExtraPropertiesNotFoundError(GroupExtraPropertiesError): ...
3130

3231

3332
@dataclass(frozen=True, slots=True, kw_only=True)
@@ -213,7 +212,6 @@ async def get_aggregated_properties_for_user_v2(
213212
product_name: str,
214213
) -> GroupExtraProperties:
215214
async with pass_or_acquire_connection(engine, connection) as conn:
216-
217215
list_stmt = _list_table_entries_ordered_by_group_type_stmt(
218216
user_id=user_id, product_name=product_name
219217
)

packages/postgres-database/src/simcore_postgres_database/utils_users.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
from typing import Any, Final
1010

1111
import sqlalchemy as sa
12-
from aiopg.sa.connection import SAConnection
13-
from aiopg.sa.result import RowProxy
12+
from common_library.async_tools import maybe_await
1413
from sqlalchemy import Column
1514

15+
from ._protocols import DBConnection
1616
from .aiopg_errors import UniqueViolation
1717
from .models.users import UserRole, UserStatus, users
1818
from .models.users_details import users_pre_registration_details
@@ -55,12 +55,12 @@ def generate_alternative_username(username: str) -> str:
5555
class UsersRepo:
5656
@staticmethod
5757
async def new_user(
58-
conn: SAConnection,
58+
conn: DBConnection,
5959
email: str,
6060
password_hash: str,
6161
status: UserStatus,
6262
expires_at: datetime | None,
63-
) -> RowProxy:
63+
) -> Any:
6464
data: dict[str, Any] = {
6565
"name": _generate_username_from_email(email),
6666
"email": email,
@@ -88,13 +88,15 @@ async def new_user(
8888
users.c.status,
8989
).where(users.c.id == user_id)
9090
)
91-
row = await result.first()
91+
row = await maybe_await(result.first())
92+
from aiopg.sa.result import RowProxy
93+
9294
assert isinstance(row, RowProxy) # nosec
9395
return row
9496

9597
@staticmethod
9698
async def join_and_update_from_pre_registration_details(
97-
conn: SAConnection, new_user_id: int, new_user_email: str
99+
conn: DBConnection, new_user_id: int, new_user_email: str
98100
) -> None:
99101
"""After a user is created, it can be associated with information provided during invitation
100102
@@ -111,6 +113,10 @@ async def join_and_update_from_pre_registration_details(
111113
.values(user_id=new_user_id)
112114
)
113115

116+
from aiopg.sa.result import ResultProxy
117+
118+
assert isinstance(result, ResultProxy) # nosec
119+
114120
if result.rowcount:
115121
pre_columns = (
116122
users_pre_registration_details.c.pre_first_name,
@@ -135,7 +141,7 @@ async def join_and_update_from_pre_registration_details(
135141
users_pre_registration_details.c.pre_email == new_user_email
136142
)
137143
)
138-
if details := await result.fetchone():
144+
if details := await maybe_await(result.fetchone()):
139145
await conn.execute(
140146
users.update()
141147
.where(users.c.id == new_user_id)
@@ -169,15 +175,14 @@ def get_billing_details_query(user_id: int):
169175
)
170176

171177
@staticmethod
172-
async def get_billing_details(conn: SAConnection, user_id: int) -> RowProxy | None:
178+
async def get_billing_details(conn: DBConnection, user_id: int) -> Any | None:
173179
result = await conn.execute(
174180
UsersRepo.get_billing_details_query(user_id=user_id)
175181
)
176-
value: RowProxy | None = await result.fetchone()
177-
return value
182+
return await maybe_await(result.fetchone())
178183

179184
@staticmethod
180-
async def get_role(conn: SAConnection, user_id: int) -> UserRole:
185+
async def get_role(conn: DBConnection, user_id: int) -> UserRole:
181186
value: UserRole | None = await conn.scalar(
182187
sa.select(users.c.role).where(users.c.id == user_id)
183188
)
@@ -188,7 +193,7 @@ async def get_role(conn: SAConnection, user_id: int) -> UserRole:
188193
raise UserNotFoundInRepoError
189194

190195
@staticmethod
191-
async def get_email(conn: SAConnection, user_id: int) -> str:
196+
async def get_email(conn: DBConnection, user_id: int) -> str:
192197
value: str | None = await conn.scalar(
193198
sa.select(users.c.email).where(users.c.id == user_id)
194199
)
@@ -199,7 +204,7 @@ async def get_email(conn: SAConnection, user_id: int) -> str:
199204
raise UserNotFoundInRepoError
200205

201206
@staticmethod
202-
async def get_active_user_email(conn: SAConnection, user_id: int) -> str:
207+
async def get_active_user_email(conn: DBConnection, user_id: int) -> str:
203208
value: str | None = await conn.scalar(
204209
sa.select(users.c.email).where(
205210
(users.c.status == UserStatus.ACTIVE) & (users.c.id == user_id)
@@ -212,7 +217,7 @@ async def get_active_user_email(conn: SAConnection, user_id: int) -> str:
212217
raise UserNotFoundInRepoError
213218

214219
@staticmethod
215-
async def is_email_used(conn: SAConnection, email: str) -> bool:
220+
async def is_email_used(conn: DBConnection, email: str) -> bool:
216221
email = email.lower()
217222

218223
registered = await conn.scalar(

packages/postgres-database/tests/test_utils_projects_nodes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
)
2929
from sqlalchemy.ext.asyncio import AsyncConnection
3030

31+
# NOTE: Temporary usage of connection_factory until asyncpg is used
32+
3133

3234
async def _delete_project(
3335
connection_factory: SAConnection, project_uuid: uuid.UUID

packages/postgres-database/tests/test_utils_users.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from pytest_simcore.helpers.faker_factories import random_user
1313
from simcore_postgres_database.models.users import UserRole, users
1414
from simcore_postgres_database.utils_users import UserNotFoundInRepoError, UsersRepo
15+
from sqlalchemy.ext.asyncio import AsyncConnection
1516

1617

1718
@pytest.fixture
@@ -25,11 +26,13 @@ async def user(connection: SAConnection, faker: Faker) -> dict[str, Any]:
2526
return data
2627

2728

28-
async def test_users_repo_get(connection: SAConnection, user: dict[str, Any]):
29+
async def test_users_repo_get(
30+
connection_factory: SAConnection | AsyncConnection, user: dict[str, Any]
31+
):
2932
repo = UsersRepo()
30-
31-
assert await repo.get_email(connection, user_id=user["id"]) == user["email"]
32-
assert await repo.get_role(connection, user_id=user["id"]) == user["role"]
33+
# NOTE: Temporary usage of connection_factory until asyncpg is used
34+
assert await repo.get_email(connection_factory, user_id=user["id"]) == user["email"]
35+
assert await repo.get_role(connection_factory, user_id=user["id"]) == user["role"]
3336

3437
with pytest.raises(UserNotFoundInRepoError):
35-
await repo.get_role(connection, user_id=55)
38+
await repo.get_role(connection_factory, user_id=55)

0 commit comments

Comments
 (0)