feat(api): offline-first sync layer
Implements the stubbed /sync endpoints: - GET /sync/changes — delta pull (likes, plays, changed playlists with their track ids, changed catalogue tracks) over the half-open window (since, cursor]; the cursor is the DB clock, so it's immune to app/DB skew. - POST /sync/push — idempotent append of client like/play events (ON CONFLICT DO NOTHING by client-supplied id); events for tracks the server doesn't have are skipped (graceful degradation). Adds a server-ingestion column `synced_at` to the likes + play_history event logs (migration) as the delta ordering key, so an event pushed with an older event time still surfaces for other devices. Repos gain list_since/add_event (likes, history), list_changed_since (playlists, tracks) and playlist.track_ids; wired via SyncService in deps. Also fixes test isolation exposed by the registry-backed /admin/sources endpoint: test_sources_api clears the process-cached source registry, and test_admin_api no longer hardcodes the environment name. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -266,6 +266,19 @@ class SqlAlchemyTrackRepository:
|
||||
)
|
||||
).scalar_one()
|
||||
|
||||
async def list_changed_since(
|
||||
self, *, since: dt.datetime | None, until: dt.datetime
|
||||
) -> list[Track]:
|
||||
"""Catalogue tracks changed in the window ``(since, until]`` (by
|
||||
``updated_at``), oldest first — the delta a client caches for offline use.
|
||||
Defined before ``list`` (name-shadowing)."""
|
||||
stmt = select(TrackModel).where(TrackModel.updated_at <= until)
|
||||
if since is not None:
|
||||
stmt = stmt.where(TrackModel.updated_at > since)
|
||||
stmt = stmt.order_by(TrackModel.updated_at)
|
||||
rows = (await self._session.execute(stmt)).scalars().all()
|
||||
return [_to_entity(r) for r in rows]
|
||||
|
||||
async def list(
|
||||
self,
|
||||
*,
|
||||
|
||||
Reference in New Issue
Block a user