Files
token-circles/worker/migrations/0034_user_state.sql
Komediruzecki 3171ec61f5 feat(worker): user_state keeps what belongs to the account, at /api/account/state
Migration 0034 adds user_state (user_id, key, value, updated_at). GET
/api/account/state answers every key, null where nothing is stored; PUT
/api/account/state/:key writes one. Both take a session only and serve the
session's own account.

The keys are an allowlist (shared/accountStateKeys.ts): onboarding, tours and
getting_started. Each value is rebuilt from checked fields
(shared/accountState.ts), capped at 1 KB, and refused with 400 and the field
named. The onboarding record keeps the furthest step, stamps started_at once
with server time and never changes after it finishes. A write stores its result
only while the row still holds what it read, so a stale tab cannot undo a finish.

Deleting the account deletes its rows, and the deletion test now walks the
schema for every table with a user_id column.
2026-10-11 19:17:01 +02:00

23 lines
1.2 KiB
SQL

-- State that belongs to a person's account rather than to the browser it is used in (migration
-- 0034): the setup wizard's record, the guided tours finished, and whether the Dashboard's
-- Getting started card is hidden. These lived in localStorage, so a new device or a fresh
-- browser forgot them. Local-first mode still keeps them there; it has no account.
--
-- One small row per account and key. The keys are an allowlist and every value is JSON the
-- Worker builds from checked fields, capped at 1 KB (shared/accountStateKeys.ts and
-- shared/accountState.ts; routes in worker/src/routes/account-state.ts). Deleting the account
-- deletes its rows (routes/account.ts).
--
-- Named user_state, not account_state: in this schema "account" is a bank account (accounts,
-- account_balance_history). Checked against every prior migration: no table or index of this
-- name exists, so CREATE TABLE IF NOT EXISTS cannot silently keep an older one (see
-- auth_sessions in 0024).
CREATE TABLE IF NOT EXISTS user_state (
user_id INTEGER NOT NULL,
-- 'onboarding' | 'tours' | 'getting_started'
key TEXT NOT NULL,
value TEXT NOT NULL,
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
PRIMARY KEY (user_id, key)
);