fix: Edge队列时间戳根治 — INSERT显式北京时间 + 存量表schema迁移
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)。
This commit is contained in:
@@ -30,11 +30,8 @@ def _get_conn() -> sqlite3.Connection:
|
|||||||
return conn
|
return conn
|
||||||
|
|
||||||
|
|
||||||
def _init_db():
|
_TASK_QUEUE_DDL = """
|
||||||
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
|
CREATE TABLE {name} (
|
||||||
conn = sqlite3.connect(DB_PATH)
|
|
||||||
conn.execute("""
|
|
||||||
CREATE TABLE IF NOT EXISTS task_queue (
|
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
nas_task_id INTEGER NOT NULL,
|
nas_task_id INTEGER NOT NULL,
|
||||||
video_filename TEXT NOT NULL,
|
video_filename TEXT NOT NULL,
|
||||||
@@ -52,7 +49,26 @@ def _init_db():
|
|||||||
delivered INTEGER DEFAULT 0,
|
delivered INTEGER DEFAULT 0,
|
||||||
UNIQUE(nas_task_id)
|
UNIQUE(nas_task_id)
|
||||||
)
|
)
|
||||||
""")
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def _init_db():
|
||||||
|
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
|
||||||
|
conn = sqlite3.connect(DB_PATH)
|
||||||
|
# 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_status ON task_queue(status)")
|
||||||
conn.execute("CREATE INDEX IF NOT EXISTS idx_delivered ON task_queue(delivered)")
|
conn.execute("CREATE INDEX IF NOT EXISTS idx_delivered ON task_queue(delivered)")
|
||||||
conn.commit()
|
conn.commit()
|
||||||
@@ -66,8 +82,9 @@ def enqueue(nas_task_id: int, video_filename: str, video_path: str,
|
|||||||
try:
|
try:
|
||||||
cur = conn.execute(
|
cur = conn.execute(
|
||||||
"INSERT OR IGNORE INTO task_queue "
|
"INSERT OR IGNORE INTO task_queue "
|
||||||
"(nas_task_id, video_filename, video_path, camera_name, event_start_time, known_members_context) "
|
"(nas_task_id, video_filename, video_path, camera_name, event_start_time, "
|
||||||
"VALUES (?, ?, ?, ?, ?, ?)",
|
"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)
|
(nas_task_id, video_filename, video_path, camera_name, event_start_time, known_members_context)
|
||||||
)
|
)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|||||||
Reference in New Issue
Block a user