no-mistakes(review): Add startup self-heal guard for show_in_form column
This commit is contained in:
+13
@@ -9,6 +9,7 @@ from pathlib import Path
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from sqlalchemy import text
|
||||
|
||||
from app.core.config import settings
|
||||
from app.core.database import Base, async_session_factory, engine
|
||||
@@ -18,12 +19,24 @@ from app.services.seed import seed_categories, seed_units, seed_users
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def ensure_legacy_schema(conn) -> None:
|
||||
"""Add columns from alembic migrations that legacy create_all databases lack."""
|
||||
result = await conn.execute(text("PRAGMA table_info(categories)"))
|
||||
columns = {row[1] for row in result}
|
||||
if "show_in_form" not in columns:
|
||||
await conn.execute(
|
||||
text("ALTER TABLE categories ADD COLUMN show_in_form BOOLEAN NOT NULL DEFAULT 1")
|
||||
)
|
||||
logger.info("Added missing categories.show_in_form column (legacy database)")
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
"""Initialise database and seed data on startup."""
|
||||
logger.info("Starting Denya OneCare …")
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
await ensure_legacy_schema(conn)
|
||||
async with async_session_factory() as session:
|
||||
await seed_users(session)
|
||||
await session.commit()
|
||||
|
||||
Reference in New Issue
Block a user