55from importlib import import_module
66from pathlib import Path
77from types import ModuleType
8- from typing import Any , Final , Optional
8+ from typing import Any , Final , Optional , Union
99
1010import yaml
1111from jsonschema .exceptions import ValidationError
@@ -64,11 +64,11 @@ def import_file(file_path: str) -> ModuleType:
6464 return module
6565
6666
67- def download_table (table : Any , engine : Any , yaml_file_name : str ) -> None :
67+ def download_table (table : Any , engine : Any , yaml_file_name : Union [ str , Path ] ) -> None :
6868 """Download a Table and store it as a .yaml file."""
69- stmt = select ([ table ] )
69+ stmt = select (table )
7070 with engine .connect () as conn :
71- result = [dict (row ) for row in conn .execute (stmt )]
71+ result = [dict (row ) for row in conn .execute (stmt ). mappings () ]
7272
7373 with Path (yaml_file_name ).open ("w" , newline = "" , encoding = "utf-8" ) as yamlfile :
7474 yamlfile .write (yaml .dump (result ))
@@ -83,7 +83,7 @@ def create_db_engine(
8383 """Create a SQLAlchemy Engine."""
8484 if use_asyncio :
8585 async_dsn = db_dsn .replace ("postgresql://" , "postgresql+asyncpg://" )
86- engine = create_async_engine (async_dsn , ** kwargs )
86+ engine : Any = create_async_engine (async_dsn , ** kwargs )
8787 event_engine = engine .sync_engine
8888 else :
8989 engine = create_engine (db_dsn , ** kwargs )
@@ -93,7 +93,7 @@ def create_db_engine(
9393
9494 @event .listens_for (event_engine , "connect" , insert = True )
9595 def connect (dbapi_connection : Any , _ : Any ) -> None :
96- set_search_path (dbapi_connection , schema_name ) # type: ignore
96+ set_search_path (dbapi_connection , schema_name )
9797
9898 return engine
9999
@@ -105,7 +105,8 @@ def set_search_path(connection: Any, schema: str) -> None:
105105 connection .autocommit = True
106106
107107 cursor = connection .cursor ()
108- cursor .execute ("SET search_path to %s;" , (schema ,))
108+ # Parametrised queries don't work with asyncpg, hence the f-string.
109+ cursor .execute (f"SET search_path TO { schema } ;" )
109110 cursor .close ()
110111
111112 connection .autocommit = existing_autocommit
0 commit comments