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:
@@ -37,3 +37,12 @@ class LikeModel(UUIDPrimaryKeyMixin, Base):
|
||||
server_default=func.now(),
|
||||
nullable=False,
|
||||
)
|
||||
# Server ingestion time — the delta-sync ordering key. Set once at insert and
|
||||
# never changed; distinct from ``created_at`` (the real event time, which a
|
||||
# sync push preserves from the client even when it happened offline earlier).
|
||||
synced_at: Mapped[dt.datetime] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
server_default=func.now(),
|
||||
nullable=False,
|
||||
index=True,
|
||||
)
|
||||
|
||||
@@ -34,3 +34,11 @@ class PlayHistoryModel(UUIDPrimaryKeyMixin, Base):
|
||||
)
|
||||
play_duration_seconds: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||||
completed: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
|
||||
# Server ingestion time — the delta-sync ordering key (see LikeModel). Distinct
|
||||
# from ``played_at`` (the real play time), which a sync push preserves.
|
||||
synced_at: Mapped[dt.datetime] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
server_default=func.now(),
|
||||
nullable=False,
|
||||
index=True,
|
||||
)
|
||||
|
||||
@@ -4,6 +4,7 @@ import datetime as dt
|
||||
import uuid
|
||||
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.dialects.postgresql import insert as pg_insert
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.domain.entities.history import PlayHistoryEntry
|
||||
@@ -46,6 +47,51 @@ class SqlAlchemyHistoryRepository:
|
||||
await self._session.refresh(row)
|
||||
return _to_entity(row)
|
||||
|
||||
async def add_event(
|
||||
self,
|
||||
*,
|
||||
id: uuid.UUID,
|
||||
user_id: uuid.UUID,
|
||||
track_id: uuid.UUID,
|
||||
played_at: dt.datetime,
|
||||
play_duration_seconds: int | None,
|
||||
completed: bool,
|
||||
) -> bool:
|
||||
"""Idempotent append for sync push: insert a client-generated play event,
|
||||
skipping it if the ``id`` already exists (a replay). Returns whether a new
|
||||
row was stored. Defined before ``list`` (name-shadowing)."""
|
||||
stmt = (
|
||||
pg_insert(PlayHistoryModel)
|
||||
.values(
|
||||
id=id,
|
||||
user_id=user_id,
|
||||
track_id=track_id,
|
||||
played_at=played_at,
|
||||
play_duration_seconds=play_duration_seconds,
|
||||
completed=completed,
|
||||
)
|
||||
.on_conflict_do_nothing(index_elements=["id"])
|
||||
.returning(PlayHistoryModel.id)
|
||||
)
|
||||
inserted = (await self._session.execute(stmt)).scalar_one_or_none()
|
||||
return inserted is not None
|
||||
|
||||
async def list_since(
|
||||
self, user_id: uuid.UUID, *, since: dt.datetime | None, until: dt.datetime
|
||||
) -> list[PlayHistoryEntry]:
|
||||
"""Play events for a user ingested in the half-open window ``(since,
|
||||
until]`` (by ``synced_at``, the server-side sync key — so events pushed
|
||||
with an older ``played_at`` still surface), oldest first. ``since=None``
|
||||
returns everything up to ``until``."""
|
||||
stmt = select(PlayHistoryModel).where(
|
||||
PlayHistoryModel.user_id == user_id, PlayHistoryModel.synced_at <= until
|
||||
)
|
||||
if since is not None:
|
||||
stmt = stmt.where(PlayHistoryModel.synced_at > since)
|
||||
stmt = stmt.order_by(PlayHistoryModel.synced_at)
|
||||
rows = (await self._session.execute(stmt)).scalars().all()
|
||||
return [_to_entity(r) for r in rows]
|
||||
|
||||
async def list(self, *, user_id: uuid.UUID, limit: int, offset: int) -> list[PlayHistoryEntry]:
|
||||
rows = (
|
||||
(
|
||||
|
||||
@@ -3,9 +3,11 @@
|
||||
Likes are an append-only event log. Current state = latest event per (user, track).
|
||||
"""
|
||||
|
||||
import datetime as dt
|
||||
import uuid
|
||||
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.dialects.postgresql import insert as pg_insert
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.domain.entities.like import Like
|
||||
@@ -59,6 +61,50 @@ class SqlAlchemyLikeRepository:
|
||||
await self._session.refresh(row)
|
||||
return _to_entity(row)
|
||||
|
||||
async def add_event(
|
||||
self,
|
||||
*,
|
||||
id: uuid.UUID,
|
||||
user_id: uuid.UUID,
|
||||
track_id: uuid.UUID,
|
||||
value: str,
|
||||
created_at: dt.datetime,
|
||||
) -> bool:
|
||||
"""Idempotent append for sync push: insert a client-generated like event,
|
||||
skipping it if the ``id`` already exists (a replay). Preserves the
|
||||
client's ``created_at`` (the event happened offline earlier). Returns
|
||||
whether a new row was stored."""
|
||||
stmt = (
|
||||
pg_insert(LikeModel)
|
||||
.values(
|
||||
id=id,
|
||||
user_id=user_id,
|
||||
track_id=track_id,
|
||||
value=value,
|
||||
created_at=created_at,
|
||||
)
|
||||
.on_conflict_do_nothing(index_elements=["id"])
|
||||
.returning(LikeModel.id)
|
||||
)
|
||||
inserted = (await self._session.execute(stmt)).scalar_one_or_none()
|
||||
return inserted is not None
|
||||
|
||||
async def list_since(
|
||||
self, user_id: uuid.UUID, *, since: dt.datetime | None, until: dt.datetime
|
||||
) -> list[Like]:
|
||||
"""Like events for a user ingested in the half-open window ``(since,
|
||||
until]`` (by ``synced_at``, the server-side sync key — so events pushed
|
||||
with an older ``created_at`` still surface), oldest first. ``since=None``
|
||||
returns everything up to ``until``."""
|
||||
stmt = select(LikeModel).where(
|
||||
LikeModel.user_id == user_id, LikeModel.synced_at <= until
|
||||
)
|
||||
if since is not None:
|
||||
stmt = stmt.where(LikeModel.synced_at > since)
|
||||
stmt = stmt.order_by(LikeModel.synced_at)
|
||||
rows = (await self._session.execute(stmt)).scalars().all()
|
||||
return [_to_entity(r) for r in rows]
|
||||
|
||||
async def get_latest_state(
|
||||
self, *, user_id: uuid.UUID, track_ids: list[uuid.UUID]
|
||||
) -> list[Like]:
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"""Playlist repository — adapter over ``AsyncSession``."""
|
||||
|
||||
import datetime as dt
|
||||
import uuid
|
||||
|
||||
from sqlalchemy import func, select
|
||||
@@ -216,6 +217,32 @@ class SqlAlchemyPlaylistRepository:
|
||||
)
|
||||
).scalar_one_or_none()
|
||||
|
||||
async def list_changed_since(
|
||||
self, *, owner_id: uuid.UUID, since: dt.datetime | None, until: dt.datetime
|
||||
) -> list[Playlist]:
|
||||
"""A user's playlists changed in the window ``(since, until]`` (metadata
|
||||
or membership — every mutation bumps ``updated_at``/``version``). Defined
|
||||
before ``list`` (name-shadowing)."""
|
||||
stmt = select(PlaylistModel).where(
|
||||
PlaylistModel.owner_id == owner_id, PlaylistModel.updated_at <= until
|
||||
)
|
||||
if since is not None:
|
||||
stmt = stmt.where(PlaylistModel.updated_at > since)
|
||||
stmt = stmt.order_by(PlaylistModel.updated_at)
|
||||
rows = (await self._session.execute(stmt)).scalars().all()
|
||||
return [_to_entity(r) for r in rows]
|
||||
|
||||
async def track_ids(self, playlist_id: uuid.UUID) -> list[uuid.UUID]:
|
||||
"""Ordered track ids of a playlist (by position) — for sync payloads."""
|
||||
rows = (
|
||||
await self._session.execute(
|
||||
select(PlaylistTrackModel.track_id)
|
||||
.where(PlaylistTrackModel.playlist_id == playlist_id)
|
||||
.order_by(PlaylistTrackModel.position)
|
||||
)
|
||||
).scalars().all()
|
||||
return list(rows)
|
||||
|
||||
# list must come after methods using list[...] in signatures (builtin name shadowing)
|
||||
async def list(self, *, owner_id: uuid.UUID, limit: int, offset: int) -> list[Playlist]:
|
||||
rows = (
|
||||
|
||||
@@ -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