chore: switch to all-SQLite stack (drop Postgres service)
This commit is contained in:
parent
74831483eb
commit
7d5ade6b6a
12 changed files with 104 additions and 62 deletions
|
|
@ -1,6 +1,7 @@
|
|||
"""Database engine, sessions, and FastAPI dependency."""
|
||||
|
||||
from collections.abc import Iterator
|
||||
from pathlib import Path
|
||||
|
||||
from sqlalchemy import Engine
|
||||
from sqlmodel import Session, SQLModel, create_engine
|
||||
|
|
@ -10,11 +11,23 @@ from .settings import settings
|
|||
_engine: Engine | None = None
|
||||
|
||||
|
||||
def _engine_kwargs(url: str) -> dict:
|
||||
if url.startswith("sqlite"):
|
||||
# FastAPI runs sync endpoints in a threadpool; connections are shared
|
||||
# across threads, so defer the same-thread check. File-based SQLite
|
||||
# needs an existing parent dir (SQLite won't create it).
|
||||
return {"connect_args": {"check_same_thread": False}}
|
||||
return {}
|
||||
|
||||
|
||||
def get_engine(url: str | None = None) -> Engine:
|
||||
"""Process-wide engine singleton (override URL in tests via settings)."""
|
||||
global _engine
|
||||
if _engine is None:
|
||||
_engine = create_engine(url or settings.database_url)
|
||||
url = url or settings.database_url
|
||||
if url.startswith("sqlite:///") and not url.startswith("sqlite:///:memory:"):
|
||||
Path(url.removeprefix("sqlite:///")).parent.mkdir(parents=True, exist_ok=True)
|
||||
_engine = create_engine(url, **_engine_kwargs(url))
|
||||
return _engine
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue