feat: 0014 schema — embeddings FK CASCADE (deferred or applied) + memories.event_id column (T109)

This commit is contained in:
Joseph Doherty
2026-04-27 05:00:57 -04:00
parent 3f1a284acb
commit 1f8b4d2078
4 changed files with 91 additions and 4 deletions
@@ -0,0 +1,25 @@
-- 0014_phase45_schema.sql — Phase 4.5 Wave 2 schema bump (T109).
--
-- Two schema concerns are bundled into this migration:
--
-- 1. ``embeddings.memory_id`` FK should ideally carry ``ON DELETE CASCADE``
-- (T88 review nit). DEFERRED to Phase 5: ``embeddings`` rows are only ever
-- deleted when the parent ``memories`` row is deleted, and ``memories``
-- rows are never deleted today (memory hide is a soft flag; the surgical
-- ``deindex_event`` path operates on ``event_log`` and does NOT cascade
-- to projection rows). The CASCADE constraint therefore can't fire under
-- current usage — adding the SQLite table-rebuild dance (rename, recreate,
-- copy, drop, reindex) for a defensive constraint is unwarranted bloat
-- in a polish wave. Revisit during the broader Phase 5 migration cleanup
-- when other table reshapes make the rebuild worthwhile.
--
-- 2. Add ``memories.event_id`` (NULLABLE INTEGER, references ``event_log.id``)
-- so cross-chat search results can deep-link back to the originating
-- turn (foundation for T111). The column is nullable so historical
-- memory rows projected before 0014 ran continue to round-trip cleanly;
-- new rows are populated by the ``memory_written`` projector handler
-- from the projecting event's id. This is a pure additive change — no
-- backfill is performed. Older rows simply read NULL until/unless a
-- later migration backfills them; T111 surfaces are coded to accept
-- NULL gracefully (no deep-link rendered).
ALTER TABLE memories ADD COLUMN event_id INTEGER REFERENCES event_log(id);
+8 -2
View File
@@ -13,13 +13,18 @@ def _row_to_dict(conn: Connection, row: tuple) -> dict:
@on("memory_written")
def _apply_memory_written(conn: Connection, e: Event) -> None:
# T109 (schema 0014): persist the projecting event's id on the memory
# row so cross-chat search results can deep-link back to the
# originating turn (T111). Older memory rows projected before 0014
# ran read NULL here — the column is nullable for that reason.
p = e.payload
conn.execute(
"INSERT INTO memories ("
"owner_id, chat_id, scene_id, pov_summary, "
"witness_you, witness_host, witness_guest, "
"chat_clock_at, source, reliability, significance, pinned, auto_pinned"
") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
"chat_clock_at, source, reliability, significance, pinned, auto_pinned, "
"event_id"
") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
(
p["owner_id"],
p["chat_id"],
@@ -34,6 +39,7 @@ def _apply_memory_written(conn: Connection, e: Event) -> None:
int(p.get("significance", 1)),
int(p.get("pinned", 0)),
int(p.get("auto_pinned", 0)),
e.id,
),
)