Skip to content

Commit

Permalink
raise error when improper use (#47)
Browse files Browse the repository at this point in the history
Changes:
- set the engine to None after disconnect. This prevents an incorrect
reuse.
- raise RuntimeError when db is not connected but connections are
  created.
  • Loading branch information
devkral authored Aug 22, 2024
1 parent 0893af7 commit 7384190
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 0 deletions.
2 changes: 2 additions & 0 deletions databasez/core/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions databasez/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 7384190

Please sign in to comment.