forge
Explore
Log in

Don't you dare go Hollow

in_old

Clone
★ 0

0 watching0 forksRustGPL-3.01.1 MiB218b775Tahinli2 months ago90 commits

0001_init.sql135 lines, 6.1 KiBHistoryRawDownload
1-- in's schema, whole, in one file.
2--
3-- One plain SQL file, applied at boot to an empty database: Turso has no
4-- migration runner of its own, and keeping the schema as SQL is what makes
5-- the store trait swappable.
6-- Timestamps are RFC 3339 text in UTC. Ids are ULIDs as text — sortable by
7-- creation.
8--
9-- This file is the first migration, not the whole schema any more. Everything
10-- after it is a numbered `0002_*.sql` beside it, and an empty database is
11-- built by applying them in order. A live database is brought up by
12-- `in reconcile`, not by re-running the files, so a change to a table lands
13-- in three places — the newest migration, this file's table, and that tool's
14-- copy map.
15
16-- An account. There is no password column on purpose: sign-in is the OIDC
17-- provider's business, and this database never sees a secret worth stealing.
18-- `oidc_sub` is the provider's stable subject for the person; the row is
19-- created on first sight of a sub (JIT provisioning) and refreshed on every
20-- sight after. The first row ever provisioned is the admin.
21--
22-- `quota_bytes` is the person's ceiling, `used_bytes` the running total of
23-- every live AND trashed byte they hold — trash counts, because a file that
24-- can be restored is a file that still costs. `used_bytes` is recomputed
25-- from the rows after every mutation, never blindly incremented, so a crash
26-- between two writes cannot drift it from the truth.
27CREATE TABLE user (
28 id TEXT PRIMARY KEY,
29 oidc_sub TEXT NOT NULL,
30 email TEXT NOT NULL,
31 display_name TEXT NOT NULL,
32 admin INTEGER NOT NULL DEFAULT 0,
33 disabled INTEGER NOT NULL DEFAULT 0,
34 quota_bytes INTEGER NOT NULL,
35 used_bytes INTEGER NOT NULL DEFAULT 0,
36 created_at TEXT NOT NULL,
37 last_seen_at TEXT
38);
39CREATE UNIQUE INDEX user_oidc_sub ON user(oidc_sub);
40
41-- A folder. `parent_id` NULL is the person's root: everything an account
42-- holds hangs, directly or not, under nothing. Names are unique among live
43-- siblings — two folders called the same thing in one place is one folder
44-- spelled twice — and trashed rows do not count, so deleting a name frees it.
45--
46-- Trash is `deleted_at`: a folder trash stamps the folder, every descendant
47-- folder and every file under them with the same timestamp, in one
48-- transaction. The bytes stay on disk until a purge takes the rows.
49CREATE TABLE folder (
50 id TEXT PRIMARY KEY,
51 owner_id TEXT NOT NULL REFERENCES user(id),
52 parent_id TEXT REFERENCES folder(id),
53 name TEXT NOT NULL,
54 created_at TEXT NOT NULL,
55 deleted_at TEXT
56);
57CREATE INDEX folder_by_parent ON folder(owner_id, parent_id);
58
59-- A file. The bytes are deliberately not on this type: they sit at
60-- `<storage>/files/<id>`, named by the row's own id, and a thumbnail — when
61-- the bytes are an image — at `<storage>/thumbs/<id>`. The directory is state
62-- the same way the database file is — back the two up together, and boot
63-- deletes any file no row names. And a name is still exactly where an
64-- uploaded file name must never end up: `../../etc` is a valid file name and
65-- a terrible path, so `name` remains a label printed on a row; the file a row
66-- names is found by its id alone, never resolved from anything the row was
67-- given.
68--
69-- `mime` is what the server decided the bytes are, never what the upload
70-- claimed. `thumb_state` is `none` for bytes no thumbnail is attempted for,
71-- `pending` while one is being made, and `ready` or `failed` once it is.
72CREATE TABLE file (
73 id TEXT PRIMARY KEY,
74 owner_id TEXT NOT NULL REFERENCES user(id),
75 folder_id TEXT REFERENCES folder(id),
76 name TEXT NOT NULL,
77 mime TEXT NOT NULL,
78 size_bytes INTEGER NOT NULL,
79 thumb_state TEXT NOT NULL DEFAULT 'none',
80 created_at TEXT NOT NULL,
81 updated_at TEXT NOT NULL,
82 deleted_at TEXT
83);
84CREATE INDEX file_by_folder ON file(owner_id, folder_id);
85
86-- A public share link. Only the hash is stored: the plaintext token is shown
87-- once when the link is created, and anyone who holds it reads through it.
88-- `kind` names which table `target_id` points at — `file` or `folder` — and
89-- `expires_at` / `revoked_at` end the link without deleting the row, so a
90-- dead link stays distinguishable from a wrong token.
91CREATE TABLE share_link (
92 id TEXT PRIMARY KEY,
93 token_hash TEXT NOT NULL,
94 kind TEXT NOT NULL,
95 target_id TEXT NOT NULL,
96 created_by TEXT NOT NULL REFERENCES user(id),
97 can_download INTEGER NOT NULL DEFAULT 1,
98 created_at TEXT NOT NULL,
99 expires_at TEXT,
100 revoked_at TEXT
101);
102CREATE UNIQUE INDEX share_link_token ON share_link(token_hash);
103
104-- A per-person share: `user_id` may see one file or folder of somebody
105-- else's. `kind` names which table `target_id` points at, as on `share_link`.
106-- The row is the whole of the permission — no role, no hierarchy — so
107-- removing it unshares completely.
108CREATE TABLE share_user (
109 kind TEXT NOT NULL,
110 target_id TEXT NOT NULL,
111 user_id TEXT NOT NULL REFERENCES user(id),
112 can_download INTEGER NOT NULL DEFAULT 1,
113 created_at TEXT NOT NULL,
114 PRIMARY KEY (kind, target_id, user_id)
115);
116
117-- A chunked upload on its way in. Chunks land at
118-- `<storage>/uploads/<id>/<n>`, staged until the finish assembles them in
119-- order, sniffs what they are, and moves the result to its file row. A
120-- session whose `expires_at` passes is aborted and its chunks deleted — by
121-- the boot sweep, and by anyone who asks — so an abandoned upload cannot pin
122-- disk forever. `state` is `active`, `done` or `aborted`: a finished or
123-- aborted session keeps its row as the record of what happened.
124CREATE TABLE upload_session (
125 id TEXT PRIMARY KEY,
126 owner_id TEXT NOT NULL REFERENCES user(id),
127 folder_id TEXT REFERENCES folder(id),
128 name TEXT NOT NULL,
129 size_bytes INTEGER NOT NULL,
130 chunk_size INTEGER NOT NULL,
131 received_bytes INTEGER NOT NULL DEFAULT 0,
132 state TEXT NOT NULL DEFAULT 'active',
133 created_at TEXT NOT NULL,
134 expires_at TEXT NOT NULL
135);