From cd6a00a9cd23e9e9b122ef257c7f09350244ab2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 5 Aug 2026 22:13:31 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=8F=20fix:=20let=20a=20one-time=20migr?= =?UTF-8?q?ation=20block=20be=20corrected=20after=20it=20is=20marked?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `migration_mark` recorded only *that* a block had run, so its content froze the moment any volume booted once: the `exam_sat_total` seed correction shipped today inside the `profile_counters` block and would never have run on a single existing database, leaving that counter holding the attempt count while the live path credits sittings — and badge awards are permanent, so the ones that fired early could not be taken back. The mark now stores a fingerprint of the block's own SQL (FNV-1a over its statements, comments and indentation dropped) and the gate compares it against the text this binary would run. An edited block runs once more and re-stamps; an unedited one still costs one record read. Nothing is hand-maintained — the number comes from the very string the server executes, so there is no version anyone must remember to bump — and blocks are discovered by their gate idiom, with a test that fails for one written any other way. A mark written before the column carries no fingerprint, so every existing volume re-runs both blocks exactly once. Safe here: the board sweep is idempotent and the counter seed's SETs are absolute. Its one ceiling — the lifetime tallies whose rows can be deleted read *lower* on a recount — is written into the block as a `ponytail:` with its upgrade path, because correcting that block is a trade rather than a free edit. Also stamps `exam_attempt.seq`, left out when its three sibling tables got their backfill: `DEFAULT 1` fills a CREATE, and 3.2.3 neither synthesizes it on read nor lets such a row be written again, so a sitting stored before 2026-07-24 would 500 on finish and on leave. --- src/database.rs | 287 ++++++++++++++++++++++++++++++++++++++++++- src/migration_sql.rs | 62 ++++++++-- 2 files changed, 336 insertions(+), 13 deletions(-) diff --git a/src/database.rs b/src/database.rs index 64cb75c..3671dd3 100644 --- a/src/database.rs +++ b/src/database.rs @@ -9,7 +9,7 @@ use crate::constant::{CAP_WRITE_BACKOFF_MS, CAP_WRITE_TRIES, CHATBOT_PENDING_STA use crate::domain::timestamp::Timestamp; use crate::domain::user::{Password, User, Username}; use crate::error::AppError; -use crate::migration_sql::MIGRATION_BATCHES; +use crate::migration_sql::{BACKFILL, MIGRATION_BATCHES}; /// The shared database handle. /// @@ -328,14 +328,150 @@ pub async fn migrate(db: &Surreal) -> Result<(), AppError> { /// Parameters the batches are executed with. Bound, not baked into the SQL /// string: a `const` cannot be interpolated into another `const`, and a -/// hand-copied 300000 would drift silently. -fn migration_binds() -> [(&'static str, i64); 1] { - [("stale_ms", CHATBOT_PENDING_STALE_SECS * 1_000)] +/// hand-copied 300000 would drift silently. The one-time blocks' fingerprints +/// ride along — see [`marked_blocks`]. Bound to all three batches, which is +/// free: an unused parameter is not an error. +fn migration_binds() -> Vec<(String, i64)> { + let mut binds = vec![("stale_ms".to_string(), CHATBOT_PENDING_STALE_SECS * 1_000)]; + binds.extend(marked_blocks()); + binds +} + +/// The line every one-time block in [`BACKFILL`] opens with. The name straight +/// after it is the block's `migration_mark` key, and `fp_` is the +/// parameter its fingerprint is bound to — so a block written in this idiom is +/// found, hashed and bound with nothing to keep by hand. A block written any +/// other way silently gets no fingerprint, which is what +/// `every_one_time_block_is_fingerprinted` exists to catch. +const MARK_GATE: &str = "IF array::len((SELECT VALUE id FROM migration_mark:"; + +/// Every one-time block in [`BACKFILL`], as (`fp_`, hash of its own SQL). +/// +/// A `migration_mark` used to say only *that* a block had run, which made a +/// marked block's content permanently uncorrectable: an edit to it never ran on +/// any volume that had booted once. The mark now stores this number and the +/// gate compares it against the number this binary computes, so an edited block +/// runs one more time and re-stamps. There is nothing to remember to bump — the +/// hash comes from the very string the server executes, which is the whole +/// point: a hand-kept version is the same discipline failure in a new place. +/// +/// Hashed from the gate through the stamp with comments and indentation +/// dropped: every re-run costs something (the `profile_counters` block says +/// what, in its own comment), so re-wording a comment must not trigger one. +fn marked_blocks() -> Vec<(String, i64)> { + let mut blocks = Vec::new(); + let mut rest = BACKFILL; + while let Some(at) = rest.find(MARK_GATE) { + let block = &rest[at..]; + let name: String = block[MARK_GATE.len()..] + .chars() + .take_while(|c| c.is_ascii_alphanumeric() || *c == '_') + .collect(); + let stamp = format!("UPSERT migration_mark:{name}"); + let end = block + .find(&stamp) + .and_then(|from| block[from..].find(';').map(|to| from + to + 1)) + .unwrap_or_else(|| panic!("the `{name}` block never stamps its own mark")); + blocks.push((format!("fp_{name}"), block_fingerprint(&block[..end]))); + rest = &block[end..]; + } + blocks +} + +/// FNV-1a over a block's statements, comments and layout dropped. +/// +/// Hand-rolled rather than [`std::hash`]: this number is *stored*, so it has to +/// mean the same thing after a compiler upgrade, and `DefaultHasher` promises +/// nothing across versions — a hash that drifted on its own would re-run every +/// one-time block on every volume. +fn block_fingerprint(block: &str) -> i64 { + let mut hash: u64 = 0xcbf2_9ce4_8422_2325; + for line in block + .lines() + .map(str::trim) + .filter(|line| !line.is_empty() && !line.starts_with("--")) + { + for byte in line.bytes().chain(std::iter::once(b'\n')) { + hash = (hash ^ u64::from(byte)).wrapping_mul(0x0000_0100_0000_01b3); + } + } + hash as i64 } #[cfg(test)] mod tests { + /// The fingerprint has to move when a *statement* moves and stay put when + /// prose does: a re-run is never free (see the `profile_counters` comment), + /// and a fingerprint that ignored an edit would put us back where this + /// mechanism started — a marked block nobody can correct. + #[test] + fn a_block_fingerprint_tracks_statements_not_prose() { + let block = "IF x = 0 {\n -- why this runs\n UPDATE t SET a = 1;\n};"; + let reworded = "IF x = 0 {\n -- why this runs, at length\n UPDATE t SET a = 1;\n};"; + let edited = "IF x = 0 {\n -- why this runs\n UPDATE t SET a = 2;\n};"; + assert_eq!( + super::block_fingerprint(block), + super::block_fingerprint(reworded), + "a re-worded comment or a re-indent must not re-run a one-time block" + ); + assert_ne!( + super::block_fingerprint(block), + super::block_fingerprint(edited), + "an edited statement must re-run its block" + ); + } + + /// The forcing function: a one-time block written outside the gate idiom + /// gets no fingerprint parameter, and its `$fp_…` would bind to nothing — a + /// gate that then matches every mark and skips forever. Nothing about the + /// compiler notices, so this does. + #[test] + fn every_one_time_block_is_fingerprinted() { + let blocks = super::marked_blocks(); + let names: Vec<&str> = blocks.iter().map(|(name, _)| name.as_str()).collect(); + assert_eq!( + names, + ["fp_board_roster", "fp_profile_counters"], + "{names:?}" + ); + assert_ne!(blocks[0].1, blocks[1].1, "two blocks, two fingerprints"); + // Two mentions of the mark table per block — its gate and its stamp. A + // third, or a block gated some other way, means a block the binder + // never saw. + assert_eq!( + super::BACKFILL.matches("migration_mark:").count(), + blocks.len() * 2, + "a one-time block outside the `{}…` idiom is invisible to the binder", + super::MARK_GATE + ); + for (param, _) in &blocks { + assert!( + super::BACKFILL.contains(&format!("${param}")), + "nothing reads ${param}" + ); + } + } + + /// The fingerprint the mark for `name` must carry once its block has run. + fn expected_fingerprint(name: &str) -> i64 { + super::marked_blocks() + .into_iter() + .find(|(param, _)| *param == format!("fp_{name}")) + .unwrap_or_else(|| panic!("no one-time block named {name}")) + .1 + } + + async fn stamped_fingerprint(db: &super::Database, name: &str) -> Vec> { + let mut rows = db + .query(format!( + "SELECT VALUE fingerprint FROM migration_mark:{name}" + )) + .await + .unwrap(); + rows.take(0).unwrap() + } + /// The admin seed must not be interruptible into a state no later boot can /// repair. `create` + `set_role` was: killed between the two writes it /// leaves a *student* row holding ADMIN_USERNAME, which `ensure_admin` then @@ -562,6 +698,12 @@ mod tests { 1, "the repair marks itself finished" ); + // …with the fingerprint of the text it ran, which is what a later edit + // to this block is compared against. + assert_eq!( + stamped_fingerprint(&db, "board_roster").await, + vec![Some(expected_fingerprint("board_roster"))] + ); db.query("CREATE board:late SET creator = user:s, title = 'E', participants = [user:p], created_at = 1") .await .unwrap() @@ -741,6 +883,10 @@ mod tests { 1, "the seed marks itself finished" ); + assert_eq!( + stamped_fingerprint(&db, "profile_counters").await, + vec![Some(expected_fingerprint("profile_counters"))] + ); db.query( "CREATE homework_submission:late SET homework = homework:h, user = user:b, submitted_at = 90, updated_at = 90;", @@ -753,11 +899,142 @@ mod tests { assert_eq!( counters(&db).await, after_first, - "a marked database skips the seed entirely" + "a mark carrying this binary's fingerprint skips the seed entirely" ); assert_eq!(writes(&db).await, first_writes); } + /// A marked block's *content* has to be correctable (2026-08-05). The mark + /// used to record only that the block had run, so an edit to a marked block + /// silently never ran on any volume that had booted once — which is how the + /// `exam_sat_total` seed below (fixed to count sittings, not attempts) + /// would have reached exactly nobody. The mark carries the fingerprint of + /// the text it ran, and a different fingerprint runs the block once more. + #[tokio::test] + async fn an_edited_one_time_block_runs_once_more_and_restamps() { + let db = super::init_mem().await.unwrap(); + // `init_mem` migrates an empty store, so the seed is already marked + // done — the state every booted volume is in. History written *after* + // that mark is the probe: a skip has to leave it uncounted, and a + // re-run has to pick it up. + db.query( + "CREATE user:a SET username = 'a', password_hash = 'x', role = 'student'; + -- One sitting, taken twice. The corrected seed counts the exam, + -- not the attempts, exactly as the request path credits it. + CREATE exam_attempt:e1 SET exam = exam:x, user = user:a, seq = 1, started_at = 1; + CREATE exam_attempt:e2 SET exam = exam:x, user = user:a, seq = 2, started_at = 2; + CREATE pomodoro_session:p1 SET user = user:a, started_at = 1000, finished_at = 2000;", + ) + .await + .unwrap() + .check() + .unwrap(); + + async fn counters(db: &super::Database) -> Vec<(i64, i64)> { + let mut rows = db + .query( + // `?? 0`, parenthesized: the columns are `option` and + // a row the seed has not touched holds neither. + "SELECT VALUE [(exam_sat_total ?? 0), (pomodoro_finished_total ?? 0)] + FROM user ORDER BY id", + ) + .await + .unwrap(); + rows.take::>>(0) + .unwrap() + .into_iter() + .map(|row| (row[0], row[1])) + .collect() + } + let fp = expected_fingerprint("profile_counters"); + + // Same binary, same fingerprint: the block does not run, and the + // history above stays uncounted. That is the mark still doing its job. + super::migrate(&db).await.unwrap(); + assert_eq!(counters(&db).await, vec![(0, 0)]); + assert_eq!( + stamped_fingerprint(&db, "profile_counters").await, + vec![Some(fp)] + ); + + // A write probe on the seeded table, defined now so it counts only the + // runs under test: the event fires *inside* the UPDATE, so "exactly + // once" is measured rather than inferred from the numbers. + db.query( + "DEFINE TABLE user_write_probe SCHEMALESS; + DEFINE EVENT user_write ON user WHEN $event = 'UPDATE' THEN { + UPSERT type::record('user_write_probe', 'n') SET n = (n ?? 0) + 1; + }; + -- A mark written by the binary before fingerprints existed: the + -- row is there, the column is not. This is the live volumes' + -- state, and it must run the corrected block exactly once. + UPDATE migration_mark:profile_counters UNSET fingerprint;", + ) + .await + .unwrap() + .check() + .unwrap(); + assert_eq!( + stamped_fingerprint(&db, "profile_counters").await, + vec![None] + ); + + super::migrate(&db).await.unwrap(); + // Two attempt rows, one exam: the corrected seed says one sitting, the + // number the request path would have given. The old text said two. + assert_eq!(counters(&db).await, vec![(1, 1)]); + assert_eq!( + stamped_fingerprint(&db, "profile_counters").await, + vec![Some(fp)] + ); + + async fn writes(db: &super::Database) -> Vec { + let mut rows = db + .query("SELECT VALUE n FROM user_write_probe:n") + .await + .unwrap(); + rows.take(0).unwrap() + } + let one_run = writes(&db).await; + // Pinned non-zero, not merely compared: "it did not run twice" holds + // vacuously if the probe stopped firing, and the test would then pass + // on a mechanism that never runs anything at all. + assert!(matches!(one_run.as_slice(), [n] if *n > 0), "{one_run:?}"); + + // Once more, not on every boot: the re-run re-stamped, so the next boot + // is a plain skip again. + super::migrate(&db).await.unwrap(); + assert_eq!(counters(&db).await, vec![(1, 1)]); + assert_eq!(writes(&db).await, one_run, "the re-run happened once"); + + // And now the edit this whole mechanism exists for: a mark whose stored + // fingerprint is some *other* text's. The block runs again and lands on + // the same numbers — every SET is absolute, so a re-run converges + // instead of doubling what the first pass wrote. + db.query("UPDATE migration_mark:profile_counters SET fingerprint = 1") + .await + .unwrap() + .check() + .unwrap(); + super::migrate(&db).await.unwrap(); + assert_eq!( + counters(&db).await, + vec![(1, 1)], + "a re-run recomputes, it does not add to what is there" + ); + assert_eq!( + writes(&db).await, + vec![one_run[0] * 2], + "the edited block did the same work a second time, and no more" + ); + assert_eq!( + stamped_fingerprint(&db, "profile_counters").await, + vec![Some(fp)] + ); + super::migrate(&db).await.unwrap(); + assert_eq!(writes(&db).await, vec![one_run[0] * 2], "and stopped again"); + } + #[tokio::test] async fn the_retired_claim_columns_are_dropped_off_a_live_row() { // Stale-data path for the 2026-07-30 removal of the chat claim queue. diff --git a/src/migration_sql.rs b/src/migration_sql.rs index d2e979d..92fac46 100644 --- a/src/migration_sql.rs +++ b/src/migration_sql.rs @@ -893,8 +893,20 @@ pub const MIGRATION: &str = " -- boot loop carries no lock and no fingerprint any more (2026-07-30), so -- there is nowhere else for that fact to live. A database that never ran the -- repair holds no row and runs it; the row is written by the same query. + -- + -- `fingerprint` is *which* text ran (2026-08-05), and it is what makes a + -- marked block correctable. Saying only 'this ran' meant an edit to a block + -- that was already marked could never reach a volume that had booted once: + -- the `exam_sat_total` correction below shipped inside one and would have + -- been dead on arrival on every existing database. The gate compares the + -- stored number against the hash of the text *this* binary would run + -- (`database::marked_blocks`), so an edited block runs once more and + -- re-stamps, and an unedited one still costs a single record read. + -- `option`: a mark written before this column existed carries none, + -- which matches no fingerprint and so re-runs its block exactly once. DEFINE TABLE IF NOT EXISTS migration_mark SCHEMAFULL; DEFINE FIELD IF NOT EXISTS done_at ON migration_mark TYPE int; + DEFINE FIELD IF NOT EXISTS fingerprint ON migration_mark TYPE option; "; /// Data backfills for rows written by older binaries. Runs *after* (and apart @@ -1074,10 +1086,21 @@ pub const BACKFILL: &str = " }; }; - -- Per-attempt history (2026-07-24): answers, drawings, and marks written - -- before retakes stopped wiping belong to the student's first sitting. - -- They already use the bare (seq==1) record key, so only the denormalized - -- `seq` field needs stamping. + -- Per-attempt history (2026-07-24): sittings, answers, drawings, and marks + -- written before retakes stopped wiping belong to the student's first + -- sitting. They already use the bare (seq==1) record key, so only the + -- denormalized `seq` field needs stamping. + -- + -- `exam_attempt` was left out of this list when the other three were added + -- and that is not a decision, it is a gap: `DEFAULT 1` fills a CREATE, never + -- a row stored before the column was defined, and 3.2.3 does not synthesize + -- it on read either — such a row comes back with no `seq` key at all, which + -- makes `ExamAttempt` (a non-`option` `i64`) fail to deserialize *and* makes + -- every later write to the row fail coercion, so finishing or leaving that + -- sitting 500s forever. Ungated like its siblings and for the same reason: + -- the cost here is the write, not a scan, and `WHERE seq = NONE` matches + -- nothing the moment it has converged. + UPDATE exam_attempt SET seq = 1 WHERE seq = NONE; UPDATE exam_answer SET seq = 1 WHERE seq = NONE; UPDATE answer_image SET seq = 1 WHERE seq = NONE; UPDATE exam_result SET seq = 1 WHERE seq = NONE; @@ -1274,12 +1297,19 @@ pub const BACKFILL: &str = " -- statement batch, so a boot killed mid-repair writes no mark and the next -- one redoes the whole sweep — which is free, because the sweep is -- idempotent. `UPSERT`, not `CREATE`: re-marking must never be an error. - IF array::len((SELECT VALUE id FROM migration_mark:board_roster)) = 0 { + -- + -- Editing this block is therefore safe at any time: the fingerprint gate + -- re-runs it once and the sweep converges on whatever it finds, exactly as + -- a boot killed mid-repair would. The mark buys the scan back, not + -- correctness. + IF array::len((SELECT VALUE id FROM migration_mark:board_roster + WHERE fingerprint = $fp_board_roster)) = 0 { FOR $row IN ((SELECT id, participants FROM board WHERE array::len(participants ?? []) > 0) ?? []) { LET $keep = (SELECT VALUE id FROM user WHERE id IN $row.participants AND role != 'parent'); UPDATE $row.id SET participants = $keep WHERE participants != $keep; }; - UPSERT migration_mark:board_roster SET done_at = time::unix(time::now()) * 1000; + UPSERT migration_mark:board_roster SET done_at = time::unix(time::now()) * 1000, + fingerprint = $fp_board_roster; }; -- The badge counters, seeded from the history that already exists @@ -1336,7 +1366,22 @@ pub const BACKFILL: &str = " -- by the very same cut, so a submission that predates this seed debits at -- withdrawal exactly what the seed credited it. Absolute like every SET -- below, so it converges on a forced re-seed. - IF array::len((SELECT VALUE id FROM migration_mark:profile_counters)) = 0 { + -- + -- ponytail: editing this block re-runs it (the fingerprint gate), and unlike + -- the board sweep above this one is only *mostly* re-runnable. Every SET is + -- absolute, so a re-run converges on the rows that exist — but two of the + -- counters are lifetime tallies the request path never decrements while the + -- rows behind them can be deleted (`exam_sat_total`, + -- `homework_submitted_total`, see the paragraph above). On a volume where a + -- teacher has deleted an exam or a homework since the last seed, a re-run + -- reads *lower* than the live count and the student's tally walks backwards + -- — badges already awarded are permanent, so nothing is revoked, but the + -- next one moves further away. Correcting this block is a deliberate trade, + -- not a free edit. The upgrade path, if it ever needs to be free: keep the + -- deleted rows' contribution on the row (a `*_retired` column the delete + -- bumps) so the recount can add it back. + IF array::len((SELECT VALUE id FROM migration_mark:profile_counters + WHERE fingerprint = $fp_profile_counters)) = 0 { UPDATE user SET homework_submitted_total = 0, homework_on_time_total = 0, exam_sat_total = 0, pomodoro_finished_total = 0, pomodoro_focus_ms_total = 0; FOR $row IN ((SELECT user, count() AS n FROM homework_submission GROUP BY user) ?? []) { @@ -1362,7 +1407,8 @@ pub const BACKFILL: &str = " UPDATE $row.user SET pomodoro_finished_total = $row.sessions, pomodoro_focus_ms_total = $row.focus_ms; }; - UPSERT migration_mark:profile_counters SET done_at = time::unix(time::now()) * 1000; + UPSERT migration_mark:profile_counters SET done_at = time::unix(time::now()) * 1000, + fingerprint = $fp_profile_counters; }; ";