From dd98d9a353ac0e9d8abd1e112b27fda6cbd4a4d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 9 Sep 2026 12:56:37 +0300 Subject: [PATCH] =?UTF-8?q?the=20old=20formatter=20trimmed=20trailing=20ze?= =?UTF-8?q?ros=20too:=20a=20nanosecond=20ending=20in=20zero=20lost=20its?= =?UTF-8?q?=20ninth=20digit,=20so=20a=20legacy=20stamp=20could=20also=20si?= =?UTF-8?q?t=20at=20twenty-nine=20characters=20('=E2=80=A6T18:21:00.347464?= =?UTF-8?q?78Z')=20=E2=80=94=20and=20Z=20outranks=20the=20digits=20as=20mu?= =?UTF-8?q?ch=20as=20it=20outranks=20the=20dot,=20so=20the=20trimmed=20sha?= =?UTF-8?q?pe=20compares=20against=20the=20pinned=20one=20exactly=20as=20w?= =?UTF-8?q?rongly=20as=20the=20whole-second=20shape=20did;=20the=20boot=20?= =?UTF-8?q?pass=20now=20pads=20both=20short=20spellings=20to=20the=20nine-?= =?UTF-8?q?digit=20shape=20(trailing=20zeros=20carry=20no=20information:?= =?UTF-8?q?=20growing=20.34746478=20to=20.347464780=20moves=20no=20instant?= =?UTF-8?q?),=20and=20the=20canonicalize=20test=20plants=20the=20trimmed?= =?UTF-8?q?=20spelling=20beside=20the=20whole-second=20one=20and=20asserts?= =?UTF-8?q?=20the=20digits=20it=20named=20are=20the=20digits=20it=20kept?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/iz-core/src/store/turso_store.rs | 57 +++++++++---- crates/iz-core/tests/store.rs | 107 ++++++++++++++++++------ 2 files changed, 123 insertions(+), 41 deletions(-) diff --git a/crates/iz-core/src/store/turso_store.rs b/crates/iz-core/src/store/turso_store.rs index 071db68..72bcb52 100644 --- a/crates/iz-core/src/store/turso_store.rs +++ b/crates/iz-core/src/store/turso_store.rs @@ -191,13 +191,15 @@ impl TursoStore { } /// Stamps written before [`stamp`] pinned its format may sit in any TEXT - /// stamp column as whole seconds (`…T08:30:00Z`, twenty characters) — and - /// because `Z` outranks `.` in a string compare, such a row hid from - /// `claim_sends_owed` for the whole of the first second it was due. Like - /// the password-sealing pass above, this is a data repair no schema - /// migration can express, run once per boot and idempotent by its own - /// WHERE clause: a twenty-character stamp of the whole-second shape is - /// the only thing it matches, and after one boot none are left to match. + /// stamp column in either of the old formatter's two short spellings — + /// whole seconds (`…T08:30:00Z`, twenty characters) or a subsecond with + /// its trailing zeros trimmed (`…T18:21:00.34746478Z`, twenty-nine) — + /// and because `Z` outranks `.` and every digit in a string compare, + /// both hid from `claim_sends_owed` for the whole of the instant they + /// name. Like the password-sealing pass above, this is a data repair no + /// schema migration can express, run once per boot and idempotent by its + /// own WHERE clauses: they match only the two short shapes, and after + /// one boot neither is left to match. /// The inventory is the live schema's stamp columns, table by table out /// of `migrations/0001` through `0006`: `workspace` (created_at, /// smtp_test_at, smtp_check_at), `user` (created_at, last_signed_in_at), @@ -240,13 +242,41 @@ impl TursoStore { let conn = self.conn.lock().await; let mut changed = 0usize; for (table, column) in STAMP_COLUMNS { + // Two legacy shapes, both from the `time` crate's Rfc3339 + // formatter, which spells an instant as short as it can: a zero + // subsecond is left out entirely (twenty characters), and a + // nonzero one has its trailing zeros trimmed — a nanosecond + // ending in zero loses a digit, so the same formatter wrote + // eight digits where the pinned shape writes nine. Both spell + // the instant faithfully, and both still compare wrong as text + // against the pinned shape: `Z` outranks every digit, so a + // shorter fraction sorts after the same instant written in + // full. The repair is padding, never re-derivation — trailing + // zeros carry no information, so growing `.34746478` to + // `.347464780` moves no instant and breaks no reader. let n = conn .execute( &format!( - "UPDATE {table} SET {column} = substr({column}, 1, 19) \ - || '.000000000Z' \ + "UPDATE {table} \ + SET {column} = substr({column}, 1, 19) || '.000000000Z' \ WHERE {column} IS NOT NULL AND length({column}) = 20 \ - AND {column} LIKE '____-__-__T__:__:__Z'" + AND {column} LIKE '____-__-__T__:__:__Z'", + ), + (), + ) + .await + .map_err(backend)?; + changed += n as usize; + let n = conn + .execute( + &format!( + "UPDATE {table} \ + SET {column} = substr({column}, 1, 20) \ + || substr(replace(substr({column}, 21), 'Z', '') || '000000000', 1, 9) \ + || 'Z' \ + WHERE {column} IS NOT NULL AND length({column}) BETWEEN 21 AND 29 \ + AND {column} LIKE '____-__-__T__:__:__.%' \ + AND {column} LIKE '%Z'", ), (), ) @@ -257,7 +287,7 @@ impl TursoStore { // Spoken only when something actually moved: a boot log that // reports every quiet pass trains the reader to skip it. if changed > 0 { - println!("iz store canonicalized {changed} legacy whole-second stamps"); + println!("iz store canonicalized {changed} legacy stamps to the pinned shape"); } Ok(()) } @@ -1176,9 +1206,8 @@ const DAY: &[BorrowedFormatItem] = format_description!("[year]-[month]-[day]"); /// Why the width is pinned: see [`stamp`] — the queue compares stamps as /// text, and text only compares like time when every stamp is the same /// shape. [`parse_stamp`] reads this and the shorter legacy width alike. -const STAMP: &[BorrowedFormatItem] = format_description!( - "[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:9]Z" -); +const STAMP: &[BorrowedFormatItem] = + format_description!("[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:9]Z"); fn day_text(day: Date) -> Result { day.format(&DAY) diff --git a/crates/iz-core/tests/store.rs b/crates/iz-core/tests/store.rs index 880b0f5..e4595b8 100644 --- a/crates/iz-core/tests/store.rs +++ b/crates/iz-core/tests/store.rs @@ -7466,7 +7466,11 @@ async fn a_whole_second_stamp_is_written_with_nine_zero_digits() { /// rewrites every one of them to the canonical shape — so the queue's TEXT /// comparisons never again meet a `Z` sorting after a `.`. The row planted /// here is the production reminder, verbatim: due on a whole second, -/// written without a fraction. +/// written without a fraction. The old formatter's other short spelling — +/// a subsecond with its trailing zero trimmed, one digit short — is +/// planted beside it, because `Z` outranks the digits too and a +/// twenty-nine-character stamp compares no better than a twenty-character +/// one. #[tokio::test] async fn a_boot_canonicalizes_the_legacy_whole_second_stamps() { let dir = std::env::temp_dir().join(format!("iz-test-{}", Ulid::new())); @@ -7483,19 +7487,20 @@ async fn a_boot_canonicalizes_the_legacy_whole_second_stamps() { let due = clock - Duration::minutes(15); drop(store); - // The old write path, by hand: the same instants, no fraction, in a - // handful of the columns the boot pass owns. Everything else in the - // file was written by the canonical stamp already. - let columns = [ + // The old write paths, by hand: the same instants, no fraction, in a + // handful of the columns the boot pass owns — and one column carrying + // the trimmed-fraction spelling an eight-digit nanosecond used to get. + // Everything else in the file was written by the canonical stamp + // already. The reminder's own due keeps its instant: only the spelling + // is legacy, never the time it names. + let db = turso::Builder::new_local(&path).build().await.unwrap(); + let conn = db.connect().unwrap(); + for (table, column) in [ ("mail_send", "next_attempt_at"), ("mail_send", "claimed_at"), ("task", "clock_at"), ("task", "created_at"), - ("task", "updated_at"), - ]; - let db = turso::Builder::new_local(&path).build().await.unwrap(); - let conn = db.connect().unwrap(); - for (table, column) in columns { + ] { conn.execute( &format!( "UPDATE {table} SET {column} = substr({column}, 1, 19) || 'Z' \ @@ -7506,6 +7511,14 @@ async fn a_boot_canonicalizes_the_legacy_whole_second_stamps() { .await .unwrap(); } + // A subsecond whose last digit the old formatter would have trimmed + // away: `.123456780` written as `.12345678`. + conn.execute( + "UPDATE task SET updated_at = substr(updated_at, 1, 20) || '12345678Z'", + (), + ) + .await + .unwrap(); drop(conn); drop(db); @@ -7513,7 +7526,13 @@ async fn a_boot_canonicalizes_the_legacy_whole_second_stamps() { let reopened = TursoStore::open(&path, &dir.join("storage")).await.unwrap(); let db = turso::Builder::new_local(&path).build().await.unwrap(); let conn = db.connect().unwrap(); - for (table, column) in columns { + for (table, column) in [ + ("mail_send", "next_attempt_at"), + ("mail_send", "claimed_at"), + ("task", "clock_at"), + ("task", "created_at"), + ("task", "updated_at"), + ] { let mut rows = conn .query( &format!("SELECT {column} FROM {table} WHERE {column} IS NOT NULL"), @@ -7524,15 +7543,22 @@ async fn a_boot_canonicalizes_the_legacy_whole_second_stamps() { let mut seen = 0; while let Some(row) = rows.next().await.unwrap() { let raw: String = row.get(0).unwrap(); - assert_eq!( - raw.len(), - 30, - "{table}.{column} came back canonical: {raw}" - ); + assert_eq!(raw.len(), 30, "{table}.{column} came back canonical: {raw}"); seen += 1; } assert!(seen > 0, "{table}.{column} had rows to rewrite"); } + // The trimmed fraction came back padded, not re-derived: the digits it + // named are the digits it kept. + let mut rows = conn.query("SELECT updated_at FROM task", ()).await.unwrap(); + let raw: String = rows + .next() + .await + .unwrap() + .expect("one task row") + .get(0) + .unwrap(); + assert!(raw.ends_with(".123456780Z"), "padded with a zero: {raw}"); drop(conn); drop(db); @@ -8442,7 +8468,10 @@ async fn demoting_a_member_to_viewer_takes_their_assignments_with_them() { for task in [&first, &second] { let chips = store.assignees_for_task(task).await.unwrap(); assert_eq!( - chips.iter().map(|p| p.display_name.as_str()).collect::>(), + chips + .iter() + .map(|p| p.display_name.as_str()) + .collect::>(), ["Emre"], "the demoted person is still a chip on {task}" ); @@ -8571,7 +8600,8 @@ async fn set_user_disabled_round_trips() { async fn a_synced_member_is_born_linked_and_never_signed_in() { let (scratch, workspace, _admin) = workspace_with_admin().await; let sync = scratch - .store.sync_member("sub-mert", "mert@iz.sh", "Mert", false, 0, "UTC+03:00") + .store + .sync_member("sub-mert", "mert@iz.sh", "Mert", false, 0, "UTC+03:00") .await .unwrap(); assert_eq!(sync, iz_core::store::MemberSync::Inserted); @@ -8586,7 +8616,8 @@ async fn a_synced_member_is_born_linked_and_never_signed_in() { assert!(row.last_signed_in_at.is_none()); // An im admin the workspace has never met is Admin from the first beat. scratch - .store.sync_member("sub-boss", "boss@iz.sh", "Boss", true, 0, "UTC+03:00") + .store + .sync_member("sub-boss", "boss@iz.sh", "Boss", true, 0, "UTC+03:00") .await .unwrap(); let boss = scratch @@ -8607,7 +8638,15 @@ async fn a_sync_claims_an_unclaimed_row_without_touching_its_sign_in_fact() { .await .unwrap(); let sync = scratch - .store.sync_member("sub-mert", "MERT@iz.sh", "Mert Yılmaz", false, 0, "UTC+03:00") + .store + .sync_member( + "sub-mert", + "MERT@iz.sh", + "Mert Yılmaz", + false, + 0, + "UTC+03:00", + ) .await .unwrap(); assert_eq!(sync, iz_core::store::MemberSync::Claimed); @@ -8628,7 +8667,8 @@ async fn a_sync_follows_provider_drift_but_not_the_sign_in_fact() { let before = scratch.store.user(&admin_id).await.unwrap().unwrap(); let seen = before.last_signed_in_at.clone(); let sync = scratch - .store.sync_member("sub-ada", "ada@iz.sh", "Ada Lovelace", true, 0, "UTC+03:00") + .store + .sync_member("sub-ada", "ada@iz.sh", "Ada Lovelace", true, 0, "UTC+03:00") .await .unwrap(); assert_eq!(sync, iz_core::store::MemberSync::Refreshed); @@ -8637,7 +8677,15 @@ async fn a_sync_follows_provider_drift_but_not_the_sign_in_fact() { assert_eq!(after.last_signed_in_at, seen); // Losing the flag in the directory drops an Admin to Member. scratch - .store.sync_member("sub-ada", "ada@iz.sh", "Ada Lovelace", false, 0, "UTC+03:00") + .store + .sync_member( + "sub-ada", + "ada@iz.sh", + "Ada Lovelace", + false, + 0, + "UTC+03:00", + ) .await .unwrap(); let demoted = scratch.store.user(&admin_id).await.unwrap().unwrap(); @@ -8649,7 +8697,8 @@ async fn an_agreeing_sync_is_untouched_and_silent() { let (scratch, _, _) = workspace_with_admin().await; let mut rx = scratch.store.subscribe(); let sync = scratch - .store.sync_member("sub-ada", "ada@iz.sh", "Ada", true, 0, "UTC") + .store + .sync_member("sub-ada", "ada@iz.sh", "Ada", true, 0, "UTC") .await .unwrap(); assert_eq!(sync, iz_core::store::MemberSync::Untouched); @@ -8660,7 +8709,8 @@ async fn an_agreeing_sync_is_untouched_and_silent() { async fn a_sync_without_a_workspace_skips_and_writes_nothing() { let scratch = Scratch::open().await; let sync = scratch - .store.sync_member("sub-mert", "mert@iz.sh", "Mert", false, 0, "UTC+03:00") + .store + .sync_member("sub-mert", "mert@iz.sh", "Mert", false, 0, "UTC+03:00") .await .unwrap(); assert_eq!(sync, iz_core::store::MemberSync::Skipped); @@ -8676,7 +8726,8 @@ async fn a_photo_version_bump_refreshes_and_announces() { let (scratch, workspace, _admin) = workspace_with_admin().await; let mut rx = scratch.store.subscribe(); let sync = scratch - .store.sync_member("sub-mert", "mert@iz.sh", "Mert", false, 2, "UTC+03:00") + .store + .sync_member("sub-mert", "mert@iz.sh", "Mert", false, 2, "UTC+03:00") .await .unwrap(); assert_eq!(sync, iz_core::store::MemberSync::Inserted); @@ -8691,7 +8742,8 @@ async fn a_photo_version_bump_refreshes_and_announces() { // The same version again: the row agrees, and silence is the answer. let again = scratch - .store.sync_member("sub-mert", "mert@iz.sh", "Mert", false, 2, "UTC+03:00") + .store + .sync_member("sub-mert", "mert@iz.sh", "Mert", false, 2, "UTC+03:00") .await .unwrap(); assert_eq!(again, iz_core::store::MemberSync::Untouched); @@ -8699,7 +8751,8 @@ async fn a_photo_version_bump_refreshes_and_announces() { // im counted another upload: the stamp moves and the move is news. let bumped = scratch - .store.sync_member("sub-mert", "mert@iz.sh", "Mert", false, 3, "UTC+03:00") + .store + .sync_member("sub-mert", "mert@iz.sh", "Mert", false, 3, "UTC+03:00") .await .unwrap(); assert_eq!(bumped, iz_core::store::MemberSync::Refreshed);