Cotton code
94 lines · 6.2 KB · RustRaw
//! Schema 142: the object decides, so the copy keeps no rule the object does//! not know on the rows it serves. Four went, each by rebuilding its table in//! place with every row and rowid: `cotton_documents.status_changed_at` and//! `cotton_events.collection_id` take null, and `cotton_notes`'//! managed_by/external_id CHECK and `cotton_money_accounts`' kind CHECK go//! (27 Oct 2026: a phone held 259 served receipts parked behind them).use cotton_store::{    create::{apply_schema, SchemaObject},    schema::move_intents::{rebuilt_above, INTENTS},    shape_move::{crossing, move_to_current},    Driver,};const TABLES: [&str; 4] = ["cotton_documents", "cotton_events", "cotton_notes", "cotton_money_accounts"];/// A file of this build whose four tables are 141's, stamped 141.fn at_141() -> (Driver, Vec<SchemaObject>) {    let had: Vec<SchemaObject> = serde_json::from_str(include_str!("fixtures/schema-v141.json")).unwrap();    let db = Driver::open_in_memory().unwrap();    apply_schema(&db).unwrap();    db.exec("PRAGMA foreign_keys=OFF").unwrap();    for table in TABLES {        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);        }    }    db.exec("PRAGMA foreign_keys=ON").unwrap();    db.exec("PRAGMA user_version=141").unwrap();    (db, had)}const SEED: &str = "INSERT INTO cotton_workspaces(id,name,owner_user_id,created_at) VALUES ('synthetic','Synthetic','person','now');INSERT INTO cotton_collections(rowid,id,workspace_id,kind,name,created_at) VALUES (5,'calendar','synthetic','calendar','Synthetic','now');INSERT INTO cotton_documents(rowid,id,workspace_id,title,original_name,mime,byte_size,sha256,storage_path,status,status_changed_at,created_at,updated_at)  VALUES (31,'d-old','synthetic','A synthetic statement','statement.pdf','application/pdf',10,'0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef','synthetic/statement.pdf','ready','now','now','now');INSERT INTO cotton_events(rowid,id,collection_id,title,starts_at,created_at,updated_at)  VALUES (17,'e-old','calendar','A synthetic meeting','2026-10-28T09:00:00.000Z','now','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_money_accounts(rowid,id,workspace_id,kind,subtype,name,currency,created_at,updated_at)  VALUES (3,'a-old','synthetic','asset','bank','A synthetic account','INR','now','now');INSERT INTO _parked(op_id,direction,class,reason,first_at,tried_at,tries,bytes,tbl,row_id)  VALUES ('op-refused','pull','conflict','NOT NULL constraint failed: cotton_documents.status_changed_at','then','then',3000,X'7b7d','cotton_documents','d-new');";fn rows(db: &Driver, table: &str) -> Vec<cotton_store::driver::Row> {    db.all(&format!("SELECT rowid, * FROM {table} ORDER BY rowid"), &[]).unwrap()}/// RULE: The 142 rise rebuilds the four tables in place — every old row and rowid kept — and afterwards a row the object serves lands with the rules only the copy kept gone: a document with no status_changed_at, an event in no calendar, a session's note with no external id, an account of the object's kind; and what the old rules refused is due again.#[test]fn the_rules_only_the_copy_kept_go_and_every_row_stays() {    let (db, had) = at_141();    db.exec(SEED).unwrap();    let before: Vec<_> = TABLES.iter().map(|table| rows(&db, table)).collect();    let plan = crossing(&had, &cotton_store::create::fresh_schema().unwrap()).unwrap();    let rebuilt: std::collections::BTreeSet<&str> = plan.rebuild.iter().map(String::as_str).collect();    assert_eq!(        rebuilt.difference(&rebuilt_above(143)).copied().collect::<std::collections::BTreeSet<_>>(),        TABLES.into_iter().collect(),        "the four, and beside them only a later rise's rebuilds"    );    assert_eq!(rebuilt_above(141), rebuilt, "REBUILDS says so");    move_to_current(&db).unwrap();    let after: Vec<_> = TABLES.iter().map(|table| rows(&db, table)).collect();    assert_eq!(after, before, "every row and rowid stays");    assert_eq!(        db.get("SELECT tried_at FROM _parked WHERE op_id = 'op-refused'", &[]).unwrap().unwrap().text("tried_at"),        None,        "what the old rules refused is due again"    );    db.exec("INSERT INTO cotton_documents(id,workspace_id,title,original_name,mime,byte_size,sha256,storage_path,created_at,updated_at)        VALUES ('d-new','synthetic','Filed on the object','filed.pdf','application/pdf',10,'fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210','synthetic/filed.pdf','now','now')").unwrap();    db.exec("INSERT INTO cotton_events(id,title,starts_at,created_at,updated_at) VALUES ('e-new','From Google','2026-10-29T09:00:00.000Z','now','now')").unwrap();    db.exec("INSERT INTO cotton_notes(id,kind,title,body,author_kind,author_label,managed_by,created_at,updated_at,workspace_id)        VALUES ('n-new','note','A session note','Nothing real.','agent','A synthetic session','A synthetic session','now','now','synthetic')").unwrap();    db.exec("INSERT INTO cotton_money_accounts(id,workspace_id,kind,subtype,name,currency,created_at,updated_at) VALUES ('a-new','synthetic','bank','bank','The object''s kind','INR','now','now')").unwrap();    // 143 went on to drop the key a named calendar kept: an event naming one    // this copy never holds lands too (events_are_the_databases_shape.rs).    db.exec("INSERT INTO cotton_events(id,collection_id,title,starts_at,created_at,updated_at) VALUES ('e-named','no-such-calendar','x','2026-10-29T09:00:00.000Z','now','now')").unwrap();    assert!(db.all("PRAGMA foreign_key_check", &[]).unwrap().is_empty());}/// RULE: 142 is one rise and not additive: it rebuilds four synced tables, and it discards no stored value.#[test]fn the_142_rise_rebuilds_and_discards_nothing() {    let rises: Vec<_> = INTENTS.iter().filter(|i| i.version == 142).collect();    assert_eq!(rises.len(), 1);    assert!(!rises[0].additive && rises[0].losses.is_empty());}