use rusqlite::Connection;
pub const GRAPH_SCHEMA_VERSION: i64 = 1;
const GRAPH_TABLES: &[&str] = &["graph_edges", "graph_symbols", "graph_files"];
const VERSION_TABLE: &str = "graph_schema_version";
pub fn migrate_graph_if_stale(conn: &Connection) -> rusqlite::Result<()> {
conn.execute_batch(&format!(
"CREATE TABLE IF NOT EXISTS {VERSION_TABLE} (version INTEGER NOT NULL);"
))?;
let stored: Option<i64> = conn
.query_row(
&format!("SELECT version FROM {VERSION_TABLE} LIMIT 1"),
[],
|r| r.get(0),
)
.ok();
if stored == Some(GRAPH_SCHEMA_VERSION) {
return Ok(()); }
for table in GRAPH_TABLES {
conn.execute_batch(&format!("DROP TABLE IF EXISTS {table};"))?;
}
conn.execute(&format!("DELETE FROM {VERSION_TABLE}"), [])?;
conn.execute(
&format!("INSERT INTO {VERSION_TABLE} (version) VALUES (?1)"),
[GRAPH_SCHEMA_VERSION],
)?;
Ok(())
}
pub fn init_graph_schema(conn: &Connection) -> rusqlite::Result<()> {
conn.execute_batch(
r#"
CREATE TABLE IF NOT EXISTS graph_files (
path TEXT PRIMARY KEY,
lang TEXT NOT NULL,
content_hash TEXT NOT NULL,
mtime INTEGER NOT NULL,
indexed_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS graph_symbols (
id INTEGER PRIMARY KEY AUTOINCREMENT,
file TEXT NOT NULL,
name TEXT NOT NULL,
kind TEXT NOT NULL,
start_line INTEGER NOT NULL,
end_line INTEGER NOT NULL,
signature TEXT,
-- Enclosing type/class/trait for a method (e.g. "ContextAssembler" for
-- ContextAssembler::build), NULL for free functions. Gives same-file
-- same-name defs a qualified identity so they can be disambiguated.
container TEXT
);
CREATE TABLE IF NOT EXISTS graph_edges (
src_symbol INTEGER,
dst_symbol INTEGER,
dst_name TEXT NOT NULL,
kind TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_symbols_name ON graph_symbols(name);
CREATE INDEX IF NOT EXISTS idx_symbols_file ON graph_symbols(file);
CREATE INDEX IF NOT EXISTS idx_edges_src ON graph_edges(src_symbol);
CREATE INDEX IF NOT EXISTS idx_edges_dst ON graph_edges(dst_symbol);
CREATE INDEX IF NOT EXISTS idx_edges_dstname ON graph_edges(dst_name);
"#,
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn init_is_idempotent_and_creates_tables() {
let conn = Connection::open_in_memory().unwrap();
init_graph_schema(&conn).unwrap();
init_graph_schema(&conn).unwrap();
let count: i64 = conn
.query_row(
"SELECT count(*) FROM sqlite_master WHERE type='table' \
AND name IN ('graph_files','graph_symbols','graph_edges')",
[],
|r| r.get(0),
)
.unwrap();
assert_eq!(count, 3);
}
#[test]
fn stale_version_wipes_graph_tables_but_keeps_siblings() {
let conn = Connection::open_in_memory().unwrap();
conn.execute_batch(
"CREATE TABLE run_meta (k TEXT, v TEXT);
INSERT INTO run_meta VALUES ('run', 'keep-me');",
)
.unwrap();
migrate_graph_if_stale(&conn).unwrap();
init_graph_schema(&conn).unwrap();
conn.execute(
"INSERT INTO graph_symbols (file, name, kind, start_line, end_line) \
VALUES ('a.rs','old','function',1,1)",
[],
)
.unwrap();
conn.execute(
&format!("UPDATE {VERSION_TABLE} SET version = ?1"),
[GRAPH_SCHEMA_VERSION - 1],
)
.unwrap();
migrate_graph_if_stale(&conn).unwrap();
init_graph_schema(&conn).unwrap();
let graph_rows: i64 = conn
.query_row("SELECT COUNT(*) FROM graph_symbols", [], |r| r.get(0))
.unwrap();
assert_eq!(graph_rows, 0, "stale graph rows must be wiped");
let sibling: String = conn
.query_row("SELECT v FROM run_meta WHERE k='run'", [], |r| r.get(0))
.unwrap();
assert_eq!(sibling, "keep-me", "non-graph tables must survive the wipe");
let ver: i64 = conn
.query_row(&format!("SELECT version FROM {VERSION_TABLE}"), [], |r| {
r.get(0)
})
.unwrap();
assert_eq!(ver, GRAPH_SCHEMA_VERSION, "version stamped to current");
}
#[test]
fn matching_version_is_a_noop() {
let conn = Connection::open_in_memory().unwrap();
migrate_graph_if_stale(&conn).unwrap();
init_graph_schema(&conn).unwrap();
conn.execute(
"INSERT INTO graph_symbols (file, name, kind, start_line, end_line) \
VALUES ('a.rs','keep','function',1,1)",
[],
)
.unwrap();
migrate_graph_if_stale(&conn).unwrap();
init_graph_schema(&conn).unwrap();
let rows: i64 = conn
.query_row("SELECT COUNT(*) FROM graph_symbols", [], |r| r.get(0))
.unwrap();
assert_eq!(rows, 1, "matching version must preserve indexed rows");
}
}