Cotton code
74 lines · 4.4 KB · RustRaw
//! Schema 150: Cotton Chat's disappearing messages (21 Nov 2026). A message//! that disappears names the moment it goes (`meta.cotton.expiresAt`) and a//! chat's timer is its newest timer line (`meta.cotton.timer`); two partial//! indexes let the copy find the messages due and a chat's timer without//! reading anything else. The rise is the indexes alone: a copy stamped 149//! gains them in place, and no column, row or rule moves. Invented rows only.use cotton_store::{    create::{apply_schema, fresh_schema, SchemaObject},    schema::move_intents::{rebuilt_above, INTENTS},    shape_move::{crossing, move_to_current},    Driver,};const INDEXES: [&str; 2] = ["idx_cotton_msg_messages_expires", "idx_cotton_msg_messages_timer"];fn at_149() -> (Driver, Vec<SchemaObject>) {    let had: Vec<SchemaObject> = serde_json::from_str(include_str!("fixtures/schema-v149.json")).unwrap();    assert!(!had.iter().any(|o| INDEXES.contains(&o.name.as_str())), "149 has neither index");    let db = Driver::open_in_memory().unwrap();    apply_schema(&db).unwrap();    for index in INDEXES {        db.exec(&format!("DROP INDEX {index}")).unwrap();    }    db.exec("PRAGMA user_version=149").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_msg_threads(id,workspace_id,kind,title,created_at,updated_at) VALUES ('t','synthetic','group','Invented friends','now','now');INSERT INTO cotton_msg_messages(id,thread_id,workspace_id,direction,channel,origin,body,sent_at,created_at,meta)  VALUES ('m-1','t','synthetic','out','cotton','live','Invented hello','2026-11-21T10:00:00.000Z','now','{}'),         ('m-2','t','synthetic','out','cotton','live','You turned on disappearing messages.','2026-11-21T10:01:00.000Z','now','{\"system\":true,\"cotton\":{\"timer\":604800}}'),         ('m-3','t','synthetic','in','cotton','live','Invented soon gone','2026-11-21T10:02:00.000Z','now','{\"cotton\":{\"expiresAt\":\"2026-11-28T10:02:00.000Z\"}}');";fn plan(db: &Driver, sql: &str) -> Vec<String> {    db.all(&format!("EXPLAIN QUERY PLAN {sql}"), &[]).unwrap().iter().filter_map(|row| row.text("detail").map(str::to_string)).collect()}/// RULE: A copy stamped 149 gains the expiry and timer indexes in place, every message and rowid as it was, and the copy's two questions read them.#[test]fn a_149_copy_gains_the_timer_indexes_and_keeps_every_row() {    let (db, had) = at_149();    db.exec(SEED).unwrap();    let before = db.all("SELECT rowid, * FROM cotton_msg_messages ORDER BY rowid", &[]).unwrap();    let crossing = crossing(&had, &fresh_schema().unwrap()).unwrap();    let later = rebuilt_above(150);    assert!(crossing.rebuild.iter().all(|t| later.contains(t.as_str())) && crossing.replace.is_empty());    // A later rise's own new objects on other tables (153's mail sender index) stand beside these.    let created: Vec<&str> = crossing.create_rest.iter().filter(|o| !later.contains(o.tbl_name.as_str()) && o.tbl_name == "cotton_msg_messages").map(|o| o.name.as_str()).collect();    assert_eq!(created, INDEXES, "only the two indexes are new");    move_to_current(&db).unwrap();    assert_eq!(db.all("SELECT rowid, * FROM cotton_msg_messages ORDER BY rowid", &[]).unwrap(), before);    let due = plan(&db, "SELECT id FROM cotton_msg_messages WHERE json_extract(meta, '$.cotton.expiresAt') IS NOT NULL AND json_extract(meta, '$.cotton.expiresAt') <= '2026-12-01'");    assert!(due.iter().any(|d| d.contains(INDEXES[0])), "{due:?}");    let timer = plan(&db, "SELECT meta FROM cotton_msg_messages WHERE thread_id = 't' AND json_extract(meta, '$.cotton.timer') IS NOT NULL ORDER BY sent_at DESC LIMIT 1");    assert!(timer.iter().any(|d| d.contains(INDEXES[1])), "{timer:?}");    let found: Vec<String> = db        .all("SELECT id FROM cotton_msg_messages WHERE json_extract(meta, '$.cotton.expiresAt') IS NOT NULL AND json_extract(meta, '$.cotton.expiresAt') <= '2026-12-01'", &[])        .unwrap()        .iter()        .map(|r| r.text("id").unwrap().to_string())        .collect();    assert_eq!(found, ["m-3"]);}/// RULE: 150 is one rise, additive, and discards no stored value.#[test]fn the_150_rise_is_two_indexes_and_discards_nothing() {    let rises: Vec<_> = INTENTS.iter().filter(|intent| intent.version == 150).collect();    assert_eq!(rises.len(), 1);    assert!(rises[0].additive && rises[0].losses.is_empty());}