93182fa251
API surface for the web replay viewer to come:
- `POST /api/replays` — auth required; persists the JSON body
verbatim, mints a server-side UUID, returns `{id}`. Three columns
(final_score, time_seconds, recorded_at) are projected out of the
payload at insert time so list endpoints don't have to scan blobs.
- `GET /api/replays/recent` — public; returns the N most-recent
replays across users (limit defaults to 20, capped at 50). Joins
the username so the feed reads as "AliceWon · 2:14 win".
- `GET /api/replays/:id` — public; returns the full replay JSON
the desktop client uploaded.
Migration `002_replays.sql` adds the `replays` table with indexes
on `received_at DESC` (recent feed) and `user_id` (per-user views).
Schema-version compatibility is the playback side's responsibility,
matching the desktop's existing `schema_version` gate — the server
just stores and serves whatever JSON came in.
`AppError::NotFound` added so `GET /api/replays/:id` can return a
proper 404 instead of an internal-server-error.
`.sqlx` cache regenerated for the new `query!` invocations.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
34 lines
1.8 KiB
SQL
34 lines
1.8 KiB
SQL
-- Migration 002: winning-replay storage
|
|
--
|
|
-- One row per winning replay uploaded via POST /api/replays. The replay
|
|
-- itself is stored as the canonical JSON the desktop client wrote — it
|
|
-- already carries a schema_version field, so the server doesn't need to
|
|
-- shape-validate the payload beyond ensuring it parses as JSON.
|
|
--
|
|
-- The handful of denormalised columns (final_score, time_seconds,
|
|
-- recorded_at) are projected out of the JSON at insert time so list
|
|
-- endpoints (e.g. recent / per-user / leaderboard-style sorts) can be
|
|
-- served via a covering query without touching every row's blob.
|
|
|
|
CREATE TABLE IF NOT EXISTS replays (
|
|
id TEXT PRIMARY KEY, -- UUID v4 minted server-side
|
|
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
seed INTEGER NOT NULL, -- replay's deal seed
|
|
draw_mode TEXT NOT NULL, -- "DrawOne" | "DrawThree"
|
|
mode TEXT NOT NULL, -- "Classic" | "Zen" | "Challenge" | "TimeAttack"
|
|
time_seconds INTEGER NOT NULL, -- duration of the win
|
|
final_score INTEGER NOT NULL, -- final score at the win
|
|
recorded_at TEXT NOT NULL, -- replay-side date (YYYY-MM-DD)
|
|
received_at TEXT NOT NULL, -- server insert timestamp (ISO 8601)
|
|
replay_json TEXT NOT NULL -- full Replay serialisation
|
|
);
|
|
|
|
-- Recent-replays list endpoint sorts by received_at DESC; the index
|
|
-- keeps that scan cheap on a populated table.
|
|
CREATE INDEX IF NOT EXISTS replays_received_at_idx
|
|
ON replays(received_at DESC);
|
|
|
|
-- Lookups by user (e.g. "my replays" view) are common too.
|
|
CREATE INDEX IF NOT EXISTS replays_user_id_idx
|
|
ON replays(user_id);
|