//! Schema 146: Owed's cut and heads read one index (8 Nov 2026). //! `idx_cotton_money_transactions_owed` covers every money row naming a //! person — by workspace, status, trash, person and day, with the columns the //! cut and the heads read — so a whole Owed read never visits the table. The //! rise is the index alone: a copy stamped 145 gains it in place, and no //! column, row or rule moves. use cotton_store::{ create::{apply_schema, fresh_schema, SchemaObject}, schema::move_intents::{rebuilt_above, INTENTS}, shape_move::{crossing, move_to_current}, Driver, }; const INDEX: &str = "idx_cotton_money_transactions_owed"; /// A file of this build as 145 left it: the owed index absent, stamped 145. fn at_145() -> (Driver, Vec) { let had: Vec = serde_json::from_str(include_str!("fixtures/schema-v145.json")).unwrap(); assert!(!had.iter().any(|o| o.name == INDEX), "145 has no owed index"); let db = Driver::open_in_memory().unwrap(); apply_schema(&db).unwrap(); db.exec(&format!("DROP INDEX {INDEX}")).unwrap(); db.exec("PRAGMA user_version=145").unwrap(); (db, had) } /// Synthetic money rows between you and two people, one of them trashed. const SEED: &str = "INSERT INTO cotton_workspaces(id,name,owner_user_id,created_at) VALUES ('synthetic','Synthetic','person','now'); INSERT INTO cotton_people(id,workspace_id,full_name,created_at,updated_at) VALUES ('p-1','synthetic','Invented One','now','now'), ('p-2','synthetic','Invented Two','now','now'); UPDATE cotton_people SET trashed_at = 'now' WHERE id = 'p-2'; INSERT INTO cotton_money_transactions(id,workspace_id,occurred_on,description,amount_minor,direction,currency,status,counterparty_person_id,created_at,updated_at) VALUES ('m-1','synthetic','2026-11-01','Invented lunch',1200,'out','INR','actual','p-1','now','now'), ('m-2','synthetic','2026-11-02','Invented repay',500,'in','INR','actual','p-1','now','now'), ('m-3','synthetic','2026-11-03','Invented gone',700,'out','INR','actual','p-2','now','now'), ('m-4','synthetic','2026-11-04','Invented alone',300,'out','INR','actual',NULL,'now','now');"; fn rows(db: &Driver) -> Vec { db.all("SELECT rowid, * FROM cotton_money_transactions ORDER BY rowid", &[]).unwrap() } /// RULE: A copy stamped 145 gains the owed index in place — created, nothing rebuilt or replaced, every money row and rowid as it was — and the crossing is additive. #[test] fn a_145_copy_gains_the_owed_index_and_keeps_every_row() { let (db, had) = at_145(); db.exec(SEED).unwrap(); let before = rows(&db); let plan = crossing(&had, &fresh_schema().unwrap()).unwrap(); // A later rise's rebuilds stand beside this one's; 146 itself is the index alone. let later = rebuilt_above(146); assert_eq!(plan.rebuild.iter().map(String::as_str).collect::>(), later, "only a later rise rebuilds"); // A later rise's columns stand beside it too: on a table of the file's own (151's _writes.implied), and 152's // on cotton_emails (all his mail on the phone), and 156's on cotton_groups (hidden categories). assert!(plan.add_columns.iter().all(|(table, _)| later.contains(table.as_str()) || table.starts_with('_') || table == "cotton_emails" || table == "cotton_groups") && plan.replace.is_empty(), "the index alone"); // A later rise's own new objects on other tables (150's message indexes) stand beside this one. let created: Vec<&str> = plan.create_rest.iter().filter(|o| !later.contains(o.tbl_name.as_str()) && o.tbl_name == "cotton_money_transactions").map(|o| o.name.as_str()).collect(); assert_eq!(created, [INDEX], "only the owed index is new"); assert!(plan.not_additive().iter().all(|why| later.iter().any(|table| why.contains(table))), "{:?}", plan.not_additive()); let report = move_to_current(&db).unwrap(); assert!(report.created.iter().any(|created| created.contains(INDEX)), "{report}"); assert_eq!(rows(&db), before, "every row and rowid stays"); let sql = db .get("SELECT sql FROM sqlite_master WHERE type = 'index' AND name = ?", &[INDEX.to_string().into()]) .unwrap() .expect("the copy holds the owed index") .text("sql") .unwrap() .to_string(); let fresh = fresh_schema().unwrap().into_iter().find(|o| o.name == INDEX).unwrap(); assert_eq!(sql, fresh.sql, "in a fresh file's bytes"); // The person's rows read off it alone, in the index's order. let plan: Vec = db .all( &format!("EXPLAIN QUERY PLAN SELECT id, occurred_on, description FROM cotton_money_transactions INDEXED BY {INDEX} WHERE workspace_id = 'synthetic' AND status = 'actual' AND trashed_at IS NULL AND counterparty_person_id IS NOT NULL"), &[], ) .unwrap() .iter() .filter_map(|row| row.text("detail").map(str::to_string)) .collect(); assert!(plan.iter().any(|step| step.contains(&format!("COVERING INDEX {INDEX}"))), "{plan:?}"); } /// RULE: 146 is one rise, additive, and discards no stored value. #[test] fn the_146_rise_is_an_index_and_discards_nothing() { let rises: Vec<_> = INTENTS.iter().filter(|intent| intent.version == 146).collect(); assert_eq!(rises.len(), 1); assert!(rises[0].additive && rises[0].losses.is_empty()); }