//! Schema 152 (MAIL ALL ON PHONE, 24 Nov 2026) adds twelve columns to `cotton_emails` by ADD COLUMN. A copy kept by a //! build before 152 may already hold them: the object's definition carried them and that build served them on the //! copy (`package_shape::add_served`, `CONSTRAINT served NULL`). The move then met "duplicate column name: to_line", //! stopped, and the copy could not be opened again (Days Dev on emulator-5600, 24 Nov 2026). The move takes such a //! column over: the build's own declaration stands, every value stays, and a served column the build does not own //! stays served. Synthetic rows only. use cotton_store::{ create::{apply_schema, fresh_schema, SchemaObject}, schema::package_shape::{without_added, SERVED_MARK}, shape_move::move_to_current, Driver, }; const TABLE: &str = "cotton_emails"; const SERVED_152: [&str; 12] = [ "to_line", "cc_line", "direction", "message_id", "message_refs", "list_unsubscribe", "list_unsubscribe_post", "list_id", "precedence", "auto_submitted", "gmail_category", "archived_at", ]; /// A file whose mail table is 151's, stamped 151, with 152's columns and one more served on it as the older build /// served them. fn at_151_served() -> Driver { let had: Vec = serde_json::from_str(include_str!("fixtures/schema-v151.json")).unwrap(); let db = Driver::open_in_memory().unwrap(); apply_schema(&db).unwrap(); db.exec("PRAGMA foreign_keys=OFF").unwrap(); let old = had.iter().find(|o| o.kind == "table" && o.name == TABLE).unwrap(); db.exec(&format!("DROP TABLE {TABLE}")).unwrap(); db.exec(&old.sql).unwrap(); for object in had.iter().filter(|o| o.tbl_name == TABLE && o.kind != "table") { let _ = db.exec(&object.sql); } for column in SERVED_152.iter().chain(["media"].iter()) { db.exec(&format!("ALTER TABLE \"{TABLE}\" ADD COLUMN \"{column}\" TEXT {SERVED_MARK}")).unwrap(); } db.exec( "INSERT INTO cotton_emails(id, workspace_id, thread_id, external_id, from_email, sent_at, created_at, to_line, archived_at, media) VALUES ('m-1', 'synthetic', 't-1', 'ext-1', 'shop@invented.example', '2026-11-20T10:00:00Z', 'now', 'Asha Example ', '2026-11-21T10:00:00Z', '[]')", ) .unwrap(); db.exec("PRAGMA foreign_keys=ON").unwrap(); db.exec("PRAGMA user_version=151").unwrap(); db } fn table_sql(db: &Driver) -> String { db.get("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?", &[rusqlite::types::Value::Text(TABLE.into())]) .unwrap() .and_then(|row| row.text("sql").map(str::to_string)) .unwrap() } /// RULE: A copy at 151 that already holds 152's mail columns as served moves to 152: each becomes the build's own column with its value, the table is declared as this build declares it, and a served column the build does not own stays served. #[test] fn a_copy_that_was_served_the_new_columns_moves_and_keeps_every_value() { let db = at_151_served(); let report = move_to_current(&db).expect("the move takes the served columns over instead of stopping"); // Taking a column over rewrites every row of the table: never a move that only makes objects (SCHEMA 155 REVIEW). assert!(report.whole_file_checked, "a move that took a column over ran the whole-file checks: {report}"); let row = db.get("SELECT to_line, archived_at, media FROM cotton_emails WHERE id = 'm-1'", &[]).unwrap().unwrap(); assert_eq!(row.text("to_line"), Some("Asha Example ")); assert_eq!(row.text("archived_at"), Some("2026-11-21T10:00:00Z")); assert_eq!(row.text("media"), Some("[]")); let sql = table_sql(&db); let fresh = fresh_schema().unwrap().into_iter().find(|o| o.kind == "table" && o.name == TABLE).unwrap().sql; let (declared, added) = without_added(&sql); assert_eq!(declared, fresh, "the mail table is this build's, column for column"); assert_eq!(added, vec![format!("\"media\" TEXT {SERVED_MARK}")], "and only the column the build does not own stays served"); assert!(!sql.contains("_shape_move_aside_"), "nothing set aside is left"); }