-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
91 lines (82 loc) 路 4.16 KB
/
Copy pathschema.sql
File metadata and controls
91 lines (82 loc) 路 4.16 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
-- Base table for technical snippets
CREATE TABLE IF NOT EXISTS technical_knowledge (
id TEXT PRIMARY KEY, -- UUID
topic TEXT NOT NULL, -- Subject (e.g., "Laravel Rate Limiting")
content TEXT NOT NULL, -- The technical snippet/tip
category TEXT, -- (e.g., "Backend", "Frontend", "DevOps")
parent_id TEXT REFERENCES technical_knowledge(id) ON DELETE SET NULL, -- Parent snippet for hierarchy
is_validated BOOLEAN DEFAULT FALSE,
last_validated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
source_url TEXT, -- The URL used for validation
confidence_score INTEGER DEFAULT 0, -- 1-10 rating
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- NOTE: column `project_id TEXT REFERENCES projects(id) ON DELETE SET NULL`
-- is added by auto-migration in src/db.ts so it lands on pre-existing databases too.
-- New databases pick it up via the same migration on first init.
-- Full Text Search virtual table for search fallback
CREATE VIRTUAL TABLE IF NOT EXISTS technical_knowledge_fts USING fts5(
id UNINDEXED,
topic,
content,
category
);
-- Keep FTS table in sync on INSERT
CREATE TRIGGER IF NOT EXISTS technical_knowledge_ai AFTER INSERT ON technical_knowledge BEGIN
INSERT INTO technical_knowledge_fts(id, topic, content, category)
VALUES (new.id, new.topic, new.content, new.category);
END;
-- Keep FTS table in sync on DELETE
CREATE TRIGGER IF NOT EXISTS technical_knowledge_ad AFTER DELETE ON technical_knowledge BEGIN
DELETE FROM technical_knowledge_fts WHERE id = old.id;
END;
-- Keep FTS table in sync on UPDATE
CREATE TRIGGER IF NOT EXISTS technical_knowledge_au AFTER UPDATE ON technical_knowledge BEGIN
DELETE FROM technical_knowledge_fts WHERE id = old.id;
INSERT INTO technical_knowledge_fts(id, topic, content, category)
VALUES (new.id, new.topic, new.content, new.category);
END;
-- Embeddings table for semantic vector search
CREATE TABLE IF NOT EXISTS technical_knowledge_embeddings (
id TEXT PRIMARY KEY REFERENCES technical_knowledge(id) ON DELETE CASCADE,
embedding TEXT NOT NULL
);
-- Projects table: identifies a workspace whose Project Context snippets cohere together.
-- root_path is NULL for "proto-projects" created by topic-prefix migration; they get
-- adopted in place the first time their workspace is opened.
CREATE TABLE IF NOT EXISTS projects (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
root_path TEXT UNIQUE,
detected_stack TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_active_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Allows at most one orphan (proto) project per name; once adopted, the partial index no longer applies.
CREATE UNIQUE INDEX IF NOT EXISTS idx_projects_name_orphan
ON projects(name) WHERE root_path IS NULL;
-- Materialized edges between snippets. Replaces on-demand brute-force similarity in /api/graph.
CREATE TABLE IF NOT EXISTS knowledge_relations (
id TEXT PRIMARY KEY,
source_id TEXT NOT NULL REFERENCES technical_knowledge(id) ON DELETE CASCADE,
target_id TEXT NOT NULL REFERENCES technical_knowledge(id) ON DELETE CASCADE,
relation_type TEXT NOT NULL,
weight REAL NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE(source_id, target_id, relation_type)
);
CREATE INDEX IF NOT EXISTS idx_relations_source ON knowledge_relations(source_id);
CREATE INDEX IF NOT EXISTS idx_relations_target ON knowledge_relations(target_id);
-- Context isolation invariant: a relation may exist only when at least one endpoint is
-- generic (project_id IS NULL) OR both endpoints belong to the same project.
CREATE TRIGGER IF NOT EXISTS enforce_relation_isolation
BEFORE INSERT ON knowledge_relations
WHEN (
(SELECT project_id FROM technical_knowledge WHERE id = NEW.source_id) IS NOT NULL
AND (SELECT project_id FROM technical_knowledge WHERE id = NEW.target_id) IS NOT NULL
AND (SELECT project_id FROM technical_knowledge WHERE id = NEW.source_id)
!= (SELECT project_id FROM technical_knowledge WHERE id = NEW.target_id)
)
BEGIN
SELECT RAISE(ABORT, 'context_isolation_violation: cross-project relation forbidden');
END;