Fix missing view_count column migration on startup

create_all doesn't add columns to existing tables. Add _add_column_if_missing
helper that checks PRAGMA table_info and runs ALTER TABLE if needed, called
on every startup before FTS setup.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Mattias Tall
2026-05-26 11:20:40 +02:00
parent 8221177615
commit ffd46b5e08

View File

@@ -70,13 +70,22 @@ END;
"""
def _add_column_if_missing(raw_conn, table: str, column: str, definition: str):
existing = {row[1] for row in raw_conn.execute(f"PRAGMA table_info({table})")}
if column not in existing:
raw_conn.execute(f"ALTER TABLE {table} ADD COLUMN {column} {definition}")
def init_db():
from . import models # noqa: F401
Base.metadata.create_all(bind=engine)
# executescript handles multi-statement SQL including trigger BEGIN...END blocks
raw_conn = engine.raw_connection()
try:
# Column migrations — safe to run on every startup
_add_column_if_missing(raw_conn, "videos", "view_count", "INTEGER")
raw_conn.commit()
# executescript handles multi-statement SQL including trigger BEGIN...END blocks
raw_conn.executescript(FTS_SETUP_SQL)
finally:
raw_conn.close()