mirror of
https://github.com/aljazceru/turso.git
synced 2026-02-05 00:04:23 +01:00
## Trigger Support This PR adds support for triggers: - `CREATE TRIGGER` - `DROP TRIGGER` Supported - `BEFORE/AFTER INSERT` - `BEFORE/AFTER DELETE` - `BEFORE/AFTER UPDATE [OF <col1,col2,col3>]` Not supported: - `INSTEAD OF` - `TEMPORARY` ### Implementation details - Triggers are executed within a new `Insn::Program` instruction. The spec of the insn differs a bit from SQlite: we store a `Statement` inside that instruction that we can `reset()` for every invocation. - Like Sqlite, trigger programs take `NEW` and `OLD` rows as program parameters. Whenever there are triggers that would fire as the result of a DML statement: - `DELETE` writes the rows being deleted into a `RowSet` first. - `UPDATE` and `INSERT` write the rows being updated into an ephemeral table first. ### Other shit Also added `EXPLAIN` support - the bytecode plans for trigger subprograms are appended after the main program. ### AI disclosure Used Cursor quite a bit for generating boilerplate code for this - you can blame all the bad code on the AI of course 🤡 ### Follow-ups: 1. ALTER TABLE ops need to rewrite the sql in the CREATE TRIGGER statement e.g. if a column is renamed. Columns cannot be dropped if referenced in triggers. 2. Fix weird rowid -1 fallback: https://github.com/tursodatabase/turso/pull/3979#issuecomment-3547999449 Closes #3979