Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: testclient can break with force_rollback on (especially drop_all) #42

Merged
merged 8 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions databasez/core/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ async def decr_refcount(self) -> bool:
return False

async def connect_hook(self) -> None:
"""Refcount protected connect hook"""
"""Refcount protected connect hook. Executed after engine and before global connection setup."""

async def connect(self) -> bool:
"""
Expand All @@ -247,13 +247,13 @@ async def connect(self) -> bool:
self.is_connected = True

assert self._global_connection is None
await self.connect_hook()

self._global_connection = Connection(self, self.backend, force_rollback=True)
await self.connect_hook()
return True

async def disconnect_hook(self) -> None:
"""Refcount protected disconnect hook"""
"""Refcount protected disconnect hook. Executed after connection cleanup but before engine disconnect."""

async def disconnect(self, force: bool = False) -> bool:
"""
Expand All @@ -268,13 +268,13 @@ async def disconnect(self, force: bool = False) -> bool:
else:
return False

assert self._global_connection is not None
try:
await self.disconnect_hook()
finally:
assert self._global_connection is not None
await self._global_connection.__aexit__()
self._global_connection = None
self._connection = None
await self.disconnect_hook()
finally:
try:
await self.backend.disconnect()
logger.info(
Expand Down
2 changes: 2 additions & 0 deletions databasez/testclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ async def create_database(
dialect_name = url.sqla_url.get_dialect(True).name
dialect_driver = url.sqla_url.get_dialect(True).driver

# we don't want to connect to a not existing db
if dialect_name == "postgresql":
url = url.replace(database="postgres")
elif dialect_name == "mssql":
Expand Down Expand Up @@ -223,6 +224,7 @@ async def create_database(

elif dialect_name == "sqlite" and database != ":memory:":
if database:
# create a sqlite file
async with db_client.engine.begin() as conn: # type: ignore
await conn.execute(sa.text("CREATE TABLE DB(id int)"))
await conn.execute(sa.text("DROP TABLE DB"))
Expand Down
8 changes: 8 additions & 0 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Release Notes


## 0.9.5

### Fixed

- `disconnect_hook` was called too early.
- `connect_hook` was called too late.

## 0.9.4

### Changed
Expand Down