|
2 | 2 | -- See docs/TEMPORAL.md. No asserted_at — occurred_at is effective time; |
3 | 3 | -- ingested_at is when the memory plane learned the content. |
4 | 4 |
|
5 | | -ALTER TABLE "memory"."version" |
6 | | - ADD COLUMN IF NOT EXISTS "temporal_class" text NOT NULL DEFAULT 'event'; |
7 | | - |
8 | | -ALTER TABLE "memory"."version" |
9 | | - DROP CONSTRAINT IF EXISTS "version_temporal_class_check"; |
| 5 | +-- The backfill runs only in the replay that adds the column, so distilled |
| 6 | +-- claims (inferred provenance) written before this migration default to |
| 7 | +-- state ranking exactly once; later inferred `event` rows are left alone. |
| 8 | +DO $$ |
| 9 | +BEGIN |
| 10 | + IF NOT EXISTS ( |
| 11 | + SELECT 1 FROM information_schema.columns |
| 12 | + WHERE table_schema = 'memory' AND table_name = 'version' |
| 13 | + AND column_name = 'temporal_class' |
| 14 | + ) THEN |
| 15 | + ALTER TABLE "memory"."version" |
| 16 | + ADD COLUMN "temporal_class" text NOT NULL DEFAULT 'event'; |
| 17 | + UPDATE "memory"."version" |
| 18 | + SET "temporal_class" = 'state' |
| 19 | + WHERE "provenance" = 'inferred'; |
| 20 | + END IF; |
| 21 | +END $$; |
10 | 22 |
|
11 | | -ALTER TABLE "memory"."version" |
12 | | - ADD CONSTRAINT "version_temporal_class_check" |
13 | | - CHECK ("temporal_class" IN ('event', 'deadline', 'state', 'lesson')); |
| 23 | +DO $$ |
| 24 | +BEGIN |
| 25 | + IF NOT EXISTS ( |
| 26 | + SELECT 1 FROM pg_constraint |
| 27 | + WHERE conname = 'version_temporal_class_check' AND conrelid = '"memory"."version"'::regclass |
| 28 | + ) THEN |
| 29 | + ALTER TABLE "memory"."version" |
| 30 | + ADD CONSTRAINT "version_temporal_class_check" |
| 31 | + CHECK ("temporal_class" IN ('event', 'deadline', 'state', 'lesson')); |
| 32 | + END IF; |
| 33 | +END $$; |
14 | 34 |
|
15 | 35 | ALTER TABLE "memory"."version" |
16 | 36 | ADD COLUMN IF NOT EXISTS "valid_from" timestamp; |
17 | 37 |
|
18 | 38 | ALTER TABLE "memory"."version" |
19 | 39 | ADD COLUMN IF NOT EXISTS "valid_until" timestamp; |
20 | 40 |
|
21 | | --- Distilled claims (inferred provenance) default to state ranking. |
22 | | -UPDATE "memory"."version" |
23 | | - SET "temporal_class" = 'state' |
24 | | - WHERE "provenance" = 'inferred' |
25 | | - AND "temporal_class" = 'event'; |
|
0 commit comments