//! Schema 144: the home-screen widgets left the phone (27 Oct 2026), and //! `cotton_widgets`, one row per widget a phone had placed, went with them — //! declared, with every row, and nothing else of the file moves. use cotton_store::{ create::{schema_objects, SchemaObject}, migrate::is_retired_table, schema::{ddl::SCHEMA_VERSION, move_intents::{floor, rebuilt_above, Loss, INTENTS}}, shape_move::move_to_current, Driver, }; /// A fresh file of schema 143, stamped 143. fn at_143() -> Driver { let objects: Vec = serde_json::from_str(include_str!("fixtures/schema-v143.json")).unwrap(); let db = Driver::open_in_memory().unwrap(); let phase = |o: &SchemaObject| match o.kind.as_str() { "table" if o.is_fts5_virtual() => 1, "table" => 0, "index" => 2, _ => 3, }; let mut ordered = objects; ordered.sort_by_key(|o| phase(o)); for object in &ordered { db.exec(&object.sql).unwrap(); } db.exec("PRAGMA user_version = 143").unwrap(); db } const SEED: &str = "INSERT INTO cotton_workspaces(id,name,owner_user_id,created_at) VALUES ('synthetic','Synthetic','person','now'); INSERT INTO cotton_notes(rowid,id,kind,title,body,created_at,updated_at,workspace_id) VALUES (8,'n-old','note','A synthetic note','Nothing real.','now','now','synthetic'); INSERT INTO cotton_widgets(id,workspace_id,device_id,kind,size,last_drawn_at,created_at,updated_at) VALUES ('w-old','synthetic','a-synthetic-phone','shows','medium','now','now','now');"; /// RULE: The 144 rise drops cotton_widgets with every row and what it leaves behind and rebuilds no table of its own; a move from 143 rebuilds only what a later rise declares (145: cotton_msg_messages), and keeps every other row and rowid. #[test] fn the_widgets_table_goes_and_nothing_else_moves() { let db = at_143(); db.exec(SEED).unwrap(); let notes = db.all("SELECT rowid, * FROM cotton_notes ORDER BY rowid", &[]).unwrap(); let report = move_to_current(&db).unwrap(); assert_eq!((report.from, report.to), (143, SCHEMA_VERSION)); assert_eq!(report.dropped, vec!["cotton_widgets".to_string()]); let rebuilt: std::collections::BTreeSet<&str> = report.rebuilt.iter().map(String::as_str).collect(); assert_eq!(rebuilt, rebuilt_above(143), "only what a later rise declares is rebuilt: {:?}", report.rebuilt); assert!( rebuilt.difference(&rebuilt_above(144)).next().is_none(), "144 itself rebuilds nothing: {:?}", rebuilt.difference(&rebuilt_above(144)).collect::>() ); assert!(schema_objects(&db).unwrap().iter().all(|o| o.tbl_name != "cotton_widgets"), "no index of it stays"); assert_eq!(db.all("SELECT rowid, * FROM cotton_notes ORDER BY rowid", &[]).unwrap(), notes); assert!(db.all("PRAGMA foreign_key_check", &[]).unwrap().is_empty()); } /// RULE: 144 is one rise, not additive, that declares the one table it drops — so the floor is at least 144 and a receipt still naming the table is silence. #[test] fn the_144_rise_declares_the_one_table_it_drops() { let rises: Vec<_> = INTENTS.iter().filter(|i| i.version == 144).collect(); assert_eq!(rises.len(), 1); assert!(!rises[0].additive); assert_eq!(rises[0].losses, &[Loss::DropTable { table: "cotton_widgets" }]); assert!(floor() >= 144, "a later non-additive rise may lift the floor, never lower it below 144: {}", floor()); assert!(is_retired_table("cotton_widgets")); }