"""Generated by Drydock. A fresh store with the declared schema, for criteria and tests."""
from __future__ import annotations
import tempfile
from collections.abc import Iterator
from contextlib import contextmanager
from pathlib import Path
from .db import Database
from .schema import open_database
SCHEMA_PATH = Path(__file__).resolve().parent.parent / "schema.sql"
@contextmanager
def db_session(dsn: str | None = None) -> Iterator[Database]:
"""Open ``dsn``, or a temporary SQLite store, with ``schema.sql`` applied."""
with tempfile.TemporaryDirectory() as directory:
database = open_database(dsn or str(Path(directory, "test.db")))
try:
if SCHEMA_PATH.is_file():
database.execute_script(SCHEMA_PATH.read_text(encoding="utf-8"))
yield database
finally:
database.close()
Run artifact