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;