macrame/schema/ddl.rs
1//! DDL statements for the Macrame bitemporal schema as specified in §4.
2
3/// GLOB pattern matching the canonical timestamp form `YYYY-MM-DDTHH:MM:SS.ffffffZ`.
4///
5/// A macro rather than a `const` so it can be spliced into the DDL literals by
6/// `concat!`, which only accepts literals. Kept byte-identical to
7/// [`crate::util::timestamp::CANONICAL_TS_GLOB`] by the unit test at the bottom
8/// of this file — the storage-layer guard and the Rust-layer guard must agree
9/// or one of them is decorative.
10macro_rules! ts_glob {
11 () => {
12 "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9][0-9][0-9][0-9][0-9]Z"
13 };
14}
15
16/// Table-level CHECK asserting every temporal column is canonical (§4.1, 0.5.4).
17///
18/// Timestamps are compared lexicographically everywhere — in SQL predicates, in
19/// `MAX(recorded_at)` when the clock recovers its floor, and in Rust `str`
20/// ordering. That is sound only if every value has the same width, so mixing
21/// `...T00:00:00Z` with `...T00:00:00.000000Z` makes `<=` disagree with
22/// chronology and traversals return empty sets with no error. The `Z` suffix
23/// alone does not achieve this; a fixed width does, and a CHECK is what makes
24/// it a property of the data rather than a convention.
25macro_rules! canonical_ts_check {
26 ($($col:literal),+ $(,)?) => {
27 concat!("CHECK (", $( $col, " GLOB '", ts_glob!(), "' AND ", )+ "1)")
28 };
29}
30
31/// A macro rather than a `const` for the same reason as [`ts_glob`]: `concat!`
32/// splices it into the table DDL and only accepts literals. [`WEIGHT_CHECK`] is
33/// the same text as a value, and carries the reasoning.
34macro_rules! weight_check {
35 () => {
36 "CHECK (weight >= 0.0 AND weight < 9e999 AND typeof(weight) = 'real')"
37 };
38}
39
40/// Table-level CHECK on `links.weight` (§4.7, T2.1, D-083).
41///
42/// Three clauses. Only the first is the one the item asked for; the other two
43/// were found by probing what the first still admits.
44///
45/// `weight >= 0.0` is the item as written: shortest-path analytics are unsound
46/// over negative weights, so Dijkstra and A\* refuse the graph at load time
47/// (D-039). Until now that refusal was the *only* place the property was
48/// enforced, which made it §4.7's one genuinely open gap — a database this crate
49/// wrote by itself could hold a row this crate would not read back.
50///
51/// `typeof(weight) = 'real'` closes a hole the item does not mention and which
52/// probing found. `REAL` in SQLite is an **affinity**, not a type: values that
53/// can be converted are, and values that cannot are stored as they came. `'abc'`
54/// cannot become a number, so it is stored as TEXT — and in SQLite's type
55/// ordering every text value sorts above every numeric one, so `'abc' >= 0.0`
56/// is *true* and the first clause passes it through.
57///
58/// That is not a wrong answer on the read side. It is a **panic**: reading a
59/// text `weight` as `f64` reaches `unreachable!("invalid value type")` inside
60/// libsql 0.9.30, in whatever unrelated query first touches the row. Measured,
61/// not reasoned about — see `examples/weight_check_probe.rs`.
62///
63/// The clause costs one `typeof` per insert and refuses nothing legitimate:
64/// `3`, `'5'` and `1.0` all arrive as REAL through affinity conversion and pass.
65/// It is taken **now** rather than in a later rung because SQLite has no
66/// `ADD CONSTRAINT` — every clause added later costs another full rebuild of the
67/// largest table in the schema.
68///
69/// `weight < 9e999` refuses `+∞`, and the reason is not the one anybody
70/// predicted. The plan expected the CHECK to admit infinity and argued the
71/// loader guard would catch it; the guard tests `< 0.0` and `is_nan()`, so it
72/// does not. The next guess — mine — was that this is harmless, since IEEE
73/// infinity propagates through addition and stays totally ordered, leaving
74/// Dijkstra terminating with "that edge is unusable": an odd answer, not a wrong
75/// one.
76///
77/// Both were wrong, and a test found it. **An infinite weight makes the
78/// transaction log unreplayable.** The log trigger serialises the row to JSON,
79/// and JSON has no representation for infinity, so the payload round-trips into
80/// `ReplayCorrupt { reason: "number out of range" }` — every later
81/// `reconstruct()` fails, including the one `close()` performs. The ledger is
82/// the source of truth under Doctrine III, so a value that cannot survive the
83/// log is not an eccentric weight, it is a corrupt one.
84///
85/// `9e999` is the idiom because SQLite has no `isinf`: the literal overflows to
86/// `+∞` on parse, and `inf < inf` is false. Finite values, including `1e308`,
87/// pass.
88///
89/// The loader guard still **stays**, for the reason the constraint cannot cover:
90/// `links_current` carries no CHECK, and neither do cold files created before
91/// this rung.
92pub const WEIGHT_CHECK: &str = weight_check!();
93
94/// The `RAISE(ABORT, …)` messages the schema's guards emit (§4.3).
95///
96/// Spliced into the trigger DDL *and* matched by [`crate::error::abort_kind`],
97/// so the guard and its classifier cannot drift. When they drift the failure is
98/// silent in the worst direction: the guard still fires, but the typed error
99/// (`SingleOpenViolation`, `RecordedAtRegression`, `ArchiveViolation`) degrades
100/// into an opaque `Engine` error that no caller can match on.
101macro_rules! abort_single_open {
102 () => {
103 "macrame: edge already has an open interval; retire it first"
104 };
105}
106macro_rules! abort_monotonic_ra {
107 () => {
108 "macrame: concept recorded_at must be strictly increasing"
109 };
110}
111macro_rules! abort_delete_guard {
112 () => {
113 "macrame: physical delete blocked outside archive session"
114 };
115}
116
117pub const ABORT_SINGLE_OPEN: &str = abort_single_open!();
118pub const ABORT_MONOTONIC_RA: &str = abort_monotonic_ra!();
119pub const ABORT_DELETE_GUARD: &str = abort_delete_guard!();
120
121/// Marker table probed by the delete guards (D-008 revised).
122///
123/// The archive session creates this table and drops it again inside the single
124/// `BEGIN IMMEDIATE … COMMIT` archive transaction, so it never exists as
125/// committed state. Connection-locality — the property the original
126/// `temp.sqlite_master` probe was reaching for — is preserved by two
127/// independent mechanisms: uncommitted DDL is visible only to the writing
128/// connection, and the archive transaction holds the write lock for its
129/// duration, so no other connection can reach the guard at all.
130pub const ARCHIVE_SESSION_MARKER: &str = "macrame_archive_session";
131
132/// The `concepts` ledger table (§4.1).
133///
134/// # `rowid_pk` is explicit, and that is the whole point (v8, D-119)
135///
136/// Through v7 this table declared `id TEXT PRIMARY KEY`, which left its rowid
137/// **implicit** — and `concepts_fts` is external-content keyed on that rowid.
138/// `VACUUM` renumbers implicit rowids, which would silently decouple the search
139/// index from the rows it indexes: no error, no integrity-check failure, just
140/// results that stop matching.
141///
142/// [D-071](../../docs/architecture/s13-decision-register.md) proved the hazard
143/// unreachable *by consequence rather than by design* — `trg_concepts_guard_delete`
144/// is unconditional, so rowids are dense `1..n` and `VACUUM`'s renumbering is
145/// the identity map. 0.9.0's archival makes them sparse and makes the hazard
146/// real, so v8 replaces the accident with a column: an `INTEGER PRIMARY KEY` is
147/// a stored value, and `VACUUM` preserves it whether the numbering is dense or
148/// not (measured in `examples/concepts_rebuild_probe.rs` §5).
149///
150/// SQLite permits one primary key per table, so `id` becomes `NOT NULL UNIQUE`.
151/// That keeps it a valid foreign-key parent for `links.source_id` /
152/// `links.target_id` and keeps `ON CONFLICT(id)` working, but it **is** a
153/// primary-key change — which [D-036](../../docs/architecture/s13-decision-register.md)
154/// forbids outright after 1.0. Taken pre-1.0 on purpose, or never.
155pub const CREATE_CONCEPTS_TABLE: &str = concat!(
156 r#"
157CREATE TABLE IF NOT EXISTS concepts (
158 rowid_pk INTEGER PRIMARY KEY,
159 id TEXT NOT NULL UNIQUE,
160 title TEXT NOT NULL,
161 content TEXT NOT NULL DEFAULT '',
162 embedding_model TEXT,
163 valid_from TEXT NOT NULL,
164 valid_to TEXT NOT NULL DEFAULT '9999-12-31T23:59:59.999999Z',
165 recorded_at TEXT NOT NULL,
166 retired INTEGER NOT NULL DEFAULT 0,
167 "#,
168 canonical_ts_check!("valid_from", "valid_to", "recorded_at"),
169 r#"
170);
171"#
172);
173
174pub const CREATE_LINKS_TABLE: &str = concat!(
175 r#"
176CREATE TABLE IF NOT EXISTS links (
177 source_id TEXT NOT NULL REFERENCES concepts(id),
178 target_id TEXT NOT NULL REFERENCES concepts(id),
179 edge_type TEXT NOT NULL,
180 valid_from TEXT NOT NULL,
181 recorded_at TEXT NOT NULL,
182 valid_to TEXT NOT NULL DEFAULT '9999-12-31T23:59:59.999999Z',
183 weight REAL NOT NULL DEFAULT 1.0,
184 properties TEXT NOT NULL DEFAULT '{}',
185 PRIMARY KEY (source_id, target_id, edge_type, valid_from, recorded_at),
186 "#,
187 weight_check!(),
188 r#",
189 "#,
190 canonical_ts_check!("valid_from", "valid_to", "recorded_at"),
191 r#"
192);
193"#
194);
195
196pub const CREATE_LINKS_CURRENT_TABLE: &str = concat!(
197 r#"
198CREATE TABLE IF NOT EXISTS links_current (
199 source_id TEXT NOT NULL,
200 target_id TEXT NOT NULL,
201 edge_type TEXT NOT NULL,
202 valid_from TEXT NOT NULL,
203 valid_to TEXT NOT NULL,
204 weight REAL NOT NULL,
205 properties TEXT NOT NULL,
206 recorded_at TEXT NOT NULL,
207 PRIMARY KEY (source_id, target_id, edge_type, valid_from),
208 "#,
209 canonical_ts_check!("valid_from", "valid_to", "recorded_at"),
210 r#"
211);
212"#
213);
214
215pub const CREATE_TRANSACTION_LOG_TABLE: &str = concat!(
216 r#"
217CREATE TABLE IF NOT EXISTS transaction_log (
218 seq_id INTEGER PRIMARY KEY AUTOINCREMENT,
219 table_name TEXT NOT NULL,
220 entity_id TEXT NOT NULL,
221 operation TEXT NOT NULL,
222 payload TEXT NOT NULL,
223 recorded_at TEXT NOT NULL,
224 "#,
225 canonical_ts_check!("recorded_at"),
226 r#"
227);
228"#
229);
230
231/// The per-model embedding table (§4.1, D-005), for a validated model name.
232///
233/// A function rather than a `const` because the table's identity *and its
234/// column type* both depend on the model: `F32_BLOB(dim)` carries the declared
235/// dimension in the schema, which is what [`crate::vector::declared_dimension`]
236/// reads back so the crate never keeps a second copy of it.
237///
238/// Deliberately not part of the baseline migration. Which models exist is an
239/// application's choice made over time, not a property of the schema version,
240/// and D-036 classifies these tables as disposable periphery: a migration may
241/// drop one and re-embed. `IF NOT EXISTS` makes registration idempotent.
242///
243/// No temporal columns, on purpose. Doctrine VII makes an embedding a derived
244/// artifact of a model applied to content — it has no valid time of its own, and
245/// giving it a `recorded_at` would put a third clock next to the two §2 permits
246/// and invite queries that mix them.
247pub fn create_embeddings_table(model: &crate::vector::ModelName, dim: usize) -> String {
248 format!(
249 "CREATE TABLE IF NOT EXISTS {table} (
250 concept_id TEXT PRIMARY KEY REFERENCES concepts(id),
251 embedding F32_BLOB({dim}) NOT NULL
252);",
253 table = model.table(),
254 )
255}
256
257/// The DiskANN index over a model's vectors.
258///
259/// **Load-bearing for correctness, not only for speed.** Measured against
260/// libSQL 0.9.30: a blob of the wrong length inserted into an `F32_BLOB(4)`
261/// column is *accepted* while no vector index exists, and rejected — with the
262/// row not landing — once one does. §4.1 previously claimed the column type
263/// enforced its own dimension at insert time; it does not. So this index is
264/// created together with the table it indexes and is never optional, and
265/// dropping it to speed up a bulk load would silently disarm the only
266/// storage-layer check on dimension.
267pub fn create_embeddings_index(model: &crate::vector::ModelName) -> String {
268 format!(
269 "CREATE INDEX IF NOT EXISTS {index} ON {table} (libsql_vector_idx(embedding));",
270 index = model.index(),
271 table = model.table(),
272 )
273}
274
275/// Derived analytics output, keyed by concept and label (§5.4, D-041).
276///
277/// Deliberately outside the ledger. Three properties are load-bearing and each
278/// is the opposite of what the four normative tables above do.
279///
280/// **No log trigger.** Nothing in [`CREATE_TRIGGERS`] fires on this table, so an
281/// annotation never reaches `transaction_log`. That is Doctrine VII's reasoning
282/// about embeddings applied to the other derived artifact: a community label is
283/// a function of an algorithm, a version of that algorithm, and a graph — not a
284/// statement about the world, and a ledger that records it is recording the
285/// analytics schedule as though it were history. A reconstruction that wants
286/// labels recomputes them, which is the only honest way to ask what a past
287/// graph's communities *were*.
288///
289/// **No delete guard.** Doctrine V protects the hot ledger tables; this table is
290/// derivative state in Doctrine VI's second category, so wiping it must stay a
291/// legal, ordinary operation — a rerun replaces the previous pass, and dropping
292/// the whole table costs nothing but the recomputation.
293///
294/// **Upsert on `(concept_id, label)`.** One current value per label per concept.
295/// Storing a history of successive runs here would be the ledger again, by
296/// another name.
297///
298/// The foreign key is safe in a way `links_current`'s omitted ones are not:
299/// concepts are never physically deleted (D-022), and this table is rebuilt by
300/// re-running an algorithm that read `concepts` in the first place, so there is
301/// no insertion-order problem to solve.
302pub const CREATE_ANALYTICS_ANNOTATIONS_TABLE: &str = concat!(
303 r#"
304CREATE TABLE IF NOT EXISTS analytics_annotations (
305 concept_id TEXT NOT NULL REFERENCES concepts(id),
306 label TEXT NOT NULL,
307 value TEXT NOT NULL,
308 computed_at TEXT NOT NULL,
309 PRIMARY KEY (concept_id, label),
310 "#,
311 canonical_ts_check!("computed_at"),
312 r#"
313);
314"#
315);
316
317/// The keyword half of hybrid search: an FTS5 index over concept text (§5.9).
318///
319/// **External content.** The table declares `content='concepts'`, so the tokens
320/// are indexed but the text itself is not duplicated — FTS5 reads it back from
321/// `concepts` by rowid when it needs a column value. Two reasons beyond the
322/// storage saving, and the second is the one that decided it:
323///
324/// * There is exactly one copy of the text, so the index cannot disagree with
325/// the concept about what the concept says. A standalone FTS table would be a
326/// second description of data the ledger already holds, which is the failure
327/// class D-030 and D-035 exist to prevent.
328/// * `INSERT INTO concepts_fts(concepts_fts) VALUES('rebuild')` reconstructs the
329/// whole index from the content table in one statement. D-036 requires every
330/// derivative table to be rebuildable from the ledger, and here that is the
331/// engine's own operation rather than code of ours that has to be kept honest.
332///
333/// The cost is that external-content tables do not maintain themselves: an
334/// `UPDATE` must retract the *old* terms before adding the new ones, using the
335/// old column values. That is what `trg_concepts_fts_update` does, and getting
336/// it wrong leaves an index that still matches text no concept contains.
337///
338/// **`content_rowid` names `rowid_pk`, not `rowid` (v8, D-119).** They are the
339/// same value — an `INTEGER PRIMARY KEY` *is* the rowid — but naming the column
340/// is what makes the key a declared one rather than an implicit one `VACUUM` is
341/// free to renumber. See [`CREATE_CONCEPTS_TABLE`].
342pub const CREATE_CONCEPTS_FTS: &str = r#"
343CREATE VIRTUAL TABLE IF NOT EXISTS concepts_fts USING fts5(
344 title,
345 content,
346 content='concepts',
347 content_rowid='rowid_pk'
348);
349"#;
350
351/// FTS5's own consistency check — **and it cannot see the failure that matters**
352/// (§5.9, D-071).
353///
354/// Kept as a named constant so the finding has somewhere to live, and used by
355/// `an_emptied_fts_index_still_passes_integrity_check`, which is a tripwire
356/// rather than a guarantee.
357///
358/// On this libSQL build (0.9.30), `'integrity-check'` verifies the index's
359/// *internal* consistency and not its agreement with the content table. Measured:
360/// after `'delete-all'` the index answers zero matches where it answered ten, and
361/// both `'integrity-check'` and `'integrity-check', 0` still report success. So a
362/// `verify_fts()` built on this would report a healthy index for an empty one —
363/// which is why there is no `verify_fts()`. See D-071.
364pub const VERIFY_CONCEPTS_FTS: &str =
365 "INSERT INTO concepts_fts (concepts_fts) VALUES ('integrity-check');";
366
367/// Reconstruct the FTS index from `concepts` (§5.9, D-036).
368///
369/// The engine's own operation, so the rebuild path is not a second
370/// implementation of the triggers that could drift from them.
371pub const REBUILD_CONCEPTS_FTS: &str =
372 "INSERT INTO concepts_fts (concepts_fts) VALUES ('rebuild');";
373
374/// Every index the schema declares.
375///
376/// # Two entries left in v8, and why the list is now allowed to be short
377///
378/// `idx_annotations_label` and `idx_lc_tgt_active` were dropped by the v7 → v8
379/// rung ([D-089](../../docs/architecture/s13-decision-register.md), completed by
380/// D-118). Neither had a reader anywhere in the crate — `analytics_annotations`
381/// is never selected from here at all, and no query seeks on
382/// `links_current.target_id` as a leading column — so each was an index write
383/// per insert, forever, buying nothing. One of them was on the crate's hottest
384/// write path.
385///
386/// `tests/index_plan_tests.rs` now requires the unread set to be **empty**,
387/// which turns "these two are known bad" into "an index with no reader is a red
388/// test". That is the guarantee this list is kept short by.
389pub const CREATE_INDICES: &[&str] = &[
390 // Covering index for the traversal CTE (§5.2, D-042).
391 //
392 // Column order is load-bearing and was measured with EXPLAIN QUERY PLAN.
393 // The seek column is `source_id`; everything after it is there so the
394 // recursive step never touches the base table. The two range columns come
395 // next and `edge_type` comes *after* them, because `edge_types` is empty
396 // unless a caller sets it: with `edge_type` in second position SQLite
397 // declines the index for the unfiltered traversal — the default one — and
398 // silently falls back to a non-covering plan.
399 //
400 // (source_id, edge_type, valid_from, ...) filtered: COVERING
401 // unfiltered: NOT covering
402 // (source_id, valid_from, valid_to, weight, edge_type, target_id)
403 // both: COVERING
404 //
405 // This subsumes the former idx_lc_src_active (source_id, valid_to): same
406 // prefix column, strictly more payload. Keeping both would pay two index
407 // writes per assertion on a table that already takes three writes.
408 "CREATE INDEX IF NOT EXISTS idx_lc_traversal_cover ON links_current \
409 (source_id, valid_from, valid_to, weight, edge_type, target_id);",
410 // The single-open-interval probe's own index (D-059, shipped v5 -> v6).
411 //
412 // `trg_links_single_open` runs an `EXISTS` on every edge insert, keyed on
413 // (source_id, target_id, edge_type, valid_to) with valid_from as an
414 // inequality. Before this index the planner served that probe from
415 // `idx_lc_traversal_cover` with only `source_id` bound — it wins as a
416 // covering index over the primary-key autoindex, which lacks `valid_to` —
417 // so **every insert scanned its source's entire out-degree**. Measured on a
418 // fixed 90-row chunk: 4.4 ms into an empty table, 18.4 ms into a
419 // 2,000-edge hub, 47.7 ms into an 8,000-edge one, and 1.06 s into 90,000.
420 // Growth in the table, not in the chunk.
421 //
422 // With this index the same 90 rows into the 8,000-edge hub take 8.0 ms and
423 // stay flat. It matters beyond bulk import: the probe is on the insert path,
424 // so an interactive `assert_edge` against a high-degree node paid the same
425 // scan, and that is the path CHUNK_BUDGET's 3 ms exists to protect.
426 //
427 // Column order follows the trigger's WHERE exactly — the three equalities
428 // first, then `valid_to` which is compared to the sentinel, then
429 // `valid_from` which is the `<>` and cannot be a seek column. This does not
430 // subsume `idx_lc_traversal_cover` and is not subsumed by it: that one leads
431 // on `source_id` alone for the recursive walk, this one needs all three
432 // equality columns bound. Both are kept, which is a fourth index write per
433 // assertion buying a scan's removal from the same operation.
434 "CREATE INDEX IF NOT EXISTS idx_lc_open_interval ON links_current \
435 (source_id, target_id, edge_type, valid_to, valid_from);",
436 "CREATE INDEX IF NOT EXISTS idx_txlog_time ON transaction_log (recorded_at);",
437 "CREATE INDEX IF NOT EXISTS idx_txlog_entity ON transaction_log (entity_id);",
438];
439
440/// Every trigger the schema declares.
441///
442/// **`IF NOT EXISTS` means a changed body does not reach an existing file.**
443/// `migrations::verify` checks trigger *presence by name*, which is deliberate
444/// (a count refuses healthy databases) but does not and cannot notice that a
445/// trigger present under the right name carries an older body. A database
446/// stamped v5 by an earlier build therefore keeps whatever trigger text it was
447/// created with until a rung drops and recreates it.
448///
449/// This is why the payload carries a version. Changing a log trigger's payload
450/// splits the database population in two — files created after the change write
451/// the new shape, files created before keep writing the old one — and the only
452/// thing that makes that survivable is that every reader accepts both. A
453/// payload change that did *not* bump `v` would be indistinguishable at read
454/// time from corruption, which is the case `DbError::PayloadVersion` exists for.
455///
456/// The v1 → v2 concept payload (defect V) is deliberately left to ride along on
457/// the next rung that has to move `user_version` anyway rather than claiming one
458/// of its own: an old file loses `embedding_model` from its temporal reads, which
459/// is exactly the behaviour it had before, and gains it the moment it is
460/// migrated. Nothing regresses in the meantime.
461pub const CREATE_TRIGGERS: &[&str] = &[
462 r#"
463 CREATE TRIGGER IF NOT EXISTS trg_links_current_sync
464 AFTER INSERT ON links
465 BEGIN
466 INSERT INTO links_current
467 (source_id, target_id, edge_type, valid_from, valid_to,
468 weight, properties, recorded_at)
469 VALUES
470 (NEW.source_id, NEW.target_id, NEW.edge_type, NEW.valid_from,
471 NEW.valid_to, NEW.weight, NEW.properties, NEW.recorded_at)
472 ON CONFLICT(source_id, target_id, edge_type, valid_from) DO UPDATE SET
473 valid_to = excluded.valid_to,
474 weight = excluded.weight,
475 properties = excluded.properties,
476 recorded_at = excluded.recorded_at
477 WHERE excluded.recorded_at > links_current.recorded_at;
478 END;
479 "#,
480 concat!(
481 r#"
482 CREATE TRIGGER IF NOT EXISTS trg_links_single_open
483 BEFORE INSERT ON links
484 WHEN NEW.valid_to = '9999-12-31T23:59:59.999999Z'
485 AND EXISTS (
486 SELECT 1 FROM links_current
487 WHERE source_id = NEW.source_id
488 AND target_id = NEW.target_id
489 AND edge_type = NEW.edge_type
490 AND valid_from <> NEW.valid_from
491 AND valid_to = '9999-12-31T23:59:59.999999Z'
492 )
493 BEGIN
494 SELECT RAISE(ABORT, '"#,
495 abort_single_open!(),
496 r#"');
497 END;
498 "#
499 ),
500 concat!(
501 r#"
502 CREATE TRIGGER IF NOT EXISTS trg_concepts_monotonic_ra
503 BEFORE UPDATE ON concepts
504 WHEN NEW.recorded_at <= OLD.recorded_at
505 BEGIN
506 SELECT RAISE(ABORT, '"#,
507 abort_monotonic_ra!(),
508 r#"');
509 END;
510 "#
511 ),
512 // Payload v2 adds `embedding_model` (defect V). Before it, the field was
513 // written by nobody and read by two — `replay::fold_delta` and
514 // `as_of::hydrate_attributes` both asked the payload for it and both always
515 // saw null, so `AttributeMode::AtTime`, the faithful mode Doctrine VIII
516 // exists to offer, returned a *less* complete record than `Current`.
517 //
518 // The version number moves because the shape is a compat surface: readers
519 // must be able to tell "this build wrote no model" from "this payload
520 // predates the field". v1 is still accepted and folds with the field absent,
521 // which is what makes this safe without a migration rung — see the note on
522 // [`CREATE_TRIGGERS`].
523 r#"
524 CREATE TRIGGER IF NOT EXISTS trg_concepts_log_insert
525 AFTER INSERT ON concepts
526 BEGIN
527 INSERT INTO transaction_log (table_name, entity_id, operation, payload, recorded_at)
528 VALUES ('concepts', NEW.id, 'I',
529 json_object('v', 2, 'title', NEW.title, 'content', NEW.content,
530 'valid_from', NEW.valid_from, 'valid_to', NEW.valid_to,
531 'retired', NEW.retired,
532 'embedding_model', NEW.embedding_model),
533 NEW.recorded_at);
534 END;
535 "#,
536 r#"
537 CREATE TRIGGER IF NOT EXISTS trg_concepts_log_update
538 AFTER UPDATE ON concepts
539 BEGIN
540 INSERT INTO transaction_log (table_name, entity_id, operation, payload, recorded_at)
541 VALUES ('concepts', NEW.id, 'U',
542 json_object('v', 2, 'title', NEW.title, 'content', NEW.content,
543 'valid_from', NEW.valid_from, 'valid_to', NEW.valid_to,
544 'retired', NEW.retired,
545 'embedding_model', NEW.embedding_model),
546 NEW.recorded_at);
547 END;
548 "#,
549 r#"
550 CREATE TRIGGER IF NOT EXISTS trg_links_log_insert
551 AFTER INSERT ON links
552 BEGIN
553 INSERT INTO transaction_log (table_name, entity_id, operation, payload, recorded_at)
554 VALUES ('links',
555 NEW.source_id || '|' || NEW.target_id || '|' || NEW.edge_type || '|' || NEW.valid_from,
556 'I',
557 json_object('v', 1, 'source_id', NEW.source_id, 'target_id', NEW.target_id,
558 'edge_type', NEW.edge_type, 'valid_from', NEW.valid_from,
559 'valid_to', NEW.valid_to, 'weight', NEW.weight,
560 'properties', json(NEW.properties)),
561 NEW.recorded_at);
562 END;
563 "#,
564 // Concepts are NEVER physically archived (D-022), so this guard is
565 // unconditional -- there is no session in which the delete becomes legal.
566 r#"
567 CREATE TRIGGER IF NOT EXISTS trg_concepts_guard_delete
568 BEFORE DELETE ON concepts
569 BEGIN
570 SELECT RAISE(ABORT, 'macrame: concepts are never physically archived (D-022)');
571 END;
572 "#,
573 // D-008 (revised): probe main.sqlite_master for the archive-session marker.
574 // SQLite forbids a trigger in `main` from referencing objects in another
575 // database, temp included, so the original temp.sqlite_master probe fails
576 // at CREATE TRIGGER time and is unimplementable.
577 concat!(
578 r#"
579 CREATE TRIGGER IF NOT EXISTS trg_links_guard_delete
580 BEFORE DELETE ON links
581 WHEN NOT EXISTS (
582 SELECT 1 FROM sqlite_master
583 WHERE type = 'table' AND name = 'macrame_archive_session'
584 )
585 BEGIN
586 SELECT RAISE(ABORT, '"#,
587 abort_delete_guard!(),
588 r#"');
589 END;
590 "#
591 ),
592 concat!(
593 r#"
594 CREATE TRIGGER IF NOT EXISTS trg_txlog_guard_delete
595 BEFORE DELETE ON transaction_log
596 WHEN NOT EXISTS (
597 SELECT 1 FROM sqlite_master
598 WHERE type = 'table' AND name = 'macrame_archive_session'
599 )
600 BEGIN
601 SELECT RAISE(ABORT, '"#,
602 abort_delete_guard!(),
603 r#"');
604 END;
605 "#
606 ),
607 // --- FTS sync (§5.9) ------------------------------------------------
608 //
609 // These write to `concepts_fts` and to nothing else. In particular they do
610 // not touch `transaction_log`: an FTS index is derived from concept text
611 // the ledger already records, so logging it would record the same fact
612 // twice — the reasoning Doctrine VII applies to embeddings, and the reason
613 // `doctrine_static_tests` scans this array.
614 r#"
615 CREATE TRIGGER IF NOT EXISTS trg_concepts_fts_insert
616 AFTER INSERT ON concepts
617 BEGIN
618 INSERT INTO concepts_fts (rowid, title, content)
619 VALUES (NEW.rowid_pk, NEW.title, NEW.content);
620 END;
621 "#,
622 // The retraction is not optional and not symmetric with the insert. An
623 // external-content FTS5 index stores terms, not text, so replacing a row
624 // means telling it which terms to *remove* — and it needs the old column
625 // values to work that out. Omit this and the index keeps matching words the
626 // concept no longer contains, with no error and no way to notice except by
627 // searching for something that is no longer there.
628 r#"
629 CREATE TRIGGER IF NOT EXISTS trg_concepts_fts_update
630 AFTER UPDATE ON concepts
631 BEGIN
632 INSERT INTO concepts_fts (concepts_fts, rowid, title, content)
633 VALUES ('delete', OLD.rowid_pk, OLD.title, OLD.content);
634 INSERT INTO concepts_fts (rowid, title, content)
635 VALUES (NEW.rowid_pk, NEW.title, NEW.content);
636 END;
637 "#,
638 // The third trigger, installed **inert** by v8 (§4.6, D-119).
639 //
640 // Through v7 this array had no delete trigger, and the stated reason was
641 // that `trg_concepts_guard_delete` is unconditional (D-022) so no delete
642 // path exists to keep in sync. That was true and it was the wrong shape:
643 // the index's correctness depended on a *different* trigger staying
644 // unconditional, and nothing connected the two except a comment.
645 //
646 // It cannot fire today — the guard is a `BEFORE DELETE` that always aborts,
647 // so the statement never reaches `AFTER DELETE`. It is here because 0.9.0's
648 // archive session is what makes the guard conditional, and the moment that
649 // lands the index would go silently stale without this. Installing the
650 // capability in the rung that is already rebuilding the table costs nothing
651 // and means 0.9.0 needs no migration of its own.
652 //
653 // `the_fts_delete_trigger_is_installed_and_inert` (wave1_regression_tests)
654 // pins both halves rather than assuming either.
655 r#"
656 CREATE TRIGGER IF NOT EXISTS trg_concepts_fts_delete
657 AFTER DELETE ON concepts
658 BEGIN
659 INSERT INTO concepts_fts (concepts_fts, rowid, title, content)
660 VALUES ('delete', OLD.rowid_pk, OLD.title, OLD.content);
661 END;
662 "#,
663];
664
665#[cfg(test)]
666mod tests {
667 use crate::util::timestamp::{CANONICAL_TS_GLOB, OPEN_SENTINEL};
668
669 /// The DDL's CHECK pattern and the Rust-side pattern must be the same
670 /// pattern. If they drift, one layer accepts what the other rejects and the
671 /// canonical-form invariant is enforced in name only.
672 #[test]
673 fn ddl_glob_matches_the_rust_canonical_pattern() {
674 assert_eq!(format!("'{}'", ts_glob!()), CANONICAL_TS_GLOB);
675 }
676
677 /// Every DDL statement that declares a temporal default must use the
678 /// canonical sentinel; a second-precision default would be rejected by the
679 /// very CHECK sitting next to it.
680 #[test]
681 fn ddl_defaults_use_the_canonical_sentinel() {
682 for ddl in [
683 super::CREATE_CONCEPTS_TABLE,
684 super::CREATE_LINKS_TABLE,
685 super::CREATE_LINKS_CURRENT_TABLE,
686 super::CREATE_TRANSACTION_LOG_TABLE,
687 ] {
688 assert!(
689 !ddl.contains("9999-12-31T23:59:59Z"),
690 "DDL still carries the pre-0.5.4 second-precision sentinel: {ddl}"
691 );
692 }
693 for trigger in super::CREATE_TRIGGERS {
694 assert!(
695 !trigger.contains("9999-12-31T23:59:59Z"),
696 "trigger still carries the pre-0.5.4 sentinel: {trigger}"
697 );
698 }
699 assert!(super::CREATE_LINKS_TABLE.contains(OPEN_SENTINEL));
700 }
701
702 /// Every abort message the classifier matches on must actually appear in the
703 /// DDL that emits it. `concat!` makes this true by construction today; the
704 /// test is what keeps it true if someone re-inlines a literal.
705 #[test]
706 fn every_abort_message_appears_in_a_trigger() {
707 for msg in [
708 super::ABORT_SINGLE_OPEN,
709 super::ABORT_MONOTONIC_RA,
710 super::ABORT_DELETE_GUARD,
711 ] {
712 assert!(
713 super::CREATE_TRIGGERS.iter().any(|t| t.contains(msg)),
714 "no trigger emits {msg:?}, so its typed error is unreachable"
715 );
716 }
717 }
718}