Cotton code
46 lines · 2.6 KB · RustRaw
//! Which copies an update moves in place (21 Nov 2026): a phone's copy of the//! object whose rise to this build only ADDS is moved where it stands, every//! row kept (`shape_move::only_adds`); any other is made again from the//! object. Every live rise is add-only by rule, so 148 and 149 copies move;//! a 141 copy (tables whose columns changed) is rebuilt, so it is not one.use cotton_store::{create::apply_schema, shape_move::only_adds, Driver};fn this_build_without(names: &[&str], stamp: u32) -> Driver {    let db = Driver::open_in_memory().unwrap();    apply_schema(&db).unwrap();    for name in names {        let kind: String = db.get("SELECT type FROM sqlite_master WHERE name = ?", &[rusqlite::types::Value::Text((*name).into())]).unwrap().and_then(|row| row.text("type").map(str::to_string)).unwrap();        db.exec(&format!("DROP {} IF EXISTS \"{name}\"", kind.to_uppercase())).unwrap();    }    db.exec(&format!("PRAGMA user_version={stamp}")).unwrap();    db}/// RULE: A copy at 149 or 148 only gains objects on the way to this build, so it is moved in place; one at this build, above it or at 141 is not.#[test]fn copies_whose_rise_only_adds_are_the_ones_moved_in_place() {    let indexes = ["idx_cotton_msg_messages_expires", "idx_cotton_msg_messages_timer"];    assert!(only_adds(&this_build_without(&indexes, 149)).unwrap(), "149: two indexes");    let search = ["fts_chat_ad", "fts_chat_ai", "fts_chat_au", "fts_chat"];    assert!(only_adds(&this_build_without(&[&indexes[..], &search[..]].concat(), 148)).unwrap(), "148: a search index and two indexes");    let current = Driver::open_in_memory().unwrap();    apply_schema(&current).unwrap();    assert!(!only_adds(&current).unwrap(), "this build's own copy has nothing to move");    let newer = this_build_without(&[], cotton_store::migrate::SCHEMA_VERSION + 1);    assert!(!only_adds(&newer).unwrap(), "a newer build's copy is not moved back");    let objects: Vec<cotton_store::create::SchemaObject> = serde_json::from_str(include_str!("fixtures/schema-v141.json")).unwrap();    let old = Driver::open_in_memory().unwrap();    for object in objects.iter().filter(|o| o.kind == "table" && !o.is_fts5_virtual()) {        old.exec(&object.sql).unwrap();    }    for object in objects.iter().filter(|o| o.kind == "table" && o.is_fts5_virtual()) {        old.exec(&object.sql).unwrap();    }    for object in objects.iter().filter(|o| o.kind != "table") {        old.exec(&object.sql).unwrap();    }    old.exec("PRAGMA user_version=141").unwrap();    assert!(!only_adds(&old).unwrap(), "141: tables are rebuilt, so the copy is made again");}