From 738419016f74c7ea74d48699ef73c84a56849813 Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 22 Aug 2024 15:11:52 +0200 Subject: [PATCH] raise error when improper use (#47) Changes: - set the engine to None after disconnect. This prevents an incorrect reuse. - raise RuntimeError when db is not connected but connections are created. --- databasez/core/database.py | 2 ++ databasez/sqlalchemy.py | 1 + 2 files changed, 3 insertions(+) diff --git a/databasez/core/database.py b/databasez/core/database.py index 1f564e7..70da951 100644 --- a/databasez/core/database.py +++ b/databasez/core/database.py @@ -449,6 +449,8 @@ async def drop_all(self, meta: MetaData, **kwargs: typing.Any) -> None: @multiloop_protector(False) def connection(self) -> Connection: + if not self.is_connected: + raise RuntimeError("Database is not connected") if self.force_rollback: return typing.cast(Connection, self._global_connection) diff --git a/databasez/sqlalchemy.py b/databasez/sqlalchemy.py index c2cdcfe..75884ed 100644 --- a/databasez/sqlalchemy.py +++ b/databasez/sqlalchemy.py @@ -295,5 +295,6 @@ async def connect(self, database_url: DatabaseURL, **options: typing.Any) -> Non async def disconnect(self) -> None: engine = self.engine + self.engine = None assert engine is not None, "database is not initialized" await engine.dispose()