forge
Explore
Log in

Don't you dare go Hollow

the old formatter trimmed trailing zeros too: a nanosecond ending in zero lost its ninth digit, so a legacy stamp could also sit at twenty-nine characters ('…T18:21:00.34746478Z') — and Z outranks the digits as much as it outranks the dot, so the trimmed shape compares against the pinned one exactly as wrongly as the whole-second shape did; the boot pass now pads both short spellings to the nine-digit shape (trailing zeros carry no information: growing .34746478 to .347464780 moves no instant), and the canonicalize test plants the trimmed spelling beside the whole-second one and asserts the digits it named are the digits it kept

Browse files at this commit

dd98d9a353ac0e9d8abd1e112b27fda6cbd4a4d1Ahmet Kaan GümüşParent: 6f9e149
crates/iz-core/src/store/turso_store.rsmodified+43 -14
@@ -191,13 +191,15 @@ impl TursoStore {
191191 }
192192
193193 /// Stamps written before [`stamp`] pinned its format may sit in any TEXT
194- /// stamp column as whole seconds (`…T08:30:00Z`, twenty characters) — and
195- /// because `Z` outranks `.` in a string compare, such a row hid from
196- /// `claim_sends_owed` for the whole of the first second it was due. Like
197- /// the password-sealing pass above, this is a data repair no schema
198- /// migration can express, run once per boot and idempotent by its own
199- /// WHERE clause: a twenty-character stamp of the whole-second shape is
200- /// the only thing it matches, and after one boot none are left to match.
194+ /// stamp column in either of the old formatter's two short spellings —
195+ /// whole seconds (`…T08:30:00Z`, twenty characters) or a subsecond with
196+ /// its trailing zeros trimmed (`…T18:21:00.34746478Z`, twenty-nine) —
197+ /// and because `Z` outranks `.` and every digit in a string compare,
198+ /// both hid from `claim_sends_owed` for the whole of the instant they
199+ /// name. Like the password-sealing pass above, this is a data repair no
200+ /// schema migration can express, run once per boot and idempotent by its
201+ /// own WHERE clauses: they match only the two short shapes, and after
202+ /// one boot neither is left to match.
201203 /// The inventory is the live schema's stamp columns, table by table out
202204 /// of `migrations/0001` through `0006`: `workspace` (created_at,
203205 /// smtp_test_at, smtp_check_at), `user` (created_at, last_signed_in_at),
@@ -240,13 +242,41 @@ impl TursoStore {
240242 let conn = self.conn.lock().await;
241243 let mut changed = 0usize;
242244 for (table, column) in STAMP_COLUMNS {
245+ // Two legacy shapes, both from the `time` crate's Rfc3339
246+ // formatter, which spells an instant as short as it can: a zero
247+ // subsecond is left out entirely (twenty characters), and a
248+ // nonzero one has its trailing zeros trimmed — a nanosecond
249+ // ending in zero loses a digit, so the same formatter wrote
250+ // eight digits where the pinned shape writes nine. Both spell
251+ // the instant faithfully, and both still compare wrong as text
252+ // against the pinned shape: `Z` outranks every digit, so a
253+ // shorter fraction sorts after the same instant written in
254+ // full. The repair is padding, never re-derivation — trailing
255+ // zeros carry no information, so growing `.34746478` to
256+ // `.347464780` moves no instant and breaks no reader.
243257 let n = conn
244258 .execute(
245259 &format!(
246- "UPDATE {table} SET {column} = substr({column}, 1, 19) \
247- || '.000000000Z' \
260+ "UPDATE {table} \
261+ SET {column} = substr({column}, 1, 19) || '.000000000Z' \
248262 WHERE {column} IS NOT NULL AND length({column}) = 20 \
249- AND {column} LIKE '____-__-__T__:__:__Z'"
263+ AND {column} LIKE '____-__-__T__:__:__Z'",
264+ ),
265+ (),
266+ )
267+ .await
268+ .map_err(backend)?;
269+ changed += n as usize;
270+ let n = conn
271+ .execute(
272+ &format!(
273+ "UPDATE {table} \
274+ SET {column} = substr({column}, 1, 20) \
275+ || substr(replace(substr({column}, 21), 'Z', '') || '000000000', 1, 9) \
276+ || 'Z' \
277+ WHERE {column} IS NOT NULL AND length({column}) BETWEEN 21 AND 29 \
278+ AND {column} LIKE '____-__-__T__:__:__.%' \
279+ AND {column} LIKE '%Z'",
250280 ),
251281 (),
252282 )
@@ -257,7 +287,7 @@ impl TursoStore {
257287 // Spoken only when something actually moved: a boot log that
258288 // reports every quiet pass trains the reader to skip it.
259289 if changed > 0 {
260- println!("iz store canonicalized {changed} legacy whole-second stamps");
290+ println!("iz store canonicalized {changed} legacy stamps to the pinned shape");
261291 }
262292 Ok(())
263293 }
@@ -1176,9 +1206,8 @@ const DAY: &[BorrowedFormatItem] = format_description!("[year]-[month]-[day]");
11761206 /// Why the width is pinned: see [`stamp`] — the queue compares stamps as
11771207 /// text, and text only compares like time when every stamp is the same
11781208 /// shape. [`parse_stamp`] reads this and the shorter legacy width alike.
1179-const STAMP: &[BorrowedFormatItem] = format_description!(
1180- "[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:9]Z"
1181-);
1209+const STAMP: &[BorrowedFormatItem] =
1210+ format_description!("[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:9]Z");
11821211
11831212 fn day_text(day: Date) -> Result<String> {
11841213 day.format(&DAY)
crates/iz-core/tests/store.rsmodified+80 -27
@@ -7466,7 +7466,11 @@ async fn a_whole_second_stamp_is_written_with_nine_zero_digits() {
74667466 /// rewrites every one of them to the canonical shape — so the queue's TEXT
74677467 /// comparisons never again meet a `Z` sorting after a `.`. The row planted
74687468 /// here is the production reminder, verbatim: due on a whole second,
7469-/// written without a fraction.
7469+/// written without a fraction. The old formatter's other short spelling —
7470+/// a subsecond with its trailing zero trimmed, one digit short — is
7471+/// planted beside it, because `Z` outranks the digits too and a
7472+/// twenty-nine-character stamp compares no better than a twenty-character
7473+/// one.
74707474 #[tokio::test]
74717475 async fn a_boot_canonicalizes_the_legacy_whole_second_stamps() {
74727476 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() {
74837487 let due = clock - Duration::minutes(15);
74847488 drop(store);
74857489
7486- // The old write path, by hand: the same instants, no fraction, in a
7487- // handful of the columns the boot pass owns. Everything else in the
7488- // file was written by the canonical stamp already.
7489- let columns = [
7490+ // The old write paths, by hand: the same instants, no fraction, in a
7491+ // handful of the columns the boot pass owns — and one column carrying
7492+ // the trimmed-fraction spelling an eight-digit nanosecond used to get.
7493+ // Everything else in the file was written by the canonical stamp
7494+ // already. The reminder's own due keeps its instant: only the spelling
7495+ // is legacy, never the time it names.
7496+ let db = turso::Builder::new_local(&path).build().await.unwrap();
7497+ let conn = db.connect().unwrap();
7498+ for (table, column) in [
74907499 ("mail_send", "next_attempt_at"),
74917500 ("mail_send", "claimed_at"),
74927501 ("task", "clock_at"),
74937502 ("task", "created_at"),
7494- ("task", "updated_at"),
7495- ];
7496- let db = turso::Builder::new_local(&path).build().await.unwrap();
7497- let conn = db.connect().unwrap();
7498- for (table, column) in columns {
7503+ ] {
74997504 conn.execute(
75007505 &format!(
75017506 "UPDATE {table} SET {column} = substr({column}, 1, 19) || 'Z' \
@@ -7506,6 +7511,14 @@ async fn a_boot_canonicalizes_the_legacy_whole_second_stamps() {
75067511 .await
75077512 .unwrap();
75087513 }
7514+ // A subsecond whose last digit the old formatter would have trimmed
7515+ // away: `.123456780` written as `.12345678`.
7516+ conn.execute(
7517+ "UPDATE task SET updated_at = substr(updated_at, 1, 20) || '12345678Z'",
7518+ (),
7519+ )
7520+ .await
7521+ .unwrap();
75097522 drop(conn);
75107523 drop(db);
75117524
@@ -7513,7 +7526,13 @@ async fn a_boot_canonicalizes_the_legacy_whole_second_stamps() {
75137526 let reopened = TursoStore::open(&path, &dir.join("storage")).await.unwrap();
75147527 let db = turso::Builder::new_local(&path).build().await.unwrap();
75157528 let conn = db.connect().unwrap();
7516- for (table, column) in columns {
7529+ for (table, column) in [
7530+ ("mail_send", "next_attempt_at"),
7531+ ("mail_send", "claimed_at"),
7532+ ("task", "clock_at"),
7533+ ("task", "created_at"),
7534+ ("task", "updated_at"),
7535+ ] {
75177536 let mut rows = conn
75187537 .query(
75197538 &format!("SELECT {column} FROM {table} WHERE {column} IS NOT NULL"),
@@ -7524,15 +7543,22 @@ async fn a_boot_canonicalizes_the_legacy_whole_second_stamps() {
75247543 let mut seen = 0;
75257544 while let Some(row) = rows.next().await.unwrap() {
75267545 let raw: String = row.get(0).unwrap();
7527- assert_eq!(
7528- raw.len(),
7529- 30,
7530- "{table}.{column} came back canonical: {raw}"
7531- );
7546+ assert_eq!(raw.len(), 30, "{table}.{column} came back canonical: {raw}");
75327547 seen += 1;
75337548 }
75347549 assert!(seen > 0, "{table}.{column} had rows to rewrite");
75357550 }
7551+ // The trimmed fraction came back padded, not re-derived: the digits it
7552+ // named are the digits it kept.
7553+ let mut rows = conn.query("SELECT updated_at FROM task", ()).await.unwrap();
7554+ let raw: String = rows
7555+ .next()
7556+ .await
7557+ .unwrap()
7558+ .expect("one task row")
7559+ .get(0)
7560+ .unwrap();
7561+ assert!(raw.ends_with(".123456780Z"), "padded with a zero: {raw}");
75367562 drop(conn);
75377563 drop(db);
75387564
@@ -8442,7 +8468,10 @@ async fn demoting_a_member_to_viewer_takes_their_assignments_with_them() {
84428468 for task in [&first, &second] {
84438469 let chips = store.assignees_for_task(task).await.unwrap();
84448470 assert_eq!(
8445- chips.iter().map(|p| p.display_name.as_str()).collect::<Vec<_>>(),
8471+ chips
8472+ .iter()
8473+ .map(|p| p.display_name.as_str())
8474+ .collect::<Vec<_>>(),
84468475 ["Emre"],
84478476 "the demoted person is still a chip on {task}"
84488477 );
@@ -8571,7 +8600,8 @@ async fn set_user_disabled_round_trips() {
85718600 async fn a_synced_member_is_born_linked_and_never_signed_in() {
85728601 let (scratch, workspace, _admin) = workspace_with_admin().await;
85738602 let sync = scratch
8574- .store.sync_member("sub-mert", "mert@iz.sh", "Mert", false, 0, "UTC+03:00")
8603+ .store
8604+ .sync_member("sub-mert", "mert@iz.sh", "Mert", false, 0, "UTC+03:00")
85758605 .await
85768606 .unwrap();
85778607 assert_eq!(sync, iz_core::store::MemberSync::Inserted);
@@ -8586,7 +8616,8 @@ async fn a_synced_member_is_born_linked_and_never_signed_in() {
85868616 assert!(row.last_signed_in_at.is_none());
85878617 // An im admin the workspace has never met is Admin from the first beat.
85888618 scratch
8589- .store.sync_member("sub-boss", "boss@iz.sh", "Boss", true, 0, "UTC+03:00")
8619+ .store
8620+ .sync_member("sub-boss", "boss@iz.sh", "Boss", true, 0, "UTC+03:00")
85908621 .await
85918622 .unwrap();
85928623 let boss = scratch
@@ -8607,7 +8638,15 @@ async fn a_sync_claims_an_unclaimed_row_without_touching_its_sign_in_fact() {
86078638 .await
86088639 .unwrap();
86098640 let sync = scratch
8610- .store.sync_member("sub-mert", "MERT@iz.sh", "Mert Yılmaz", false, 0, "UTC+03:00")
8641+ .store
8642+ .sync_member(
8643+ "sub-mert",
8644+ "MERT@iz.sh",
8645+ "Mert Yılmaz",
8646+ false,
8647+ 0,
8648+ "UTC+03:00",
8649+ )
86118650 .await
86128651 .unwrap();
86138652 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() {
86288667 let before = scratch.store.user(&admin_id).await.unwrap().unwrap();
86298668 let seen = before.last_signed_in_at.clone();
86308669 let sync = scratch
8631- .store.sync_member("sub-ada", "ada@iz.sh", "Ada Lovelace", true, 0, "UTC+03:00")
8670+ .store
8671+ .sync_member("sub-ada", "ada@iz.sh", "Ada Lovelace", true, 0, "UTC+03:00")
86328672 .await
86338673 .unwrap();
86348674 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() {
86378677 assert_eq!(after.last_signed_in_at, seen);
86388678 // Losing the flag in the directory drops an Admin to Member.
86398679 scratch
8640- .store.sync_member("sub-ada", "ada@iz.sh", "Ada Lovelace", false, 0, "UTC+03:00")
8680+ .store
8681+ .sync_member(
8682+ "sub-ada",
8683+ "ada@iz.sh",
8684+ "Ada Lovelace",
8685+ false,
8686+ 0,
8687+ "UTC+03:00",
8688+ )
86418689 .await
86428690 .unwrap();
86438691 let demoted = scratch.store.user(&admin_id).await.unwrap().unwrap();
@@ -8649,7 +8697,8 @@ async fn an_agreeing_sync_is_untouched_and_silent() {
86498697 let (scratch, _, _) = workspace_with_admin().await;
86508698 let mut rx = scratch.store.subscribe();
86518699 let sync = scratch
8652- .store.sync_member("sub-ada", "ada@iz.sh", "Ada", true, 0, "UTC")
8700+ .store
8701+ .sync_member("sub-ada", "ada@iz.sh", "Ada", true, 0, "UTC")
86538702 .await
86548703 .unwrap();
86558704 assert_eq!(sync, iz_core::store::MemberSync::Untouched);
@@ -8660,7 +8709,8 @@ async fn an_agreeing_sync_is_untouched_and_silent() {
86608709 async fn a_sync_without_a_workspace_skips_and_writes_nothing() {
86618710 let scratch = Scratch::open().await;
86628711 let sync = scratch
8663- .store.sync_member("sub-mert", "mert@iz.sh", "Mert", false, 0, "UTC+03:00")
8712+ .store
8713+ .sync_member("sub-mert", "mert@iz.sh", "Mert", false, 0, "UTC+03:00")
86648714 .await
86658715 .unwrap();
86668716 assert_eq!(sync, iz_core::store::MemberSync::Skipped);
@@ -8676,7 +8726,8 @@ async fn a_photo_version_bump_refreshes_and_announces() {
86768726 let (scratch, workspace, _admin) = workspace_with_admin().await;
86778727 let mut rx = scratch.store.subscribe();
86788728 let sync = scratch
8679- .store.sync_member("sub-mert", "mert@iz.sh", "Mert", false, 2, "UTC+03:00")
8729+ .store
8730+ .sync_member("sub-mert", "mert@iz.sh", "Mert", false, 2, "UTC+03:00")
86808731 .await
86818732 .unwrap();
86828733 assert_eq!(sync, iz_core::store::MemberSync::Inserted);
@@ -8691,7 +8742,8 @@ async fn a_photo_version_bump_refreshes_and_announces() {
86918742
86928743 // The same version again: the row agrees, and silence is the answer.
86938744 let again = scratch
8694- .store.sync_member("sub-mert", "mert@iz.sh", "Mert", false, 2, "UTC+03:00")
8745+ .store
8746+ .sync_member("sub-mert", "mert@iz.sh", "Mert", false, 2, "UTC+03:00")
86958747 .await
86968748 .unwrap();
86978749 assert_eq!(again, iz_core::store::MemberSync::Untouched);
@@ -8699,7 +8751,8 @@ async fn a_photo_version_bump_refreshes_and_announces() {
86998751
87008752 // im counted another upload: the stamp moves and the move is news.
87018753 let bumped = scratch
8702- .store.sync_member("sub-mert", "mert@iz.sh", "Mert", false, 3, "UTC+03:00")
8754+ .store
8755+ .sync_member("sub-mert", "mert@iz.sh", "Mert", false, 3, "UTC+03:00")
87038756 .await
87048757 .unwrap();
87058758 assert_eq!(bumped, iz_core::store::MemberSync::Refreshed);