generated from john/python-template
Added fix to db.py to accommodate new fields in db
This commit is contained in:
@@ -5,14 +5,18 @@ startup/shutdown behavior is predictable and lifespan-managed.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import contextlib
|
import contextlib
|
||||||
|
import logging
|
||||||
from collections.abc import Generator
|
from collections.abc import Generator
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from sqlalchemy import inspect, text
|
||||||
from sqlalchemy.engine import Engine
|
from sqlalchemy.engine import Engine
|
||||||
from sqlmodel import Session, SQLModel, create_engine
|
from sqlmodel import Session, SQLModel, create_engine
|
||||||
|
|
||||||
from transcription.config import Settings, get_settings
|
from transcription.config import Settings, get_settings
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class DatabaseRuntime:
|
class DatabaseRuntime:
|
||||||
@@ -71,8 +75,37 @@ def should_bootstrap_schema(settings: Settings) -> bool:
|
|||||||
|
|
||||||
def create_all(*, engine: Engine | None = None) -> None:
|
def create_all(*, engine: Engine | None = None) -> None:
|
||||||
"""Create all tables on the selected engine."""
|
"""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
|
active_engine = engine or get_database_runtime().engine
|
||||||
SQLModel.metadata.create_all(active_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
|
@contextlib.contextmanager
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 1.7 MiB |
Reference in New Issue
Block a user