Four query shapes had no index that could serve both their predicate and
their sort, and a class of foreign-key columns was relying on an implicit
index that only MariaDB and MySQL provide.
New portable indexes:
- `screenshots (rom_id, user_id)`. `Save.screenshot` / `State.screenshot`
resolve a thumbnail per row, so this lookup runs once per card on the
continue-playing rail. 0093 added the equivalent to `states` and noted
`saves` already had it; `screenshots` was missed.
- `rom_user (user_id, rom_id)` and `rom_user (user_id, last_played)`.
`unique_rom_user_props` leads with `rom_id`, which covers the gallery's
outer join but not the reverse direction, where sorting or scoping by a
per-user column starts from this table.
- `roms (platform_id, name_sort_key)`. The gallery is browsed one platform
at a time and ordered by name, and no index carried both columns. On a
120k-row library across 20 platforms this takes the first page from
5.8ms/1568 buffers to 0.6ms/75 buffers. Deep offsets converge, since the
skipped rows are walked either way.
New PostgreSQL-only foreign-key indexes:
MariaDB and MySQL index every foreign key whose column is not already some
index's leftmost prefix; PostgreSQL does not. Eight such columns are queried
directly or walked by `ON DELETE`, so they get a real index there. Creating
them everywhere would leave the other backends with duplicates, so they are
dialect-gated and exempt from autogenerate.
Model/schema reconciliation:
Fifteen live indexes existed only in the migrations. Worse, `alembic/env.py`
never imported six models, so `devices`, `client_tokens`, `device_save_sync`,
`sync_sessions`, `play_sessions` and `streaming_container_adoptions` were
absent from the metadata autogenerate compares against, and autogenerate in
fact failed outright resolving `saves.origin_device_id`. With the imports
fixed it would have proposed dropping every one of those indexes.
Declares all of them on their models, matches `PlaySession` to the unique
index 0076 actually created, and adds a test that fails on any future index
drift so this cannot silently recur.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WgeQBXh6SJyxXVHSL5EuoF
Implement device registration and save sync tracking to enable
multi-device save management with conflict detection.
- Device CRUD endpoints (POST/GET/PUT/DELETE /api/devices)
- Save sync state tracking per device
- Conflict detection on upload (409 when device has stale sync)
- Download sync tracking (optimistic and confirmed modes)
- Track/untrack saves per device
- DEVICES_READ/WRITE scopes for authorization