feat: 实现内存记录的去重和统一 scope_key 为 "default"

This commit is contained in:
ooodc 2026-06-06 15:07:06 +08:00
parent abb2d596f4
commit cf1152571d

View File

@ -1729,6 +1729,26 @@ fn ensure_scheduler_schema(conn: &Connection) -> Result<(), StorageError> {
} }
fn ensure_memory_scope_key_migration(conn: &Connection) -> Result<(), StorageError> { fn ensure_memory_scope_key_migration(conn: &Connection) -> Result<(), StorageError> {
// 步骤1去重。多条记录 scope_key 不同,改为 "default" 后会违反唯一约束。
// 对每个 (scope_kind, namespace, memory_key) 组合保留 updated_at 最新的一条。
conn.execute(
"
DELETE FROM memories
WHERE rowid NOT IN (
SELECT rowid FROM (
SELECT rowid, ROW_NUMBER() OVER (
PARTITION BY scope_kind, namespace, memory_key
ORDER BY updated_at DESC
) AS rn
FROM memories
)
WHERE rn = 1
)
",
[],
)?;
// 步骤2统一 scope_key
conn.execute( conn.execute(
"UPDATE memories SET scope_key = 'default' WHERE scope_key != 'default'", "UPDATE memories SET scope_key = 'default' WHERE scope_key != 'default'",
[], [],