1+ -- ===================================
2+ -- Package Management System Tables
3+ -- ===================================
4+
5+ -- Content storage (immutable, content-addressed)
6+ CREATE TABLE IF NOT EXISTS matter_content_v0 (
7+ hash TEXT PRIMARY KEY ,
8+ content_type TEXT NOT NULL CHECK (content_type IN (' function' , ' type' , ' value' )),
9+ content BLOB NOT NULL ,
10+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
11+ );
12+
13+ CREATE INDEX idx_matter_content_type ON matter_content_v0(content_type);
14+
15+ -- Name pointers (mutable pointers to content)
16+ CREATE TABLE IF NOT EXISTS matter_names_v0 (
17+ id UUID PRIMARY KEY DEFAULT (uuid()),
18+ owner TEXT NOT NULL ,
19+ modules TEXT NOT NULL ,
20+ name TEXT NOT NULL ,
21+ hash TEXT NOT NULL REFERENCES matter_content_v0(hash),
22+ deprecated BOOLEAN NOT NULL DEFAULT FALSE,
23+ deprecation_reason TEXT ,
24+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
25+ updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
26+ UNIQUE(owner, modules, name)
27+ );
28+
29+ CREATE INDEX idx_matter_names_location ON matter_names_v0(owner, modules, name);
30+ CREATE INDEX idx_matter_names_hash ON matter_names_v0(hash);
31+
32+ -- Instances
33+ CREATE TABLE IF NOT EXISTS matter_instances_v0 (
34+ id UUID PRIMARY KEY DEFAULT (uuid()),
35+ name TEXT NOT NULL UNIQUE,
36+ instance_type TEXT NOT NULL CHECK (instance_type IN (' LocalCLI' , ' HttpServer' )),
37+ last_sync_at TIMESTAMP ,
38+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
39+ );
40+
41+ -- Sessions
42+ CREATE TABLE IF NOT EXISTS matter_sessions_v0 (
43+ id UUID PRIMARY KEY DEFAULT (uuid()),
44+ name TEXT NOT NULL ,
45+ intent TEXT NOT NULL ,
46+ owner TEXT NOT NULL ,
47+ current_patch_id UUID,
48+ state TEXT NOT NULL CHECK (state IN (' Active' , ' Suspended' , ' Completed' )),
49+ workspace_state BLOB, -- Serialized workspace state
50+ started_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
51+ last_active_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
52+ );
53+
54+ CREATE INDEX idx_matter_sessions_owner ON matter_sessions_v0(owner);
55+ CREATE INDEX idx_matter_sessions_state ON matter_sessions_v0(state);
56+
57+ -- Patches
58+ CREATE TABLE IF NOT EXISTS matter_patches_v0 (
59+ id UUID PRIMARY KEY DEFAULT (uuid()),
60+ intent TEXT NOT NULL ,
61+ author TEXT NOT NULL ,
62+ status TEXT NOT NULL CHECK (status IN (' Draft' , ' Ready' , ' Applied' , ' Rejected' )),
63+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
64+ updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
65+ metadata BLOB -- Serialized patch metadata (todos, tags, etc)
66+ );
67+
68+ CREATE INDEX idx_matter_patches_author ON matter_patches_v0(author);
69+ CREATE INDEX idx_matter_patches_status ON matter_patches_v0(status);
70+
71+ -- Operations
72+ CREATE TABLE IF NOT EXISTS matter_ops_v0 (
73+ id UUID PRIMARY KEY DEFAULT (uuid()),
74+ patch_id UUID NOT NULL REFERENCES matter_patches_v0(id),
75+ sequence_num INTEGER NOT NULL ,
76+ op_type TEXT NOT NULL ,
77+ op_data BLOB NOT NULL , -- Serialized Op.T
78+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
79+ UNIQUE(patch_id, sequence_num)
80+ );
81+
82+ CREATE INDEX idx_matter_ops_patch ON matter_ops_v0(patch_id);
83+
84+ -- Patch dependencies
85+ CREATE TABLE IF NOT EXISTS matter_patch_dependencies_v0 (
86+ patch_id UUID NOT NULL REFERENCES matter_patches_v0(id),
87+ depends_on_patch_id UUID NOT NULL REFERENCES matter_patches_v0(id),
88+ PRIMARY KEY (patch_id, depends_on_patch_id)
89+ );
90+
91+ -- Session patches (which patches belong to which session)
92+ CREATE TABLE IF NOT EXISTS matter_session_patches_v0 (
93+ session_id UUID NOT NULL REFERENCES matter_sessions_v0(id),
94+ patch_id UUID NOT NULL REFERENCES matter_patches_v0(id),
95+ added_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
96+ PRIMARY KEY (session_id, patch_id)
97+ );
98+
99+ CREATE INDEX idx_matter_session_patches_session ON matter_session_patches_v0(session_id);
100+ CREATE INDEX idx_matter_session_patches_patch ON matter_session_patches_v0(patch_id);
0 commit comments