diff --git a/src/transcription/db.py b/src/transcription/db.py index e2c1508..67f75fc 100644 --- a/src/transcription/db.py +++ b/src/transcription/db.py @@ -5,14 +5,18 @@ startup/shutdown behavior is predictable and lifespan-managed. """ import contextlib +import logging from collections.abc import Generator from dataclasses import dataclass +from sqlalchemy import inspect, text from sqlalchemy.engine import Engine from sqlmodel import Session, SQLModel, create_engine from transcription.config import Settings, get_settings +logger = logging.getLogger(__name__) + @dataclass(frozen=True) class DatabaseRuntime: @@ -71,8 +75,37 @@ def should_bootstrap_schema(settings: Settings) -> bool: def create_all(*, engine: Engine | None = None) -> None: """Create all tables on the selected engine.""" + # Import models so SQLModel metadata is fully registered before bootstrap. + from transcription import models as _models # noqa: F401 + active_engine = engine or get_database_runtime().engine SQLModel.metadata.create_all(active_engine) + _ensure_sqlite_compat_columns(active_engine) + + +def _ensure_sqlite_compat_columns(engine: Engine) -> None: + """Apply lightweight dev/test SQLite compatibility column patches. + + This keeps local bootstrap resilient when models evolve but no full + migration tooling is in place yet. + """ + if engine.url.get_backend_name() != "sqlite": + return + + inspector = inspect(engine) + table_names = set(inspector.get_table_names()) + if "job" not in table_names: + return + + columns = {column["name"] for column in inspector.get_columns("job")} + if "retry_count" not in columns: + with engine.begin() as connection: + connection.execute( + text("ALTER TABLE job ADD COLUMN retry_count INTEGER NOT NULL DEFAULT 0") + ) + logger.warning( + "Applied SQLite compatibility schema patch table=job column=retry_count default=0" + ) @contextlib.contextmanager diff --git a/uploads/b04b640b-dce2-4543-b8da-74ea764bdae7_Time Rolls On - page 011.jpg b/uploads/b04b640b-dce2-4543-b8da-74ea764bdae7_Time Rolls On - page 011.jpg new file mode 100644 index 0000000..693cae5 Binary files /dev/null and b/uploads/b04b640b-dce2-4543-b8da-74ea764bdae7_Time Rolls On - page 011.jpg differ