| 1 | -- Entries are immutable, and cards are deleted by being marked gone. |
| 2 | -- |
| 3 | -- Both were true only by convention: there was no UpdateEntry, and |
| 4 | -- DeleteCard set `gone` — but a stray `Save` on an entry_values row |
| 5 | -- rewrote it, and a stray `Delete` on a card hard-deleted it AND cascaded |
| 6 | -- its values, entries and refs away while the events survived pointing at |
| 7 | -- nothing. "No function exists for it" is not an invariant. |
| 8 | -- |
| 9 | -- Written by hand rather than generated: `rastrillo migration generate` |
| 10 | -- diffs Models against migrations and knows nothing about triggers, so it |
| 11 | -- would neither write these nor notice them missing. |
| 12 | |
| 13 | -- §3: an entry is a fact about a past moment. A correction is a new entry |
| 14 | -- that supersedes, never an edit. |
| 15 | CREATE TRIGGER entries_are_immutable |
| 16 | BEFORE UPDATE ON entries |
| 17 | BEGIN |
| 18 | SELECT RAISE(ABORT, 'entries are immutable: write a new entry that supersedes this one'); |
| 19 | END; |
| 20 | |
| 21 | CREATE TRIGGER entry_values_are_immutable |
| 22 | BEFORE UPDATE ON entry_values |
| 23 | BEGIN |
| 24 | SELECT RAISE(ABORT, 'entry values are immutable: write a new entry that supersedes this one'); |
| 25 | END; |
| 26 | |
| 27 | -- An entry may not be removed either: its event would outlive it and name |
| 28 | -- a row that is not there. |
| 29 | CREATE TRIGGER entries_are_not_deleted |
| 30 | BEFORE DELETE ON entries |
| 31 | WHEN (SELECT COUNT(*) FROM cards WHERE cards.id = OLD.card_id) > 0 |
| 32 | BEGIN |
| 33 | SELECT RAISE(ABORT, 'entries are not deleted: mark the card gone instead'); |
| 34 | END; |
| 35 | |
| 36 | -- §5 and suite ruling 14: retirement is by feed, never by erasure. A hard |
| 37 | -- delete here would take the card's values, entries and refs with it |
| 38 | -- through the cascades, silently and completely. |
| 39 | CREATE TRIGGER cards_are_not_hard_deleted |
| 40 | BEFORE DELETE ON cards |
| 41 | BEGIN |
| 42 | SELECT RAISE(ABORT, 'cards are not deleted: set gone, which is what DeleteCard does'); |
| 43 | END; |
| 44 | |