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    PRIMARY KEY (project_id, id)
121);
122
123CREATE INDEX "index_project_repositories_on_project_id" ON "project_repositories" ("project_id");
124
125CREATE TABLE "project_repository_statuses" (
126    "project_id" INTEGER NOT NULL,
127    "repository_id" INTEGER NOT NULL,
128    "repo_path" VARCHAR NOT NULL,
129    "status" INT8 NOT NULL,
130    "status_kind" INT4 NOT NULL,
131    "first_status" INT4 NULL,
132    "second_status" INT4 NULL,
133    "scan_id" INT8 NOT NULL,
134    "is_deleted" BOOL NOT NULL,
135    PRIMARY KEY (project_id, repository_id, repo_path)
136);
137
138CREATE INDEX "index_project_repos_statuses_on_project_id" ON "project_repository_statuses" ("project_id");
139
140CREATE INDEX "index_project_repos_statuses_on_project_id_and_repo_id" ON "project_repository_statuses" ("project_id", "repository_id");
141
142CREATE TABLE "worktree_settings_files" (
143    "project_id" INTEGER NOT NULL,
144    "worktree_id" INTEGER NOT NULL,
145    "path" VARCHAR NOT NULL,
146    "content" TEXT,
147    "kind" VARCHAR,
148    PRIMARY KEY (project_id, worktree_id, path),
149    FOREIGN KEY (project_id, worktree_id) REFERENCES worktrees (project_id, id) ON DELETE CASCADE
150);
151
152CREATE INDEX "index_worktree_settings_files_on_project_id" ON "worktree_settings_files" ("project_id");
153
154CREATE INDEX "index_worktree_settings_files_on_project_id_and_worktree_id" ON "worktree_settings_files" ("project_id", "worktree_id");
155
156CREATE TABLE "worktree_diagnostic_summaries" (
157    "project_id" INTEGER NOT NULL,
158    "worktree_id" INTEGER NOT NULL,
159    "path" VARCHAR NOT NULL,
160    "language_server_id" INTEGER NOT NULL,
161    "error_count" INTEGER NOT NULL,
162    "warning_count" INTEGER NOT NULL,
163    PRIMARY KEY (project_id, worktree_id, path),
164    FOREIGN KEY (project_id, worktree_id) REFERENCES worktrees (project_id, id) ON DELETE CASCADE
165);
166
167CREATE INDEX "index_worktree_diagnostic_summaries_on_project_id" ON "worktree_diagnostic_summaries" ("project_id");
168
169CREATE INDEX "index_worktree_diagnostic_summaries_on_project_id_and_worktree_id" ON "worktree_diagnostic_summaries" ("project_id", "worktree_id");
170
171CREATE TABLE "language_servers" (
172    "id" INTEGER NOT NULL,
173    "project_id" INTEGER NOT NULL REFERENCES projects (id) ON DELETE CASCADE,
174    "name" VARCHAR NOT NULL,
175    PRIMARY KEY (project_id, id)
176);
177
178CREATE INDEX "index_language_servers_on_project_id" ON "language_servers" ("project_id");
179
180CREATE TABLE "project_collaborators" (
181    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
182    "project_id" INTEGER NOT NULL REFERENCES projects (id) ON DELETE CASCADE,
183    "connection_id" INTEGER NOT NULL,
184    "connection_server_id" INTEGER NOT NULL REFERENCES servers (id) ON DELETE CASCADE,
185    "user_id" INTEGER NOT NULL,
186    "replica_id" INTEGER NOT NULL,
187    "is_host" BOOLEAN NOT NULL
188);
189
190CREATE INDEX "index_project_collaborators_on_project_id" ON "project_collaborators" ("project_id");
191
192CREATE UNIQUE INDEX "index_project_collaborators_on_project_id_and_replica_id" ON "project_collaborators" ("project_id", "replica_id");
193
194CREATE INDEX "index_project_collaborators_on_connection_server_id" ON "project_collaborators" ("connection_server_id");
195
196CREATE INDEX "index_project_collaborators_on_connection_id" ON "project_collaborators" ("connection_id");
197
198CREATE UNIQUE INDEX "index_project_collaborators_on_project_id_connection_id_and_server_id" ON "project_collaborators" (
199    "project_id",
200    "connection_id",
201    "connection_server_id"
202);
203
204CREATE TABLE "room_participants" (
205    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
206    "room_id" INTEGER NOT NULL REFERENCES rooms (id),
207    "user_id" INTEGER NOT NULL REFERENCES users (id),
208    "answering_connection_id" INTEGER,
209    "answering_connection_server_id" INTEGER REFERENCES servers (id) ON DELETE CASCADE,
210    "answering_connection_lost" BOOLEAN NOT NULL,
211    "location_kind" INTEGER,
212    "location_project_id" INTEGER,
213    "initial_project_id" INTEGER,
214    "calling_user_id" INTEGER NOT NULL REFERENCES users (id),
215    "calling_connection_id" INTEGER NOT NULL,
216    "calling_connection_server_id" INTEGER REFERENCES servers (id) ON DELETE SET NULL,
217    "participant_index" INTEGER,
218    "role" TEXT,
219    "in_call" BOOLEAN NOT NULL DEFAULT FALSE
220);
221
222CREATE UNIQUE INDEX "index_room_participants_on_user_id" ON "room_participants" ("user_id");
223
224CREATE INDEX "index_room_participants_on_room_id" ON "room_participants" ("room_id");
225
226CREATE INDEX "index_room_participants_on_answering_connection_server_id" ON "room_participants" ("answering_connection_server_id");
227
228CREATE INDEX "index_room_participants_on_calling_connection_server_id" ON "room_participants" ("calling_connection_server_id");
229
230CREATE INDEX "index_room_participants_on_answering_connection_id" ON "room_participants" ("answering_connection_id");
231
232CREATE UNIQUE INDEX "index_room_participants_on_answering_connection_id_and_answering_connection_server_id" ON "room_participants" (
233    "answering_connection_id",
234    "answering_connection_server_id"
235);
236
237CREATE TABLE "servers" (
238    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
239    "environment" VARCHAR NOT NULL
240);
241
242CREATE TABLE "followers" (
243    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
244    "room_id" INTEGER NOT NULL REFERENCES rooms (id) ON DELETE CASCADE,
245    "project_id" INTEGER NOT NULL REFERENCES projects (id) ON DELETE CASCADE,
246    "leader_connection_server_id" INTEGER NOT NULL REFERENCES servers (id) ON DELETE CASCADE,
247    "leader_connection_id" INTEGER NOT NULL,
248    "follower_connection_server_id" INTEGER NOT NULL REFERENCES servers (id) ON DELETE CASCADE,
249    "follower_connection_id" INTEGER NOT NULL
250);
251
252CREATE 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" (
253    "project_id",
254    "leader_connection_server_id",
255    "leader_connection_id",
256    "follower_connection_server_id",
257    "follower_connection_id"
258);
259
260CREATE INDEX "index_followers_on_room_id" ON "followers" ("room_id");
261
262CREATE TABLE "channels" (
263    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
264    "name" VARCHAR NOT NULL,
265    "created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
266    "visibility" VARCHAR NOT NULL,
267    "parent_path" TEXT NOT NULL,
268    "requires_zed_cla" BOOLEAN NOT NULL DEFAULT FALSE
269);
270
271CREATE INDEX "index_channels_on_parent_path" ON "channels" ("parent_path");
272
273CREATE TABLE IF NOT EXISTS "channel_chat_participants" (
274    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
275    "user_id" INTEGER NOT NULL REFERENCES users (id),
276    "channel_id" INTEGER NOT NULL REFERENCES channels (id) ON DELETE CASCADE,
277    "connection_id" INTEGER NOT NULL,
278    "connection_server_id" INTEGER NOT NULL REFERENCES servers (id) ON DELETE CASCADE
279);
280
281CREATE INDEX "index_channel_chat_participants_on_channel_id" ON "channel_chat_participants" ("channel_id");
282
283CREATE TABLE IF NOT EXISTS "channel_messages" (
284    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
285    "channel_id" INTEGER NOT NULL REFERENCES channels (id) ON DELETE CASCADE,
286    "sender_id" INTEGER NOT NULL REFERENCES users (id),
287    "body" TEXT NOT NULL,
288    "sent_at" TIMESTAMP,
289    "edited_at" TIMESTAMP,
290    "nonce" BLOB NOT NULL,
291    "reply_to_message_id" INTEGER DEFAULT NULL
292);
293
294CREATE INDEX "index_channel_messages_on_channel_id" ON "channel_messages" ("channel_id");
295
296CREATE UNIQUE INDEX "index_channel_messages_on_sender_id_nonce" ON "channel_messages" ("sender_id", "nonce");
297
298CREATE TABLE "channel_message_mentions" (
299    "message_id" INTEGER NOT NULL REFERENCES channel_messages (id) ON DELETE CASCADE,
300    "start_offset" INTEGER NOT NULL,
301    "end_offset" INTEGER NOT NULL,
302    "user_id" INTEGER NOT NULL REFERENCES users (id) ON DELETE CASCADE,
303    PRIMARY KEY (message_id, start_offset)
304);
305
306CREATE TABLE "channel_members" (
307    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
308    "channel_id" INTEGER NOT NULL REFERENCES channels (id) ON DELETE CASCADE,
309    "user_id" INTEGER NOT NULL REFERENCES users (id) ON DELETE CASCADE,
310    "role" VARCHAR NOT NULL,
311    "accepted" BOOLEAN NOT NULL DEFAULT false,
312    "updated_at" TIMESTAMP NOT NULL DEFAULT now
313);
314
315CREATE UNIQUE INDEX "index_channel_members_on_channel_id_and_user_id" ON "channel_members" ("channel_id", "user_id");
316
317CREATE TABLE "buffers" (
318    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
319    "channel_id" INTEGER NOT NULL REFERENCES channels (id) ON DELETE CASCADE,
320    "epoch" INTEGER NOT NULL DEFAULT 0,
321    "latest_operation_epoch" INTEGER,
322    "latest_operation_replica_id" INTEGER,
323    "latest_operation_lamport_timestamp" INTEGER
324);
325
326CREATE INDEX "index_buffers_on_channel_id" ON "buffers" ("channel_id");
327
328CREATE TABLE "buffer_operations" (
329    "buffer_id" INTEGER NOT NULL REFERENCES buffers (id) ON DELETE CASCADE,
330    "epoch" INTEGER NOT NULL,
331    "replica_id" INTEGER NOT NULL,
332    "lamport_timestamp" INTEGER NOT NULL,
333    "value" BLOB NOT NULL,
334    PRIMARY KEY (buffer_id, epoch, lamport_timestamp, replica_id)
335);
336
337CREATE TABLE "buffer_snapshots" (
338    "buffer_id" INTEGER NOT NULL REFERENCES buffers (id) ON DELETE CASCADE,
339    "epoch" INTEGER NOT NULL,
340    "text" TEXT NOT NULL,
341    "operation_serialization_version" INTEGER NOT NULL,
342    PRIMARY KEY (buffer_id, epoch)
343);
344
345CREATE TABLE "channel_buffer_collaborators" (
346    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
347    "channel_id" INTEGER NOT NULL REFERENCES channels (id) ON DELETE CASCADE,
348    "connection_id" INTEGER NOT NULL,
349    "connection_server_id" INTEGER NOT NULL REFERENCES servers (id) ON DELETE CASCADE,
350    "connection_lost" BOOLEAN NOT NULL DEFAULT false,
351    "user_id" INTEGER NOT NULL REFERENCES users (id) ON DELETE CASCADE,
352    "replica_id" INTEGER NOT NULL
353);
354
355CREATE INDEX "index_channel_buffer_collaborators_on_channel_id" ON "channel_buffer_collaborators" ("channel_id");
356
357CREATE UNIQUE INDEX "index_channel_buffer_collaborators_on_channel_id_and_replica_id" ON "channel_buffer_collaborators" ("channel_id", "replica_id");
358
359CREATE INDEX "index_channel_buffer_collaborators_on_connection_server_id" ON "channel_buffer_collaborators" ("connection_server_id");
360
361CREATE INDEX "index_channel_buffer_collaborators_on_connection_id" ON "channel_buffer_collaborators" ("connection_id");
362
363CREATE UNIQUE INDEX "index_channel_buffer_collaborators_on_channel_id_connection_id_and_server_id" ON "channel_buffer_collaborators" (
364    "channel_id",
365    "connection_id",
366    "connection_server_id"
367);
368
369CREATE TABLE "feature_flags" (
370    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
371    "flag" TEXT NOT NULL UNIQUE,
372    "enabled_for_all" BOOLEAN NOT NULL DEFAULT false
373);
374
375CREATE INDEX "index_feature_flags" ON "feature_flags" ("id");
376
377CREATE TABLE "user_features" (
378    "user_id" INTEGER NOT NULL REFERENCES users (id) ON DELETE CASCADE,
379    "feature_id" INTEGER NOT NULL REFERENCES feature_flags (id) ON DELETE CASCADE,
380    PRIMARY KEY (user_id, feature_id)
381);
382
383CREATE UNIQUE INDEX "index_user_features_user_id_and_feature_id" ON "user_features" ("user_id", "feature_id");
384
385CREATE INDEX "index_user_features_on_user_id" ON "user_features" ("user_id");
386
387CREATE INDEX "index_user_features_on_feature_id" ON "user_features" ("feature_id");
388
389CREATE TABLE "observed_buffer_edits" (
390    "user_id" INTEGER NOT NULL REFERENCES users (id) ON DELETE CASCADE,
391    "buffer_id" INTEGER NOT NULL REFERENCES buffers (id) ON DELETE CASCADE,
392    "epoch" INTEGER NOT NULL,
393    "lamport_timestamp" INTEGER NOT NULL,
394    "replica_id" INTEGER NOT NULL,
395    PRIMARY KEY (user_id, buffer_id)
396);
397
398CREATE UNIQUE INDEX "index_observed_buffers_user_and_buffer_id" ON "observed_buffer_edits" ("user_id", "buffer_id");
399
400CREATE TABLE IF NOT EXISTS "observed_channel_messages" (
401    "user_id" INTEGER NOT NULL REFERENCES users (id) ON DELETE CASCADE,
402    "channel_id" INTEGER NOT NULL REFERENCES channels (id) ON DELETE CASCADE,
403    "channel_message_id" INTEGER NOT NULL,
404    PRIMARY KEY (user_id, channel_id)
405);
406
407CREATE UNIQUE INDEX "index_observed_channel_messages_user_and_channel_id" ON "observed_channel_messages" ("user_id", "channel_id");
408
409CREATE TABLE "notification_kinds" (
410    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
411    "name" VARCHAR NOT NULL
412);
413
414CREATE UNIQUE INDEX "index_notification_kinds_on_name" ON "notification_kinds" ("name");
415
416CREATE TABLE "notifications" (
417    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
418    "created_at" TIMESTAMP NOT NULL default CURRENT_TIMESTAMP,
419    "recipient_id" INTEGER NOT NULL REFERENCES users (id) ON DELETE CASCADE,
420    "kind" INTEGER NOT NULL REFERENCES notification_kinds (id),
421    "entity_id" INTEGER,
422    "content" TEXT,
423    "is_read" BOOLEAN NOT NULL DEFAULT FALSE,
424    "response" BOOLEAN
425);
426
427CREATE INDEX "index_notifications_on_recipient_id_is_read_kind_entity_id" ON "notifications" ("recipient_id", "is_read", "kind", "entity_id");
428
429CREATE TABLE contributors (
430    user_id INTEGER REFERENCES users (id),
431    signed_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
432    PRIMARY KEY (user_id)
433);
434
435CREATE TABLE extensions (
436    id INTEGER PRIMARY KEY AUTOINCREMENT,
437    external_id TEXT NOT NULL,
438    name TEXT NOT NULL,
439    latest_version TEXT NOT NULL,
440    total_download_count INTEGER NOT NULL DEFAULT 0
441);
442
443CREATE TABLE extension_versions (
444    extension_id INTEGER REFERENCES extensions (id),
445    version TEXT NOT NULL,
446    published_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
447    authors TEXT NOT NULL,
448    repository TEXT NOT NULL,
449    description TEXT NOT NULL,
450    schema_version INTEGER NOT NULL DEFAULT 0,
451    wasm_api_version TEXT,
452    download_count INTEGER NOT NULL DEFAULT 0,
453    provides_themes BOOLEAN NOT NULL DEFAULT FALSE,
454    provides_icon_themes BOOLEAN NOT NULL DEFAULT FALSE,
455    provides_languages BOOLEAN NOT NULL DEFAULT FALSE,
456    provides_grammars BOOLEAN NOT NULL DEFAULT FALSE,
457    provides_language_servers BOOLEAN NOT NULL DEFAULT FALSE,
458    provides_context_servers BOOLEAN NOT NULL DEFAULT FALSE,
459    provides_slash_commands BOOLEAN NOT NULL DEFAULT FALSE,
460    provides_indexed_docs_providers BOOLEAN NOT NULL DEFAULT FALSE,
461    provides_snippets BOOLEAN NOT NULL DEFAULT FALSE,
462    PRIMARY KEY (extension_id, version)
463);
464
465CREATE UNIQUE INDEX "index_extensions_external_id" ON "extensions" ("external_id");
466
467CREATE INDEX "index_extensions_total_download_count" ON "extensions" ("total_download_count");
468
469CREATE TABLE rate_buckets (
470    user_id INT NOT NULL,
471    rate_limit_name VARCHAR(255) NOT NULL,
472    token_count INT NOT NULL,
473    last_refill TIMESTAMP WITHOUT TIME ZONE NOT NULL,
474    PRIMARY KEY (user_id, rate_limit_name),
475    FOREIGN KEY (user_id) REFERENCES users (id)
476);
477
478CREATE INDEX idx_user_id_rate_limit ON rate_buckets (user_id, rate_limit_name);
479
480CREATE TABLE IF NOT EXISTS billing_preferences (
481    id INTEGER PRIMARY KEY AUTOINCREMENT,
482    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
483    user_id INTEGER NOT NULL REFERENCES users (id),
484    max_monthly_llm_usage_spending_in_cents INTEGER NOT NULL
485);
486
487CREATE UNIQUE INDEX "uix_billing_preferences_on_user_id" ON billing_preferences (user_id);
488
489CREATE TABLE IF NOT EXISTS billing_customers (
490    id INTEGER PRIMARY KEY AUTOINCREMENT,
491    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
492    user_id INTEGER NOT NULL REFERENCES users (id),
493    has_overdue_invoices BOOLEAN NOT NULL DEFAULT FALSE,
494    stripe_customer_id TEXT NOT NULL
495);
496
497CREATE UNIQUE INDEX "uix_billing_customers_on_user_id" ON billing_customers (user_id);
498
499CREATE UNIQUE INDEX "uix_billing_customers_on_stripe_customer_id" ON billing_customers (stripe_customer_id);
500
501CREATE TABLE IF NOT EXISTS billing_subscriptions (
502    id INTEGER PRIMARY KEY AUTOINCREMENT,
503    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
504    billing_customer_id INTEGER NOT NULL REFERENCES billing_customers (id),
505    stripe_subscription_id TEXT NOT NULL,
506    stripe_subscription_status TEXT NOT NULL,
507    stripe_cancel_at TIMESTAMP,
508    stripe_cancellation_reason TEXT,
509    kind TEXT,
510    stripe_current_period_start BIGINT,
511    stripe_current_period_end BIGINT
512);
513
514CREATE INDEX "ix_billing_subscriptions_on_billing_customer_id" ON billing_subscriptions (billing_customer_id);
515
516CREATE UNIQUE INDEX "uix_billing_subscriptions_on_stripe_subscription_id" ON billing_subscriptions (stripe_subscription_id);
517
518CREATE TABLE IF NOT EXISTS processed_stripe_events (
519    stripe_event_id TEXT PRIMARY KEY,
520    stripe_event_type TEXT NOT NULL,
521    stripe_event_created_timestamp INTEGER NOT NULL,
522    processed_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
523);
524
525CREATE INDEX "ix_processed_stripe_events_on_stripe_event_created_timestamp" ON processed_stripe_events (stripe_event_created_timestamp);
526
527CREATE TABLE IF NOT EXISTS "breakpoints" (
528    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
529    "project_id" INTEGER NOT NULL REFERENCES projects (id) ON DELETE CASCADE,
530    "position" INTEGER NOT NULL,
531    "log_message" TEXT NULL,
532    "worktree_id" BIGINT NOT NULL,
533    "path" TEXT NOT NULL,
534    "kind" VARCHAR NOT NULL
535);
536
537CREATE INDEX "index_breakpoints_on_project_id" ON "breakpoints" ("project_id");