From ffd46b5e0893fe3059e8a10854d01cfe7314157b Mon Sep 17 00:00:00 2001 From: Mattias Tall Date: Tue, 26 May 2026 11:20:40 +0200 Subject: [PATCH] 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 --- backend/database.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/backend/database.py b/backend/database.py index a2de257..b3c2725 100644 --- a/backend/database.py +++ b/backend/database.py @@ -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()