use std::collections::HashMap;
use khive_runtime::secret_gate::SESSION_MIRROR_STORED_TARGET;
use super::SESSION_SCHEMA_PLAN_STMTS;
fn parse_ddl_columns(stmt: &str) -> Option<(String, Vec<String>)> {
const MARKER: &str = "CREATE TABLE IF NOT EXISTS ";
if !stmt.starts_with(MARKER) {
return None;
}
let rest = &stmt[MARKER.len()..];
let open = rest.find('(')?;
let close = rest.rfind(')')?;
let table = rest[..open].trim().to_string();
let columns = rest[open + 1..close]
.split(',')
.filter_map(|col| col.split_whitespace().next().map(str::to_string))
.collect();
Some((table, columns))
}
#[test]
fn session_mirror_stored_target_names_only_real_columns() {
let mut tables: HashMap<String, Vec<String>> = HashMap::new();
for stmt in &SESSION_SCHEMA_PLAN_STMTS {
if let Some((table, columns)) = parse_ddl_columns(stmt) {
tables.insert(table, columns);
}
}
assert!(
tables.contains_key("sessions") && tables.contains_key("session_messages"),
"DDL parser must find both tables in the schema plan: {tables:?}"
);
for entry in SESSION_MIRROR_STORED_TARGET.split(',') {
let entry = entry
.trim()
.trim_start_matches("and ")
.trim_end_matches('.');
let (table, column) = entry
.split_once('.')
.unwrap_or_else(|| panic!("stored-target entry {entry:?} is not table.column shaped"));
let columns = tables.get(table).unwrap_or_else(|| {
panic!("{table} named in SESSION_MIRROR_STORED_TARGET is not a real table")
});
assert!(
columns.iter().any(|c| c == column),
"{table}.{column} named in SESSION_MIRROR_STORED_TARGET does not exist; \
real {table} columns: {columns:?}"
);
}
}