fix: 防御性编程确保数据库表存在,解决user_messages表不存在错误

- 添加_ensure_user_messages_table()方法:检查并创建user_messages表
- 添加_ensure_bot_messages_table()方法:检查并创建bot_messages表
- 添加_ensure_ocr_staging_table()方法:检查并创建ocr_staging表
- 在record_user_message、record_bot_reply、record_ocr_message、clear_ocr_staging方法开始处调用相应的防御性检查
- 解决0字节msg_history.db文件导致的'no such table: user_messages'错误

Task #1655
This commit is contained in:
youpaishidifu
2026-08-31 22:28:14 +08:00
parent ba6f3a3afb
commit 589ea7ef4e
+76
View File
@@ -396,6 +396,9 @@ class MessageHistory:
def record_bot_reply(self, platform: str, content: str): def record_bot_reply(self, platform: str, content: str):
"""记录bot回复到bot_messages表(环形缓冲区,max 10条/平台)""" """记录bot回复到bot_messages表(环形缓冲区,max 10条/平台)"""
# ⭐ Task #1655: 确保表存在(防御性编程)
self._ensure_bot_messages_table()
content_hash = hashlib.md5(content.strip().encode('utf-8')).hexdigest() content_hash = hashlib.md5(content.strip().encode('utf-8')).hexdigest()
conn = self._get_conn() conn = self._get_conn()
try: try:
@@ -539,8 +542,75 @@ class MessageHistory:
return False return False
def _ensure_user_messages_table(self):
"""确保user_messages表存在(防御性编程)"""
conn = self._get_conn()
try:
cursor = conn.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='user_messages'")
if not cursor.fetchone():
logger.warning("[msg-history] user_messages表不存在,重新创建")
conn.execute("""
CREATE TABLE IF NOT EXISTS user_messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
platform TEXT NOT NULL,
content TEXT NOT NULL,
content_hash TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
conn.execute("CREATE INDEX IF NOT EXISTS idx_user_content_hash ON user_messages(content_hash)")
conn.execute("CREATE INDEX IF NOT EXISTS idx_user_platform_time ON user_messages(platform, created_at)")
conn.commit()
finally:
conn.close()
def _ensure_bot_messages_table(self):
"""确保bot_messages表存在(防御性编程)"""
conn = self._get_conn()
try:
cursor = conn.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='bot_messages'")
if not cursor.fetchone():
logger.warning("[msg-history] bot_messages表不存在,重新创建")
conn.execute("""
CREATE TABLE IF NOT EXISTS bot_messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
platform TEXT NOT NULL,
content TEXT NOT NULL,
content_hash TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
conn.execute("CREATE INDEX IF NOT EXISTS idx_bot_content_hash ON bot_messages(content_hash)")
conn.execute("CREATE INDEX IF NOT EXISTS idx_bot_platform_time ON bot_messages(platform, created_at)")
conn.commit()
finally:
conn.close()
def _ensure_ocr_staging_table(self):
"""确保ocr_staging表存在(防御性编程)"""
conn = self._get_conn()
try:
cursor = conn.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='ocr_staging'")
if not cursor.fetchone():
logger.warning("[msg-history] ocr_staging表不存在,重新创建")
conn.execute("""
CREATE TABLE IF NOT EXISTS ocr_staging (
id INTEGER PRIMARY KEY AUTOINCREMENT,
platform TEXT NOT NULL,
content TEXT NOT NULL,
content_hash TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
conn.commit()
finally:
conn.close()
def record_user_message(self, platform: str, content: str): def record_user_message(self, platform: str, content: str):
"""记录用户消息到user_messages表(环形缓冲区,max 10条/平台)""" """记录用户消息到user_messages表(环形缓冲区,max 10条/平台)"""
# ⭐ Task #1655: 确保表存在(防御性编程)
self._ensure_user_messages_table()
# ⭐ Task #1654: 修复去重误判问题 # ⭐ Task #1654: 修复去重误判问题
# 问题:exclude_self=False 导致自我比较产生100%相似度,误丢弃正常消息 # 问题:exclude_self=False 导致自我比较产生100%相似度,误丢弃正常消息
# 修复:使用 exclude_self=True 避免自我比较,只与历史消息比对 # 修复:使用 exclude_self=True 避免自我比较,只与历史消息比对
@@ -720,6 +790,9 @@ class MessageHistory:
def record_ocr_message(self, platform: str, content: str): def record_ocr_message(self, platform: str, content: str):
"""记录OCR识别的消息到暂存表""" """记录OCR识别的消息到暂存表"""
# ⭐ Task #1655: 确保表存在(防御性编程)
self._ensure_ocr_staging_table()
content_hash = hashlib.md5(content.strip().encode('utf-8')).hexdigest() content_hash = hashlib.md5(content.strip().encode('utf-8')).hexdigest()
conn = self._get_conn() conn = self._get_conn()
try: try:
@@ -735,6 +808,9 @@ class MessageHistory:
def clear_ocr_staging(self): def clear_ocr_staging(self):
"""清空OCR暂存表(重启时调用)""" """清空OCR暂存表(重启时调用)"""
# ⭐ Task #1655: 确保表存在(防御性编程)
self._ensure_ocr_staging_table()
conn = self._get_conn() conn = self._get_conn()
try: try:
conn.execute("DELETE FROM ocr_staging") conn.execute("DELETE FROM ocr_staging")