-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.go
More file actions
75 lines (69 loc) · 2.27 KB
/
schema.go
File metadata and controls
75 lines (69 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package graph
import (
"fmt"
"strings"
"github.com/randomcodespace/codeiq/internal/model"
)
// ApplySchema creates the single CodeNode node table plus one REL table per
// EdgeKind. Idempotent — repeated calls are no-ops via `IF NOT EXISTS`.
// Mirrors the implicit label-driven schema Spring Data Neo4j gives the Java
// side; on Kuzu the schema is explicit.
//
// CodeNode is one table backing all 34 NodeKinds — `kind` is a column, not
// a label. Properties round-trip through a JSON-serialised `props` column
// plus a small set of first-class columns we want to index / project on.
func (s *Store) ApplySchema() error {
// GraphMeta stores small key→value strings (e.g., manifest hash for the
// incremental enrich short-circuit). One row per key; PK enforces uniqueness.
metaDDL := `CREATE NODE TABLE IF NOT EXISTS GraphMeta(
meta_key STRING,
value STRING,
PRIMARY KEY(meta_key))`
if _, err := s.Cypher(metaDDL); err != nil {
return fmt.Errorf("graph: create GraphMeta: %w", err)
}
nodeDDL := `CREATE NODE TABLE IF NOT EXISTS CodeNode(
id STRING,
kind STRING,
label STRING,
fqn STRING,
file_path STRING,
line_start INT64,
line_end INT64,
module STRING,
layer STRING,
language STRING,
framework STRING,
confidence STRING,
source STRING,
label_lower STRING,
fqn_lower STRING,
prop_lex_comment STRING,
prop_lex_config_keys STRING,
props STRING,
PRIMARY KEY(id))`
if _, err := s.Cypher(nodeDDL); err != nil {
return fmt.Errorf("graph: create CodeNode: %w", err)
}
// One REL table per EdgeKind. `props` holds the JSON-serialised property
// map; first-class `id`, `confidence`, and `source` columns mirror what
// every detector emits.
for _, ek := range model.AllEdgeKinds() {
ddl := fmt.Sprintf(`CREATE REL TABLE IF NOT EXISTS %s(
FROM CodeNode TO CodeNode,
id STRING,
confidence STRING,
source STRING,
props STRING)`, relTableName(ek))
if _, err := s.Cypher(ddl); err != nil {
return fmt.Errorf("graph: create rel %s: %w", ek, err)
}
}
return nil
}
// relTableName converts an EdgeKind ("calls" -> "CALLS"). Kuzu rel-table
// names are uppercase by convention so the Cypher `:KIND` notation lines up
// with the table name directly.
func relTableName(ek model.EdgeKind) string {
return strings.ToUpper(ek.String())
}