lore-forge/crates/forge-store/migrations/0001_initial.sql

168 lines
6.7 KiB
SQL

-- Phase 1 schema: accounts, namespaces and access, and the token issuer.
-- Times are Unix seconds. Secrets are stored only as SHA-256 hashes or AES-GCM ciphertext.
-- Before 0.1.0 this migration may be rewritten in place rather than followed by new ones.
-- Accounts -------------------------------------------------------------------------------
CREATE TABLE users (
-- Stable internal ID. Lore records it in revision history as the `identity` claim.
id TEXT PRIMARY KEY,
username TEXT NOT NULL UNIQUE COLLATE NOCASE,
display_name TEXT NOT NULL,
-- NULL when the account cannot sign in with a password.
password_hash TEXT,
instance_role TEXT NOT NULL CHECK (instance_role IN ('owner', 'admin', 'member')),
disabled INTEGER NOT NULL DEFAULT 0 CHECK (disabled IN (0, 1)),
created_at INTEGER NOT NULL
);
CREATE TABLE sessions (
id TEXT PRIMARY KEY,
token_hash BLOB NOT NULL UNIQUE,
user_id TEXT NOT NULL REFERENCES users (id) ON DELETE CASCADE,
auth_method TEXT NOT NULL CHECK (auth_method IN ('password', 'provider', 'passkey')),
user_agent TEXT NOT NULL DEFAULT '',
persistent INTEGER NOT NULL CHECK (persistent IN (0, 1)),
created_at INTEGER NOT NULL,
last_seen_at INTEGER NOT NULL,
expires_at INTEGER NOT NULL,
revoked_at INTEGER
);
CREATE INDEX sessions_user ON sessions (user_id);
CREATE TABLE settings (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
);
-- Namespaces and access ------------------------------------------------------------------
CREATE TABLE orgs (
id TEXT PRIMARY KEY,
display_name TEXT NOT NULL,
-- Preset every member holds on every repository of the organisation, or NULL for none.
base_preset TEXT CHECK (base_preset IN ('reader', 'writer', 'maintainer', 'admin')),
-- Whether members who are not owners may create repositories.
members_can_create_repos INTEGER NOT NULL DEFAULT 0 CHECK (members_can_create_repos IN (0, 1)),
created_at INTEGER NOT NULL
);
-- The single name table shared by users and organisations, so the two cannot collide.
CREATE TABLE namespaces (
name TEXT PRIMARY KEY COLLATE NOCASE,
kind TEXT NOT NULL CHECK (kind IN ('user', 'org')),
user_id TEXT UNIQUE REFERENCES users (id) ON DELETE CASCADE,
org_id TEXT UNIQUE REFERENCES orgs (id) ON DELETE CASCADE,
CHECK ((kind = 'user' AND user_id IS NOT NULL AND org_id IS NULL)
OR (kind = 'org' AND org_id IS NOT NULL AND user_id IS NULL))
);
CREATE TABLE org_members (
org_id TEXT NOT NULL REFERENCES orgs (id) ON DELETE CASCADE,
user_id TEXT NOT NULL REFERENCES users (id) ON DELETE CASCADE,
role TEXT NOT NULL CHECK (role IN ('owner', 'member')),
PRIMARY KEY (org_id, user_id)
);
CREATE INDEX org_members_user ON org_members (user_id);
CREATE TABLE teams (
id TEXT PRIMARY KEY,
org_id TEXT NOT NULL REFERENCES orgs (id) ON DELETE CASCADE,
name TEXT NOT NULL COLLATE NOCASE,
UNIQUE (org_id, name)
);
CREATE TABLE team_members (
team_id TEXT NOT NULL REFERENCES teams (id) ON DELETE CASCADE,
user_id TEXT NOT NULL REFERENCES users (id) ON DELETE CASCADE,
PRIMARY KEY (team_id, user_id)
);
CREATE INDEX team_members_user ON team_members (user_id);
CREATE TABLE repos (
-- Lore's repository ID: 16 bytes as 32 lowercase hex digits.
id TEXT PRIMARY KEY CHECK (length(id) = 32),
namespace TEXT NOT NULL REFERENCES namespaces (name) COLLATE NOCASE,
name TEXT NOT NULL COLLATE NOCASE,
created_by TEXT REFERENCES users (id) ON DELETE SET NULL,
created_at INTEGER NOT NULL,
UNIQUE (namespace, name)
);
CREATE TABLE grants (
repo_id TEXT NOT NULL REFERENCES repos (id) ON DELETE CASCADE,
subject_kind TEXT NOT NULL CHECK (subject_kind IN ('user', 'team')),
subject_id TEXT NOT NULL,
preset TEXT CHECK (preset IN ('reader', 'writer', 'maintainer', 'admin', 'owner')),
-- Space-separated Lore actions granted on top of the preset.
extra_actions TEXT NOT NULL DEFAULT '',
PRIMARY KEY (repo_id, subject_kind, subject_id)
);
CREATE INDEX grants_subject ON grants (subject_kind, subject_id);
-- Issuer ---------------------------------------------------------------------------------
CREATE TABLE signing_keys (
kid TEXT PRIMARY KEY,
alg TEXT NOT NULL CHECK (alg = 'ES256'),
-- AES-GCM ciphertext of the private scalar, under the environment-supplied key.
private_key BLOB NOT NULL,
public_jwk TEXT NOT NULL,
state TEXT NOT NULL CHECK (state IN ('current', 'previous', 'retired')),
created_at INTEGER NOT NULL
);
CREATE UNIQUE INDEX signing_keys_one_current ON signing_keys (state) WHERE state = 'current';
CREATE TABLE device_codes (
id TEXT PRIMARY KEY,
device_code_hash BLOB NOT NULL UNIQUE,
user_code TEXT NOT NULL UNIQUE,
client_id TEXT NOT NULL,
scope TEXT NOT NULL DEFAULT '',
-- What the requesting device said about itself, shown on the approval page.
requested_by TEXT NOT NULL DEFAULT '',
status TEXT NOT NULL CHECK (status IN ('pending', 'approved', 'denied', 'consumed')),
user_id TEXT REFERENCES users (id) ON DELETE CASCADE,
interval_secs INTEGER NOT NULL,
created_at INTEGER NOT NULL,
expires_at INTEGER NOT NULL,
last_polled_at INTEGER
);
-- One row per Lore CLI login. Its refresh tokens rotate; revoking the row ends the login.
CREATE TABLE cli_logins (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES users (id) ON DELETE CASCADE,
client_id TEXT NOT NULL,
scope TEXT NOT NULL DEFAULT '',
description TEXT NOT NULL DEFAULT '',
created_at INTEGER NOT NULL,
last_used_at INTEGER NOT NULL,
-- Absolute end of the login, regardless of use.
expires_at INTEGER NOT NULL,
revoked_at INTEGER
);
CREATE INDEX cli_logins_user ON cli_logins (user_id);
CREATE TABLE refresh_tokens (
token_hash BLOB PRIMARY KEY,
login_id TEXT NOT NULL REFERENCES cli_logins (id) ON DELETE CASCADE,
created_at INTEGER NOT NULL,
-- Set when the token is rotated out. A rotated-out token presented again after the grace
-- period ends the login.
superseded_at INTEGER
);
CREATE INDEX refresh_tokens_login ON refresh_tokens (login_id);
-- Shared ---------------------------------------------------------------------------------
CREATE TABLE audit_log (
id INTEGER PRIMARY KEY AUTOINCREMENT,
at INTEGER NOT NULL,
actor_id TEXT,
action TEXT NOT NULL,
target TEXT NOT NULL DEFAULT '',
detail TEXT NOT NULL DEFAULT ''
);
CREATE INDEX audit_log_at ON audit_log (at);