Download this file
| 1 | -- doc_snapshots holds one Yjs update per row encoding a document's |
| 2 | -- entire state as of `seq` (the snapshots design, D2). It is an |
| 3 | -- accelerator, never the source of truth: doc_updates remains complete |
| 4 | -- and authoritative, and a row here that cannot be read is discarded in |
| 5 | -- favour of replaying the log. |
| 6 | -- |
| 7 | -- (doc_id, seq) is deliberately NOT unique. Two concurrent POSTs at the |
| 8 | -- cadence boundary can both snapshot the same replayed prefix, and those |
| 9 | -- rows are equivalent by construction, so a collision is not an error to |
| 10 | -- raise. "Newest usable" resolves as highest seq, then highest id. |
| 11 | -- |
| 12 | -- abi_version records what produced the row, which is what makes |
| 13 | -- discarding a snapshot from an older core implementable rather than |
| 14 | -- merely promised. |
| 15 | -- |
| 16 | -- Additive: creates a table, touches no existing row. |
| 17 | CREATE TABLE IF NOT EXISTS doc_snapshots ( |
| 18 | id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 19 | doc_id INTEGER NOT NULL, |
| 20 | seq INTEGER NOT NULL, |
| 21 | state BLOB NOT NULL, |
| 22 | abi_version INTEGER NOT NULL, |
| 23 | created_at DATETIME NOT NULL |
| 24 | ); |
| 25 | |
| 26 | CREATE INDEX IF NOT EXISTS idx_doc_snapshots_doc_seq |
| 27 | ON doc_snapshots (doc_id, seq DESC, id DESC); |
| 28 | |