Files
romm/backend/handler/database/device_save_sync_handler.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

216 lines
7.1 KiB
Python

from collections.abc import Sequence
from datetime import datetime, timezone
from sqlalchemy import delete, select, update
from sqlalchemy.orm import Session
from decorators.database import begin_session
from models.assets import CONTENT_HASH_MAX_LENGTH
from models.device import Device
from models.device_save_sync import DeviceSaveSync
from .base_handler import DBBaseHandler
def _clean_hash(value: str | None) -> str | None:
"""Absent, empty and over-long hashes all mean unknown."""
if not value or len(value) > CONTENT_HASH_MAX_LENGTH:
return None
return value
class DBDeviceSaveSyncHandler(DBBaseHandler):
@begin_session
def get_sync(
self,
device_id: str,
save_id: int,
session: Session = None, # type: ignore[assignment]
) -> DeviceSaveSync | None:
return session.scalar(
select(DeviceSaveSync)
.filter_by(device_id=device_id, save_id=save_id)
.limit(1)
)
@begin_session
def get_syncs_for_device_and_saves(
self,
device_id: str,
save_ids: list[int],
session: Session = None, # type: ignore[assignment]
) -> Sequence[DeviceSaveSync]:
if not save_ids:
return []
return session.scalars(
select(DeviceSaveSync).filter(
DeviceSaveSync.device_id == device_id,
DeviceSaveSync.save_id.in_(save_ids),
)
).all()
@begin_session
def get_syncs_for_saves(
self,
save_ids: list[int],
session: Session = None, # type: ignore[assignment]
) -> dict[int, list[tuple[DeviceSaveSync, str | None]]]:
"""Fetch every device sync row for the given saves, grouped by save id.
Each row is paired with its device name so callers can attribute a save
to the device that created it without triggering the lazy-raise
``DeviceSaveSync.device`` relationship.
"""
if not save_ids:
return {}
rows = session.execute(
select(DeviceSaveSync, Device.name)
.join(Device, DeviceSaveSync.device_id == Device.id)
.filter(DeviceSaveSync.save_id.in_(save_ids))
.order_by(DeviceSaveSync.last_synced_at.desc(), DeviceSaveSync.device_id)
).all()
grouped: dict[int, list[tuple[DeviceSaveSync, str | None]]] = {}
for sync, device_name in rows:
grouped.setdefault(sync.save_id, []).append((sync, device_name))
return grouped
@begin_session
def upsert_sync(
self,
device_id: str,
save_id: int,
synced_at: datetime | None = None,
last_sync_hash: str | None = None,
last_sync_server_hash: str | None = None,
session: Session = None, # type: ignore[assignment]
) -> DeviceSaveSync:
now = synced_at or datetime.now(timezone.utc)
client_hash = _clean_hash(last_sync_hash)
server_hash = _clean_hash(last_sync_server_hash)
existing = session.scalar(
select(DeviceSaveSync)
.filter_by(device_id=device_id, save_id=save_id)
.limit(1)
)
if existing:
session.execute(
update(DeviceSaveSync)
.where(
DeviceSaveSync.device_id == device_id,
DeviceSaveSync.save_id == save_id,
)
.values(
last_synced_at=now,
is_untracked=False,
last_sync_hash=client_hash,
last_sync_server_hash=server_hash,
)
.execution_options(synchronize_session="evaluate")
)
existing.last_synced_at = now
existing.is_untracked = False
existing.last_sync_hash = client_hash
existing.last_sync_server_hash = server_hash
return existing
else:
sync = DeviceSaveSync(
device_id=device_id,
save_id=save_id,
last_synced_at=now,
is_untracked=False,
last_sync_hash=client_hash,
last_sync_server_hash=server_hash,
)
session.add(sync)
session.flush()
return sync
@begin_session
def record_identical_content(
self,
device_id: str,
save_id: int,
content_hash: str | None,
synced_at: datetime | None = None,
session: Session = None, # type: ignore[assignment]
) -> None:
"""Record both sides holding ``content_hash``, unless untracked or already recorded."""
content_hash = _clean_hash(content_hash)
if not content_hash:
return
existing = self.get_sync(device_id, save_id, session=session)
if existing and (
existing.is_untracked
or (
existing.last_sync_hash == content_hash
and existing.last_sync_server_hash == content_hash
)
):
return
self.upsert_sync(
device_id,
save_id,
synced_at=synced_at,
last_sync_hash=content_hash,
last_sync_server_hash=content_hash,
session=session,
)
@begin_session
def set_untracked(
self,
device_id: str,
save_id: int,
untracked: bool,
session: Session = None, # type: ignore[assignment]
) -> DeviceSaveSync | None:
existing = session.scalar(
select(DeviceSaveSync)
.filter_by(device_id=device_id, save_id=save_id)
.limit(1)
)
if existing:
# While a save is untracked both sides can move, so the boundary
# recorded before it is no longer a statement about either side.
session.execute(
update(DeviceSaveSync)
.where(
DeviceSaveSync.device_id == device_id,
DeviceSaveSync.save_id == save_id,
)
.values(
is_untracked=untracked,
last_sync_hash=None,
last_sync_server_hash=None,
)
.execution_options(synchronize_session="evaluate")
)
existing.is_untracked = untracked
existing.last_sync_hash = None
existing.last_sync_server_hash = None
return existing
elif untracked:
now = datetime.now(timezone.utc)
sync = DeviceSaveSync(
device_id=device_id,
save_id=save_id,
last_synced_at=now,
is_untracked=True,
)
session.add(sync)
session.flush()
return sync
return None
@begin_session
def delete_syncs_for_device(
self,
device_id: str,
session: Session = None, # type: ignore[assignment]
) -> None:
session.execute(
delete(DeviceSaveSync)
.where(DeviceSaveSync.device_id == device_id)
.execution_options(synchronize_session="evaluate")
)