forge
Explore
Log in

Don't you dare go Hollow

in

Clone
★ 0

0 watching0 forksRustAGPL-3.0528.7 KiB9215377Tahinli27 days ago37 commits

0001_init.sql180 lines, 7.8 KiBHistoryRawDownload
1-- 0001: the first schema of `in`. Creates every table in docs/architecture.md section 3, with its
2-- constraints and indexes. Timestamps are fixed-width UTC text (see time.rs); booleans are INTEGER 0/1.
3
4CREATE TABLE settings (
5 key TEXT PRIMARY KEY NOT NULL,
6 value TEXT NOT NULL,
7 updated_at TEXT NOT NULL
8);
9
10-- Mirror of the people in `im`. `sub` is the OIDC subject and never changes for a person.
11CREATE TABLE members (
12 sub TEXT PRIMARY KEY NOT NULL,
13 email TEXT NOT NULL,
14 name TEXT NOT NULL,
15 is_admin INTEGER NOT NULL DEFAULT 0 CHECK (is_admin IN (0, 1)),
16 disabled INTEGER NOT NULL DEFAULT 0 CHECK (disabled IN (0, 1)),
17 locale TEXT NOT NULL DEFAULT 'en',
18 zoneinfo TEXT NOT NULL DEFAULT 'UTC',
19 picture_version INTEGER NOT NULL DEFAULT 0 CHECK (picture_version >= 0),
20 im_updated_at TEXT NOT NULL,
21 first_seen_at TEXT NOT NULL,
22 last_sign_in_at TEXT,
23 theme TEXT NOT NULL DEFAULT 'system' CHECK (theme IN ('system', 'light', 'dark')),
24 locale_override TEXT,
25 quota_bytes INTEGER NOT NULL CHECK (quota_bytes >= 0),
26 used_bytes INTEGER NOT NULL DEFAULT 0 CHECK (used_bytes >= 0)
27);
28
29-- Local sessions. Only the SHA-256 of the cookie token is stored.
30CREATE TABLE sessions (
31 id TEXT PRIMARY KEY NOT NULL,
32 sub TEXT NOT NULL REFERENCES members (sub) ON DELETE CASCADE,
33 token_hash TEXT NOT NULL UNIQUE,
34 csrf_token TEXT NOT NULL,
35 im_sid TEXT NOT NULL,
36 created_at TEXT NOT NULL,
37 expires_at TEXT NOT NULL,
38 last_seen_at TEXT NOT NULL,
39 ip TEXT,
40 user_agent TEXT
41);
42CREATE INDEX sessions_im_sid ON sessions (im_sid);
43CREATE INDEX sessions_sub ON sessions (sub);
44
45-- Back-channel logout token ids already used. Kept until the token would have expired anyway.
46CREATE TABLE seen_logout_jti (
47 jti TEXT PRIMARY KEY NOT NULL,
48 expires_at TEXT NOT NULL
49);
50
51-- Folders. Live names are unique per owner and parent. A NULL parent is written as '' so that the
52-- root level is covered too.
53CREATE TABLE folders (
54 id TEXT PRIMARY KEY NOT NULL,
55 owner TEXT NOT NULL REFERENCES members (sub) ON DELETE RESTRICT,
56 parent_id TEXT REFERENCES folders (id) ON DELETE RESTRICT,
57 name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 255),
58 created_at TEXT NOT NULL,
59 updated_at TEXT NOT NULL,
60 trashed_at TEXT,
61 trash_root TEXT REFERENCES folders (id) ON DELETE SET NULL,
62 CHECK (parent_id IS NULL OR parent_id <> id)
63);
64CREATE UNIQUE INDEX folders_live_name
65 ON folders (owner, COALESCE(parent_id, ''), name)
66 WHERE trashed_at IS NULL;
67CREATE INDEX folders_parent ON folders (parent_id);
68CREATE INDEX folders_trash_root ON folders (trash_root);
69
70-- Files. `sha256` is the hex digest of the content; `blob_key` names the bytes in the blob store.
71CREATE TABLE files (
72 id TEXT PRIMARY KEY NOT NULL,
73 owner TEXT NOT NULL REFERENCES members (sub) ON DELETE RESTRICT,
74 folder_id TEXT REFERENCES folders (id) ON DELETE RESTRICT,
75 name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 255),
76 mime TEXT NOT NULL,
77 size INTEGER NOT NULL CHECK (size >= 0),
78 sha256 TEXT NOT NULL CHECK (length(sha256) = 64),
79 blob_key TEXT NOT NULL UNIQUE,
80 thumb_state TEXT NOT NULL DEFAULT 'none'
81 CHECK (thumb_state IN ('none', 'pending', 'ready', 'failed', 'skip')),
82 download_count INTEGER NOT NULL DEFAULT 0 CHECK (download_count >= 0),
83 created_at TEXT NOT NULL,
84 updated_at TEXT NOT NULL,
85 trashed_at TEXT,
86 trash_root TEXT REFERENCES folders (id) ON DELETE SET NULL
87);
88CREATE UNIQUE INDEX files_live_name
89 ON files (owner, COALESCE(folder_id, ''), name)
90 WHERE trashed_at IS NULL;
91CREATE INDEX files_folder ON files (folder_id);
92CREATE INDEX files_trash_root ON files (trash_root);
93CREATE INDEX files_thumb_pending ON files (thumb_state) WHERE thumb_state = 'pending';
94
95-- Per-person shares. `target_id` names a file or folder; it cannot be a foreign key because it points
96-- at either table, so the application removes grants when their target is purged.
97CREATE TABLE grants (
98 id TEXT PRIMARY KEY NOT NULL,
99 target_kind TEXT NOT NULL CHECK (target_kind IN ('file', 'folder')),
100 target_id TEXT NOT NULL,
101 owner TEXT NOT NULL REFERENCES members (sub) ON DELETE RESTRICT,
102 grantee TEXT NOT NULL REFERENCES members (sub) ON DELETE CASCADE,
103 can_download INTEGER NOT NULL DEFAULT 0 CHECK (can_download IN (0, 1)),
104 created_at TEXT NOT NULL,
105 UNIQUE (target_kind, target_id, grantee)
106);
107CREATE INDEX grants_grantee ON grants (grantee);
108
109-- Public share links. The token itself is never stored: `token_hash` is looked up, and `token_sealed`
110-- lets the owner see the URL again.
111CREATE TABLE links (
112 id TEXT PRIMARY KEY NOT NULL,
113 token_hash TEXT NOT NULL UNIQUE,
114 token_sealed TEXT NOT NULL,
115 target_kind TEXT NOT NULL CHECK (target_kind IN ('file', 'folder')),
116 target_id TEXT NOT NULL,
117 owner TEXT NOT NULL REFERENCES members (sub) ON DELETE RESTRICT,
118 can_download INTEGER NOT NULL DEFAULT 0 CHECK (can_download IN (0, 1)),
119 password_hash TEXT,
120 expires_at TEXT,
121 revoked_at TEXT,
122 created_at TEXT NOT NULL,
123 last_used_at TEXT,
124 use_count INTEGER NOT NULL DEFAULT 0 CHECK (use_count >= 0)
125);
126CREATE INDEX links_target ON links (target_kind, target_id);
127CREATE INDEX links_owner ON links (owner);
128
129-- Resumable uploads in progress. Chunks are staged on disk until `finish` turns them into a file.
130CREATE TABLE uploads (
131 id TEXT PRIMARY KEY NOT NULL,
132 owner TEXT NOT NULL REFERENCES members (sub) ON DELETE RESTRICT,
133 folder_id TEXT REFERENCES folders (id) ON DELETE RESTRICT,
134 name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 255),
135 size INTEGER NOT NULL CHECK (size >= 0),
136 chunk_size INTEGER NOT NULL CHECK (chunk_size > 0),
137 state TEXT NOT NULL DEFAULT 'active' CHECK (state IN ('active', 'done', 'aborted')),
138 file_id TEXT REFERENCES files (id) ON DELETE SET NULL,
139 created_at TEXT NOT NULL,
140 expires_at TEXT NOT NULL
141);
142CREATE INDEX uploads_owner ON uploads (owner);
143CREATE INDEX uploads_expires ON uploads (expires_at);
144
145-- Namespaces for machine callers (other family apps). The key is the token's `svc` claim.
146CREATE TABLE services (
147 key TEXT PRIMARY KEY NOT NULL,
148 quota_bytes INTEGER NOT NULL CHECK (quota_bytes >= 0),
149 used_bytes INTEGER NOT NULL DEFAULT 0 CHECK (used_bytes >= 0),
150 created_at TEXT NOT NULL
151);
152
153-- Objects stored by services. The primary key is the service's own id for the object.
154CREATE TABLE objects (
155 service TEXT NOT NULL REFERENCES services (key) ON DELETE RESTRICT,
156 external_id TEXT NOT NULL,
157 size INTEGER NOT NULL CHECK (size >= 0),
158 sha256 TEXT NOT NULL CHECK (length(sha256) = 64),
159 content_type TEXT NOT NULL,
160 name TEXT,
161 blob_key TEXT NOT NULL UNIQUE,
162 created_at TEXT NOT NULL,
163 updated_at TEXT NOT NULL,
164 PRIMARY KEY (service, external_id)
165);
166
167-- Append-only record of changes. `data` is JSON and never holds secrets.
168CREATE TABLE audit_log (
169 id TEXT PRIMARY KEY NOT NULL,
170 at TEXT NOT NULL,
171 actor_id TEXT,
172 actor_kind TEXT NOT NULL CHECK (actor_kind IN ('member', 'service', 'link', 'system')),
173 action TEXT NOT NULL,
174 target_kind TEXT NOT NULL,
175 target_id TEXT,
176 ip TEXT,
177 data TEXT NOT NULL DEFAULT '{}' CHECK (json_valid(data))
178);
179CREATE INDEX audit_log_at ON audit_log (at);
180CREATE INDEX audit_log_target ON audit_log (target_kind, target_id);