88import shutil
99import subprocess
1010import sys
11- from collections .abc import AsyncGenerator , AsyncIterator , Callable
11+ from collections .abc import AsyncGenerator , AsyncIterator , Callable , Iterable
1212from pathlib import Path
1313
14- import aiopg .sa
15- import aiopg .sa .engine as aiopg_sa_engine
1614import httpx
1715import pytest
1816import simcore_postgres_database .cli as pg_cli
1917import sqlalchemy as sa
20- import sqlalchemy .engine as sa_engine
18+ import sqlalchemy .engine
2119import yaml
2220from aiopg .sa .connection import SAConnection
2321from fastapi import FastAPI
4139## POSTGRES -----
4240
4341
44- CURRENT_DIR = Path (sys .argv [0 ] if __name__ == "__main__" else __file__ ).resolve ().parent
42+ _CURRENT_DIR = (
43+ Path (sys .argv [0 ] if __name__ == "__main__" else __file__ ).resolve ().parent
44+ )
4545
4646
4747@pytest .fixture (scope = "session" )
@@ -54,7 +54,7 @@ def docker_compose_file(
5454 environ = dict (os .environ )
5555 environ .update (default_app_env_vars )
5656
57- src_path = CURRENT_DIR / "data" / "docker-compose.yml"
57+ src_path = _CURRENT_DIR / "data" / "docker-compose.yml"
5858 assert src_path .exists
5959
6060 dst_path = Path (str (tmpdir_factory .mktemp ("config" ).join ("docker-compose.yml" )))
@@ -114,19 +114,14 @@ def is_postgres_responsive() -> bool:
114114
115115
116116@pytest .fixture (scope = "session" )
117- def make_engine (postgres_service : dict ) -> Callable :
118- dsn = postgres_service ["dsn" ] # session scope freezes dsn
119-
120- def maker (* , is_async = True ) -> aiopg_sa_engine .Engine | sa_engine .Engine :
121- if is_async :
122- return aiopg .sa .create_engine (dsn )
123- return sa .create_engine (dsn )
124-
125- return maker
117+ def sync_engine (postgres_service : str ) -> Iterable [sqlalchemy .engine .Engine ]:
118+ _engine : sqlalchemy .engine .Engine = sa .create_engine (url = postgres_service )
119+ yield _engine
120+ _engine .dispose ()
126121
127122
128123@pytest .fixture
129- def migrated_db (postgres_service : dict , make_engine : Callable ):
124+ def migrated_db (postgres_service : dict , sync_engine : sqlalchemy . engine . Engin ):
130125 # NOTE: this is equivalent to packages/pytest-simcore/src/pytest_simcore/postgres_service.py::postgres_db
131126 # but we do override postgres_dsn -> postgres_engine -> postgres_db because we want the latter
132127 # fixture to have local scope
@@ -140,13 +135,8 @@ def migrated_db(postgres_service: dict, make_engine: Callable):
140135
141136 pg_cli .downgrade .callback ("base" )
142137 pg_cli .clean .callback ()
143- # FIXME: deletes all because downgrade is not reliable!
144138
145- try :
146- sync_engine = make_engine (is_async = False )
147- postgres_tools .force_drop_all_tables (sync_engine )
148- finally :
149- sync_engine .dispose ()
139+ postgres_tools .force_drop_all_tables (sync_engine )
150140
151141
152142@pytest .fixture
0 commit comments