?
TRCKR-178
sync phase-3
Created: 2025-12-09 Updated: 2025-12-16
Relationships Loading...
Attachments
Loading...
Comments (1)
agent · 2025-12-10
Implemented automatic snapshot creation with the following changes: **What was broken:** - Snapshots were only created manually via POST /api/sync/snapshot - Fresh clients syncing when no snapshot existed would build full snapshots in memory but never persist them - No automatic mechanism to keep snapshots fresh as data changed **Implementation (server/routes/sync.py):** 1. Added `_should_create_snapshot(store, latest_snapshot)` (lines 141-179): - Returns True when no snapshot exists - Returns True when latest snapshot is >1000 changes old - Returns True when latest snapshot is >24 hours old - Otherwise returns False 2. Added `_create_snapshot_if_needed(store)` (lines 182-197): - Background task handler that checks conditions and creates snapshot - Wrapped in try/except to ensure sync doesn't fail if snapshot creation fails - Logs detailed information about why snapshot is/isn't created 3. Modified `create_snapshot(store)` (lines 1063-1109): - Now checks if snapshot already exists at current server_version - If exists: updates existing snapshot (data, entity_counts, created_at) - If not: inserts new snapshot - This handles the edge case where server_version hasn't changed but snapshot is old 4. Modified `sync()` endpoint (line 341): - Added background task call for fresh clients (last_sync_version=0) - Uses FastAPI BackgroundTasks to create snapshot asynchronously - Doesn't block sync response **Tests (tests/test_auto_snapshot.py):** - test_auto_snapshot_when_no_snapshot_exists - Verified snapshot created on first sync - test_auto_snapshot_after_n_changes - Verified snapshot created after >1000 changes - test_auto_snapshot_after_time_elapsed - Verified snapshot updated when >24 hours old - test_no_auto_snapshot_when_recent - Verified no snapshot when conditions not met - test_auto_snapshot_not_created_for_incremental_sync - Verified only fresh clients trigger - test_auto_snapshot_handles_concurrent_creation - Verified idempotency **Verification:** - All 6 new tests pass - All 19 existing snapshot tests still pass - No regressions in other test suites - Used Option 1 from requirements: FastAPI BackgroundTasks on fresh-client sync **Edge cases handled:** - Snapshot at same server_version gets updated (not duplicate insert) - Background task failures don't break sync - Only fresh clients trigger auto-snapshot (not incremental) - Concurrent snapshot creation handled via UNIQUE constraint or UPDATE