lore-forge/.ai/designs/storage.md

137 lines
6.5 KiB
Markdown

# Storage
Status: phase 1 schema built (`crates/forge-store/migrations/`: `0001_initial.sql`, and
`0002_accounts.sql` for invites, providers, API tokens and service accounts). Last updated 2026-11-25.
## What lore-forge stores
lore-forge stores everything about people, permissions and collaboration. It stores **no
repository data**; that is entirely in `loreserver`.
## SQLite
The owner chose SQLite, matching piki.
- **One binary, one data directory.** Running an instance means one process and one directory
to back up, alongside `loreserver`'s own storage.
- **Write-ahead logging** so readers do not block the single writer.
- **Numbered SQL migrations** applied at startup, as in piki.
- **No high availability.** One instance, one database file. This fits the assumed audience of
a single self-hosted studio or community.
### The token exchange path must not write
Every active Lore client exchanges a token for each repository it uses about every five
minutes. With SQLite's single writer, that path is kept read-only: it resolves grants and
signs. Anything recorded about exchanges (last-used times, audit entries) is batched and
written periodically, not per request. See [token-issuer.md](token-issuer.md).
### Library
**Decided (owner, 2026-11-25):** `sqlx`, with its built-in migrations embedded in the binary.
Queries use sqlx's runtime-checked functions (`query`, `query_as` with `FromRow`), not its
compile-time `query!` macros, which need a live database or a committed `.sqlx` cache and
`sqlx-cli` on every schema change. sqlx 0.9 refuses SQL built at runtime, so every query is a
static string. Moving to the macros later is a mechanical change.
Before 0.1.0, migrations may be edited in place rather than followed by new ones, as piki does
before its first release. `0002_accounts.sql` was added as a new migration anyway, so
deployments already started from `run/` keep their data.
## Data directory
| Item | Contents |
| --- | --- |
| Main database | All tables below |
| Render cache | Rendered and highlighted trees, files and diffs, keyed by content hash. Disposable; kept out of the main database so backups stay small |
| Generated `loreserver` config | The auth settings lore-forge produces for the Lore server |
## Secrets
One 32-byte key, supplied through the environment (`LORE_FORGE_SECRET_KEY`), a file
(`LORE_FORGE_SECRET_KEY_FILE`) or the systemd credential `secret-key`, encrypts:
- provider client secrets (GitHub, Discord, Google);
- lore-forge's private token signing keys.
The key is not stored in the data directory. A deployment from `deploy/` keeps it in
`/etc/lore-forge/secret.key` (root only). `scripts/start.sh` keeps it in `run/secret.key`
(mode 600), next to the data directory `run/forge-data/` rather than inside it, and tells the
operator to back it up separately. As in piki, losing it means provider secrets must
be re-entered; here it also means new signing keys, which invalidates outstanding tokens and
requires `loreserver` to pick up the new keys.
Stored only as hashes, never recoverable: passwords, session tokens, refresh tokens, API
tokens, device codes, invite-flow state.
## Tables (phase 1)
### Accounts — following piki
| Table | Holds |
| --- | --- |
| `users` | Username, display name, password hash, instance role, disabled flag, kind (`person` or `service`), owning namespace for service accounts |
| `sessions` | Hashed token, user, method used, user agent, created, expires, revoked |
| `invite_keys`, `invite_key_uses` | Key, name, maximum uses, active flag; who used it |
| `oauth_provider_configs` | Provider, client ID, encrypted secret, enabled |
| `oauth_identities` | `(provider, subject id)` → user; unique per user and provider |
| `oauth_flows` | Hashed state, intent, PKCE verifier, nonce, expiry, used |
| `passkey_credentials`, `passkey_ceremonies` | Public keys; single-use ceremony state. Not created; passkeys are dropped for now |
| `settings` | Key-value instance settings: `signup.mode`, `orgs.create`. The canonical URLs come from the environment |
### Namespaces and access
| Table | Holds |
| --- | --- |
| `namespaces` | Name, kind (user or organisation). The single name table |
| `orgs`, `org_members` | Organisation details, base preset, whether members may create repositories; member and organisation role |
| `teams`, `team_members` | Teams within an organisation |
| `repos` | Lore repository ID, namespace, name, base permission, per-branch landing settings |
| `grants` | Repository, subject (user, team or service account), preset, extra actions |
### Issuer
| Table | Holds |
| --- | --- |
| `signing_keys` | Key ID, algorithm, encrypted private key, public key, state |
| `device_codes` | Hashed device code, user code, client, status, polling interval, expiry |
| `cli_logins` | One per `lore auth login`: user, client, created, last used, absolute expiry, revoked, the API token it started from. Shown in the session list |
| `refresh_tokens` | Hashed token, its login, created, superseded (for rotation and reuse detection) |
| `api_tokens` | Hashed token, owner, name, scopes, expiry, last used, revoked |
Service accounts have no table of their own: they are `users` rows with `kind = 'service'`.
### Shared
| Table | Holds |
| --- | --- |
| `audit_log` | Who did what, when: sign-ins, grant changes, settings changes, landings |
### Later phases
Change requests, reviews, comments, activity events, webhooks and their deliveries, check
statuses. Full-text search can use SQLite's FTS5.
## Backups
Built (2026-11-26); see [deployment.md](deployment.md) and `deploy/README.md`.
- `lore-forge backup-database <file>` writes a consistent copy of the main database with
`VACUUM INTO` while lore-forge runs. It opens the database read-only and without
migrations, so running it as root leaves no files the service cannot open. The copy is
integrity-checked before the command returns.
- `scripts/backup.sh` takes the database and `loreserver`'s store at the same moment. It
copies the store live, stops `loreserver`, copies again with the database, then restarts
`loreserver`. Backups are timestamped directories, hard-linked to the previous one.
- `scripts/restore.sh` puts one back, moving what it replaces aside.
- The encryption key is never in a backup; it is kept separately.
- The render cache does not need backing up.
- Verified by `scripts/e2e-lore.sh`: a revision pushed just before the backup is restored, and
one pushed after it is not.
## Open questions
- How long to retain audit entries and activity events.
- Whether exchanged-token activity is audited at all.