1CREATE TABLE IF NOT EXISTS "sessions" (
2 "id" VARCHAR NOT NULL PRIMARY KEY,
3 "expires" TIMESTAMP WITH TIME ZONE NULL,
4 "session" TEXT NOT NULL
5);
6
7CREATE TABLE IF NOT EXISTS "users" (
8 "id" INTEGER PRIMARY KEY AUTOINCREMENT,
9 "github_login" VARCHAR,
10 "admin" BOOLEAN,
11 email_address VARCHAR(255) DEFAULT NULL,
12 invite_code VARCHAR(64),
13 invite_count INTEGER NOT NULL DEFAULT 0,
14 inviter_id INTEGER REFERENCES users (id),
15 connected_once BOOLEAN NOT NULL DEFAULT false,
16 created_at TIMESTAMP NOT NULL DEFAULT now,
17 "github_user_id" INTEGER
18);
19CREATE UNIQUE INDEX "index_users_github_login" ON "users" ("github_login");
20CREATE UNIQUE INDEX "index_invite_code_users" ON "users" ("invite_code");
21CREATE INDEX "index_users_on_email_address" ON "users" ("email_address");
22CREATE INDEX "index_users_on_github_user_id" ON "users" ("github_user_id");
23
24CREATE TABLE IF NOT EXISTS "access_tokens" (
25 "id" INTEGER PRIMARY KEY AUTOINCREMENT,
26 "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 IF NOT EXISTS "orgs" (
32 "id" SERIAL PRIMARY KEY,
33 "name" VARCHAR NOT NULL,
34 "slug" VARCHAR NOT NULL
35);
36CREATE UNIQUE INDEX "index_orgs_slug" ON "orgs" ("slug");
37
38CREATE TABLE IF NOT EXISTS "org_memberships" (
39 "id" SERIAL PRIMARY KEY,
40 "org_id" INTEGER REFERENCES orgs (id) NOT NULL,
41 "user_id" INTEGER REFERENCES users (id) NOT NULL,
42 "admin" BOOLEAN NOT NULL
43);
44CREATE INDEX "index_org_memberships_user_id" ON "org_memberships" ("user_id");
45CREATE UNIQUE INDEX "index_org_memberships_org_id_and_user_id" ON "org_memberships" ("org_id", "user_id");
46
47CREATE TABLE IF NOT EXISTS "channels" (
48 "id" SERIAL PRIMARY KEY,
49 "owner_id" INTEGER NOT NULL,
50 "owner_is_user" BOOLEAN NOT NULL,
51 "name" VARCHAR NOT NULL
52);
53CREATE UNIQUE INDEX "index_channels_owner_and_name" ON "channels" ("owner_is_user", "owner_id", "name");
54
55CREATE TABLE IF NOT EXISTS "channel_memberships" (
56 "id" SERIAL PRIMARY KEY,
57 "channel_id" INTEGER REFERENCES channels (id) NOT NULL,
58 "user_id" INTEGER REFERENCES users (id) NOT NULL,
59 "admin" BOOLEAN NOT NULL
60);
61CREATE INDEX "index_channel_memberships_user_id" ON "channel_memberships" ("user_id");
62CREATE UNIQUE INDEX "index_channel_memberships_channel_id_and_user_id" ON "channel_memberships" ("channel_id", "user_id");
63
64CREATE TABLE IF NOT EXISTS "channel_messages" (
65 "id" SERIAL PRIMARY KEY,
66 "channel_id" INTEGER REFERENCES channels (id) NOT NULL,
67 "sender_id" INTEGER REFERENCES users (id) NOT NULL,
68 "body" TEXT NOT NULL,
69 "sent_at" TIMESTAMP
70);
71CREATE INDEX "index_channel_messages_channel_id" ON "channel_messages" ("channel_id");
72
73CREATE TABLE IF NOT EXISTS "contacts" (
74 "id" SERIAL PRIMARY KEY,
75 "user_id_a" INTEGER REFERENCES users (id) NOT NULL,
76 "user_id_b" INTEGER REFERENCES users (id) NOT NULL,
77 "a_to_b" BOOLEAN NOT NULL,
78 "should_notify" BOOLEAN NOT NULL,
79 "accepted" BOOLEAN NOT NULL
80);
81CREATE UNIQUE INDEX "index_contacts_user_ids" ON "contacts" ("user_id_a", "user_id_b");
82CREATE INDEX "index_contacts_user_id_b" ON "contacts" ("user_id_b");
83
84CREATE TABLE IF NOT EXISTS "projects" (
85 "id" SERIAL PRIMARY KEY,
86 "host_user_id" INTEGER REFERENCES users (id) NOT NULL,
87 "unregistered" BOOLEAN NOT NULL DEFAULT false
88);
89
90CREATE TABLE IF NOT EXISTS "worktree_extensions" (
91 "id" SERIAL PRIMARY KEY,
92 "project_id" INTEGER REFERENCES projects (id) NOT NULL,
93 "worktree_id" INTEGER NOT NULL,
94 "extension" VARCHAR(255),
95 "count" INTEGER NOT NULL
96);
97CREATE UNIQUE INDEX "index_worktree_extensions_on_project_id_and_worktree_id_and_extension" ON "worktree_extensions" ("project_id", "worktree_id", "extension");
98
99CREATE TABLE IF NOT EXISTS "project_activity_periods" (
100 "id" SERIAL PRIMARY KEY,
101 "duration_millis" INTEGER NOT NULL,
102 "ended_at" TIMESTAMP NOT NULL,
103 "user_id" INTEGER REFERENCES users (id) NOT NULL,
104 "project_id" INTEGER REFERENCES projects (id) NOT NULL
105);
106CREATE INDEX "index_project_activity_periods_on_ended_at" ON "project_activity_periods" ("ended_at");
107
108CREATE TABLE IF NOT EXISTS "signups" (
109 "id" SERIAL PRIMARY KEY,
110 "email_address" VARCHAR NOT NULL,
111 "email_confirmation_code" VARCHAR(64) NOT NULL,
112 "email_confirmation_sent" BOOLEAN NOT NULL,
113 "created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
114 "device_id" VARCHAR,
115 "user_id" INTEGER REFERENCES users (id) ON DELETE CASCADE,
116 "inviting_user_id" INTEGER REFERENCES users (id) ON DELETE SET NULL,
117
118 "platform_mac" BOOLEAN NOT NULL,
119 "platform_linux" BOOLEAN NOT NULL,
120 "platform_windows" BOOLEAN NOT NULL,
121 "platform_unknown" BOOLEAN NOT NULL,
122
123 "editor_features" VARCHAR[],
124 "programming_languages" VARCHAR[]
125);
126CREATE UNIQUE INDEX "index_signups_on_email_address" ON "signups" ("email_address");
127CREATE INDEX "index_signups_on_email_confirmation_sent" ON "signups" ("email_confirmation_sent");