The rule table stops naming its triggers
Browse files at this commitcc4fb3fe7bae29794e3fb33b5eda662e6dc4b0b2Parent: a672cd4- feat
- the rule table stops naming its triggers
3 changed files +225 -3
- crates/izlek-core/migrations/0013_more_than_a_move.sql +48 -0
- crates/izlek-core/src/store/turso_store.rs +4 -0
- crates/izlek-core/tests/store.rs +173 -3
crates/izlek-core/migrations/0013_more_than_a_move.sqladded+48 -0
| @@ -0,0 +1,48 @@ | |||
| 1 | +-- The rule table named its two triggers in a CHECK: 'status' or 'unblocked', | ||
| 2 | +-- nothing else. Every new trigger has meant a migration just to widen that | ||
| 3 | +-- list. This rebuild drops the CHECK on `trigger_kind` entirely — a future | ||
| 4 | +-- trigger is a value, not a schema change — and widens `audience` to add | ||
| 5 | +-- 'creator' the same way 0004_mail.sql wrote it in the first place. | ||
| 6 | +-- | ||
| 7 | +-- SQLite cannot drop or relax a CHECK in place, so the table is rebuilt, | ||
| 8 | +-- same shape of migration as 0005_freeing.sql and 0012. | ||
| 9 | +-- | ||
| 10 | +-- `mail_send.rule_id` and `mail_decision.rule_id` both point at `mail_rule` | ||
| 11 | +-- with ON DELETE CASCADE, and every connection this store opens runs with | ||
| 12 | +-- foreign keys on. SQLite performs an implicit DELETE FROM before a DROP | ||
| 13 | +-- TABLE takes effect, and that implicit delete walks ON DELETE CASCADE like | ||
| 14 | +-- any other — dropping `mail_rule` with rows in it would carry every send and | ||
| 15 | +-- every decision away with it. `PRAGMA foreign_keys` is documented as a | ||
| 16 | +-- no-op inside a transaction, which is where every migration in this file | ||
| 17 | +-- runs, so the pragma is set here anyway and then proven by the reopen test | ||
| 18 | +-- rather than trusted from the manual. | ||
| 19 | +PRAGMA foreign_keys = OFF; | ||
| 20 | + | ||
| 21 | +CREATE TABLE mail_rule_next ( | ||
| 22 | + id TEXT PRIMARY KEY, | ||
| 23 | + board_id TEXT NOT NULL REFERENCES board(id), | ||
| 24 | + -- No CHECK list: the set of triggers grows without a schema change now. | ||
| 25 | + trigger_kind TEXT NOT NULL, | ||
| 26 | + trigger_column TEXT REFERENCES board_column(id), | ||
| 27 | + subject TEXT NOT NULL, | ||
| 28 | + -- 'assignees' — the people the card points at; 'board' — everyone who can | ||
| 29 | + -- write on the board; 'creator' — whoever opened the card. | ||
| 30 | + audience TEXT NOT NULL CHECK (audience IN ('assignees', 'board', 'creator')), | ||
| 31 | + enabled INTEGER NOT NULL DEFAULT 1, | ||
| 32 | + created_at TEXT NOT NULL, | ||
| 33 | + -- A status rule names a column and an unblocked rule does not. The check | ||
| 34 | + -- is here so a half-written rule cannot be stored at all. | ||
| 35 | + CHECK ((trigger_kind = 'status') = (trigger_column IS NOT NULL)) | ||
| 36 | +); | ||
| 37 | + | ||
| 38 | +INSERT INTO mail_rule_next | ||
| 39 | +SELECT id, board_id, trigger_kind, trigger_column, subject, audience, enabled, created_at | ||
| 40 | +FROM mail_rule; | ||
| 41 | + | ||
| 42 | +DROP INDEX IF EXISTS mail_rule_by_board; | ||
| 43 | +DROP TABLE mail_rule; | ||
| 44 | +ALTER TABLE mail_rule_next RENAME TO mail_rule; | ||
| 45 | + | ||
| 46 | +CREATE INDEX mail_rule_by_board ON mail_rule(board_id); | ||
| 47 | + | ||
| 48 | +PRAGMA foreign_keys = ON; | ||
crates/izlek-core/src/store/turso_store.rsmodified+4 -0
| @@ -52,6 +52,10 @@ const MIGRATIONS: &[(i64, &str)] = &[ | |||
| 52 | 12, | 52 | 12, |
| 53 | include_str!("../../migrations/0012_a_mail_that_owes_no_rule.sql"), | 53 | include_str!("../../migrations/0012_a_mail_that_owes_no_rule.sql"), |
| 54 | ), | 54 | ), |
| 55 | + ( | ||
| 56 | + 13, | ||
| 57 | + include_str!("../../migrations/0013_more_than_a_move.sql"), | ||
| 58 | + ), | ||
| 55 | ]; | 59 | ]; |
| 56 | 60 | ||
| 57 | /// The board a fresh workspace gets, and its columns. `Done` is the column | 61 | /// The board a fresh workspace gets, and its columns. `Done` is the column |
crates/izlek-core/tests/store.rsmodified+173 -3
| @@ -94,6 +94,141 @@ async fn member(store: &TursoStore, workspace_id: &str, email: &str, name: &str) | |||
| 94 | .id | 94 | .id |
| 95 | } | 95 | } |
| 96 | 96 | ||
| 97 | +/// Builds a database file shaped like schema version 12 — every migration | ||
| 98 | +/// through 0012 applied, nothing after — by replaying the migration files | ||
| 99 | +/// straight off disk on a raw connection, the same way [`TursoStore::apply`] | ||
| 100 | +/// does it one at a time. Used only to put a rule, a send and a decision in | ||
| 101 | +/// place before 0013 exists, so 0013 runs against real rows rather than an | ||
| 102 | +/// empty table. | ||
| 103 | +async fn a_pre_0013_store_with_a_rule_send_and_decision() | ||
| 104 | +-> (PathBuf, String, String, String, String) { | ||
| 105 | + let dir = std::env::temp_dir().join(format!("izlek-test-{}", Uuid::new_v4())); | ||
| 106 | + std::fs::create_dir_all(&dir).unwrap(); | ||
| 107 | + let path = dir.join("izlek.db").to_string_lossy().into_owned(); | ||
| 108 | + | ||
| 109 | + let db = turso::Builder::new_local(&path).build().await.unwrap(); | ||
| 110 | + let conn = db.connect().unwrap(); | ||
| 111 | + conn.execute("PRAGMA foreign_keys = ON", ()).await.unwrap(); | ||
| 112 | + conn.execute( | ||
| 113 | + "CREATE TABLE schema_version (version INTEGER PRIMARY KEY, applied_at TEXT NOT NULL)", | ||
| 114 | + (), | ||
| 115 | + ) | ||
| 116 | + .await | ||
| 117 | + .unwrap(); | ||
| 118 | + | ||
| 119 | + let migrations_dir = | ||
| 120 | + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("migrations"); | ||
| 121 | + let mut files: Vec<_> = std::fs::read_dir(&migrations_dir) | ||
| 122 | + .unwrap() | ||
| 123 | + .map(|e| e.unwrap().path()) | ||
| 124 | + .filter(|p| { | ||
| 125 | + p.file_name() | ||
| 126 | + .and_then(|n| n.to_str()) | ||
| 127 | + .map(|n| n < "0013") | ||
| 128 | + .unwrap_or(false) | ||
| 129 | + }) | ||
| 130 | + .collect(); | ||
| 131 | + files.sort(); | ||
| 132 | + for (i, file) in files.iter().enumerate() { | ||
| 133 | + let sql = std::fs::read_to_string(file).unwrap(); | ||
| 134 | + conn.execute_batch(&sql).await.unwrap(); | ||
| 135 | + conn.execute( | ||
| 136 | + "INSERT INTO schema_version (version, applied_at) VALUES (?1, '2026-08-26T00:00:00Z')", | ||
| 137 | + turso::params![(i + 1) as i64], | ||
| 138 | + ) | ||
| 139 | + .await | ||
| 140 | + .unwrap(); | ||
| 141 | + } | ||
| 142 | + | ||
| 143 | + let workspace = Uuid::new_v4().to_string(); | ||
| 144 | + let admin = Uuid::new_v4().to_string(); | ||
| 145 | + let board = Uuid::new_v4().to_string(); | ||
| 146 | + let backlog = Uuid::new_v4().to_string(); | ||
| 147 | + let done = Uuid::new_v4().to_string(); | ||
| 148 | + let task = Uuid::new_v4().to_string(); | ||
| 149 | + let rule = Uuid::new_v4().to_string(); | ||
| 150 | + let transition = Uuid::new_v4().to_string(); | ||
| 151 | + conn.execute( | ||
| 152 | + "INSERT INTO workspace (id, name, created_at) VALUES (?1, 'Izlek', '2026-08-26T00:00:00Z')", | ||
| 153 | + turso::params![workspace.clone()], | ||
| 154 | + ) | ||
| 155 | + .await | ||
| 156 | + .unwrap(); | ||
| 157 | + conn.execute( | ||
| 158 | + "INSERT INTO user (id, workspace_id, email, display_name, role, password_hash, \ | ||
| 159 | + created_at) VALUES (?1, ?2, 'ada@izlek.sh', 'Ada', 'admin', 'x', '2026-08-26T00:00:00Z')", | ||
| 160 | + turso::params![admin.clone(), workspace.clone()], | ||
| 161 | + ) | ||
| 162 | + .await | ||
| 163 | + .unwrap(); | ||
| 164 | + conn.execute( | ||
| 165 | + "INSERT INTO board (id, workspace_id, name, created_at) VALUES (?1, ?2, 'Board', \ | ||
| 166 | + '2026-08-26T00:00:00Z')", | ||
| 167 | + turso::params![board.clone(), workspace.clone()], | ||
| 168 | + ) | ||
| 169 | + .await | ||
| 170 | + .unwrap(); | ||
| 171 | + conn.execute( | ||
| 172 | + "INSERT INTO board_column (id, board_id, name, position, is_done) VALUES (?1, ?2, \ | ||
| 173 | + 'Backlog', 0, 0)", | ||
| 174 | + turso::params![backlog.clone(), board.clone()], | ||
| 175 | + ) | ||
| 176 | + .await | ||
| 177 | + .unwrap(); | ||
| 178 | + conn.execute( | ||
| 179 | + "INSERT INTO board_column (id, board_id, name, position, is_done) VALUES (?1, ?2, \ | ||
| 180 | + 'Done', 1, 1)", | ||
| 181 | + turso::params![done.clone(), board.clone()], | ||
| 182 | + ) | ||
| 183 | + .await | ||
| 184 | + .unwrap(); | ||
| 185 | + conn.execute( | ||
| 186 | + "INSERT INTO task (id, board_id, task_key, title, column_id, created_by, created_at, \ | ||
| 187 | + updated_at) VALUES (?1, ?2, 'DZ-1', 'Ship it', ?3, ?4, '2026-08-26T00:00:00Z', '2026-08-26T00:00:00Z')", | ||
| 188 | + turso::params![task.clone(), board.clone(), done.clone(), admin.clone()], | ||
| 189 | + ) | ||
| 190 | + .await | ||
| 191 | + .unwrap(); | ||
| 192 | + conn.execute( | ||
| 193 | + "INSERT INTO transition (id, task_id, from_column, to_column, actor_id, created_at) \ | ||
| 194 | + VALUES (?1, ?2, ?3, ?4, ?5, '2026-08-26T00:00:00Z')", | ||
| 195 | + turso::params![ | ||
| 196 | + transition.clone(), | ||
| 197 | + task.clone(), | ||
| 198 | + backlog.clone(), | ||
| 199 | + done.clone(), | ||
| 200 | + admin.clone() | ||
| 201 | + ], | ||
| 202 | + ) | ||
| 203 | + .await | ||
| 204 | + .unwrap(); | ||
| 205 | + conn.execute( | ||
| 206 | + "INSERT INTO mail_rule (id, board_id, trigger_kind, trigger_column, subject, audience, \ | ||
| 207 | + enabled, created_at) VALUES (?1, ?2, 'status', ?3, 'Task completed', 'assignees', 1, \ | ||
| 208 | + '2026-08-26T00:00:00Z')", | ||
| 209 | + turso::params![rule.clone(), board.clone(), done.clone()], | ||
| 210 | + ) | ||
| 211 | + .await | ||
| 212 | + .unwrap(); | ||
| 213 | + conn.execute( | ||
| 214 | + "INSERT INTO mail_send (id, rule_id, event_id, task_id, recipient, state, attempts, \ | ||
| 215 | + claimed_at) VALUES ('s1', ?1, ?2, ?3, 'ada@izlek.sh', 'pending', 0, '2026-08-26T00:00:00Z')", | ||
| 216 | + turso::params![rule.clone(), transition.clone(), task.clone()], | ||
| 217 | + ) | ||
| 218 | + .await | ||
| 219 | + .unwrap(); | ||
| 220 | + conn.execute( | ||
| 221 | + "INSERT INTO mail_decision (id, rule_id, event_id, task_id, outcome, detail, \ | ||
| 222 | + created_at) VALUES ('d1', ?1, ?2, ?3, 'owed', '', '2026-08-26T00:00:00Z')", | ||
| 223 | + turso::params![rule.clone(), transition.clone(), task.clone()], | ||
| 224 | + ) | ||
| 225 | + .await | ||
| 226 | + .unwrap(); | ||
| 227 | + drop(conn); | ||
| 228 | + | ||
| 229 | + (PathBuf::from(path), workspace, board, rule, task) | ||
| 230 | +} | ||
| 231 | + | ||
| 97 | #[tokio::test] | 232 | #[tokio::test] |
| 98 | async fn migrations_apply_once_and_survive_reopen() { | 233 | async fn migrations_apply_once_and_survive_reopen() { |
| 99 | let dir = std::env::temp_dir().join(format!("izlek-test-{}", Uuid::new_v4())); | 234 | let dir = std::env::temp_dir().join(format!("izlek-test-{}", Uuid::new_v4())); |
| @@ -101,19 +236,54 @@ async fn migrations_apply_once_and_survive_reopen() { | |||
| 101 | let path = dir.join("izlek.db").to_string_lossy().into_owned(); | 236 | let path = dir.join("izlek.db").to_string_lossy().into_owned(); |
| 102 | 237 | ||
| 103 | let first = TursoStore::open(&path).await.unwrap(); | 238 | let first = TursoStore::open(&path).await.unwrap(); |
| 104 | - assert_eq!(first.schema_version().await.unwrap(), 12); | 239 | + assert_eq!(first.schema_version().await.unwrap(), 13); |
| 105 | claim(&first).await; | 240 | claim(&first).await; |
| 106 | drop(first); | 241 | drop(first); |
| 107 | 242 | ||
| 108 | // Re-opening must not re-run 0001 (which would fail on CREATE TABLE) and | 243 | // Re-opening must not re-run 0001 (which would fail on CREATE TABLE) and |
| 109 | // must not lose what the first open wrote. | 244 | // must not lose what the first open wrote. |
| 110 | let second = TursoStore::open(&path).await.unwrap(); | 245 | let second = TursoStore::open(&path).await.unwrap(); |
| 111 | - assert_eq!(second.schema_version().await.unwrap(), 12); | 246 | + assert_eq!(second.schema_version().await.unwrap(), 13); |
| 112 | assert_eq!(second.workspace().await.unwrap().unwrap().name, "Izlek"); | 247 | assert_eq!(second.workspace().await.unwrap().unwrap().name, "Izlek"); |
| 113 | drop(second); | 248 | drop(second); |
| 114 | let _ = std::fs::remove_dir_all(&dir); | 249 | let _ = std::fs::remove_dir_all(&dir); |
| 115 | } | 250 | } |
| 116 | 251 | ||
| 252 | +#[tokio::test] | ||
| 253 | +async fn migration_0013_rebuilds_mail_rule_without_losing_its_ledger() { | ||
| 254 | + // `mail_send.rule_id` and `mail_decision.rule_id` both carry ON DELETE | ||
| 255 | + // CASCADE at `mail_rule`, and every connection this store opens runs with | ||
| 256 | + // foreign keys on. Rebuilding `mail_rule` — dropping the old table while | ||
| 257 | + // rows still point at it — would, without a guard, cascade the drop | ||
| 258 | + // straight through this rule's own send and its own decision. | ||
| 259 | + let (path, workspace, board, rule, task) = | ||
| 260 | + a_pre_0013_store_with_a_rule_send_and_decision().await; | ||
| 261 | + | ||
| 262 | + let store = TursoStore::open(path.to_str().unwrap()).await.unwrap(); | ||
| 263 | + assert_eq!(store.schema_version().await.unwrap(), 13); | ||
| 264 | + assert_eq!(store.board(&workspace).await.unwrap().unwrap().id, board); | ||
| 265 | + | ||
| 266 | + let rules = store.mail_rules(&board).await.unwrap(); | ||
| 267 | + assert_eq!(rules.len(), 1, "the rule survived the rebuild"); | ||
| 268 | + assert_eq!(rules[0].id, rule); | ||
| 269 | + assert_eq!(rules[0].audience, Audience::Assignees); | ||
| 270 | + | ||
| 271 | + let sends = store.sends_for_rule(&rule, 10).await.unwrap(); | ||
| 272 | + assert_eq!(sends.len(), 1, "the send survived, joined to its rule"); | ||
| 273 | + assert_eq!(sends[0].task_id, Some(task.clone())); | ||
| 274 | + | ||
| 275 | + let decisions = store.recent_mail_decisions(10).await.unwrap(); | ||
| 276 | + assert_eq!( | ||
| 277 | + decisions.len(), | ||
| 278 | + 1, | ||
| 279 | + "the decision survived, joined to its rule" | ||
| 280 | + ); | ||
| 281 | + assert_eq!(decisions[0].rule_id, rule); | ||
| 282 | + | ||
| 283 | + drop(store); | ||
| 284 | + let _ = std::fs::remove_dir_all(path.parent().unwrap()); | ||
| 285 | +} | ||
| 286 | + | ||
| 117 | #[tokio::test] | 287 | #[tokio::test] |
| 118 | async fn the_first_account_owns_the_workspace() { | 288 | async fn the_first_account_owns_the_workspace() { |
| 119 | let scratch = Scratch::open().await; | 289 | let scratch = Scratch::open().await; |
| @@ -2692,7 +2862,7 @@ async fn an_orphan_row_is_refused() { | |||
| 2692 | .execute( | 2862 | .execute( |
| 2693 | "INSERT INTO user (id, workspace_id, email, display_name, role, created_at) \ | 2863 | "INSERT INTO user (id, workspace_id, email, display_name, role, created_at) \ |
| 2694 | VALUES ('u-orphan', 'no-such-workspace', 'orphan@izlek.sh', 'Orphan', \ | 2864 | VALUES ('u-orphan', 'no-such-workspace', 'orphan@izlek.sh', 'Orphan', \ |
| 2695 | - 'member', '2026-08-26T00:00:00Z')", | 2865 | + 'member', '2026-08-26')", |
| 2696 | (), | 2866 | (), |
| 2697 | ) | 2867 | ) |
| 2698 | .await; | 2868 | .await; |