Skip to content

Commit 134c8ee

Browse files
committed
rm new db features
1 parent 40e4d3f commit 134c8ee

File tree

6 files changed

+46
-259
lines changed

6 files changed

+46
-259
lines changed

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

Lines changed: 0 additions & 97 deletions
This file was deleted.

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

Lines changed: 44 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
"""
33

44
import itertools
5+
from dataclasses import dataclass
56
from typing import TypedDict
67

7-
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncEngine
8+
from aiopg.sa.connection import SAConnection
89

9-
from .base_repo import MinimalRepo, get_or_create_connection, transaction_context
10-
from .models.tags import tags as tags_table
1110
from .tags_sql import (
1211
count_users_with_access_rights_stmt,
1312
create_tag_stmt,
@@ -50,46 +49,43 @@ class TagDict(TypedDict, total=True):
5049
delete: bool
5150

5251

52+
@dataclass(frozen=True)
5353
class TagsRepo:
54-
def __init__(self, engine: AsyncEngine):
55-
self.engine = engine
56-
self._impl = MinimalRepo(engine=engine, table=tags_table)
54+
user_id: int # Determines access-rights
5755

5856
async def access_count(
5957
self,
60-
user_id: int,
58+
conn: SAConnection,
6159
tag_id: int,
60+
*,
6261
read: bool | None = None,
6362
write: bool | None = None,
6463
delete: bool | None = None,
65-
connection: AsyncConnection | None = None,
6664
) -> int:
6765
"""
6866
Returns 0 if tag does not match access
6967
Returns >0 if it does and represents the number of groups granting this access to the user
7068
"""
71-
async with get_or_create_connection(self.engine, connection) as conn:
72-
count_stmt = count_users_with_access_rights_stmt(
73-
user_id=user_id, tag_id=tag_id, read=read, write=write, delete=delete
74-
)
75-
permissions_count: int | None = await conn.scalar(count_stmt)
76-
return permissions_count if permissions_count else 0
69+
count_stmt = count_users_with_access_rights_stmt(
70+
user_id=self.user_id, tag_id=tag_id, read=read, write=write, delete=delete
71+
)
72+
permissions_count: int | None = await conn.scalar(count_stmt)
73+
return permissions_count if permissions_count else 0
7774

7875
#
7976
# CRUD operations
8077
#
8178

8279
async def create(
8380
self,
81+
conn: SAConnection,
8482
*,
85-
user_id: int,
8683
name: str,
8784
color: str,
8885
description: str | None = None, # =nullable
8986
read: bool = True,
9087
write: bool = True,
9188
delete: bool = True,
92-
connection: AsyncConnection | None = None,
9389
) -> TagDict:
9490
values = {
9591
"name": name,
@@ -98,58 +94,44 @@ async def create(
9894
if description:
9995
values["description"] = description
10096

101-
async with transaction_context(self.engine, connection) as conn:
97+
async with conn.begin():
10298
# insert new tag
10399
insert_stmt = create_tag_stmt(**values)
104100
result = await conn.execute(insert_stmt)
105-
tag = result.first()
101+
tag = await result.first()
106102
assert tag # nosec
107103

108104
# take tag ownership
109105
access_stmt = set_tag_access_rights_stmt(
110106
tag_id=tag.id,
111-
user_id=user_id,
107+
user_id=self.user_id,
112108
read=read,
113109
write=write,
114110
delete=delete,
115111
)
116112
result = await conn.execute(access_stmt)
117-
access = result.first()
113+
access = await result.first()
118114
assert access
119115

120116
return TagDict(itertools.chain(tag.items(), access.items())) # type: ignore
121117

122-
async def list_all(
123-
self,
124-
*,
125-
user_id: int,
126-
connection: AsyncConnection | None = None,
127-
) -> list[TagDict]:
128-
async with get_or_create_connection(self.engine, connection) as conn:
129-
stmt_list = list_tags_stmt(user_id=user_id)
130-
return [TagDict(row.items()) async for row in conn.execute(stmt_list)] # type: ignore
131-
132-
async def get(
133-
self,
134-
user_id: int,
135-
tag_id: int,
136-
connection: AsyncConnection | None = None,
137-
) -> TagDict:
138-
async with get_or_create_connection(self.engine, connection) as conn:
139-
stmt_get = get_tag_stmt(user_id=user_id, tag_id=tag_id)
140-
result = await conn.execute(stmt_get)
141-
row = result.first()
142-
if not row:
143-
msg = f"{tag_id=} not found: either no access or does not exists"
144-
raise TagNotFoundError(msg)
145-
return TagDict(row.items()) # type: ignore
118+
async def list_all(self, conn: SAConnection) -> list[TagDict]:
119+
stmt_list = list_tags_stmt(user_id=self.user_id)
120+
return [TagDict(row.items()) async for row in conn.execute(stmt_list)] # type: ignore
121+
122+
async def get(self, conn: SAConnection, tag_id: int) -> TagDict:
123+
stmt_get = get_tag_stmt(user_id=self.user_id, tag_id=tag_id)
124+
result = await conn.execute(stmt_get)
125+
row = await result.first()
126+
if not row:
127+
msg = f"{tag_id=} not found: either no access or does not exists"
128+
raise TagNotFoundError(msg)
129+
return TagDict(row.items()) # type: ignore
146130

147131
async def update(
148132
self,
149-
*,
133+
conn: SAConnection,
150134
tag_id: int,
151-
user_id: int,
152-
connection: AsyncConnection | None = None,
153135
**fields,
154136
) -> TagDict:
155137
updates = {
@@ -160,28 +142,21 @@ async def update(
160142

161143
if not updates:
162144
# no updates == get
163-
return await self.get(user_id=user_id, tag_id=tag_id, connection=connection)
145+
return await self.get(conn, tag_id=tag_id)
164146

165-
async with get_or_create_connection(self.engine, connection) as conn:
166-
update_stmt = update_tag_stmt(user_id=user_id, tag_id=tag_id, **updates)
167-
result = await conn.execute(update_stmt)
168-
row = result.first()
169-
if not row:
170-
msg = f"{tag_id=} not updated: either no access or not found"
171-
raise TagOperationNotAllowedError(msg)
147+
update_stmt = update_tag_stmt(user_id=self.user_id, tag_id=tag_id, **updates)
148+
result = await conn.execute(update_stmt)
149+
row = await result.first()
150+
if not row:
151+
msg = f"{tag_id=} not updated: either no access or not found"
152+
raise TagOperationNotAllowedError(msg)
172153

173-
return TagDict(row.items()) # type: ignore
154+
return TagDict(row.items()) # type: ignore
174155

175-
async def delete(
176-
self,
177-
*,
178-
user_id: int,
179-
tag_id: int,
180-
connection: AsyncConnection | None = None,
181-
) -> None:
182-
async with get_or_create_connection(self.engine, connection) as conn:
183-
stmt_delete = delete_tag_stmt(user_id=user_id, tag_id=tag_id)
184-
deleted = await conn.scalar(stmt_delete)
185-
if not deleted:
186-
msg = f"Could not delete {tag_id=}. Not found or insuficient access."
187-
raise TagOperationNotAllowedError(msg)
156+
async def delete(self, conn: SAConnection, tag_id: int) -> None:
157+
stmt_delete = delete_tag_stmt(user_id=self.user_id, tag_id=tag_id)
158+
159+
deleted = await conn.scalar(stmt_delete)
160+
if not deleted:
161+
msg = f"Could not delete {tag_id=}. Not found or insuficient access."
162+
raise TagOperationNotAllowedError(msg)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def get_tags_for_project_stmt(*, project_index: int):
172172
)
173173

174174

175-
def add_tag_to_project_stmt(*, project_index: int, tag_id: int) -> None:
175+
def add_tag_to_project_stmt(*, project_index: int, tag_id: int):
176176
return (
177177
pg_insert(projects_tags)
178178
.values(
@@ -190,7 +190,7 @@ def get_tags_for_services_stmt(*, key: str, version: str):
190190
)
191191

192192

193-
def add_tag_to_services_stmt(*, key: str, version: str, tag_id: int) -> None:
193+
def add_tag_to_services_stmt(*, key: str, version: str, tag_id: int):
194194
return (
195195
pg_insert(services_tags)
196196
.values(

packages/postgres-database/tests/test_base_repo.py

Lines changed: 0 additions & 48 deletions
This file was deleted.

services/web/server/src/simcore_service_webserver/tags/_api.py

Whitespace-only changes.

0 commit comments

Comments
 (0)