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>
This commit is contained in:
parent
ac1b01ea68
commit
9f0fb01878
11 changed files with 1129 additions and 300 deletions
31
flickr_mail/database.py
Normal file
31
flickr_mail/database.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
"""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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue