20221109000000_test_schema.sql

  1CREATE TABLE "users" (
  2    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
  3    "github_login" VARCHAR,
  4    "admin" BOOLEAN,
  5    "email_address" VARCHAR(255) DEFAULT NULL,
  6    "name" TEXT,
  7    "invite_code" VARCHAR(64),
  8    "invite_count" INTEGER NOT NULL DEFAULT 0,
  9    "inviter_id" INTEGER REFERENCES users (id),
 10    "connected_once" BOOLEAN NOT NULL DEFAULT false,
 11    "created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
 12    "metrics_id" TEXT,
 13    "github_user_id" INTEGER NOT NULL,
 14    "accepted_tos_at" TIMESTAMP WITHOUT TIME ZONE,
 15    "github_user_created_at" TIMESTAMP WITHOUT TIME ZONE,
 16    "custom_llm_monthly_allowance_in_cents" INTEGER
 17);
 18CREATE UNIQUE INDEX "index_users_github_login" ON "users" ("github_login");
 19CREATE UNIQUE INDEX "index_invite_code_users" ON "users" ("invite_code");
 20CREATE INDEX "index_users_on_email_address" ON "users" ("email_address");
 21CREATE UNIQUE INDEX "index_users_on_github_user_id" ON "users" ("github_user_id");
 22
 23CREATE TABLE "access_tokens" (
 24    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
 25    "user_id" INTEGER REFERENCES users (id),
 26    "impersonated_user_id" INTEGER REFERENCES users (id),
 27    "hash" VARCHAR(128)
 28);
 29CREATE INDEX "index_access_tokens_user_id" ON "access_tokens" ("user_id");
 30
 31CREATE TABLE "contacts" (
 32    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
 33    "user_id_a" INTEGER REFERENCES users (id) NOT NULL,
 34    "user_id_b" INTEGER REFERENCES users (id) NOT NULL,
 35    "a_to_b" BOOLEAN NOT NULL,
 36    "should_notify" BOOLEAN NOT NULL,
 37    "accepted" BOOLEAN NOT NULL
 38);
 39CREATE UNIQUE INDEX "index_contacts_user_ids" ON "contacts" ("user_id_a", "user_id_b");
 40CREATE INDEX "index_contacts_user_id_b" ON "contacts" ("user_id_b");
 41
 42CREATE TABLE "rooms" (
 43    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
 44    "live_kit_room" VARCHAR NOT NULL,
 45    "environment" VARCHAR,
 46    "channel_id" INTEGER REFERENCES channels (id) ON DELETE CASCADE
 47);
 48CREATE UNIQUE INDEX "index_rooms_on_channel_id" ON "rooms" ("channel_id");
 49
 50CREATE TABLE "projects" (
 51    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
 52    "room_id" INTEGER REFERENCES rooms (id) ON DELETE CASCADE,
 53    "host_user_id" INTEGER REFERENCES users (id),
 54    "host_connection_id" INTEGER,
 55    "host_connection_server_id" INTEGER REFERENCES servers (id) ON DELETE CASCADE,
 56    "unregistered" BOOLEAN NOT NULL DEFAULT FALSE
 57);
 58CREATE INDEX "index_projects_on_host_connection_server_id" ON "projects" ("host_connection_server_id");
 59CREATE INDEX "index_projects_on_host_connection_id_and_host_connection_server_id" ON "projects" ("host_connection_id", "host_connection_server_id");
 60
 61CREATE TABLE "worktrees" (
 62    "project_id" INTEGER NOT NULL REFERENCES projects (id) ON DELETE CASCADE,
 63    "id" INTEGER NOT NULL,
 64    "root_name" VARCHAR NOT NULL,
 65    "abs_path" VARCHAR NOT NULL,
 66    "visible" BOOL NOT NULL,
 67    "scan_id" INTEGER NOT NULL,
 68    "is_complete" BOOL NOT NULL DEFAULT FALSE,
 69    "completed_scan_id" INTEGER NOT NULL,
 70    PRIMARY KEY(project_id, id)
 71);
 72CREATE INDEX "index_worktrees_on_project_id" ON "worktrees" ("project_id");
 73
 74CREATE TABLE "worktree_entries" (
 75    "project_id" INTEGER NOT NULL,
 76    "worktree_id" INTEGER NOT NULL,
 77    "scan_id" INTEGER NOT NULL,
 78    "id" INTEGER NOT NULL,
 79    "is_dir" BOOL NOT NULL,
 80    "path" VARCHAR NOT NULL,
 81    "canonical_path" TEXT,
 82    "inode" INTEGER NOT NULL,
 83    "mtime_seconds" INTEGER NOT NULL,
 84    "mtime_nanos" INTEGER NOT NULL,
 85    "is_external" BOOL NOT NULL,
 86    "is_ignored" BOOL NOT NULL,
 87    "is_deleted" BOOL NOT NULL,
 88    "git_status" INTEGER,
 89    "is_fifo" BOOL NOT NULL,
 90    PRIMARY KEY(project_id, worktree_id, id),
 91    FOREIGN KEY(project_id, worktree_id) REFERENCES worktrees (project_id, id) ON DELETE CASCADE
 92);
 93CREATE INDEX "index_worktree_entries_on_project_id" ON "worktree_entries" ("project_id");
 94CREATE INDEX "index_worktree_entries_on_project_id_and_worktree_id" ON "worktree_entries" ("project_id", "worktree_id");
 95
 96CREATE TABLE "worktree_repositories" (
 97    "project_id" INTEGER NOT NULL,
 98    "worktree_id" INTEGER NOT NULL,
 99    "work_directory_id" INTEGER NOT NULL,
100    "branch" VARCHAR,
101    "scan_id" INTEGER NOT NULL,
102    "is_deleted" BOOL NOT NULL,
103    "current_merge_conflicts" VARCHAR,
104    "branch_summary" VARCHAR,
105    PRIMARY KEY(project_id, worktree_id, work_directory_id),
106    FOREIGN KEY(project_id, worktree_id) REFERENCES worktrees (project_id, id) ON DELETE CASCADE,
107    FOREIGN KEY(project_id, worktree_id, work_directory_id) REFERENCES worktree_entries (project_id, worktree_id, id) ON DELETE CASCADE
108);
109CREATE INDEX "index_worktree_repositories_on_project_id" ON "worktree_repositories" ("project_id");
110CREATE INDEX "index_worktree_repositories_on_project_id_and_worktree_id" ON "worktree_repositories" ("project_id", "worktree_id");
111
112CREATE TABLE "worktree_repository_statuses" (
113    "project_id" INTEGER NOT NULL,
114    "worktree_id" INT8 NOT NULL,
115    "work_directory_id" INT8 NOT NULL,
116    "repo_path" VARCHAR NOT NULL,
117    "status" INT8 NOT NULL,
118    "status_kind" INT4 NOT NULL,
119    "first_status" INT4 NULL,
120    "second_status" INT4 NULL,
121    "scan_id" INT8 NOT NULL,
122    "is_deleted" BOOL NOT NULL,
123    PRIMARY KEY(project_id, worktree_id, work_directory_id, repo_path),
124    FOREIGN KEY(project_id, worktree_id) REFERENCES worktrees (project_id, id) ON DELETE CASCADE,
125    FOREIGN KEY(project_id, worktree_id, work_directory_id) REFERENCES worktree_entries (project_id, worktree_id, id) ON DELETE CASCADE
126);
127CREATE INDEX "index_wt_repos_statuses_on_project_id" ON "worktree_repository_statuses" ("project_id");
128CREATE INDEX "index_wt_repos_statuses_on_project_id_and_wt_id" ON "worktree_repository_statuses" ("project_id", "worktree_id");
129CREATE INDEX "index_wt_repos_statuses_on_project_id_and_wt_id_and_wd_id" ON "worktree_repository_statuses" ("project_id", "worktree_id", "work_directory_id");
130
131CREATE TABLE "worktree_settings_files" (
132    "project_id" INTEGER NOT NULL,
133    "worktree_id" INTEGER NOT NULL,
134    "path" VARCHAR NOT NULL,
135    "content" TEXT,
136    "kind" VARCHAR,
137    PRIMARY KEY(project_id, worktree_id, path),
138    FOREIGN KEY(project_id, worktree_id) REFERENCES worktrees (project_id, id) ON DELETE CASCADE
139);
140CREATE INDEX "index_worktree_settings_files_on_project_id" ON "worktree_settings_files" ("project_id");
141CREATE INDEX "index_worktree_settings_files_on_project_id_and_worktree_id" ON "worktree_settings_files" ("project_id", "worktree_id");
142
143CREATE TABLE "worktree_diagnostic_summaries" (
144    "project_id" INTEGER NOT NULL,
145    "worktree_id" INTEGER NOT NULL,
146    "path" VARCHAR NOT NULL,
147    "language_server_id" INTEGER NOT NULL,
148    "error_count" INTEGER NOT NULL,
149    "warning_count" INTEGER NOT NULL,
150    PRIMARY KEY(project_id, worktree_id, path),
151    FOREIGN KEY(project_id, worktree_id) REFERENCES worktrees (project_id, id) ON DELETE CASCADE
152);
153CREATE INDEX "index_worktree_diagnostic_summaries_on_project_id" ON "worktree_diagnostic_summaries" ("project_id");
154CREATE INDEX "index_worktree_diagnostic_summaries_on_project_id_and_worktree_id" ON "worktree_diagnostic_summaries" ("project_id", "worktree_id");
155
156CREATE TABLE "language_servers" (
157    "id" INTEGER NOT NULL,
158    "project_id" INTEGER NOT NULL REFERENCES projects (id) ON DELETE CASCADE,
159    "name" VARCHAR NOT NULL,
160    PRIMARY KEY(project_id, id)
161);
162CREATE INDEX "index_language_servers_on_project_id" ON "language_servers" ("project_id");
163
164CREATE TABLE "project_collaborators" (
165    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
166    "project_id" INTEGER NOT NULL REFERENCES projects (id) ON DELETE CASCADE,
167    "connection_id" INTEGER NOT NULL,
168    "connection_server_id" INTEGER NOT NULL REFERENCES servers (id) ON DELETE CASCADE,
169    "user_id" INTEGER NOT NULL,
170    "replica_id" INTEGER NOT NULL,
171    "is_host" BOOLEAN NOT NULL
172);
173CREATE INDEX "index_project_collaborators_on_project_id" ON "project_collaborators" ("project_id");
174CREATE UNIQUE INDEX "index_project_collaborators_on_project_id_and_replica_id" ON "project_collaborators" ("project_id", "replica_id");
175CREATE INDEX "index_project_collaborators_on_connection_server_id" ON "project_collaborators" ("connection_server_id");
176CREATE INDEX "index_project_collaborators_on_connection_id" ON "project_collaborators" ("connection_id");
177CREATE UNIQUE INDEX "index_project_collaborators_on_project_id_connection_id_and_server_id" ON "project_collaborators" ("project_id", "connection_id", "connection_server_id");
178
179CREATE TABLE "room_participants" (
180    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
181    "room_id" INTEGER NOT NULL REFERENCES rooms (id),
182    "user_id" INTEGER NOT NULL REFERENCES users (id),
183    "answering_connection_id" INTEGER,
184    "answering_connection_server_id" INTEGER REFERENCES servers (id) ON DELETE CASCADE,
185    "answering_connection_lost" BOOLEAN NOT NULL,
186    "location_kind" INTEGER,
187    "location_project_id" INTEGER,
188    "initial_project_id" INTEGER,
189    "calling_user_id" INTEGER NOT NULL REFERENCES users (id),
190    "calling_connection_id" INTEGER NOT NULL,
191    "calling_connection_server_id" INTEGER REFERENCES servers (id) ON DELETE SET NULL,
192    "participant_index" INTEGER,
193    "role" TEXT,
194    "in_call" BOOLEAN NOT NULL DEFAULT FALSE
195);
196CREATE UNIQUE INDEX "index_room_participants_on_user_id" ON "room_participants" ("user_id");
197CREATE INDEX "index_room_participants_on_room_id" ON "room_participants" ("room_id");
198CREATE INDEX "index_room_participants_on_answering_connection_server_id" ON "room_participants" ("answering_connection_server_id");
199CREATE INDEX "index_room_participants_on_calling_connection_server_id" ON "room_participants" ("calling_connection_server_id");
200CREATE INDEX "index_room_participants_on_answering_connection_id" ON "room_participants" ("answering_connection_id");
201CREATE UNIQUE INDEX "index_room_participants_on_answering_connection_id_and_answering_connection_server_id" ON "room_participants" ("answering_connection_id", "answering_connection_server_id");
202
203CREATE TABLE "servers" (
204    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
205    "environment" VARCHAR NOT NULL
206);
207
208CREATE TABLE "followers" (
209    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
210    "room_id" INTEGER NOT NULL REFERENCES rooms (id) ON DELETE CASCADE,
211    "project_id" INTEGER NOT NULL REFERENCES projects (id) ON DELETE CASCADE,
212    "leader_connection_server_id" INTEGER NOT NULL REFERENCES servers (id) ON DELETE CASCADE,
213    "leader_connection_id" INTEGER NOT NULL,
214    "follower_connection_server_id" INTEGER NOT NULL REFERENCES servers (id) ON DELETE CASCADE,
215    "follower_connection_id" INTEGER NOT NULL
216);
217CREATE UNIQUE INDEX
218    "index_followers_on_project_id_and_leader_connection_server_id_and_leader_connection_id_and_follower_connection_server_id_and_follower_connection_id"
219ON "followers" ("project_id", "leader_connection_server_id", "leader_connection_id", "follower_connection_server_id", "follower_connection_id");
220CREATE INDEX "index_followers_on_room_id" ON "followers" ("room_id");
221
222CREATE TABLE "channels" (
223    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
224    "name" VARCHAR NOT NULL,
225    "created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
226    "visibility" VARCHAR NOT NULL,
227    "parent_path" TEXT NOT NULL,
228    "requires_zed_cla" BOOLEAN NOT NULL DEFAULT FALSE
229);
230
231CREATE INDEX "index_channels_on_parent_path" ON "channels" ("parent_path");
232
233CREATE TABLE IF NOT EXISTS "channel_chat_participants" (
234    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
235    "user_id" INTEGER NOT NULL REFERENCES users (id),
236    "channel_id" INTEGER NOT NULL REFERENCES channels (id) ON DELETE CASCADE,
237    "connection_id" INTEGER NOT NULL,
238    "connection_server_id" INTEGER NOT NULL REFERENCES servers (id) ON DELETE CASCADE
239);
240CREATE INDEX "index_channel_chat_participants_on_channel_id" ON "channel_chat_participants" ("channel_id");
241
242CREATE TABLE IF NOT EXISTS "channel_messages" (
243    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
244    "channel_id" INTEGER NOT NULL REFERENCES channels (id) ON DELETE CASCADE,
245    "sender_id" INTEGER NOT NULL REFERENCES users (id),
246    "body" TEXT NOT NULL,
247    "sent_at" TIMESTAMP,
248    "edited_at" TIMESTAMP,
249    "nonce" BLOB NOT NULL,
250    "reply_to_message_id" INTEGER DEFAULT NULL
251);
252CREATE INDEX "index_channel_messages_on_channel_id" ON "channel_messages" ("channel_id");
253CREATE UNIQUE INDEX "index_channel_messages_on_sender_id_nonce" ON "channel_messages" ("sender_id", "nonce");
254
255CREATE TABLE "channel_message_mentions" (
256    "message_id" INTEGER NOT NULL REFERENCES channel_messages (id) ON DELETE CASCADE,
257    "start_offset" INTEGER NOT NULL,
258    "end_offset" INTEGER NOT NULL,
259    "user_id" INTEGER NOT NULL REFERENCES users (id) ON DELETE CASCADE,
260    PRIMARY KEY(message_id, start_offset)
261);
262
263CREATE TABLE "channel_members" (
264    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
265    "channel_id" INTEGER NOT NULL REFERENCES channels (id) ON DELETE CASCADE,
266    "user_id" INTEGER NOT NULL REFERENCES users (id) ON DELETE CASCADE,
267    "role" VARCHAR NOT NULL,
268    "accepted" BOOLEAN NOT NULL DEFAULT false,
269    "updated_at" TIMESTAMP NOT NULL DEFAULT now
270);
271
272CREATE UNIQUE INDEX "index_channel_members_on_channel_id_and_user_id" ON "channel_members" ("channel_id", "user_id");
273
274CREATE TABLE "buffers" (
275    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
276    "channel_id" INTEGER NOT NULL REFERENCES channels (id) ON DELETE CASCADE,
277    "epoch" INTEGER NOT NULL DEFAULT 0,
278    "latest_operation_epoch" INTEGER,
279    "latest_operation_replica_id" INTEGER,
280    "latest_operation_lamport_timestamp" INTEGER
281);
282
283CREATE INDEX "index_buffers_on_channel_id" ON "buffers" ("channel_id");
284
285CREATE TABLE "buffer_operations" (
286    "buffer_id" INTEGER NOT NULL REFERENCES buffers (id) ON DELETE CASCADE,
287    "epoch" INTEGER NOT NULL,
288    "replica_id" INTEGER NOT NULL,
289    "lamport_timestamp" INTEGER NOT NULL,
290    "value" BLOB NOT NULL,
291    PRIMARY KEY(buffer_id, epoch, lamport_timestamp, replica_id)
292);
293
294CREATE TABLE "buffer_snapshots" (
295    "buffer_id" INTEGER NOT NULL REFERENCES buffers (id) ON DELETE CASCADE,
296    "epoch" INTEGER NOT NULL,
297    "text" TEXT NOT NULL,
298    "operation_serialization_version" INTEGER NOT NULL,
299    PRIMARY KEY(buffer_id, epoch)
300);
301
302CREATE TABLE "channel_buffer_collaborators" (
303    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
304    "channel_id" INTEGER NOT NULL REFERENCES channels (id) ON DELETE CASCADE,
305    "connection_id" INTEGER NOT NULL,
306    "connection_server_id" INTEGER NOT NULL REFERENCES servers (id) ON DELETE CASCADE,
307    "connection_lost" BOOLEAN NOT NULL DEFAULT false,
308    "user_id" INTEGER NOT NULL REFERENCES users (id) ON DELETE CASCADE,
309    "replica_id" INTEGER NOT NULL
310);
311
312CREATE INDEX "index_channel_buffer_collaborators_on_channel_id" ON "channel_buffer_collaborators" ("channel_id");
313CREATE UNIQUE INDEX "index_channel_buffer_collaborators_on_channel_id_and_replica_id" ON "channel_buffer_collaborators" ("channel_id", "replica_id");
314CREATE INDEX "index_channel_buffer_collaborators_on_connection_server_id" ON "channel_buffer_collaborators" ("connection_server_id");
315CREATE INDEX "index_channel_buffer_collaborators_on_connection_id" ON "channel_buffer_collaborators" ("connection_id");
316CREATE 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");
317
318
319CREATE TABLE "feature_flags" (
320    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
321    "flag" TEXT NOT NULL UNIQUE,
322    "enabled_for_all" BOOLEAN NOT NULL DEFAULT false
323);
324
325CREATE INDEX "index_feature_flags" ON "feature_flags" ("id");
326
327
328CREATE TABLE "user_features" (
329    "user_id" INTEGER NOT NULL REFERENCES users (id) ON DELETE CASCADE,
330    "feature_id" INTEGER NOT NULL REFERENCES feature_flags (id) ON DELETE CASCADE,
331    PRIMARY KEY (user_id, feature_id)
332);
333
334CREATE UNIQUE INDEX "index_user_features_user_id_and_feature_id" ON "user_features" ("user_id", "feature_id");
335CREATE INDEX "index_user_features_on_user_id" ON "user_features" ("user_id");
336CREATE INDEX "index_user_features_on_feature_id" ON "user_features" ("feature_id");
337
338
339CREATE TABLE "observed_buffer_edits" (
340    "user_id" INTEGER NOT NULL REFERENCES users (id) ON DELETE CASCADE,
341    "buffer_id" INTEGER NOT NULL REFERENCES buffers (id) ON DELETE CASCADE,
342    "epoch" INTEGER NOT NULL,
343    "lamport_timestamp" INTEGER NOT NULL,
344    "replica_id" INTEGER NOT NULL,
345    PRIMARY KEY (user_id, buffer_id)
346);
347
348CREATE UNIQUE INDEX "index_observed_buffers_user_and_buffer_id" ON "observed_buffer_edits" ("user_id", "buffer_id");
349
350CREATE TABLE IF NOT EXISTS "observed_channel_messages" (
351    "user_id" INTEGER NOT NULL REFERENCES users (id) ON DELETE CASCADE,
352    "channel_id" INTEGER NOT NULL REFERENCES channels (id) ON DELETE CASCADE,
353    "channel_message_id" INTEGER NOT NULL,
354    PRIMARY KEY (user_id, channel_id)
355);
356
357CREATE UNIQUE INDEX "index_observed_channel_messages_user_and_channel_id" ON "observed_channel_messages" ("user_id", "channel_id");
358
359CREATE TABLE "notification_kinds" (
360    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
361    "name" VARCHAR NOT NULL
362);
363
364CREATE UNIQUE INDEX "index_notification_kinds_on_name" ON "notification_kinds" ("name");
365
366CREATE TABLE "notifications" (
367    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
368    "created_at" TIMESTAMP NOT NULL default CURRENT_TIMESTAMP,
369    "recipient_id" INTEGER NOT NULL REFERENCES users (id) ON DELETE CASCADE,
370    "kind" INTEGER NOT NULL REFERENCES notification_kinds (id),
371    "entity_id" INTEGER,
372    "content" TEXT,
373    "is_read" BOOLEAN NOT NULL DEFAULT FALSE,
374    "response" BOOLEAN
375);
376
377CREATE INDEX
378    "index_notifications_on_recipient_id_is_read_kind_entity_id"
379    ON "notifications"
380    ("recipient_id", "is_read", "kind", "entity_id");
381
382CREATE TABLE contributors (
383    user_id INTEGER REFERENCES users(id),
384    signed_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
385    PRIMARY KEY (user_id)
386);
387
388CREATE TABLE extensions (
389    id INTEGER PRIMARY KEY AUTOINCREMENT,
390    external_id TEXT NOT NULL,
391    name TEXT NOT NULL,
392    latest_version TEXT NOT NULL,
393    total_download_count INTEGER NOT NULL DEFAULT 0
394);
395
396CREATE TABLE extension_versions (
397    extension_id INTEGER REFERENCES extensions(id),
398    version TEXT NOT NULL,
399    published_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
400    authors TEXT NOT NULL,
401    repository TEXT NOT NULL,
402    description TEXT NOT NULL,
403    schema_version INTEGER NOT NULL DEFAULT 0,
404    wasm_api_version TEXT,
405    download_count INTEGER NOT NULL DEFAULT 0,
406    provides_themes BOOLEAN NOT NULL DEFAULT FALSE,
407    provides_icon_themes BOOLEAN NOT NULL DEFAULT FALSE,
408    provides_languages BOOLEAN NOT NULL DEFAULT FALSE,
409    provides_grammars BOOLEAN NOT NULL DEFAULT FALSE,
410    provides_language_servers BOOLEAN NOT NULL DEFAULT FALSE,
411    provides_context_servers BOOLEAN NOT NULL DEFAULT FALSE,
412    provides_slash_commands BOOLEAN NOT NULL DEFAULT FALSE,
413    provides_indexed_docs_providers BOOLEAN NOT NULL DEFAULT FALSE,
414    provides_snippets BOOLEAN NOT NULL DEFAULT FALSE,
415    PRIMARY KEY (extension_id, version)
416);
417
418CREATE UNIQUE INDEX "index_extensions_external_id" ON "extensions" ("external_id");
419CREATE INDEX "index_extensions_total_download_count" ON "extensions" ("total_download_count");
420
421CREATE TABLE rate_buckets (
422    user_id INT NOT NULL,
423    rate_limit_name VARCHAR(255) NOT NULL,
424    token_count INT NOT NULL,
425    last_refill TIMESTAMP WITHOUT TIME ZONE NOT NULL,
426    PRIMARY KEY (user_id, rate_limit_name),
427    FOREIGN KEY (user_id) REFERENCES users(id)
428);
429CREATE INDEX idx_user_id_rate_limit ON rate_buckets (user_id, rate_limit_name);
430
431CREATE TABLE IF NOT EXISTS billing_preferences (
432    id INTEGER PRIMARY KEY AUTOINCREMENT,
433    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
434    user_id INTEGER NOT NULL REFERENCES users(id),
435    max_monthly_llm_usage_spending_in_cents INTEGER NOT NULL
436);
437
438CREATE UNIQUE INDEX "uix_billing_preferences_on_user_id" ON billing_preferences (user_id);
439
440CREATE TABLE IF NOT EXISTS billing_customers (
441    id INTEGER PRIMARY KEY AUTOINCREMENT,
442    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
443    user_id INTEGER NOT NULL REFERENCES users(id),
444    has_overdue_invoices BOOLEAN NOT NULL DEFAULT FALSE,
445    stripe_customer_id TEXT NOT NULL
446);
447
448CREATE UNIQUE INDEX "uix_billing_customers_on_user_id" ON billing_customers (user_id);
449CREATE UNIQUE INDEX "uix_billing_customers_on_stripe_customer_id" ON billing_customers (stripe_customer_id);
450
451CREATE TABLE IF NOT EXISTS billing_subscriptions (
452    id INTEGER PRIMARY KEY AUTOINCREMENT,
453    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
454    billing_customer_id INTEGER NOT NULL REFERENCES billing_customers(id),
455    stripe_subscription_id TEXT NOT NULL,
456    stripe_subscription_status TEXT NOT NULL,
457    stripe_cancel_at TIMESTAMP,
458    stripe_cancellation_reason TEXT
459);
460
461CREATE INDEX "ix_billing_subscriptions_on_billing_customer_id" ON billing_subscriptions (billing_customer_id);
462CREATE UNIQUE INDEX "uix_billing_subscriptions_on_stripe_subscription_id" ON billing_subscriptions (stripe_subscription_id);
463
464CREATE TABLE IF NOT EXISTS processed_stripe_events (
465    stripe_event_id TEXT PRIMARY KEY,
466    stripe_event_type TEXT NOT NULL,
467    stripe_event_created_timestamp INTEGER NOT NULL,
468    processed_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
469);
470
471CREATE INDEX "ix_processed_stripe_events_on_stripe_event_created_timestamp" ON processed_stripe_events (stripe_event_created_timestamp);