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:
@@ -0,0 +1,57 @@
|
||||
"""sync: server-ingestion columns on the event logs
|
||||
|
||||
Revision ID: 20260728_sync_synced_at
|
||||
Revises: 20260728_user_settings
|
||||
Create Date: 2026-07-28 11:00:00.000000
|
||||
|
||||
Adds ``synced_at`` to ``likes`` and ``play_history`` — the server-side ingestion
|
||||
time used as the delta-sync ordering key (distinct from the event time
|
||||
``created_at``/``played_at``, which a sync push preserves from the client even
|
||||
when the event happened offline earlier). Existing rows are backfilled from
|
||||
their event time so a first sync after upgrade behaves sensibly.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "20260728_sync_synced_at"
|
||||
down_revision: str | None = "20260728_user_settings"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column(
|
||||
"likes",
|
||||
sa.Column(
|
||||
"synced_at",
|
||||
sa.DateTime(timezone=True),
|
||||
server_default=sa.text("now()"),
|
||||
nullable=False,
|
||||
),
|
||||
)
|
||||
op.execute("UPDATE likes SET synced_at = created_at")
|
||||
op.create_index(op.f("ix_likes_synced_at"), "likes", ["synced_at"])
|
||||
|
||||
op.add_column(
|
||||
"play_history",
|
||||
sa.Column(
|
||||
"synced_at",
|
||||
sa.DateTime(timezone=True),
|
||||
server_default=sa.text("now()"),
|
||||
nullable=False,
|
||||
),
|
||||
)
|
||||
op.execute("UPDATE play_history SET synced_at = played_at")
|
||||
op.create_index(op.f("ix_play_history_synced_at"), "play_history", ["synced_at"])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index(op.f("ix_play_history_synced_at"), table_name="play_history")
|
||||
op.drop_column("play_history", "synced_at")
|
||||
op.drop_index(op.f("ix_likes_synced_at"), table_name="likes")
|
||||
op.drop_column("likes", "synced_at")
|
||||
Reference in New Issue
Block a user