One schema file again, and the check it should have had
Browse files at this commitabdae7a9ec137f7c0b81cb41bb51cac519344e4eParent: 096e7e6- refactor
- one schema file again, and the check it should have had
4 changed files +65 -60
- crates/izlek-core/migrations/0001_init.sql +27 -29
- crates/izlek-core/migrations/0002_public_url.sql +0 -6
- crates/izlek-core/src/store/turso_store.rs +13 -16
- crates/izlek-core/tests/store.rs +25 -9
crates/izlek-core/migrations/0001_init.sqlmodified+27 -29
| @@ -6,12 +6,13 @@ | |||
| 6 | -- creation, and Crockford-uppercase already, which is what lets a task key | 6 | -- creation, and Crockford-uppercase already, which is what lets a task key |
| 7 | -- borrow its tail. | 7 | -- borrow its tail. |
| 8 | -- | 8 | -- |
| 9 | --- This file replaces a chain of twenty migrations. Three of them (the old | 9 | +-- This file is the whole schema, not the first link of a chain. Nothing has |
| 10 | --- 0013, 0014, 0019) did not tweak a table but rebuilt it — SQLite cannot drop | 10 | +-- been released, so a change to a table is made here, in the table, and the |
| 11 | --- a CHECK or a foreign key in place — so the shape a reader found here no | 11 | +-- database it describes is recreated — twenty migrations were collapsed into |
| 12 | --- longer matched the shape a running database had. Nothing has been released, | 12 | +-- this file once for that reason, and a twenty-first would start the same rot |
| 13 | --- so the chain was collapsed into this: the tables as they actually are. The | 13 | +-- over. The reasoning the old migrations carried is kept, on the tables it |
| 14 | --- reasoning those migrations carried is kept, on the tables it explains. | 14 | +-- explains. The day Izlek runs somewhere with data worth keeping, the next |
| 15 | +-- change becomes 0002 and this note goes away. | ||
| 15 | 16 | ||
| 16 | CREATE TABLE workspace ( | 17 | CREATE TABLE workspace ( |
| 17 | id TEXT PRIMARY KEY, | 18 | id TEXT PRIMARY KEY, |
| @@ -47,9 +48,15 @@ CREATE TABLE workspace ( | |||
| 47 | -- back tomorrow wondering whether it ever worked. `smtp_test_error` is the | 48 | -- back tomorrow wondering whether it ever worked. `smtp_test_error` is the |
| 48 | -- mail server's own words, so the admin can act on them; the mailer builds | 49 | -- mail server's own words, so the admin can act on them; the mailer builds |
| 49 | -- its errors from what the server said, never from the credentials it sent. | 50 | -- its errors from what the server said, never from the credentials it sent. |
| 50 | - smtp_test_at INTEGER, | 51 | + smtp_test_at TEXT, |
| 51 | smtp_test_ms INTEGER, | 52 | smtp_test_ms INTEGER, |
| 52 | - smtp_test_error TEXT | 53 | + smtp_test_error TEXT, |
| 54 | + -- The address mail links point at, when the one the process was | ||
| 55 | + -- configured with is not the one people reach. A box behind a proxy | ||
| 56 | + -- answers on localhost and is known by a public name, and only an admin | ||
| 57 | + -- knows which — so it is workspace content, and `config/izlek.toml`'s | ||
| 58 | + -- `base_url` is what an empty one falls back to. | ||
| 59 | + public_url TEXT | ||
| 53 | ); | 60 | ); |
| 54 | 61 | ||
| 55 | CREATE TABLE user ( | 62 | CREATE TABLE user ( |
| @@ -203,6 +210,10 @@ CREATE TABLE task_dependency ( | |||
| 203 | PRIMARY KEY (blocked_task_id, blocking_task_id), | 210 | PRIMARY KEY (blocked_task_id, blocking_task_id), |
| 204 | CHECK (blocked_task_id <> blocking_task_id) | 211 | CHECK (blocked_task_id <> blocking_task_id) |
| 205 | ); | 212 | ); |
| 213 | +-- The primary key answers "what is this task waiting on"; the other | ||
| 214 | +-- direction — "who is waiting on this one", which every finish and every | ||
| 215 | +-- delete asks — has no prefix of it to use. | ||
| 216 | +CREATE INDEX task_dependency_by_blocking ON task_dependency(blocking_task_id); | ||
| 206 | 217 | ||
| 207 | CREATE TABLE comment ( | 218 | CREATE TABLE comment ( |
| 208 | id TEXT PRIMARY KEY, | 219 | id TEXT PRIMARY KEY, |
| @@ -309,9 +320,10 @@ CREATE TABLE mail_rule ( | |||
| 309 | -- being all the recipient reads. Off by default. | 320 | -- being all the recipient reads. Off by default. |
| 310 | include_task_details INTEGER NOT NULL DEFAULT 0, | 321 | include_task_details INTEGER NOT NULL DEFAULT 0, |
| 311 | created_at TEXT NOT NULL, | 322 | created_at TEXT NOT NULL, |
| 312 | - -- A status rule names a column and an unblocked rule does not. The check | 323 | + -- Only a status rule names a column, and a status rule that names none |
| 313 | - -- is here so a half-written rule cannot be stored at all. | 324 | + -- watches every column. The check is here so a rule that names a column |
| 314 | - CHECK ((trigger_kind = 'status') = (trigger_column IS NOT NULL)) | 325 | + -- it could not act on cannot be stored at all. |
| 326 | + CHECK (trigger_column IS NULL OR trigger_kind = 'status') | ||
| 315 | ); | 327 | ); |
| 316 | CREATE INDEX mail_rule_by_board ON mail_rule(board_id); | 328 | CREATE INDEX mail_rule_by_board ON mail_rule(board_id); |
| 317 | 329 | ||
| @@ -348,6 +360,9 @@ CREATE TABLE mail_send ( | |||
| 348 | CREATE UNIQUE INDEX mail_send_once ON mail_send(rule_id, event_id, task_id, recipient); | 360 | CREATE UNIQUE INDEX mail_send_once ON mail_send(rule_id, event_id, task_id, recipient); |
| 349 | CREATE INDEX mail_send_owed ON mail_send(next_attempt_at); | 361 | CREATE INDEX mail_send_owed ON mail_send(next_attempt_at); |
| 350 | CREATE INDEX mail_send_by_rule ON mail_send(rule_id, sent_at); | 362 | CREATE INDEX mail_send_by_rule ON mail_send(rule_id, sent_at); |
| 363 | +-- The task's own mail strip reads by task, which neither index above starts | ||
| 364 | +-- with: both lead on `rule_id`, and an invite has none. | ||
| 365 | +CREATE INDEX mail_send_by_task ON mail_send(task_id); | ||
| 351 | 366 | ||
| 352 | -- What the mail engine decided, for every rule and every event, not just the | 367 | -- What the mail engine decided, for every rule and every event, not just the |
| 353 | -- mails it sent. | 368 | -- mails it sent. |
| @@ -373,21 +388,4 @@ CREATE TABLE mail_decision ( | |||
| 373 | ); | 388 | ); |
| 374 | CREATE UNIQUE INDEX mail_decision_once ON mail_decision(rule_id, event_id, task_id); | 389 | CREATE UNIQUE INDEX mail_decision_once ON mail_decision(rule_id, event_id, task_id); |
| 375 | CREATE INDEX mail_decision_recent ON mail_decision(created_at); | 390 | CREATE INDEX mail_decision_recent ON mail_decision(created_at); |
| 376 | - | 391 | +CREATE INDEX mail_decision_by_task ON mail_decision(task_id); |
| 377 | --- Read-only links: izlek.sh/v/<id>. Only the hash of the token is stored, so a | ||
| 378 | --- database copy does not hand out working links; revoking is server-side and | ||
| 379 | --- immediate. Designed but not yet built — no store method writes this table. | ||
| 380 | -CREATE TABLE view_link ( | ||
| 381 | - id TEXT PRIMARY KEY, | ||
| 382 | - workspace_id TEXT NOT NULL REFERENCES workspace(id), | ||
| 383 | - board_id TEXT NOT NULL REFERENCES board(id), | ||
| 384 | - token_hash TEXT NOT NULL, | ||
| 385 | - -- 'whole_board' | 'deadlines' | ||
| 386 | - scope TEXT NOT NULL, | ||
| 387 | - created_by TEXT NOT NULL REFERENCES user(id), | ||
| 388 | - created_at TEXT NOT NULL, | ||
| 389 | - expires_at TEXT, | ||
| 390 | - revoked_at TEXT, | ||
| 391 | - open_count INTEGER NOT NULL DEFAULT 0 | ||
| 392 | -); | ||
| 393 | -CREATE UNIQUE INDEX view_link_token ON view_link(token_hash); | ||
crates/izlek-core/migrations/0002_public_url.sqldeleted+0 -6
| @@ -1,6 +0,0 @@ | |||
| 1 | --- The address mail points at, when it is not the one the process was | ||
| 2 | --- configured with. A box behind a proxy answers on localhost and is reached | ||
| 3 | --- on a public name, and only an admin knows which — so this is workspace | ||
| 4 | --- content, edited in Settings, and `config/izlek.toml`'s `base_url` is what | ||
| 5 | --- it falls back to. | ||
| 6 | -ALTER TABLE workspace ADD COLUMN public_url TEXT; | ||
crates/izlek-core/src/store/turso_store.rsmodified+13 -16
| @@ -30,10 +30,11 @@ use time::Date; | |||
| 30 | use time::format_description::BorrowedFormatItem; | 30 | use time::format_description::BorrowedFormatItem; |
| 31 | use time::macros::format_description; | 31 | use time::macros::format_description; |
| 32 | 32 | ||
| 33 | -/// Every migration, in order. Adding one means appending a file and a line. | 33 | +/// Every migration, in order. There is one: nothing has been released, so a |
| 34 | +/// schema change is made in `0001_init.sql` and the database recreated. A | ||
| 35 | +/// second file is for the day there is data worth keeping. | ||
| 34 | const MIGRATIONS: &[(i64, &str)] = &[ | 36 | const MIGRATIONS: &[(i64, &str)] = &[ |
| 35 | (1, include_str!("../../migrations/0001_init.sql")), | 37 | (1, include_str!("../../migrations/0001_init.sql")), |
| 36 | - (2, include_str!("../../migrations/0002_public_url.sql")), | ||
| 37 | ]; | 38 | ]; |
| 38 | 39 | ||
| 39 | /// The board a fresh workspace gets, and its columns. `Done` is the column | 40 | /// The board a fresh workspace gets, and its columns. `Done` is the column |
| @@ -158,12 +159,12 @@ impl TursoStore { | |||
| 158 | 159 | ||
| 159 | /// One migration and the row that says it ran, in one transaction. | 160 | /// One migration and the row that says it ran, in one transaction. |
| 160 | /// | 161 | /// |
| 161 | - /// A migration is not only a thing that can fail — 0005 rebuilds | 162 | + /// A migration is not only a thing that can fail: one that rebuilds a |
| 162 | - /// `mail_send` to drop a foreign key, and a crash between the DROP and the | 163 | + /// table — which is how SQLite drops a CHECK or a foreign key — would |
| 163 | - /// RENAME would leave the mail ledger gone rather than merely unmigrated. | 164 | + /// leave the table gone rather than merely unmigrated if it died between |
| 164 | - /// So the file and its `schema_version` row commit together or not at all, | 165 | + /// the DROP and the RENAME. So the file and its `schema_version` row |
| 165 | - /// and a half-applied migration is a boot that starts over rather than a | 166 | + /// commit together or not at all, and a half-applied migration is a boot |
| 166 | - /// database with a hole in it. | 167 | + /// that starts over rather than a database with a hole in it. |
| 167 | /// | 168 | /// |
| 168 | /// SQLite's DDL is transactional, which is what makes this possible at all. | 169 | /// SQLite's DDL is transactional, which is what makes this possible at all. |
| 169 | async fn apply(&self, version: i64, sql: &str) -> Result<()> { | 170 | async fn apply(&self, version: i64, sql: &str) -> Result<()> { |
| @@ -250,12 +251,9 @@ const SEND_COLUMNS: &str = "id, rule_id, event_id, task_id, recipient, state, at | |||
| 250 | 251 | ||
| 251 | fn trigger_parts(trigger: &Trigger) -> (&'static str, Option<String>) { | 252 | fn trigger_parts(trigger: &Trigger) -> (&'static str, Option<String>) { |
| 252 | match trigger { | 253 | match trigger { |
| 253 | - // The every-column rule is its own stored kind rather than a status | 254 | + // A status rule with no column is the every-column rule; the schema |
| 254 | - // row with no column: the schema's check ties `status` to a column | 255 | + // allows the null, and nothing has to invent a second kind to say so. |
| 255 | - // it names, and a kind of its own keeps that check honest without | 256 | + Trigger::StatusBecomes(column) => ("status", column.clone()), |
| 256 | - // rebuilding the table under a live ledger. | ||
| 257 | - Trigger::StatusBecomes(Some(column)) => ("status", Some(column.clone())), | ||
| 258 | - Trigger::StatusBecomes(None) => ("status_any", None), | ||
| 259 | Trigger::Unblocked => ("unblocked", None), | 257 | Trigger::Unblocked => ("unblocked", None), |
| 260 | Trigger::Created => ("created", None), | 258 | Trigger::Created => ("created", None), |
| 261 | Trigger::Assigned => ("assigned", None), | 259 | Trigger::Assigned => ("assigned", None), |
| @@ -285,8 +283,7 @@ fn rule_from(row: &Row) -> Result<MailRule> { | |||
| 285 | let kind = text(row, 2)?; | 283 | let kind = text(row, 2)?; |
| 286 | let column = opt_text(row, 3)?; | 284 | let column = opt_text(row, 3)?; |
| 287 | let trigger = match (kind.as_str(), column) { | 285 | let trigger = match (kind.as_str(), column) { |
| 288 | - ("status", Some(column)) => Trigger::StatusBecomes(Some(column)), | 286 | + ("status", column) => Trigger::StatusBecomes(column), |
| 289 | - ("status_any", None) => Trigger::StatusBecomes(None), | ||
| 290 | ("unblocked", None) => Trigger::Unblocked, | 287 | ("unblocked", None) => Trigger::Unblocked, |
| 291 | ("created", None) => Trigger::Created, | 288 | ("created", None) => Trigger::Created, |
| 292 | ("assigned", None) => Trigger::Assigned, | 289 | ("assigned", None) => Trigger::Assigned, |
crates/izlek-core/tests/store.rsmodified+25 -9
| @@ -101,14 +101,14 @@ async fn migrations_apply_once_and_survive_reopen() { | |||
| 101 | let path = dir.join("izlek.db").to_string_lossy().into_owned(); | 101 | let path = dir.join("izlek.db").to_string_lossy().into_owned(); |
| 102 | 102 | ||
| 103 | let first = TursoStore::open(&path).await.unwrap(); | 103 | let first = TursoStore::open(&path).await.unwrap(); |
| 104 | - assert_eq!(first.schema_version().await.unwrap(), 2); | 104 | + assert_eq!(first.schema_version().await.unwrap(), 1); |
| 105 | claim(&first).await; | 105 | claim(&first).await; |
| 106 | drop(first); | 106 | drop(first); |
| 107 | 107 | ||
| 108 | // Re-opening must not re-run 0001 (which would fail on CREATE TABLE) and | 108 | // Re-opening must not re-run 0001 (which would fail on CREATE TABLE) and |
| 109 | // must not lose what the first open wrote. | 109 | // must not lose what the first open wrote. |
| 110 | let second = TursoStore::open(&path).await.unwrap(); | 110 | let second = TursoStore::open(&path).await.unwrap(); |
| 111 | - assert_eq!(second.schema_version().await.unwrap(), 2); | 111 | + assert_eq!(second.schema_version().await.unwrap(), 1); |
| 112 | assert_eq!(second.workspace().await.unwrap().unwrap().name, "Izlek"); | 112 | assert_eq!(second.workspace().await.unwrap().unwrap().name, "Izlek"); |
| 113 | drop(second); | 113 | drop(second); |
| 114 | let _ = std::fs::remove_dir_all(&dir); | 114 | let _ = std::fs::remove_dir_all(&dir); |
| @@ -3481,11 +3481,11 @@ async fn a_rule(store: &TursoStore, workspace: &str, column: &str, subject: &str | |||
| 3481 | } | 3481 | } |
| 3482 | 3482 | ||
| 3483 | #[tokio::test] | 3483 | #[tokio::test] |
| 3484 | -async fn a_rule_that_fires_on_nothing_is_refused_by_the_schema() { | 3484 | +async fn a_rule_naming_a_column_it_cannot_act_on_is_refused_by_the_schema() { |
| 3485 | - // The store's own API cannot build a half-written rule, so this reaches | 3485 | + // The store's own API cannot build a rule like these, so this reaches |
| 3486 | - // past it: a status rule with no column, straight at the table. The check | 3486 | + // past it, straight at the table. The check constraint is the guard: a |
| 3487 | - // constraint is the guard, and if it were ever dropped this insert would | 3487 | + // column on a trigger that never reads one is a rule whose author meant |
| 3488 | - // quietly succeed and the engine would carry a rule that matches nothing. | 3488 | + // something the engine will not do. |
| 3489 | let dir = std::env::temp_dir().join(format!("izlek-test-{}", Ulid::new())); | 3489 | let dir = std::env::temp_dir().join(format!("izlek-test-{}", Ulid::new())); |
| 3490 | std::fs::create_dir_all(&dir).unwrap(); | 3490 | std::fs::create_dir_all(&dir).unwrap(); |
| 3491 | let path = dir.join("izlek.db").to_string_lossy().into_owned(); | 3491 | let path = dir.join("izlek.db").to_string_lossy().into_owned(); |
| @@ -3504,11 +3504,27 @@ async fn a_rule_that_fires_on_nothing_is_refused_by_the_schema() { | |||
| 3504 | .execute( | 3504 | .execute( |
| 3505 | "INSERT INTO mail_rule \ | 3505 | "INSERT INTO mail_rule \ |
| 3506 | (id, board_id, trigger_kind, trigger_column, subject, audience, enabled, created_at) \ | 3506 | (id, board_id, trigger_kind, trigger_column, subject, audience, enabled, created_at) \ |
| 3507 | - VALUES ('r1', ?1, 'status', NULL, 'Task completed', 'assignees', 1, '2026-08-26')", | 3507 | + SELECT 'r1', ?1, 'created', id, 'Task completed', 'assignees', 1, '2026-08-26' \ |
| 3508 | + FROM board_column LIMIT 1", | ||
| 3508 | turso::params![board.id.clone()], | 3509 | turso::params![board.id.clone()], |
| 3509 | ) | 3510 | ) |
| 3510 | .await; | 3511 | .await; |
| 3511 | - assert!(refused.is_err(), "a status rule with no column was stored"); | 3512 | + assert!( |
| 3513 | + refused.is_err(), | ||
| 3514 | + "a rule that reads no column was stored carrying one" | ||
| 3515 | + ); | ||
| 3516 | + | ||
| 3517 | + // A status rule with no column is not half-written: it is the rule that | ||
| 3518 | + // watches every column, and the schema has to let it through. | ||
| 3519 | + let every_column = conn | ||
| 3520 | + .execute( | ||
| 3521 | + "INSERT INTO mail_rule \ | ||
| 3522 | + (id, board_id, trigger_kind, trigger_column, subject, audience, enabled, created_at) \ | ||
| 3523 | + VALUES ('r3', ?1, 'status', NULL, 'It moved', 'assignees', 1, '2026-08-26')", | ||
| 3524 | + turso::params![board.id.clone()], | ||
| 3525 | + ) | ||
| 3526 | + .await; | ||
| 3527 | + assert!(every_column.is_ok(), "the every-column rule was refused"); | ||
| 3512 | 3528 | ||
| 3513 | let missing_column = conn | 3529 | let missing_column = conn |
| 3514 | .execute( | 3530 | .execute( |