video_monitor/app/utils/schema_upgrade.py
2026-08-30 22:23:12 +08:00

31 lines
1.1 KiB
Python

# 作者:北小菜
"""轻量 SQLite 列升级(无 Django migrations 时使用)"""
import logging
logger = logging.getLogger("app.schema")
def _table_columns(cursor, table):
if table not in {"av_biz_algorithm"}:
raise ValueError("table is not allowed for schema upgrade")
cursor.execute('PRAGMA table_info("av_biz_algorithm")')
return {row[1] for row in cursor.fetchall()}
def ensure_biz_algorithm_line_count_columns():
from django.db import connection
table = "av_biz_algorithm"
adds = {
"forward_count_threshold": "ALTER TABLE av_biz_algorithm ADD COLUMN forward_count_threshold INTEGER NOT NULL DEFAULT 0",
"reverse_count_threshold": "ALTER TABLE av_biz_algorithm ADD COLUMN reverse_count_threshold INTEGER NOT NULL DEFAULT 0",
"detector_model_id": "ALTER TABLE av_biz_algorithm ADD COLUMN detector_model_id INTEGER NULL",
}
with connection.cursor() as cur:
existing = _table_columns(cur, table)
for col, sql in adds.items():
if col in existing:
continue
cur.execute(sql)
logger.info("schema upgrade: %s", sql)