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