From 9aba71c8ed59232ee8795d0a2130a10736955f93 Mon Sep 17 00:00:00 2001 From: ericwyuan Date: Thu, 20 Aug 2026 18:50:59 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20Edge=E9=98=9F=E5=88=97=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E6=88=B3=E6=A0=B9=E6=B2=BB=20=E2=80=94=20INSERT=E6=98=BE?= =?UTF-8?q?=E5=BC=8F=E5=8C=97=E4=BA=AC=E6=97=B6=E9=97=B4=20+=20=E5=AD=98?= =?UTF-8?q?=E9=87=8F=E8=A1=A8schema=E8=BF=81=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 795e8ac 只改了 CREATE TABLE 的 DEFAULT,但 DEFAULT 固化在已存在表的 schema 中,IF NOT EXISTS 不会更新旧表——新行 created_at 仍走旧 DEFAULT(localtime)=UTC。三处修复: 1. enqueue INSERT 显式写 created_at/updated_at (+8 hours),不再依赖 schema DEFAULT 2. _init_db 检测旧 schema 含 localtime 时重建表迁移(数据保留,幂等) 3. 存量数据修正: 行16/19 created_at +8h 验证: 本地迁移单测3例通过;线上行20(task 44) created_at=18:43:35 为北京时间(UTC机器当时10:43)。 --- fam-edge/src/fam_edge/queue/queue_manager.py | 61 +++++++++++++------- 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/fam-edge/src/fam_edge/queue/queue_manager.py b/fam-edge/src/fam_edge/queue/queue_manager.py index d8d946b..fec169f 100644 --- a/fam-edge/src/fam_edge/queue/queue_manager.py +++ b/fam-edge/src/fam_edge/queue/queue_manager.py @@ -30,29 +30,45 @@ def _get_conn() -> sqlite3.Connection: return conn +_TASK_QUEUE_DDL = """ + CREATE TABLE {name} ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + nas_task_id INTEGER NOT NULL, + video_filename TEXT NOT NULL, + video_path TEXT NOT NULL, + camera_name TEXT DEFAULT '', + event_start_time TEXT DEFAULT '', + known_members_context TEXT DEFAULT '', + status TEXT DEFAULT 'PENDING', + result_json TEXT, + error_message TEXT, + failure_stage TEXT, + retry_count INTEGER DEFAULT 0, + created_at TEXT DEFAULT (datetime('now', '+8 hours')), + updated_at TEXT DEFAULT (datetime('now', '+8 hours')), + delivered INTEGER DEFAULT 0, + UNIQUE(nas_task_id) + ) +""" + + def _init_db(): os.makedirs(os.path.dirname(DB_PATH), exist_ok=True) conn = sqlite3.connect(DB_PATH) - conn.execute(""" - CREATE TABLE IF NOT EXISTS task_queue ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - nas_task_id INTEGER NOT NULL, - video_filename TEXT NOT NULL, - video_path TEXT NOT NULL, - camera_name TEXT DEFAULT '', - event_start_time TEXT DEFAULT '', - known_members_context TEXT DEFAULT '', - status TEXT DEFAULT 'PENDING', - result_json TEXT, - error_message TEXT, - failure_stage TEXT, - retry_count INTEGER DEFAULT 0, - created_at TEXT DEFAULT (datetime('now', '+8 hours')), - updated_at TEXT DEFAULT (datetime('now', '+8 hours')), - delivered INTEGER DEFAULT 0, - UNIQUE(nas_task_id) - ) - """) + # DEFAULT 约束固化在表 schema 中,CREATE TABLE IF NOT EXISTS 不会更新已存在的旧表 + # (旧表 DEFAULT 是 localtime,UTC 机器上=UTC)。检测到旧 schema 时重建表迁移。 + row = conn.execute( + "SELECT sql FROM sqlite_master WHERE type='table' AND name='task_queue'" + ).fetchone() + if row is not None and 'localtime' in (row[0] or ''): + conn.execute("BEGIN IMMEDIATE") + conn.execute(_TASK_QUEUE_DDL.format(name='task_queue_new')) + conn.execute("INSERT INTO task_queue_new SELECT * FROM task_queue") + conn.execute("DROP TABLE task_queue") + conn.execute("ALTER TABLE task_queue_new RENAME TO task_queue") + conn.commit() + conn.execute(_TASK_QUEUE_DDL.format(name='task_queue').replace( + 'CREATE TABLE task_queue', 'CREATE TABLE IF NOT EXISTS task_queue')) conn.execute("CREATE INDEX IF NOT EXISTS idx_status ON task_queue(status)") conn.execute("CREATE INDEX IF NOT EXISTS idx_delivered ON task_queue(delivered)") conn.commit() @@ -66,8 +82,9 @@ def enqueue(nas_task_id: int, video_filename: str, video_path: str, try: cur = conn.execute( "INSERT OR IGNORE INTO task_queue " - "(nas_task_id, video_filename, video_path, camera_name, event_start_time, known_members_context) " - "VALUES (?, ?, ?, ?, ?, ?)", + "(nas_task_id, video_filename, video_path, camera_name, event_start_time, " + "known_members_context, created_at, updated_at) " + "VALUES (?, ?, ?, ?, ?, ?, datetime('now','+8 hours'), datetime('now','+8 hours'))", (nas_task_id, video_filename, video_path, camera_name, event_start_time, known_members_context) ) conn.commit()