flickr-mail/flickr_mail/database.py
Edward Betts 9f0fb01878 Extract flickr_mail package with Mapped models and shared utilities
Move from JSON file storage to SQLite database using SQLAlchemy with
Mapped type hints. Deduplicate URL utility functions into shared
flickr_mail package.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-07 13:10:49 +00:00

31 lines
818 B
Python

"""Database engine and session factory for flickr-mail."""
from pathlib import Path
from sqlalchemy import create_engine, event
from sqlalchemy.orm import Session, sessionmaker
from flickr_mail.models import Base
DB_PATH = Path(__file__).parent.parent / "flickr_mail.db"
engine = create_engine(f"sqlite:///{DB_PATH}")
SessionLocal = sessionmaker(bind=engine)
@event.listens_for(engine, "connect")
def set_sqlite_pragma(dbapi_connection, connection_record):
"""Enable WAL mode for concurrent read/write access."""
cursor = dbapi_connection.cursor()
cursor.execute("PRAGMA journal_mode=WAL")
cursor.close()
def init_db() -> None:
"""Create all tables."""
Base.metadata.create_all(engine)
def get_session() -> Session:
"""Create a new database session."""
return SessionLocal()