Commit Graph

3 Commits

Author SHA1 Message Date
Sam Dornan
5b3d0bf544 feat(sync): record a per-device save baseline to prove what did not change (#4768)
compare_save_state decides a side changed from its timestamp, and
emulators rewrite a save on exit, so an untouched save looks new and a
server change elsewhere turns into a false conflict.

device_save_sync now records what each side held at the last sync
(last_sync_hash from the device, last_sync_server_hash from the server,
migration 0140). A baseline match proves that side unchanged; a mismatch
or NULL falls back to the timestamp logic, so this only removes
conflicts. It runs after the removed_at check in compare_save_state.

- Baselines are recorded only for versions the device provably holds:
  confirm_download pins to the served version, update_save records the
  client hash only when the file was uploaded, deduplicated uploads and
  pushed saves record against the surviving save, and push-pull no-ops
  record hashes only for identical content.
- Identical-content no-ops in negotiate and the watcher record a
  boundary without re-tracking untracked saves.
- The SSH push-pull and file-transfer watcher paths hash device saves
  through hash_save_file, matching compute_content_hash for zipped
  saves, and treat unreadable saves as unknown.

Squashes rommapp/romm#4768 onto master.



Claude-Session: https://claude.ai/code/session_01WAKUdfLb6fyduEqaFbf5A8

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 15:05:57 -05:00
Sam Dornan
a429c4b9db perf(backend): add the missing gallery indexes and reconcile model/schema drift
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
2026-09-07 13:46:35 +00:00
nendo
36eec298d1 Add device-based save synchronization
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
2026-01-18 16:50:44 +09:00