1CREATE TABLE "users" (
2 "id" INTEGER PRIMARY KEY AUTOINCREMENT,
3 "github_login" VARCHAR,
4 "admin" BOOLEAN,
5 "email_address" VARCHAR(255) DEFAULT NULL,
6 "invite_code" VARCHAR(64),
7 "invite_count" INTEGER NOT NULL DEFAULT 0,
8 "inviter_id" INTEGER REFERENCES users (id),
9 "connected_once" BOOLEAN NOT NULL DEFAULT false,
10 "created_at" TIMESTAMP NOT NULL DEFAULT now,
11 "metrics_id" TEXT,
12 "github_user_id" INTEGER
13);
14CREATE UNIQUE INDEX "index_users_github_login" ON "users" ("github_login");
15CREATE UNIQUE INDEX "index_invite_code_users" ON "users" ("invite_code");
16CREATE INDEX "index_users_on_email_address" ON "users" ("email_address");
17CREATE INDEX "index_users_on_github_user_id" ON "users" ("github_user_id");
18
19CREATE TABLE "access_tokens" (
20 "id" INTEGER PRIMARY KEY AUTOINCREMENT,
21 "user_id" INTEGER REFERENCES users (id),
22 "hash" VARCHAR(128)
23);
24CREATE INDEX "index_access_tokens_user_id" ON "access_tokens" ("user_id");
25
26CREATE TABLE "contacts" (
27 "id" INTEGER PRIMARY KEY AUTOINCREMENT,
28 "user_id_a" INTEGER REFERENCES users (id) NOT NULL,
29 "user_id_b" INTEGER REFERENCES users (id) NOT NULL,
30 "a_to_b" BOOLEAN NOT NULL,
31 "should_notify" BOOLEAN NOT NULL,
32 "accepted" BOOLEAN NOT NULL
33);
34CREATE UNIQUE INDEX "index_contacts_user_ids" ON "contacts" ("user_id_a", "user_id_b");
35CREATE INDEX "index_contacts_user_id_b" ON "contacts" ("user_id_b");
36
37CREATE TABLE "rooms" (
38 "id" INTEGER PRIMARY KEY AUTOINCREMENT,
39 "live_kit_room" VARCHAR NOT NULL,
40 "enviroment" VARCHAR,
41 "channel_id" INTEGER REFERENCES channels (id) ON DELETE CASCADE
42);
43CREATE UNIQUE INDEX "index_rooms_on_channel_id" ON "rooms" ("channel_id");
44
45CREATE TABLE "projects" (
46 "id" INTEGER PRIMARY KEY AUTOINCREMENT,
47 "room_id" INTEGER REFERENCES rooms (id) ON DELETE CASCADE NOT NULL,
48 "host_user_id" INTEGER REFERENCES users (id) NOT NULL,
49 "host_connection_id" INTEGER,
50 "host_connection_server_id" INTEGER REFERENCES servers (id) ON DELETE CASCADE,
51 "unregistered" BOOLEAN NOT NULL DEFAULT FALSE
52);
53CREATE INDEX "index_projects_on_host_connection_server_id" ON "projects" ("host_connection_server_id");
54CREATE INDEX "index_projects_on_host_connection_id_and_host_connection_server_id" ON "projects" ("host_connection_id", "host_connection_server_id");
55
56CREATE TABLE "worktrees" (
57 "project_id" INTEGER NOT NULL REFERENCES projects (id) ON DELETE CASCADE,
58 "id" INTEGER NOT NULL,
59 "root_name" VARCHAR NOT NULL,
60 "abs_path" VARCHAR NOT NULL,
61 "visible" BOOL NOT NULL,
62 "scan_id" INTEGER NOT NULL,
63 "is_complete" BOOL NOT NULL DEFAULT FALSE,
64 "completed_scan_id" INTEGER NOT NULL,
65 PRIMARY KEY(project_id, id)
66);
67CREATE INDEX "index_worktrees_on_project_id" ON "worktrees" ("project_id");
68
69CREATE TABLE "worktree_entries" (
70 "project_id" INTEGER NOT NULL,
71 "worktree_id" INTEGER NOT NULL,
72 "scan_id" INTEGER NOT NULL,
73 "id" INTEGER NOT NULL,
74 "is_dir" BOOL NOT NULL,
75 "path" VARCHAR NOT NULL,
76 "inode" INTEGER NOT NULL,
77 "mtime_seconds" INTEGER NOT NULL,
78 "mtime_nanos" INTEGER NOT NULL,
79 "is_symlink" BOOL NOT NULL,
80 "is_external" BOOL NOT NULL,
81 "is_ignored" BOOL NOT NULL,
82 "is_deleted" BOOL NOT NULL,
83 "git_status" INTEGER,
84 PRIMARY KEY(project_id, worktree_id, id),
85 FOREIGN KEY(project_id, worktree_id) REFERENCES worktrees (project_id, id) ON DELETE CASCADE
86);
87CREATE INDEX "index_worktree_entries_on_project_id" ON "worktree_entries" ("project_id");
88CREATE INDEX "index_worktree_entries_on_project_id_and_worktree_id" ON "worktree_entries" ("project_id", "worktree_id");
89
90CREATE TABLE "worktree_repositories" (
91 "project_id" INTEGER NOT NULL,
92 "worktree_id" INTEGER NOT NULL,
93 "work_directory_id" INTEGER NOT NULL,
94 "branch" VARCHAR,
95 "scan_id" INTEGER NOT NULL,
96 "is_deleted" BOOL NOT NULL,
97 PRIMARY KEY(project_id, worktree_id, work_directory_id),
98 FOREIGN KEY(project_id, worktree_id) REFERENCES worktrees (project_id, id) ON DELETE CASCADE,
99 FOREIGN KEY(project_id, worktree_id, work_directory_id) REFERENCES worktree_entries (project_id, worktree_id, id) ON DELETE CASCADE
100);
101CREATE INDEX "index_worktree_repositories_on_project_id" ON "worktree_repositories" ("project_id");
102CREATE INDEX "index_worktree_repositories_on_project_id_and_worktree_id" ON "worktree_repositories" ("project_id", "worktree_id");
103
104CREATE TABLE "worktree_settings_files" (
105 "project_id" INTEGER NOT NULL,
106 "worktree_id" INTEGER NOT NULL,
107 "path" VARCHAR NOT NULL,
108 "content" TEXT,
109 PRIMARY KEY(project_id, worktree_id, path),
110 FOREIGN KEY(project_id, worktree_id) REFERENCES worktrees (project_id, id) ON DELETE CASCADE
111);
112CREATE INDEX "index_worktree_settings_files_on_project_id" ON "worktree_settings_files" ("project_id");
113CREATE INDEX "index_worktree_settings_files_on_project_id_and_worktree_id" ON "worktree_settings_files" ("project_id", "worktree_id");
114
115CREATE TABLE "worktree_diagnostic_summaries" (
116 "project_id" INTEGER NOT NULL,
117 "worktree_id" INTEGER NOT NULL,
118 "path" VARCHAR NOT NULL,
119 "language_server_id" INTEGER NOT NULL,
120 "error_count" INTEGER NOT NULL,
121 "warning_count" INTEGER NOT NULL,
122 PRIMARY KEY(project_id, worktree_id, path),
123 FOREIGN KEY(project_id, worktree_id) REFERENCES worktrees (project_id, id) ON DELETE CASCADE
124);
125CREATE INDEX "index_worktree_diagnostic_summaries_on_project_id" ON "worktree_diagnostic_summaries" ("project_id");
126CREATE INDEX "index_worktree_diagnostic_summaries_on_project_id_and_worktree_id" ON "worktree_diagnostic_summaries" ("project_id", "worktree_id");
127
128CREATE TABLE "language_servers" (
129 "id" INTEGER NOT NULL,
130 "project_id" INTEGER NOT NULL REFERENCES projects (id) ON DELETE CASCADE,
131 "name" VARCHAR NOT NULL,
132 PRIMARY KEY(project_id, id)
133);
134CREATE INDEX "index_language_servers_on_project_id" ON "language_servers" ("project_id");
135
136CREATE TABLE "project_collaborators" (
137 "id" INTEGER PRIMARY KEY AUTOINCREMENT,
138 "project_id" INTEGER NOT NULL REFERENCES projects (id) ON DELETE CASCADE,
139 "connection_id" INTEGER NOT NULL,
140 "connection_server_id" INTEGER NOT NULL REFERENCES servers (id) ON DELETE CASCADE,
141 "user_id" INTEGER NOT NULL,
142 "replica_id" INTEGER NOT NULL,
143 "is_host" BOOLEAN NOT NULL
144);
145CREATE INDEX "index_project_collaborators_on_project_id" ON "project_collaborators" ("project_id");
146CREATE UNIQUE INDEX "index_project_collaborators_on_project_id_and_replica_id" ON "project_collaborators" ("project_id", "replica_id");
147CREATE INDEX "index_project_collaborators_on_connection_server_id" ON "project_collaborators" ("connection_server_id");
148CREATE INDEX "index_project_collaborators_on_connection_id" ON "project_collaborators" ("connection_id");
149CREATE UNIQUE INDEX "index_project_collaborators_on_project_id_connection_id_and_server_id" ON "project_collaborators" ("project_id", "connection_id", "connection_server_id");
150
151CREATE TABLE "room_participants" (
152 "id" INTEGER PRIMARY KEY AUTOINCREMENT,
153 "room_id" INTEGER NOT NULL REFERENCES rooms (id),
154 "user_id" INTEGER NOT NULL REFERENCES users (id),
155 "answering_connection_id" INTEGER,
156 "answering_connection_server_id" INTEGER REFERENCES servers (id) ON DELETE CASCADE,
157 "answering_connection_lost" BOOLEAN NOT NULL,
158 "location_kind" INTEGER,
159 "location_project_id" INTEGER,
160 "initial_project_id" INTEGER,
161 "calling_user_id" INTEGER NOT NULL REFERENCES users (id),
162 "calling_connection_id" INTEGER NOT NULL,
163 "calling_connection_server_id" INTEGER REFERENCES servers (id) ON DELETE SET NULL,
164 "participant_index" INTEGER,
165 "role" TEXT
166);
167CREATE UNIQUE INDEX "index_room_participants_on_user_id" ON "room_participants" ("user_id");
168CREATE INDEX "index_room_participants_on_room_id" ON "room_participants" ("room_id");
169CREATE INDEX "index_room_participants_on_answering_connection_server_id" ON "room_participants" ("answering_connection_server_id");
170CREATE INDEX "index_room_participants_on_calling_connection_server_id" ON "room_participants" ("calling_connection_server_id");
171CREATE INDEX "index_room_participants_on_answering_connection_id" ON "room_participants" ("answering_connection_id");
172CREATE UNIQUE INDEX "index_room_participants_on_answering_connection_id_and_answering_connection_server_id" ON "room_participants" ("answering_connection_id", "answering_connection_server_id");
173
174CREATE TABLE "servers" (
175 "id" INTEGER PRIMARY KEY AUTOINCREMENT,
176 "environment" VARCHAR NOT NULL
177);
178
179CREATE TABLE "followers" (
180 "id" INTEGER PRIMARY KEY AUTOINCREMENT,
181 "room_id" INTEGER NOT NULL REFERENCES rooms (id) ON DELETE CASCADE,
182 "project_id" INTEGER NOT NULL REFERENCES projects (id) ON DELETE CASCADE,
183 "leader_connection_server_id" INTEGER NOT NULL REFERENCES servers (id) ON DELETE CASCADE,
184 "leader_connection_id" INTEGER NOT NULL,
185 "follower_connection_server_id" INTEGER NOT NULL REFERENCES servers (id) ON DELETE CASCADE,
186 "follower_connection_id" INTEGER NOT NULL
187);
188CREATE UNIQUE INDEX
189 "index_followers_on_project_id_and_leader_connection_server_id_and_leader_connection_id_and_follower_connection_server_id_and_follower_connection_id"
190ON "followers" ("project_id", "leader_connection_server_id", "leader_connection_id", "follower_connection_server_id", "follower_connection_id");
191CREATE INDEX "index_followers_on_room_id" ON "followers" ("room_id");
192
193CREATE TABLE "channels" (
194 "id" INTEGER PRIMARY KEY AUTOINCREMENT,
195 "name" VARCHAR NOT NULL,
196 "created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
197 "visibility" VARCHAR NOT NULL,
198 "parent_path" TEXT
199);
200
201CREATE INDEX "index_channels_on_parent_path" ON "channels" ("parent_path");
202
203CREATE TABLE IF NOT EXISTS "channel_chat_participants" (
204 "id" INTEGER PRIMARY KEY AUTOINCREMENT,
205 "user_id" INTEGER NOT NULL REFERENCES users (id),
206 "channel_id" INTEGER NOT NULL REFERENCES channels (id) ON DELETE CASCADE,
207 "connection_id" INTEGER NOT NULL,
208 "connection_server_id" INTEGER NOT NULL REFERENCES servers (id) ON DELETE CASCADE
209);
210CREATE INDEX "index_channel_chat_participants_on_channel_id" ON "channel_chat_participants" ("channel_id");
211
212CREATE TABLE IF NOT EXISTS "channel_messages" (
213 "id" INTEGER PRIMARY KEY AUTOINCREMENT,
214 "channel_id" INTEGER NOT NULL REFERENCES channels (id) ON DELETE CASCADE,
215 "sender_id" INTEGER NOT NULL REFERENCES users (id),
216 "body" TEXT NOT NULL,
217 "sent_at" TIMESTAMP,
218 "nonce" BLOB NOT NULL
219);
220CREATE INDEX "index_channel_messages_on_channel_id" ON "channel_messages" ("channel_id");
221CREATE UNIQUE INDEX "index_channel_messages_on_sender_id_nonce" ON "channel_messages" ("sender_id", "nonce");
222
223CREATE TABLE "channel_message_mentions" (
224 "message_id" INTEGER NOT NULL REFERENCES channel_messages (id) ON DELETE CASCADE,
225 "start_offset" INTEGER NOT NULL,
226 "end_offset" INTEGER NOT NULL,
227 "user_id" INTEGER NOT NULL REFERENCES users (id) ON DELETE CASCADE,
228 PRIMARY KEY(message_id, start_offset)
229);
230
231CREATE TABLE "channel_members" (
232 "id" INTEGER PRIMARY KEY AUTOINCREMENT,
233 "channel_id" INTEGER NOT NULL REFERENCES channels (id) ON DELETE CASCADE,
234 "user_id" INTEGER NOT NULL REFERENCES users (id) ON DELETE CASCADE,
235 "admin" BOOLEAN NOT NULL DEFAULT false,
236 "role" VARCHAR,
237 "accepted" BOOLEAN NOT NULL DEFAULT false,
238 "updated_at" TIMESTAMP NOT NULL DEFAULT now
239);
240
241CREATE UNIQUE INDEX "index_channel_members_on_channel_id_and_user_id" ON "channel_members" ("channel_id", "user_id");
242
243CREATE TABLE "buffers" (
244 "id" INTEGER PRIMARY KEY AUTOINCREMENT,
245 "channel_id" INTEGER NOT NULL REFERENCES channels (id) ON DELETE CASCADE,
246 "epoch" INTEGER NOT NULL DEFAULT 0
247);
248
249CREATE INDEX "index_buffers_on_channel_id" ON "buffers" ("channel_id");
250
251CREATE TABLE "buffer_operations" (
252 "buffer_id" INTEGER NOT NULL REFERENCES buffers (id) ON DELETE CASCADE,
253 "epoch" INTEGER NOT NULL,
254 "replica_id" INTEGER NOT NULL,
255 "lamport_timestamp" INTEGER NOT NULL,
256 "value" BLOB NOT NULL,
257 PRIMARY KEY(buffer_id, epoch, lamport_timestamp, replica_id)
258);
259
260CREATE TABLE "buffer_snapshots" (
261 "buffer_id" INTEGER NOT NULL REFERENCES buffers (id) ON DELETE CASCADE,
262 "epoch" INTEGER NOT NULL,
263 "text" TEXT NOT NULL,
264 "operation_serialization_version" INTEGER NOT NULL,
265 PRIMARY KEY(buffer_id, epoch)
266);
267
268CREATE TABLE "channel_buffer_collaborators" (
269 "id" INTEGER PRIMARY KEY AUTOINCREMENT,
270 "channel_id" INTEGER NOT NULL REFERENCES channels (id) ON DELETE CASCADE,
271 "connection_id" INTEGER NOT NULL,
272 "connection_server_id" INTEGER NOT NULL REFERENCES servers (id) ON DELETE CASCADE,
273 "connection_lost" BOOLEAN NOT NULL DEFAULT false,
274 "user_id" INTEGER NOT NULL REFERENCES users (id) ON DELETE CASCADE,
275 "replica_id" INTEGER NOT NULL
276);
277
278CREATE INDEX "index_channel_buffer_collaborators_on_channel_id" ON "channel_buffer_collaborators" ("channel_id");
279CREATE UNIQUE INDEX "index_channel_buffer_collaborators_on_channel_id_and_replica_id" ON "channel_buffer_collaborators" ("channel_id", "replica_id");
280CREATE INDEX "index_channel_buffer_collaborators_on_connection_server_id" ON "channel_buffer_collaborators" ("connection_server_id");
281CREATE INDEX "index_channel_buffer_collaborators_on_connection_id" ON "channel_buffer_collaborators" ("connection_id");
282CREATE UNIQUE INDEX "index_channel_buffer_collaborators_on_channel_id_connection_id_and_server_id" ON "channel_buffer_collaborators" ("channel_id", "connection_id", "connection_server_id");
283
284
285CREATE TABLE "feature_flags" (
286 "id" INTEGER PRIMARY KEY AUTOINCREMENT,
287 "flag" TEXT NOT NULL UNIQUE
288);
289
290CREATE INDEX "index_feature_flags" ON "feature_flags" ("id");
291
292
293CREATE TABLE "user_features" (
294 "user_id" INTEGER NOT NULL REFERENCES users (id) ON DELETE CASCADE,
295 "feature_id" INTEGER NOT NULL REFERENCES feature_flags (id) ON DELETE CASCADE,
296 PRIMARY KEY (user_id, feature_id)
297);
298
299CREATE UNIQUE INDEX "index_user_features_user_id_and_feature_id" ON "user_features" ("user_id", "feature_id");
300CREATE INDEX "index_user_features_on_user_id" ON "user_features" ("user_id");
301CREATE INDEX "index_user_features_on_feature_id" ON "user_features" ("feature_id");
302
303
304CREATE TABLE "observed_buffer_edits" (
305 "user_id" INTEGER NOT NULL REFERENCES users (id) ON DELETE CASCADE,
306 "buffer_id" INTEGER NOT NULL REFERENCES buffers (id) ON DELETE CASCADE,
307 "epoch" INTEGER NOT NULL,
308 "lamport_timestamp" INTEGER NOT NULL,
309 "replica_id" INTEGER NOT NULL,
310 PRIMARY KEY (user_id, buffer_id)
311);
312
313CREATE UNIQUE INDEX "index_observed_buffers_user_and_buffer_id" ON "observed_buffer_edits" ("user_id", "buffer_id");
314
315CREATE TABLE IF NOT EXISTS "observed_channel_messages" (
316 "user_id" INTEGER NOT NULL REFERENCES users (id) ON DELETE CASCADE,
317 "channel_id" INTEGER NOT NULL REFERENCES channels (id) ON DELETE CASCADE,
318 "channel_message_id" INTEGER NOT NULL,
319 PRIMARY KEY (user_id, channel_id)
320);
321
322CREATE UNIQUE INDEX "index_observed_channel_messages_user_and_channel_id" ON "observed_channel_messages" ("user_id", "channel_id");
323
324CREATE TABLE "notification_kinds" (
325 "id" INTEGER PRIMARY KEY AUTOINCREMENT,
326 "name" VARCHAR NOT NULL
327);
328
329CREATE UNIQUE INDEX "index_notification_kinds_on_name" ON "notification_kinds" ("name");
330
331CREATE TABLE "notifications" (
332 "id" INTEGER PRIMARY KEY AUTOINCREMENT,
333 "created_at" TIMESTAMP NOT NULL default CURRENT_TIMESTAMP,
334 "recipient_id" INTEGER NOT NULL REFERENCES users (id) ON DELETE CASCADE,
335 "kind" INTEGER NOT NULL REFERENCES notification_kinds (id),
336 "entity_id" INTEGER,
337 "content" TEXT,
338 "is_read" BOOLEAN NOT NULL DEFAULT FALSE,
339 "response" BOOLEAN
340);
341
342CREATE INDEX
343 "index_notifications_on_recipient_id_is_read_kind_entity_id"
344 ON "notifications"
345 ("recipient_id", "is_read", "kind", "entity_id");