-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.sql
More file actions
308 lines (267 loc) · 10.3 KB
/
Copy pathdb.sql
File metadata and controls
308 lines (267 loc) · 10.3 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
-- MagiTerm Database Schema for Supabase
-- This schema includes Row Level Security (RLS) policies to ensure users can only access their own data
-- ============================================================================
-- EXTENSIONS
-- ============================================================================
-- Enable UUID generation
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- User profiles
CREATE TABLE user_profiles (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID NOT NULL UNIQUE REFERENCES auth.users(id) ON DELETE CASCADE,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE TABLE terminal_sessions (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
os TEXT NOT NULL DEFAULT 'ubuntu',
title TEXT NOT NULL DEFAULT 'New Session',
seed INTEGER NOT NULL DEFAULT 0,
cpu TEXT NOT NULL DEFAULT 'Intel Xeon Silver',
gpu TEXT NOT NULL DEFAULT 'NVIDIA RTX 4090',
cwd TEXT NOT NULL DEFAULT '/',
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
-- Commands executed in sessions
CREATE TABLE commands (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
session_id UUID NOT NULL REFERENCES terminal_sessions(id) ON DELETE CASCADE,
input TEXT NOT NULL,
output TEXT,
tokens_in INTEGER NOT NULL DEFAULT 0,
tokens_out INTEGER NOT NULL DEFAULT 0,
latency_ms INTEGER NOT NULL DEFAULT 0,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
-- Terminal events for streaming/logging
CREATE TABLE terminal_events (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
session_id UUID NOT NULL REFERENCES terminal_sessions(id) ON DELETE CASCADE,
kind TEXT NOT NULL,
data JSONB NOT NULL,
ts TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
-- ============================================================================
-- INDEXES
-- ============================================================================
-- Optimize queries by user and creation time
CREATE INDEX idx_user_profiles_user_id ON user_profiles(user_id);
CREATE INDEX idx_terminal_sessions_user_created ON terminal_sessions(user_id, created_at DESC);
CREATE INDEX idx_terminal_sessions_user_id ON terminal_sessions(user_id);
CREATE INDEX idx_commands_session_id ON commands(session_id);
CREATE INDEX idx_terminal_events_session_ts ON terminal_events(session_id, ts DESC);
-- ============================================================================
-- TRIGGERS
-- ============================================================================
-- Auto-update updated_at timestamp on user_profiles
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER update_user_profiles_updated_at
BEFORE UPDATE ON user_profiles
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_terminal_sessions_updated_at
BEFORE UPDATE ON terminal_sessions
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
-- ============================================================================
-- ROW LEVEL SECURITY (RLS)
-- ============================================================================
-- Enable RLS on all tables
ALTER TABLE user_profiles ENABLE ROW LEVEL SECURITY;
ALTER TABLE terminal_sessions ENABLE ROW LEVEL SECURITY;
ALTER TABLE commands ENABLE ROW LEVEL SECURITY;
ALTER TABLE terminal_events ENABLE ROW LEVEL SECURITY;
-- ============================================================================
-- RLS POLICIES - User Profiles
-- ============================================================================
-- Users can view their own profile
CREATE POLICY "Users can view own profile"
ON user_profiles
FOR SELECT
USING (auth.uid() = user_id);
-- Users can insert their own profile
CREATE POLICY "Users can insert own profile"
ON user_profiles
FOR INSERT
WITH CHECK (auth.uid() = user_id);
-- Users can update their own profile
CREATE POLICY "Users can update own profile"
ON user_profiles
FOR UPDATE
USING (auth.uid() = user_id)
WITH CHECK (auth.uid() = user_id);
-- Users can delete their own profile
CREATE POLICY "Users can delete own profile"
ON user_profiles
FOR DELETE
USING (auth.uid() = user_id);
-- ============================================================================
-- RLS POLICIES - Terminal Sessions
-- ============================================================================
-- Users can view their own sessions
CREATE POLICY "Users can view own sessions"
ON terminal_sessions
FOR SELECT
USING (auth.uid() = user_id);
-- Users can create their own sessions
CREATE POLICY "Users can create own sessions"
ON terminal_sessions
FOR INSERT
WITH CHECK (auth.uid() = user_id);
-- Users can update their own sessions
CREATE POLICY "Users can update own sessions"
ON terminal_sessions
FOR UPDATE
USING (auth.uid() = user_id)
WITH CHECK (auth.uid() = user_id);
-- Users can delete their own sessions
CREATE POLICY "Users can delete own sessions"
ON terminal_sessions
FOR DELETE
USING (auth.uid() = user_id);
-- ============================================================================
-- RLS POLICIES - Commands
-- ============================================================================
-- Users can view commands from their own sessions
CREATE POLICY "Users can view own commands"
ON commands
FOR SELECT
USING (
EXISTS (
SELECT 1 FROM terminal_sessions
WHERE terminal_sessions.id = commands.session_id
AND terminal_sessions.user_id = auth.uid()
)
);
-- Users can insert commands into their own sessions
CREATE POLICY "Users can insert own commands"
ON commands
FOR INSERT
WITH CHECK (
EXISTS (
SELECT 1 FROM terminal_sessions
WHERE terminal_sessions.id = commands.session_id
AND terminal_sessions.user_id = auth.uid()
)
);
-- Users can update commands in their own sessions
CREATE POLICY "Users can update own commands"
ON commands
FOR UPDATE
USING (
EXISTS (
SELECT 1 FROM terminal_sessions
WHERE terminal_sessions.id = commands.session_id
AND terminal_sessions.user_id = auth.uid()
)
)
WITH CHECK (
EXISTS (
SELECT 1 FROM terminal_sessions
WHERE terminal_sessions.id = commands.session_id
AND terminal_sessions.user_id = auth.uid()
)
);
-- Users can delete commands from their own sessions
CREATE POLICY "Users can delete own commands"
ON commands
FOR DELETE
USING (
EXISTS (
SELECT 1 FROM terminal_sessions
WHERE terminal_sessions.id = commands.session_id
AND terminal_sessions.user_id = auth.uid()
)
);
-- ============================================================================
-- RLS POLICIES - Terminal Events
-- ============================================================================
-- Users can view events from their own sessions
CREATE POLICY "Users can view own terminal events"
ON terminal_events
FOR SELECT
USING (
EXISTS (
SELECT 1 FROM terminal_sessions
WHERE terminal_sessions.id = terminal_events.session_id
AND terminal_sessions.user_id = auth.uid()
)
);
-- Users can insert events into their own sessions
CREATE POLICY "Users can insert own terminal events"
ON terminal_events
FOR INSERT
WITH CHECK (
EXISTS (
SELECT 1 FROM terminal_sessions
WHERE terminal_sessions.id = terminal_events.session_id
AND terminal_sessions.user_id = auth.uid()
)
);
-- Users can update events in their own sessions
CREATE POLICY "Users can update own terminal events"
ON terminal_events
FOR UPDATE
USING (
EXISTS (
SELECT 1 FROM terminal_sessions
WHERE terminal_sessions.id = terminal_events.session_id
AND terminal_sessions.user_id = auth.uid()
)
)
WITH CHECK (
EXISTS (
SELECT 1 FROM terminal_sessions
WHERE terminal_sessions.id = terminal_events.session_id
AND terminal_sessions.user_id = auth.uid()
)
);
-- Users can delete events from their own sessions
CREATE POLICY "Users can delete own terminal events"
ON terminal_events
FOR DELETE
USING (
EXISTS (
SELECT 1 FROM terminal_sessions
WHERE terminal_sessions.id = terminal_events.session_id
AND terminal_sessions.user_id = auth.uid()
)
);
-- ============================================================================
-- HELPER FUNCTIONS (Optional)
-- ============================================================================
-- Function to create a user profile automatically when a new user signs up
CREATE OR REPLACE FUNCTION public.handle_new_user()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO public.user_profiles (user_id)
VALUES (NEW.id);
RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- Trigger to auto-create user profile on signup
CREATE TRIGGER on_auth_user_created
AFTER INSERT ON auth.users
FOR EACH ROW
EXECUTE FUNCTION public.handle_new_user();
-- ============================================================================
-- COMMENTS
-- ============================================================================
COMMENT ON TABLE user_profiles IS 'User profile information linked to auth.users';
COMMENT ON TABLE terminal_sessions IS 'Terminal sessions with OS, CPU, GPU configuration';
COMMENT ON TABLE commands IS 'Commands executed in terminal sessions with metrics';
COMMENT ON TABLE terminal_events IS 'Event stream for terminal output and status updates';
COMMENT ON COLUMN terminal_sessions.seed IS 'Random seed for reproducible AI responses';
COMMENT ON COLUMN commands.tokens_in IS 'Number of input tokens processed';
COMMENT ON COLUMN commands.tokens_out IS 'Number of output tokens generated';
COMMENT ON COLUMN commands.latency_ms IS 'Command execution latency in milliseconds';
COMMENT ON COLUMN terminal_events.kind IS 'Event type: token, stderr, status, etc.';
COMMENT ON COLUMN terminal_events.data IS 'Event payload as JSON';