File tree Expand file tree Collapse file tree 1 file changed +17
-2
lines changed
Expand file tree Collapse file tree 1 file changed +17
-2
lines changed Original file line number Diff line number Diff line change @@ -21,12 +21,27 @@ DROP INDEX IF EXISTS "memory_workflow_idx";
2121-- > statement-breakpoint
2222DROP INDEX IF EXISTS " memory_workflow_key_idx" ;
2323
24- -- Step 6: Add new foreign key and indexes
24+ -- Step 6: Deduplicate memory entries before creating unique index
25+ -- Keep only the most recently updated entry for each (workspace_id, key) pair
26+ DELETE FROM memory m1
27+ USING memory m2
28+ WHERE m1 .workspace_id = m2 .workspace_id
29+ AND m1 .key = m2 .key
30+ AND m1 .updated_at < m2 .updated_at ;
31+
32+ -- Handle ties by keeping the one with the smaller id
33+ DELETE FROM memory m1
34+ USING memory m2
35+ WHERE m1 .workspace_id = m2 .workspace_id
36+ AND m1 .key = m2 .key
37+ AND m1 .id > m2 .id ;
38+
39+ -- Step 7: Add new foreign key and indexes
2540ALTER TABLE " memory" ADD CONSTRAINT " memory_workspace_id_workspace_id_fk" FOREIGN KEY (" workspace_id" ) REFERENCES " public" ." workspace" (" id" ) ON DELETE cascade ON UPDATE no action;
2641-- > statement-breakpoint
2742CREATE INDEX "memory_workspace_idx " ON " memory" USING btree (" workspace_id" );
2843-- > statement-breakpoint
2944CREATE UNIQUE INDEX "memory_workspace_key_idx " ON " memory" USING btree (" workspace_id" ," key" );
3045
31- -- Step 7 : Drop old column
46+ -- Step 8 : Drop old column
3247ALTER TABLE " memory" DROP COLUMN IF EXISTS " workflow_id" ;
You can’t perform that action at this time.
0 commit comments