-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
30 lines (21 loc) · 926 Bytes
/
Copy pathdatabase.py
File metadata and controls
30 lines (21 loc) · 926 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""Database engine and session management with SQLModel."""
from collections.abc import Generator
from sqlmodel import Session, SQLModel, create_engine
from app.config import settings
# check_same_thread is a SQLite-only quirk; harmless to pass conditionally.
connect_args = (
{"check_same_thread": False}
if settings.database_url.startswith("sqlite")
else {}
)
engine = create_engine(settings.database_url, echo=settings.debug, connect_args=connect_args)
def create_db_and_tables() -> None:
"""Create tables from SQLModel metadata.
Handy for local dev and tests. For real schema changes over time, use
Alembic migrations (see docs/05-migrations-alembic.md).
"""
SQLModel.metadata.create_all(engine)
def get_session() -> Generator[Session, None, None]:
"""FastAPI dependency that yields a database session per request."""
with Session(engine) as session:
yield session