From 5a70027ba55a5448537f5702b0ba52fbbd2793e8 Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Tue, 19 Dec 2023 21:31:43 +0100 Subject: [PATCH] test: Fix `FATAL: sorry, too many clients already` Dispose the SQLAlchemy engine object after use within `PostgresConnector`. --- target_postgres/connector.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/target_postgres/connector.py b/target_postgres/connector.py index 3bc34e87..5452b7c9 100644 --- a/target_postgres/connector.py +++ b/target_postgres/connector.py @@ -180,8 +180,10 @@ def copy_table_structure( @contextmanager def _connect(self) -> t.Iterator[sqlalchemy.engine.Connection]: - with self._engine.connect().execution_options() as conn: + engine = self._engine + with engine.connect().execution_options() as conn: yield conn + engine.dispose() def drop_table( self, table: sqlalchemy.Table, connection: sqlalchemy.engine.Connection @@ -800,9 +802,7 @@ def column_exists( # type: ignore[override] ) def __del__(self): - """ - Dispose underlying SQLAlchemy engine object. - """ + """Dispose underlying SQLAlchemy engine object.""" if self._cached_engine is not None: self._cached_engine.dispose()