Skip to main content

gitcortex_store/
schema.rs

1use gitcortex_core::error::{GitCortexError, Result};
2use kuzu::Connection;
3
4use crate::branch;
5
6// ── Table name helpers ────────────────────────────────────────────────────────
7
8/// Node table name for a branch: `{sanitized_branch}_nodes`
9pub fn node_table(branch: &str) -> String {
10    format!("{}_nodes", branch::sanitize(branch))
11}
12
13/// Relationship table name for a branch: `{sanitized_branch}_edges`
14pub fn edge_table(branch: &str) -> String {
15    format!("{}_edges", branch::sanitize(branch))
16}
17
18// ── DDL ───────────────────────────────────────────────────────────────────────
19
20/// Create the node and edge tables for `branch` if they don't already exist.
21/// Safe to call on every operation — idempotent.
22pub fn ensure_branch(conn: &mut Connection, branch: &str) -> Result<()> {
23    let nt = node_table(branch);
24    let et = edge_table(branch);
25
26    conn.query(&format!(
27        "CREATE NODE TABLE IF NOT EXISTS {nt} (\
28            id             STRING, \
29            kind           STRING, \
30            name           STRING, \
31            qualified_name STRING, \
32            file           STRING, \
33            start_line     INT64,  \
34            end_line       INT64,  \
35            loc            INT64,  \
36            visibility     STRING, \
37            is_async       BOOL,   \
38            is_unsafe      BOOL,   \
39            PRIMARY KEY(id)\
40        )"
41    ))
42    .map_err(|e| GitCortexError::Store(format!("create node table: {e}")))?;
43
44    conn.query(&format!(
45        "CREATE REL TABLE IF NOT EXISTS {et} (\
46            FROM {nt} TO {nt},\
47            kind STRING\
48        )"
49    ))
50    .map_err(|e| GitCortexError::Store(format!("create edge table: {e}")))?;
51
52    Ok(())
53}