Files
romm/backend/models/device_save_sync.py
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

46 lines
1.5 KiB
Python

from __future__ import annotations
from datetime import datetime
from typing import TYPE_CHECKING
from sqlalchemy import TIMESTAMP, Boolean, ForeignKey, String
from sqlalchemy.orm import Mapped, mapped_column, relationship
from models.assets import CONTENT_HASH_MAX_LENGTH
from models.base import BaseModel
if TYPE_CHECKING:
from models.assets import Save
from models.device import Device
class DeviceSaveSync(BaseModel):
__tablename__ = "device_save_sync"
__table_args__ = {"extend_existing": True}
device_id: Mapped[str] = mapped_column(
String(255),
ForeignKey("devices.id", ondelete="CASCADE"),
primary_key=True,
)
save_id: Mapped[int] = mapped_column(
ForeignKey("saves.id", ondelete="CASCADE"),
primary_key=True,
index=True,
)
last_synced_at: Mapped[datetime] = mapped_column(TIMESTAMP(timezone=True))
is_untracked: Mapped[bool] = mapped_column(Boolean, default=False)
# Content each side held at `last_synced_at`, so a later comparison can prove
# a side did not change regardless of timestamps or hashing scheme.
last_sync_hash: Mapped[str | None] = mapped_column(
String(length=CONTENT_HASH_MAX_LENGTH)
)
last_sync_server_hash: Mapped[str | None] = mapped_column(
String(length=CONTENT_HASH_MAX_LENGTH)
)
device: Mapped[Device] = relationship(back_populates="save_syncs", lazy="raise")
save: Mapped[Save] = relationship(back_populates="device_syncs", lazy="raise")