26 lines
660 B
Python
26 lines
660 B
Python
from sqlmodel import SQLModel, Session, create_engine
|
|
from app.config import DATABASE_URL
|
|
|
|
# SQLite requires check_same_thread=False for FastAPI
|
|
_connect_args: dict = {}
|
|
if DATABASE_URL.startswith("sqlite"):
|
|
_connect_args = {"check_same_thread": False}
|
|
|
|
engine = create_engine(
|
|
DATABASE_URL,
|
|
echo=True,
|
|
connect_args=_connect_args,
|
|
)
|
|
|
|
|
|
def create_db_and_tables() -> None:
|
|
"""Create all tables from SQLModel metadata.
|
|
Called once at application startup."""
|
|
SQLModel.metadata.create_all(engine)
|
|
|
|
|
|
def get_session():
|
|
"""FastAPI dependency — yields a SQLModel Session."""
|
|
with Session(engine) as session:
|
|
yield session
|