From 8fdcffc990dc253a4d3ef9c850f5a1d6825e052c Mon Sep 17 00:00:00 2001 From: rock Date: Thu, 10 Sep 2026 10:40:48 +0900 Subject: [PATCH] fix: migration 009 add source_count + entity dedup index - Add source_count INTEGER DEFAULT 1 column - Dedup existing rows before creating unique index - CREATE UNIQUE INDEX idx_memory_entity_project_name (project_id, name) - Idempotent: safe to re-run --- .../migrations/009_temporal_edge_migration.sql | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/mem-store/migrations/009_temporal_edge_migration.sql b/crates/mem-store/migrations/009_temporal_edge_migration.sql index 4eeba2a..497dc0a 100644 --- a/crates/mem-store/migrations/009_temporal_edge_migration.sql +++ b/crates/mem-store/migrations/009_temporal_edge_migration.sql @@ -47,8 +47,20 @@ CREATE INDEX IF NOT EXISTS idx_memory_edge_target ON memory_edge(target_id); CREATE INDEX IF NOT EXISTS idx_memory_edge_project ON memory_edge(project_id); CREATE INDEX IF NOT EXISTS idx_memory_edge_relation ON memory_edge(relation_type); --- Ensure memory_entity has deleted_at for BFS soft-delete queries +-- Ensure memory_entity has all columns code expects ALTER TABLE memory_entity ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ; +ALTER TABLE memory_entity ADD COLUMN IF NOT EXISTS source_count INTEGER DEFAULT 1; + +-- Unique constraint for entity upsert dedup +DO $$ +BEGIN + -- Dedup existing rows before creating unique index + DELETE FROM memory_entity a USING memory_entity b + WHERE a.project_id = b.project_id AND a.name = b.name + AND a.t_created < b.t_created; +EXCEPTION WHEN OTHERS THEN NULL; +END $$; +CREATE UNIQUE INDEX IF NOT EXISTS idx_memory_entity_project_name ON memory_entity(project_id, name); -- ROLLBACK instructions: -- DROP TABLE IF EXISTS memory_edge;