//! MAIL PREDICATE phase 2 (the review of 248ad7f2c8): the cut predicate's probes are seeks on `_mail_cuts`' (kind, //! key) index, on the store's own schema (157) with the tables the kit really makes (`email_bin_cut`, `email_read_cut`, //! `email_cut`) mirrored in, for live, binned, unread, read, archived and binned_at alike; never a scan of the cuts. use cotton_core::rules::mail_predicate::cuts; use cotton_store::schema::package_tables::{from_definition, fresh_schema, generate}; use serde_json::Value; fn kit_ddl(id: &str) -> Vec { let path = cotton_store::manifest_dir().join(format!("kits/personal/tables/{id}.json")); let definition: Value = serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap(); generate(&from_definition(&definition).unwrap(), false).unwrap() } #[test] fn mail_cuts_on_the_kit_tables() { let db = rusqlite::Connection::open_in_memory().unwrap(); cotton_store::driver::register_mail_functions(&db).unwrap(); for statement in fresh_schema().unwrap() { db.execute_batch(&statement).unwrap_or_else(|e| panic!("{statement}: {e}")); } // Schema 157 gives cotton_emails its landed_at and every copy its _mail_cuts; the kit makes the cut tables and // the phone mirrors them into _mail_cuts. for id in ["email_bin_cut", "email_read_cut", "email_cut"] { for statement in kit_ddl(id) { db.execute_batch(&statement).unwrap_or_else(|e| panic!("{statement}: {e}")); } } for (table, kind) in cuts::KINDS { for statement in cuts::mirror(table, kind) { db.execute_batch(&statement).unwrap_or_else(|e| panic!("{statement}: {e}")); } } for (name, condition) in [("live", cuts::live("e.")), ("binned", cuts::binned("e.")), ("unread", cuts::unread("e.")), ("read", cuts::read("e.")), ("archived", cuts::archived("e.")), ("binned_at", format!("{} IS NOT NULL", cuts::binned_at("e.")))] { let plan: Vec = db .prepare(&format!("EXPLAIN QUERY PLAN SELECT id FROM {} e WHERE e.workspace_id = ? AND {condition}", cuts::EMAILS)) .unwrap() .query_map(["w"], |row| row.get::<_, String>(3)) .unwrap() .map(Result::unwrap) .collect(); assert!(plan.iter().any(|line| line.starts_with("SEARCH c USING INDEX _mail_cuts_key (kind=? AND key=?)") || line.starts_with("SEARCH c USING COVERING INDEX _mail_cuts_key (kind=? AND key=?)")), "{name}: {plan:?}"); assert!(!plan.iter().any(|line| line.starts_with("SCAN c")), "{name}: {plan:?}"); } }