-- 0001: the first schema of `in`. Creates every table in docs/architecture.md section 3, with its -- constraints and indexes. Timestamps are fixed-width UTC text (see time.rs); booleans are INTEGER 0/1. CREATE TABLE settings ( key TEXT PRIMARY KEY NOT NULL, value TEXT NOT NULL, updated_at TEXT NOT NULL ); -- Mirror of the people in `im`. `sub` is the OIDC subject and never changes for a person. CREATE TABLE members ( sub TEXT PRIMARY KEY NOT NULL, email TEXT NOT NULL, name TEXT NOT NULL, is_admin INTEGER NOT NULL DEFAULT 0 CHECK (is_admin IN (0, 1)), disabled INTEGER NOT NULL DEFAULT 0 CHECK (disabled IN (0, 1)), locale TEXT NOT NULL DEFAULT 'en', zoneinfo TEXT NOT NULL DEFAULT 'UTC', picture_version INTEGER NOT NULL DEFAULT 0 CHECK (picture_version >= 0), im_updated_at TEXT NOT NULL, first_seen_at TEXT NOT NULL, last_sign_in_at TEXT, theme TEXT NOT NULL DEFAULT 'system' CHECK (theme IN ('system', 'light', 'dark')), locale_override TEXT, quota_bytes INTEGER NOT NULL CHECK (quota_bytes >= 0), used_bytes INTEGER NOT NULL DEFAULT 0 CHECK (used_bytes >= 0) ); -- Local sessions. Only the SHA-256 of the cookie token is stored. CREATE TABLE sessions ( id TEXT PRIMARY KEY NOT NULL, sub TEXT NOT NULL REFERENCES members (sub) ON DELETE CASCADE, token_hash TEXT NOT NULL UNIQUE, csrf_token TEXT NOT NULL, im_sid TEXT NOT NULL, created_at TEXT NOT NULL, expires_at TEXT NOT NULL, last_seen_at TEXT NOT NULL, ip TEXT, user_agent TEXT ); CREATE INDEX sessions_im_sid ON sessions (im_sid); CREATE INDEX sessions_sub ON sessions (sub); -- Back-channel logout token ids already used. Kept until the token would have expired anyway. CREATE TABLE seen_logout_jti ( jti TEXT PRIMARY KEY NOT NULL, expires_at TEXT NOT NULL ); -- Folders. Live names are unique per owner and parent. A NULL parent is written as '' so that the -- root level is covered too. CREATE TABLE folders ( id TEXT PRIMARY KEY NOT NULL, owner TEXT NOT NULL REFERENCES members (sub) ON DELETE RESTRICT, parent_id TEXT REFERENCES folders (id) ON DELETE RESTRICT, name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 255), created_at TEXT NOT NULL, updated_at TEXT NOT NULL, trashed_at TEXT, trash_root TEXT REFERENCES folders (id) ON DELETE SET NULL, CHECK (parent_id IS NULL OR parent_id <> id) ); CREATE UNIQUE INDEX folders_live_name ON folders (owner, COALESCE(parent_id, ''), name) WHERE trashed_at IS NULL; CREATE INDEX folders_parent ON folders (parent_id); CREATE INDEX folders_trash_root ON folders (trash_root); -- Files. `sha256` is the hex digest of the content; `blob_key` names the bytes in the blob store. CREATE TABLE files ( id TEXT PRIMARY KEY NOT NULL, owner TEXT NOT NULL REFERENCES members (sub) ON DELETE RESTRICT, folder_id TEXT REFERENCES folders (id) ON DELETE RESTRICT, name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 255), mime TEXT NOT NULL, size INTEGER NOT NULL CHECK (size >= 0), sha256 TEXT NOT NULL CHECK (length(sha256) = 64), blob_key TEXT NOT NULL UNIQUE, thumb_state TEXT NOT NULL DEFAULT 'none' CHECK (thumb_state IN ('none', 'pending', 'ready', 'failed', 'skip')), download_count INTEGER NOT NULL DEFAULT 0 CHECK (download_count >= 0), created_at TEXT NOT NULL, updated_at TEXT NOT NULL, trashed_at TEXT, trash_root TEXT REFERENCES folders (id) ON DELETE SET NULL ); CREATE UNIQUE INDEX files_live_name ON files (owner, COALESCE(folder_id, ''), name) WHERE trashed_at IS NULL; CREATE INDEX files_folder ON files (folder_id); CREATE INDEX files_trash_root ON files (trash_root); CREATE INDEX files_thumb_pending ON files (thumb_state) WHERE thumb_state = 'pending'; -- Per-person shares. `target_id` names a file or folder; it cannot be a foreign key because it points -- at either table, so the application removes grants when their target is purged. CREATE TABLE grants ( id TEXT PRIMARY KEY NOT NULL, target_kind TEXT NOT NULL CHECK (target_kind IN ('file', 'folder')), target_id TEXT NOT NULL, owner TEXT NOT NULL REFERENCES members (sub) ON DELETE RESTRICT, grantee TEXT NOT NULL REFERENCES members (sub) ON DELETE CASCADE, can_download INTEGER NOT NULL DEFAULT 0 CHECK (can_download IN (0, 1)), created_at TEXT NOT NULL, UNIQUE (target_kind, target_id, grantee) ); CREATE INDEX grants_grantee ON grants (grantee); -- Public share links. The token itself is never stored: `token_hash` is looked up, and `token_sealed` -- lets the owner see the URL again. CREATE TABLE links ( id TEXT PRIMARY KEY NOT NULL, token_hash TEXT NOT NULL UNIQUE, token_sealed TEXT NOT NULL, target_kind TEXT NOT NULL CHECK (target_kind IN ('file', 'folder')), target_id TEXT NOT NULL, owner TEXT NOT NULL REFERENCES members (sub) ON DELETE RESTRICT, can_download INTEGER NOT NULL DEFAULT 0 CHECK (can_download IN (0, 1)), password_hash TEXT, expires_at TEXT, revoked_at TEXT, created_at TEXT NOT NULL, last_used_at TEXT, use_count INTEGER NOT NULL DEFAULT 0 CHECK (use_count >= 0) ); CREATE INDEX links_target ON links (target_kind, target_id); CREATE INDEX links_owner ON links (owner); -- Resumable uploads in progress. Chunks are staged on disk until `finish` turns them into a file. CREATE TABLE uploads ( id TEXT PRIMARY KEY NOT NULL, owner TEXT NOT NULL REFERENCES members (sub) ON DELETE RESTRICT, folder_id TEXT REFERENCES folders (id) ON DELETE RESTRICT, name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 255), size INTEGER NOT NULL CHECK (size >= 0), chunk_size INTEGER NOT NULL CHECK (chunk_size > 0), state TEXT NOT NULL DEFAULT 'active' CHECK (state IN ('active', 'done', 'aborted')), file_id TEXT REFERENCES files (id) ON DELETE SET NULL, created_at TEXT NOT NULL, expires_at TEXT NOT NULL ); CREATE INDEX uploads_owner ON uploads (owner); CREATE INDEX uploads_expires ON uploads (expires_at); -- Namespaces for machine callers (other family apps). The key is the token's `svc` claim. CREATE TABLE services ( key TEXT PRIMARY KEY NOT NULL, quota_bytes INTEGER NOT NULL CHECK (quota_bytes >= 0), used_bytes INTEGER NOT NULL DEFAULT 0 CHECK (used_bytes >= 0), created_at TEXT NOT NULL ); -- Objects stored by services. The primary key is the service's own id for the object. CREATE TABLE objects ( service TEXT NOT NULL REFERENCES services (key) ON DELETE RESTRICT, external_id TEXT NOT NULL, size INTEGER NOT NULL CHECK (size >= 0), sha256 TEXT NOT NULL CHECK (length(sha256) = 64), content_type TEXT NOT NULL, name TEXT, blob_key TEXT NOT NULL UNIQUE, created_at TEXT NOT NULL, updated_at TEXT NOT NULL, PRIMARY KEY (service, external_id) ); -- Append-only record of changes. `data` is JSON and never holds secrets. CREATE TABLE audit_log ( id TEXT PRIMARY KEY NOT NULL, at TEXT NOT NULL, actor_id TEXT, actor_kind TEXT NOT NULL CHECK (actor_kind IN ('member', 'service', 'link', 'system')), action TEXT NOT NULL, target_kind TEXT NOT NULL, target_id TEXT, ip TEXT, data TEXT NOT NULL DEFAULT '{}' CHECK (json_valid(data)) ); CREATE INDEX audit_log_at ON audit_log (at); CREATE INDEX audit_log_target ON audit_log (target_kind, target_id);