PostgreSQL 17. The schema is owned by migrations in
server/src/database/migrations (ADR 0004);
TypeORM never synchronises it. Diagram: ERD.
| Column | Type | Notes |
|---|---|---|
id |
uuid PK |
gen_random_uuid() (pgcrypto) |
email |
varchar(254) |
Unique (UQ_users_email); must be lower-case (CHK_users_email_lowercase). The API lower-cases and trims input. |
username |
varchar(20) |
3–20 characters (CHK_users_username_length); letters, digits, ., -, _ enforced by the DTO. Not unique. |
password_hash |
varchar(255) |
Argon2id (19 MiB memory, 2 iterations). Excluded from every API response. |
email_verified_at |
timestamptz null |
Set when the verification link is used. |
created_at, updated_at |
timestamptz |
| Column | Type | Notes |
|---|---|---|
id |
uuid PK |
|
user_id |
uuid FK → users.id |
ON DELETE CASCADE: deleting an account deletes its solves. |
client_id |
uuid |
Idempotency key generated by the client; unique per user (UQ_solves_user_client). See ADR 0003. |
puzzle |
varchar(8) |
WCA event id; CHK_solves_puzzle allows 222, 333, 444, 555, 666, 777. |
time_ms |
integer |
Raw timer reading, before penalties. CHK_solves_time_range: greater than 0 and at most 86,400,000 (24 h). |
penalty |
solve_penalty enum |
none, plus2, dnf; default none. Kept separate from the time so a penalty can be removed again. |
scramble |
varchar(600) |
Validated as WCA move notation by the API. |
created_at, updated_at |
timestamptz |
created_at is the solve time and defines the order. |
Refresh, e-mail verification and password-reset tokens.
| Column | Type | Notes |
|---|---|---|
id |
uuid PK |
|
user_id |
uuid FK → users.id |
ON DELETE CASCADE |
type |
auth_token_type enum |
refresh, email_verification, password_reset |
token_hash |
char(64) |
SHA-256 of the token, unique. The token itself is never stored. |
expires_at |
timestamptz |
|
used_at |
timestamptz null |
Set atomically when consumed (UPDATE ... WHERE used_at IS NULL). |
revoked_at |
timestamptz null |
Set on logout, logout everywhere, password change or reset, and refresh-token reuse. |
user_agent, ip |
varchar null |
Context of the session that created the token. |
created_at |
timestamptz |
Expired tokens are deleted one day after expiry by a timer in the API that runs every 6 hours.
| Index | Serves |
|---|---|
IDX_solves_user_puzzle_created (user_id, puzzle, created_at DESC, id DESC) |
Every solve read: paginated history (newest first), statistics and trend (read oldest first, same index backwards), CSV export, and per-puzzle delete. id makes the order total for stable paging. |
UQ_solves_user_client (user_id, client_id) |
Idempotent insert (ON CONFLICT DO NOTHING) and the follow-up lookup. |
UQ_users_email |
Login and signup lookups. |
UQ_auth_tokens_hash |
Looking up a presented token. |
IDX_auth_tokens_user_type (user_id, type) |
Revoking all tokens of one type for a user. |
IDX_auth_tokens_expires (expires_at) |
Purging expired tokens. |
The per-puzzle solve count (GET /solves/summary) is a GROUP BY puzzle
over the user's rows, which uses the leading user_id column of the solves
index.
Validation happens in DTOs first, but the database is the last line:
puzzle list, time range, penalty enum, lower-case e-mail, username length,
uniqueness and foreign keys. A violation that slips past the DTOs becomes a
400 (check or foreign key) or 409 (unique) from the exception filter,
never a 500 with SQL in it.
server/src/database/typeorm.config.ts: pool size DATABASE_POOL_MAX
(default 10), 5 s connection timeout, 30 s idle timeout, 10 s
statement_timeout. DATABASE_SSL=true enables TLS with certificate
verification; DATABASE_CA_CERT supplies a provider CA bundle.
cd server
# after changing an entity
npm run migration:generate -- src/database/migrations/<Name>
# review the generated SQL, then
npm run migration:run
npm run migration:revert # undo the last oneIn containers the API image's entrypoint runs migrate, migrate-revert or
seed:
docker compose run --rm migrateRules:
- Never edit a migration that has been applied anywhere; add a new one.
- Every migration has a working
down. The API e2e setup runs migrations up, down and up again on the test database, so a brokendownfails CI. - Adding a puzzle needs a migration that changes
CHK_solves_puzzle, an entry inserver/src/solves/puzzles.tsand a scramble generator in the client. - Production deploys back up the database before migrating
(
scripts/deploy.sh).
server/src/database/seed.ts (npm run seed, or
docker compose run --rm seed) is for development only. It is idempotent:
users are created if missing, and solves are generated only for a
user/puzzle pair that has none. A fixed PRNG seed makes the data the same on
every run. All data is fictional.
| Account | Password | Solves |
|---|---|---|
demo@cstimer.dev |
Demo-password-1 |
420 3x3 over 60 days (improving from about 24 s to 15.5 s), 150 2x2, 60 4x4 |
alex@cstimer.dev |
Demo-password-1 |
25 3x3 |
About 2.5% of seeded solves are DNF and 4.5% are +2. Both accounts are pre-verified.
See operations-runbook.md and
disaster-recovery.md. scripts/backup-db.sh writes a
pg_dump custom-format archive and verifies it lists the solves table;
scripts/restore-db.sh restores into a scratch database by default.