33 lines
717 B
Python
33 lines
717 B
Python
from pathlib import Path
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import DeclarativeBase, sessionmaker
|
|
|
|
from .config import settings
|
|
|
|
Path(settings.database_path).parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
engine = create_engine(
|
|
f"sqlite:///{settings.database_path}",
|
|
connect_args={"check_same_thread": False},
|
|
)
|
|
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False)
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
pass
|
|
|
|
|
|
def init_db() -> None:
|
|
from . import models # noqa: F401 (registers tables on Base.metadata)
|
|
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
|
|
def get_session():
|
|
session = SessionLocal()
|
|
try:
|
|
yield session
|
|
finally:
|
|
session.close()
|