fix: migration 009 add source_count + entity dedup index
CI / CI (pull_request) Successful in 11m49s

- 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
This commit is contained in:
2026-09-10 10:40:48 +09:00
parent a616c0ebc2
commit 8fdcffc990
@@ -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;