fix: token统计+聊天记录入库静默失败, 列名/类型/去重不匹配
根因:insert_llm_usage / insert_chat_record 因旧 iPet 遗留列 (kind/entry NOT NULL, context_token_present boolean→context_token text) 导致 INSERT 静默失败, 四个调用点 let _ = 吞掉错误。ias usage 显示 token 总量为 0。 修复: - llm_usage: 迁移 0010 ADD COLUMN IF NOT EXISTS + 条件 DROP NOT NULL, insert_llm_usage 写入 kind/entry 默认值 - chat_records: 迁移 0011 列名 context_token_present→context_token 类型转换+重命名, 迁移 0012 清理存量重复→partial UNIQUE index→恢复 ON CONFLICT - main.rs: 所有 let _ = insert_* 改为 if let Err(e) 日志输出, message_id 缺失时归为 '' 而非 '0'
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
-- 修复 llm_usage 表兼容性:旧库有 iPet 遗留的 kind/entry 列 (NOT NULL),新库没有
|
||||
-- kind/entry 保留为兼容字段,新代码写入默认值 ("llm_call" / null)
|
||||
-- 1) 添加列(如果不存在)
|
||||
ALTER TABLE llm_usage ADD COLUMN IF NOT EXISTS kind TEXT;
|
||||
ALTER TABLE llm_usage ADD COLUMN IF NOT EXISTS entry JSONB;
|
||||
-- 2) 只在列存在且 NOT NULL 时解除约束
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM information_schema.columns
|
||||
WHERE table_name = 'llm_usage' AND column_name = 'kind' AND is_nullable = 'NO') THEN
|
||||
ALTER TABLE llm_usage ALTER COLUMN kind DROP NOT NULL;
|
||||
END IF;
|
||||
IF EXISTS (SELECT 1 FROM information_schema.columns
|
||||
WHERE table_name = 'llm_usage' AND column_name = 'entry' AND is_nullable = 'NO') THEN
|
||||
ALTER TABLE llm_usage ALTER COLUMN entry DROP NOT NULL;
|
||||
END IF;
|
||||
END;
|
||||
$$;
|
||||
@@ -0,0 +1,26 @@
|
||||
-- 修复 chat_records 表中 iPet 遗留的 context_token_present (boolean) → context_token (text)
|
||||
-- 迁移 0001 创建的是 context_token TEXT,但旧库中列名是 context_token_present BOOLEAN
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM information_schema.columns
|
||||
WHERE table_name = 'chat_records' AND column_name = 'context_token_present') THEN
|
||||
|
||||
IF EXISTS (SELECT 1 FROM information_schema.columns
|
||||
WHERE table_name = 'chat_records' AND column_name = 'context_token') THEN
|
||||
-- 两列都存在(混合/半迁移库):回填数据后删除旧列
|
||||
UPDATE chat_records
|
||||
SET context_token = CASE WHEN context_token_present::boolean THEN '1' ELSE '' END
|
||||
WHERE context_token = '';
|
||||
ALTER TABLE chat_records DROP COLUMN context_token_present;
|
||||
ELSE
|
||||
-- 只有旧列:类型转换 boolean → text (true → '1', false → ''),再重命名
|
||||
ALTER TABLE chat_records
|
||||
ALTER COLUMN context_token_present TYPE TEXT
|
||||
USING CASE WHEN context_token_present::boolean THEN '1' ELSE '' END;
|
||||
ALTER TABLE chat_records RENAME COLUMN context_token_present TO context_token;
|
||||
ALTER TABLE chat_records ALTER COLUMN context_token SET DEFAULT '';
|
||||
END IF;
|
||||
|
||||
END IF;
|
||||
END;
|
||||
$$;
|
||||
@@ -0,0 +1,12 @@
|
||||
-- 聊天记录去重:清理存量重复 → 建 partial UNIQUE 索引支持 ON CONFLICT
|
||||
-- 1) 删除重复记录(保留每个 message_id 最早的那条)
|
||||
DELETE FROM chat_records a
|
||||
USING chat_records b
|
||||
WHERE a.message_id = b.message_id
|
||||
AND a.message_id NOT IN ('', '0')
|
||||
AND a.id > b.id;
|
||||
|
||||
-- 2) 替换旧普通索引为 UNIQUE partial index(排除空和占位值)
|
||||
DROP INDEX IF EXISTS idx_chat_records_message_id;
|
||||
CREATE UNIQUE INDEX idx_chat_records_message_id
|
||||
ON chat_records (message_id) WHERE message_id NOT IN ('', '0');
|
||||
Reference in New Issue
Block a user