From abdae7a9ec137f7c0b81cb41bb51cac519344e4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 30 Aug 2026 04:46:52 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A5=20refactor(store):=20one=20schema?= =?UTF-8?q?=20file=20again,=20and=20the=20check=20it=20should=20have=20had?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0002 added `public_url` in a second file because a live database was in the way. Nothing is released, so the column moves into `0001_init.sql` where the rest of the workspace is and the second file goes. The mail rule check becomes what it should have been: only a status rule may name a column, and a status rule that names none watches every column. That was the constraint the every-column rule could not satisfy, so it was stored under an invented `status_any` kind to keep the old check honest — the kind is gone and a null column says it directly. Also: `smtp_test_at` is declared TEXT, which is what has always been written into it; `view_link` is dropped, a table no store method has ever read or written; and three indexes are added for lookups that had none — who is waiting on this task, this task's mail, this task's decisions. The database this describes must be recreated: `rm izlek.db izlek.db-wal`. --- crates/izlek-core/migrations/0001_init.sql | 56 +++++++++---------- .../izlek-core/migrations/0002_public_url.sql | 6 -- crates/izlek-core/src/store/turso_store.rs | 29 +++++----- crates/izlek-core/tests/store.rs | 34 ++++++++--- 4 files changed, 65 insertions(+), 60 deletions(-) delete mode 100644 crates/izlek-core/migrations/0002_public_url.sql diff --git a/crates/izlek-core/migrations/0001_init.sql b/crates/izlek-core/migrations/0001_init.sql index 3133615..302abac 100644 --- a/crates/izlek-core/migrations/0001_init.sql +++ b/crates/izlek-core/migrations/0001_init.sql @@ -6,12 +6,13 @@ -- creation, and Crockford-uppercase already, which is what lets a task key -- borrow its tail. -- --- This file replaces a chain of twenty migrations. Three of them (the old --- 0013, 0014, 0019) did not tweak a table but rebuilt it — SQLite cannot drop --- a CHECK or a foreign key in place — so the shape a reader found here no --- longer matched the shape a running database had. Nothing has been released, --- so the chain was collapsed into this: the tables as they actually are. The --- reasoning those migrations carried is kept, on the tables it explains. +-- This file is the whole schema, not the first link of a chain. Nothing has +-- been released, so a change to a table is made here, in the table, and the +-- database it describes is recreated — twenty migrations were collapsed into +-- this file once for that reason, and a twenty-first would start the same rot +-- over. The reasoning the old migrations carried is kept, on the tables it +-- explains. The day Izlek runs somewhere with data worth keeping, the next +-- change becomes 0002 and this note goes away. CREATE TABLE workspace ( id TEXT PRIMARY KEY, @@ -47,9 +48,15 @@ CREATE TABLE workspace ( -- back tomorrow wondering whether it ever worked. `smtp_test_error` is the -- mail server's own words, so the admin can act on them; the mailer builds -- its errors from what the server said, never from the credentials it sent. - smtp_test_at INTEGER, + smtp_test_at TEXT, smtp_test_ms INTEGER, - smtp_test_error TEXT + smtp_test_error TEXT, + -- The address mail links point at, when the one the process was + -- configured with is not the one people reach. A box behind a proxy + -- answers on localhost and is known by a public name, and only an admin + -- knows which — so it is workspace content, and `config/izlek.toml`'s + -- `base_url` is what an empty one falls back to. + public_url TEXT ); CREATE TABLE user ( @@ -203,6 +210,10 @@ CREATE TABLE task_dependency ( PRIMARY KEY (blocked_task_id, blocking_task_id), CHECK (blocked_task_id <> blocking_task_id) ); +-- The primary key answers "what is this task waiting on"; the other +-- direction — "who is waiting on this one", which every finish and every +-- delete asks — has no prefix of it to use. +CREATE INDEX task_dependency_by_blocking ON task_dependency(blocking_task_id); CREATE TABLE comment ( id TEXT PRIMARY KEY, @@ -309,9 +320,10 @@ CREATE TABLE mail_rule ( -- being all the recipient reads. Off by default. include_task_details INTEGER NOT NULL DEFAULT 0, created_at TEXT NOT NULL, - -- A status rule names a column and an unblocked rule does not. The check - -- is here so a half-written rule cannot be stored at all. - CHECK ((trigger_kind = 'status') = (trigger_column IS NOT NULL)) + -- Only a status rule names a column, and a status rule that names none + -- watches every column. The check is here so a rule that names a column + -- it could not act on cannot be stored at all. + CHECK (trigger_column IS NULL OR trigger_kind = 'status') ); CREATE INDEX mail_rule_by_board ON mail_rule(board_id); @@ -348,6 +360,9 @@ CREATE TABLE mail_send ( CREATE UNIQUE INDEX mail_send_once ON mail_send(rule_id, event_id, task_id, recipient); CREATE INDEX mail_send_owed ON mail_send(next_attempt_at); CREATE INDEX mail_send_by_rule ON mail_send(rule_id, sent_at); +-- The task's own mail strip reads by task, which neither index above starts +-- with: both lead on `rule_id`, and an invite has none. +CREATE INDEX mail_send_by_task ON mail_send(task_id); -- What the mail engine decided, for every rule and every event, not just the -- mails it sent. @@ -373,21 +388,4 @@ CREATE TABLE mail_decision ( ); CREATE UNIQUE INDEX mail_decision_once ON mail_decision(rule_id, event_id, task_id); CREATE INDEX mail_decision_recent ON mail_decision(created_at); - --- Read-only links: izlek.sh/v/. Only the hash of the token is stored, so a --- database copy does not hand out working links; revoking is server-side and --- immediate. Designed but not yet built — no store method writes this table. -CREATE TABLE view_link ( - id TEXT PRIMARY KEY, - workspace_id TEXT NOT NULL REFERENCES workspace(id), - board_id TEXT NOT NULL REFERENCES board(id), - token_hash TEXT NOT NULL, - -- 'whole_board' | 'deadlines' - scope TEXT NOT NULL, - created_by TEXT NOT NULL REFERENCES user(id), - created_at TEXT NOT NULL, - expires_at TEXT, - revoked_at TEXT, - open_count INTEGER NOT NULL DEFAULT 0 -); -CREATE UNIQUE INDEX view_link_token ON view_link(token_hash); +CREATE INDEX mail_decision_by_task ON mail_decision(task_id); diff --git a/crates/izlek-core/migrations/0002_public_url.sql b/crates/izlek-core/migrations/0002_public_url.sql deleted file mode 100644 index c8a9398..0000000 --- a/crates/izlek-core/migrations/0002_public_url.sql +++ /dev/null @@ -1,6 +0,0 @@ --- The address mail points at, when it is not the one the process was --- configured with. A box behind a proxy answers on localhost and is reached --- on a public name, and only an admin knows which — so this is workspace --- content, edited in Settings, and `config/izlek.toml`'s `base_url` is what --- it falls back to. -ALTER TABLE workspace ADD COLUMN public_url TEXT; diff --git a/crates/izlek-core/src/store/turso_store.rs b/crates/izlek-core/src/store/turso_store.rs index bf266fa..9fc8da5 100644 --- a/crates/izlek-core/src/store/turso_store.rs +++ b/crates/izlek-core/src/store/turso_store.rs @@ -30,10 +30,11 @@ use time::Date; use time::format_description::BorrowedFormatItem; use time::macros::format_description; -/// Every migration, in order. Adding one means appending a file and a line. +/// Every migration, in order. There is one: nothing has been released, so a +/// schema change is made in `0001_init.sql` and the database recreated. A +/// second file is for the day there is data worth keeping. const MIGRATIONS: &[(i64, &str)] = &[ (1, include_str!("../../migrations/0001_init.sql")), - (2, include_str!("../../migrations/0002_public_url.sql")), ]; /// The board a fresh workspace gets, and its columns. `Done` is the column @@ -158,12 +159,12 @@ impl TursoStore { /// One migration and the row that says it ran, in one transaction. /// - /// A migration is not only a thing that can fail — 0005 rebuilds - /// `mail_send` to drop a foreign key, and a crash between the DROP and the - /// RENAME would leave the mail ledger gone rather than merely unmigrated. - /// So the file and its `schema_version` row commit together or not at all, - /// and a half-applied migration is a boot that starts over rather than a - /// database with a hole in it. + /// A migration is not only a thing that can fail: one that rebuilds a + /// table — which is how SQLite drops a CHECK or a foreign key — would + /// leave the table gone rather than merely unmigrated if it died between + /// the DROP and the RENAME. So the file and its `schema_version` row + /// commit together or not at all, and a half-applied migration is a boot + /// that starts over rather than a database with a hole in it. /// /// SQLite's DDL is transactional, which is what makes this possible at all. 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 fn trigger_parts(trigger: &Trigger) -> (&'static str, Option) { match trigger { - // The every-column rule is its own stored kind rather than a status - // row with no column: the schema's check ties `status` to a column - // it names, and a kind of its own keeps that check honest without - // rebuilding the table under a live ledger. - Trigger::StatusBecomes(Some(column)) => ("status", Some(column.clone())), - Trigger::StatusBecomes(None) => ("status_any", None), + // A status rule with no column is the every-column rule; the schema + // allows the null, and nothing has to invent a second kind to say so. + Trigger::StatusBecomes(column) => ("status", column.clone()), Trigger::Unblocked => ("unblocked", None), Trigger::Created => ("created", None), Trigger::Assigned => ("assigned", None), @@ -285,8 +283,7 @@ fn rule_from(row: &Row) -> Result { let kind = text(row, 2)?; let column = opt_text(row, 3)?; let trigger = match (kind.as_str(), column) { - ("status", Some(column)) => Trigger::StatusBecomes(Some(column)), - ("status_any", None) => Trigger::StatusBecomes(None), + ("status", column) => Trigger::StatusBecomes(column), ("unblocked", None) => Trigger::Unblocked, ("created", None) => Trigger::Created, ("assigned", None) => Trigger::Assigned, diff --git a/crates/izlek-core/tests/store.rs b/crates/izlek-core/tests/store.rs index 60590c2..d57c1dc 100644 --- a/crates/izlek-core/tests/store.rs +++ b/crates/izlek-core/tests/store.rs @@ -101,14 +101,14 @@ async fn migrations_apply_once_and_survive_reopen() { let path = dir.join("izlek.db").to_string_lossy().into_owned(); let first = TursoStore::open(&path).await.unwrap(); - assert_eq!(first.schema_version().await.unwrap(), 2); + assert_eq!(first.schema_version().await.unwrap(), 1); claim(&first).await; drop(first); // Re-opening must not re-run 0001 (which would fail on CREATE TABLE) and // must not lose what the first open wrote. let second = TursoStore::open(&path).await.unwrap(); - assert_eq!(second.schema_version().await.unwrap(), 2); + assert_eq!(second.schema_version().await.unwrap(), 1); assert_eq!(second.workspace().await.unwrap().unwrap().name, "Izlek"); drop(second); let _ = std::fs::remove_dir_all(&dir); @@ -3481,11 +3481,11 @@ async fn a_rule(store: &TursoStore, workspace: &str, column: &str, subject: &str } #[tokio::test] -async fn a_rule_that_fires_on_nothing_is_refused_by_the_schema() { - // The store's own API cannot build a half-written rule, so this reaches - // past it: a status rule with no column, straight at the table. The check - // constraint is the guard, and if it were ever dropped this insert would - // quietly succeed and the engine would carry a rule that matches nothing. +async fn a_rule_naming_a_column_it_cannot_act_on_is_refused_by_the_schema() { + // The store's own API cannot build a rule like these, so this reaches + // past it, straight at the table. The check constraint is the guard: a + // column on a trigger that never reads one is a rule whose author meant + // something the engine will not do. let dir = std::env::temp_dir().join(format!("izlek-test-{}", Ulid::new())); std::fs::create_dir_all(&dir).unwrap(); 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() { .execute( "INSERT INTO mail_rule \ (id, board_id, trigger_kind, trigger_column, subject, audience, enabled, created_at) \ - VALUES ('r1', ?1, 'status', NULL, 'Task completed', 'assignees', 1, '2026-08-26')", + SELECT 'r1', ?1, 'created', id, 'Task completed', 'assignees', 1, '2026-08-26' \ + FROM board_column LIMIT 1", turso::params![board.id.clone()], ) .await; - assert!(refused.is_err(), "a status rule with no column was stored"); + assert!( + refused.is_err(), + "a rule that reads no column was stored carrying one" + ); + + // A status rule with no column is not half-written: it is the rule that + // watches every column, and the schema has to let it through. + let every_column = conn + .execute( + "INSERT INTO mail_rule \ + (id, board_id, trigger_kind, trigger_column, subject, audience, enabled, created_at) \ + VALUES ('r3', ?1, 'status', NULL, 'It moved', 'assignees', 1, '2026-08-26')", + turso::params![board.id.clone()], + ) + .await; + assert!(every_column.is_ok(), "the every-column rule was refused"); let missing_column = conn .execute(