Skip to content

Commit f60a38f

Browse files
committed
use api to get engine from app
1 parent b0ec158 commit f60a38f

File tree

6 files changed

+17
-15
lines changed

6 files changed

+17
-15
lines changed

services/web/server/src/simcore_service_webserver/api_keys/_db.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
from models_library.basic_types import IdInt
1111
from models_library.products import ProductName
1212
from models_library.users import UserID
13-
from servicelib.aiohttp.application_keys import APP_AIOPG_ENGINE_KEY
1413
from simcore_postgres_database.models.api_keys import api_keys
1514
from sqlalchemy.dialects.postgresql import insert as pg_insert
1615

16+
from ..db.plugin import get_database_engine
17+
1718
_logger = logging.getLogger(__name__)
1819

1920

@@ -23,7 +24,7 @@ class ApiKeyRepo:
2324

2425
@classmethod
2526
def create_from_app(cls, app: web.Application):
26-
return cls(engine=app[APP_AIOPG_ENGINE_KEY])
27+
return cls(engine=get_database_engine(app))
2728

2829
async def list_names(
2930
self, *, user_id: UserID, product_name: ProductName

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
from aiopg.sa.engine import Engine
33
from models_library.users import UserID
44

5-
from .._constants import APP_AIOPG_ENGINE_KEY, RQT_USERID_KEY
5+
from .._constants import RQT_USERID_KEY
6+
from . import _aiopg
67

78

89
class BaseRepository:
@@ -15,13 +16,13 @@ def __init__(self, engine: Engine, user_id: UserID | None = None):
1516
@classmethod
1617
def create_from_request(cls, request: web.Request):
1718
return cls(
18-
engine=request.app[APP_AIOPG_ENGINE_KEY],
19+
engine=_aiopg.get_database_engine(request.app),
1920
user_id=request.get(RQT_USERID_KEY),
2021
)
2122

2223
@classmethod
2324
def create_from_app(cls, app: web.Application):
24-
return cls(engine=app[APP_AIOPG_ENGINE_KEY], user_id=None)
25+
return cls(engine=_aiopg.get_database_engine(app), user_id=None)
2526

2627
@property
2728
def engine(self) -> Engine:

services/web/server/src/simcore_service_webserver/db_listener/_db_comp_tasks_listening_task.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
from models_library.projects_nodes_io import NodeID
1919
from models_library.projects_state import RunningState
2020
from pydantic.types import PositiveInt
21-
from servicelib.aiohttp.application_keys import APP_AIOPG_ENGINE_KEY
2221
from simcore_postgres_database.webserver_models import DB_CHANNEL_NAME, projects
2322
from sqlalchemy.sql import select
2423

24+
from ..db.plugin import get_database_engine
2525
from ..projects import exceptions, projects_api
2626
from ..projects.nodes_utils import update_node_outputs
2727
from ._utils import convert_state_from_db
@@ -159,7 +159,7 @@ async def _comp_tasks_listening_task(app: web.Application) -> None:
159159
while True:
160160
try:
161161
# create a special connection here
162-
db_engine = app[APP_AIOPG_ENGINE_KEY]
162+
db_engine = get_database_engine(app)
163163
_logger.info("listening to comp_task events...")
164164
await _listen(app, db_engine)
165165
except asyncio.CancelledError: # noqa: PERF203

services/web/server/src/simcore_service_webserver/garbage_collector/_tasks_users.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from tenacity.before_sleep import before_sleep_log
1616
from tenacity.wait import wait_exponential
1717

18-
from .._constants import APP_AIOPG_ENGINE_KEY
18+
from ..db.plugin import get_database_engine
1919
from ..login.utils import notify_user_logout
2020
from ..security.api import clean_auth_policy_cache
2121
from ..users.api import update_expired_users
@@ -60,7 +60,7 @@ async def _update_expired_users(app: web.Application):
6060
"""
6161
It is resilient, i.e. if update goes wrong, it waits a bit and retries
6262
"""
63-
engine: Engine = app[APP_AIOPG_ENGINE_KEY]
63+
engine: Engine = get_database_engine(app)
6464
assert engine # nosec
6565

6666
if updated := await update_expired_users(engine):

services/web/server/src/simcore_service_webserver/products/_events.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import tempfile
33
from collections import OrderedDict
44
from pathlib import Path
5-
from typing import cast
65

76
from aiohttp import web
87
from aiopg.sa.engine import Engine
@@ -14,7 +13,8 @@
1413
get_or_create_product_group,
1514
)
1615

17-
from .._constants import APP_AIOPG_ENGINE_KEY, APP_PRODUCTS_KEY
16+
from .._constants import APP_PRODUCTS_KEY
17+
from ..db.plugin import get_database_engine
1818
from ..statics._constants import FRONTEND_APP_DEFAULT, FRONTEND_APPS_AVAILABLE
1919
from ._db import get_product_payment_fields, iter_products
2020
from ._model import Product
@@ -46,7 +46,7 @@ async def auto_create_products_groups(app: web.Application) -> None:
4646
NOTE: could not add this in 'setup_groups' (groups plugin)
4747
since it has to be executed BEFORE 'load_products_on_startup'
4848
"""
49-
engine = cast(Engine, app[APP_AIOPG_ENGINE_KEY])
49+
engine = get_database_engine(app)
5050

5151
async with engine.acquire() as connection:
5252
async for row in iter_products(connection):
@@ -76,7 +76,7 @@ async def load_products_on_startup(app: web.Application):
7676
Loads info on products stored in the database into app's storage (i.e. memory)
7777
"""
7878
app_products: OrderedDict[str, Product] = OrderedDict()
79-
engine: Engine = app[APP_AIOPG_ENGINE_KEY]
79+
engine: Engine = get_database_engine(app)
8080
async with engine.acquire() as connection:
8181
async for row in iter_products(connection):
8282
assert isinstance(row, RowProxy) # nosec

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import sqlalchemy as sa
88
from aiohttp import web
99
from aiopg.sa.result import ResultProxy, RowProxy
10-
from servicelib.aiohttp.application_keys import APP_AIOPG_ENGINE_KEY
1110
from simcore_postgres_database.models.scicrunch_resources import scicrunch_resources
1211
from sqlalchemy.dialects.postgresql import insert as sa_pg_insert
1312

13+
from ..db.plugin import get_database_engine
1414
from .models import ResearchResource, ResearchResourceAtdB
1515

1616
logger = logging.getLogger(__name__)
@@ -26,7 +26,7 @@ class ResearchResourceRepository:
2626
# WARNING: interfaces to both ResarchResource and ResearchResourceAtDB
2727

2828
def __init__(self, app: web.Application):
29-
self._engine = app[APP_AIOPG_ENGINE_KEY]
29+
self._engine = get_database_engine(app)
3030

3131
async def list_resources(self) -> list[ResearchResource]:
3232
async with self._engine.acquire() as conn:

0 commit comments

Comments
 (0)