//! The committed FTS text in `schema/ddl.rs` IS what `FTS_SOURCES` implies. //! //! `schema/fts.rs`'s own header has claimed for a while that "every CREATE, //! every trigger and every re-index is built from `FTS_SOURCES`" — and until //! this test, nothing held it to that. The 19 virtual tables and 57 triggers //! were written out by hand in `ddl.rs`, so a column added to a source entry //! and forgotten in the DDL gave exactly the failure that header warns about: //! the index created wide and filled narrow, nothing raised, and a search that //! silently never matches. //! //! `ddl.rs` stays ORDINARY SOURCE (step 5i deleted the generator and it is not //! coming back). This is the other way to get one statement of the shape: the //! text is written by hand and a test derives what it must say. use std::collections::BTreeSet; use cotton_store::schema::ddl::SCHEMA_DDL; use cotton_store::schema::fts::FTS_SOURCES; /// Whitespace-insensitive: the committed text is wrapped for reading, and a /// guard that also pinned the line breaks would go red for a reformat. The /// spacing just inside brackets goes the same way, since that is where the /// wrapping lands. fn squashed(sql: &str) -> String { let one_line = sql.split_whitespace().collect::>().join(" "); one_line.replace("( ", "(").replace(" )", ")") } fn declared() -> BTreeSet { SCHEMA_DDL.iter().map(|s| squashed(s)).collect() } fn virtual_table(source: &cotton_store::schema::decl::FtsSource) -> String { squashed(&format!( "CREATE VIRTUAL TABLE {fts} USING fts5({columns}, content = '{table}', \ content_rowid = 'rowid', tokenize = '{tokenize}')", fts = source.fts, table = source.source, columns = source.columns.join(", "), tokenize = source.tokenize, )) } /// The documented external-content delete: a rowid seek, with the OLD values /// FTS5 needs to find the tokens it must retract. Never a `DELETE FROM fts_x /// WHERE = …`, which is a full scan of the index per row — /// O(N) a row, and what this shape exists to have stopped being. fn retract(source: &cotton_store::schema::decl::FtsSource) -> String { format!( "INSERT INTO {fts} ({fts}, rowid, {columns}) VALUES ('delete', old.rowid, {olds});", fts = source.fts, columns = source.columns.join(", "), olds = source .columns .iter() .map(|c| format!("old.{c}")) .collect::>() .join(", "), ) } fn insert(source: &cotton_store::schema::decl::FtsSource) -> String { format!( "INSERT INTO {fts} (rowid, {columns}) VALUES (new.rowid, {news});", fts = source.fts, columns = source.columns.join(", "), news = source .columns .iter() .map(|c| format!("new.{c}")) .collect::>() .join(", "), ) } /// THE FILL FLOOR (v122): every trigger acts only on rows at or above the /// index's floor in `_fts_fill`, so an index filled in the background never /// retracts an entry it does not hold — for an external-content index that is /// corruption, not a no-op. No row reads 0: the index is whole. fn gate(row: &str, fts: &str) -> String { format!("{row}.rowid >= coalesce((SELECT floor FROM _fts_fill WHERE fts = '{fts}'), 0)") } fn triggers(source: &cotton_store::schema::decl::FtsSource) -> Vec { let columns = source.columns.join(", "); // The `WHEN` guard is not decoration: an UPDATE that did not touch indexed // text must not re-index the row, or every unrelated write pays for the // search index. let when = source .columns .iter() .map(|c| format!("new.{c} IS NOT old.{c}")) .collect::>() .join(" OR "); vec![ squashed(&format!( "CREATE TRIGGER {fts}_ai AFTER INSERT ON {table} WHEN {} BEGIN {} END", gate("new", source.fts), insert(source), fts = source.fts, table = source.source )), squashed(&format!( "CREATE TRIGGER {fts}_ad AFTER DELETE ON {table} WHEN {} BEGIN {} END", gate("old", source.fts), retract(source), fts = source.fts, table = source.source )), squashed(&format!( "CREATE TRIGGER {fts}_au AFTER UPDATE OF {columns} ON {table} \ FOR EACH ROW WHEN {} AND ({when}) BEGIN {} {} END", gate("old", source.fts), retract(source), insert(source), fts = source.fts, table = source.source )), ] } /// RULE: Every search index is created exactly as it is declared, and nothing unclaimed exists. #[test] fn every_index_is_created_exactly_as_its_source_entry_says() { let committed = declared(); for source in FTS_SOURCES { let wanted = virtual_table(source); assert!( committed.contains(&wanted), "{}: SCHEMA_DDL does not create the index FTS_SOURCES declares.\n\nexpected:\n {}\n", source.fts, wanted ); } } #[test] fn every_trigger_is_written_exactly_as_its_source_entry_says() { let committed = declared(); for source in FTS_SOURCES { for wanted in triggers(source) { assert!( committed.contains(&wanted), "{}: SCHEMA_DDL does not carry the trigger FTS_SOURCES implies.\n\nexpected:\n \ {}\n", source.fts, wanted ); } } } #[test] fn no_fts_object_in_the_ddl_is_one_no_source_entry_asks_for() { // The other direction, and the reason the two above are not enough: an // index or trigger left behind after a source entry was removed would pass // every check above and go on mirroring a table nothing reads. let expected: BTreeSet = FTS_SOURCES .iter() .flat_map(|s| std::iter::once(virtual_table(s)).chain(triggers(s))) .collect(); for statement in declared() { let is_fts = statement.contains("VIRTUAL TABLE fts_") || statement.contains("TRIGGER fts_"); assert!( !is_fts || expected.contains(&statement), "SCHEMA_DDL carries an FTS object no FTS_SOURCES entry asks for:\n {statement}" ); } } /// RULE: Removing a row never makes the database scan a whole search index. #[test] fn no_delete_trigger_scans_the_index() { // The regression this whole shape exists to prevent, keyed on the SHAPE // rather than on a name: `DELETE FROM fts_x WHERE …` is a full scan of the // index for every deleted row, because an FTS5 table has no index but its // own rowid. for statement in declared() { assert!( !statement.contains("DELETE FROM fts_"), "a trigger deletes from an FTS5 table by predicate, which scans the whole index per \ row — use the external-content retract, INSERT INTO fts_x (fts_x, rowid, …) VALUES \ ('delete', old.rowid, …):\n {statement}" ); } }