schema.sql 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. -- CodeGraph SQLite Schema
  2. -- Version 1
  3. -- Schema version tracking
  4. CREATE TABLE IF NOT EXISTS schema_versions (
  5. version INTEGER PRIMARY KEY,
  6. applied_at INTEGER NOT NULL,
  7. description TEXT
  8. );
  9. -- Insert initial version
  10. INSERT INTO schema_versions (version, applied_at, description)
  11. VALUES (1, strftime('%s', 'now') * 1000, 'Initial schema');
  12. -- =============================================================================
  13. -- Core Tables
  14. -- =============================================================================
  15. -- Nodes: Code symbols (functions, classes, variables, etc.)
  16. CREATE TABLE IF NOT EXISTS nodes (
  17. id TEXT PRIMARY KEY,
  18. kind TEXT NOT NULL,
  19. name TEXT NOT NULL,
  20. qualified_name TEXT NOT NULL,
  21. file_path TEXT NOT NULL,
  22. language TEXT NOT NULL,
  23. start_line INTEGER NOT NULL,
  24. end_line INTEGER NOT NULL,
  25. start_column INTEGER NOT NULL,
  26. end_column INTEGER NOT NULL,
  27. docstring TEXT,
  28. signature TEXT,
  29. visibility TEXT,
  30. is_exported INTEGER DEFAULT 0,
  31. is_async INTEGER DEFAULT 0,
  32. is_static INTEGER DEFAULT 0,
  33. is_abstract INTEGER DEFAULT 0,
  34. decorators TEXT, -- JSON array
  35. type_parameters TEXT, -- JSON array
  36. return_type TEXT, -- normalized return/result type name (e.g. C++ method return, for receiver-type inference)
  37. updated_at INTEGER NOT NULL
  38. );
  39. -- Edges: Relationships between nodes
  40. CREATE TABLE IF NOT EXISTS edges (
  41. id INTEGER PRIMARY KEY AUTOINCREMENT,
  42. source TEXT NOT NULL,
  43. target TEXT NOT NULL,
  44. kind TEXT NOT NULL,
  45. metadata TEXT, -- JSON object
  46. line INTEGER,
  47. col INTEGER,
  48. provenance TEXT DEFAULT NULL,
  49. FOREIGN KEY (source) REFERENCES nodes(id) ON DELETE CASCADE,
  50. FOREIGN KEY (target) REFERENCES nodes(id) ON DELETE CASCADE
  51. );
  52. -- Files: Tracked source files
  53. CREATE TABLE IF NOT EXISTS files (
  54. path TEXT PRIMARY KEY,
  55. content_hash TEXT NOT NULL,
  56. language TEXT NOT NULL,
  57. size INTEGER NOT NULL,
  58. modified_at INTEGER NOT NULL,
  59. indexed_at INTEGER NOT NULL,
  60. node_count INTEGER DEFAULT 0,
  61. errors TEXT -- JSON array
  62. );
  63. -- Unresolved References: References that need resolution after full indexing
  64. CREATE TABLE IF NOT EXISTS unresolved_refs (
  65. id INTEGER PRIMARY KEY AUTOINCREMENT,
  66. from_node_id TEXT NOT NULL,
  67. reference_name TEXT NOT NULL,
  68. reference_kind TEXT NOT NULL,
  69. line INTEGER NOT NULL,
  70. col INTEGER NOT NULL,
  71. candidates TEXT, -- JSON array
  72. file_path TEXT NOT NULL DEFAULT '',
  73. language TEXT NOT NULL DEFAULT 'unknown',
  74. FOREIGN KEY (from_node_id) REFERENCES nodes(id) ON DELETE CASCADE
  75. );
  76. -- =============================================================================
  77. -- Indexes for Query Performance
  78. -- =============================================================================
  79. -- Node indexes
  80. CREATE INDEX IF NOT EXISTS idx_nodes_kind ON nodes(kind);
  81. CREATE INDEX IF NOT EXISTS idx_nodes_name ON nodes(name);
  82. CREATE INDEX IF NOT EXISTS idx_nodes_qualified_name ON nodes(qualified_name);
  83. CREATE INDEX IF NOT EXISTS idx_nodes_file_path ON nodes(file_path);
  84. CREATE INDEX IF NOT EXISTS idx_nodes_language ON nodes(language);
  85. CREATE INDEX IF NOT EXISTS idx_nodes_file_line ON nodes(file_path, start_line);
  86. CREATE INDEX IF NOT EXISTS idx_nodes_lower_name ON nodes(lower(name));
  87. -- Full-text search index on node names, docstrings, and signatures
  88. CREATE VIRTUAL TABLE IF NOT EXISTS nodes_fts USING fts5(
  89. id,
  90. name,
  91. qualified_name,
  92. docstring,
  93. signature,
  94. content='nodes',
  95. content_rowid='rowid'
  96. );
  97. -- Triggers to keep FTS index in sync
  98. CREATE TRIGGER IF NOT EXISTS nodes_ai AFTER INSERT ON nodes BEGIN
  99. INSERT INTO nodes_fts(rowid, id, name, qualified_name, docstring, signature)
  100. VALUES (NEW.rowid, NEW.id, NEW.name, NEW.qualified_name, NEW.docstring, NEW.signature);
  101. END;
  102. CREATE TRIGGER IF NOT EXISTS nodes_ad AFTER DELETE ON nodes BEGIN
  103. INSERT INTO nodes_fts(nodes_fts, rowid, id, name, qualified_name, docstring, signature)
  104. VALUES ('delete', OLD.rowid, OLD.id, OLD.name, OLD.qualified_name, OLD.docstring, OLD.signature);
  105. END;
  106. CREATE TRIGGER IF NOT EXISTS nodes_au AFTER UPDATE ON nodes BEGIN
  107. INSERT INTO nodes_fts(nodes_fts, rowid, id, name, qualified_name, docstring, signature)
  108. VALUES ('delete', OLD.rowid, OLD.id, OLD.name, OLD.qualified_name, OLD.docstring, OLD.signature);
  109. INSERT INTO nodes_fts(rowid, id, name, qualified_name, docstring, signature)
  110. VALUES (NEW.rowid, NEW.id, NEW.name, NEW.qualified_name, NEW.docstring, NEW.signature);
  111. END;
  112. -- Prose-word → symbol-name lookup for the prompt hook's graph-derived gate.
  113. -- One row per (segment, name): segment is a lowercased word of a symbol name
  114. -- ("OrderStateMachine" → order, state, machine — see identifier-segments.ts),
  115. -- which lets natural-language prompt words be verified against the graph in
  116. -- any language whose technical nouns are Latin script. File nodes are
  117. -- excluded — a file's basename duplicates the symbols inside it and skews the
  118. -- singleton-vs-cluster rarity statistics. FTS can't serve this lookup (its
  119. -- tokenizer keeps camelCase names as single tokens), so segments are
  120. -- materialized on the node write path.
  121. -- Deletions leave orphan rows ON PURPOSE: rows are PROPOSALS, always
  122. -- re-verified against nodes before being surfaced (CodeGraph.getSegmentMatches),
  123. -- and a full index clears the table at its start. Populated lazily on old
  124. -- databases (empty until the next index/sync heals it).
  125. CREATE TABLE IF NOT EXISTS name_segment_vocab (
  126. segment TEXT NOT NULL,
  127. name TEXT NOT NULL,
  128. PRIMARY KEY (segment, name)
  129. ) WITHOUT ROWID;
  130. -- Edge indexes.
  131. -- idx_edges_source / idx_edges_target are intentionally omitted —
  132. -- the (source, kind) and (target, kind) composites below cover the
  133. -- corresponding source-only / target-only lookups via SQLite's
  134. -- left-prefix scan, so the narrow indexes are dead weight on writes.
  135. -- Migration v4 drops them on existing databases.
  136. CREATE INDEX IF NOT EXISTS idx_edges_kind ON edges(kind);
  137. CREATE INDEX IF NOT EXISTS idx_edges_source_kind ON edges(source, kind);
  138. CREATE INDEX IF NOT EXISTS idx_edges_target_kind ON edges(target, kind);
  139. -- Edge identity uniqueness. An edge IS uniquely (source, target, kind, line,
  140. -- col); insertEdge uses `INSERT OR IGNORE`, but without something UNIQUE to
  141. -- conflict on it behaved like a plain INSERT, so two passes emitting the same
  142. -- edge produced byte-identical duplicate rows that inflated counts and flowed
  143. -- into callers/impact (#1034). IFNULL folds the nullable line/col so
  144. -- coordinate-less edges (synthesized / file-level) dedup too — SQLite treats
  145. -- each NULL as distinct otherwise. Migration v6 dedups existing rows + adds
  146. -- this on older databases.
  147. CREATE UNIQUE INDEX IF NOT EXISTS idx_edges_identity
  148. ON edges(source, target, kind, IFNULL(line, -1), IFNULL(col, -1));
  149. -- File indexes
  150. CREATE INDEX IF NOT EXISTS idx_files_language ON files(language);
  151. CREATE INDEX IF NOT EXISTS idx_files_modified_at ON files(modified_at);
  152. -- Unresolved refs indexes
  153. CREATE INDEX IF NOT EXISTS idx_unresolved_from_node ON unresolved_refs(from_node_id);
  154. CREATE INDEX IF NOT EXISTS idx_unresolved_name ON unresolved_refs(reference_name);
  155. CREATE INDEX IF NOT EXISTS idx_unresolved_file_path ON unresolved_refs(file_path);
  156. CREATE INDEX IF NOT EXISTS idx_unresolved_from_name ON unresolved_refs(from_node_id, reference_name);
  157. CREATE INDEX IF NOT EXISTS idx_edges_provenance ON edges(provenance);
  158. -- Project metadata for version/provenance tracking
  159. CREATE TABLE IF NOT EXISTS project_metadata (
  160. key TEXT PRIMARY KEY,
  161. value TEXT NOT NULL,
  162. updated_at INTEGER NOT NULL
  163. );