Files
govoplan-core/tests/db_isolation.py
Albrecht Degering 635d25c74c
Some checks failed
Dependency Audit / dependency-audit (push) Has been cancelled
Module Matrix / module-matrix (push) Has been cancelled
chore: consolidate platform split checks
2026-07-10 12:51:19 +02:00

27 lines
766 B
Python

from __future__ import annotations
from collections.abc import Iterator
from contextlib import contextmanager
from govoplan_core.db.session import DatabaseHandle, configure_database, get_database, reset_database, set_database
@contextmanager
def temporary_database(database_url: str) -> Iterator[DatabaseHandle]:
try:
previous_database = get_database()
except RuntimeError:
previous_database = None
database = configure_database(database_url)
should_dispose = database is not previous_database
try:
yield database
finally:
if should_dispose:
database.engine.dispose()
if previous_database is None:
reset_database()
else:
set_database(previous_database)