Add sync redesign with offline fallback (M9)

- Migration 003: adds logged_at to sync_log for TTL pruning; migrates
  settings_history to UUID TEXT PK with updated_at column
- SyncStore: Prune() deletes rows older than 30d and writes a '_pruned'
  marker at the boundary version; Pull() calls Prune lazily and returns
  ErrSyncStale (410) when the client's since_version is behind the marker
- sync_handler.go: GET /api/sync/pull?since=N; POST /api/sync/push with
  last-updated_at-wins conflict resolution for entries, balance_adjustments,
  settings_history; closed_days/closed_weeks skipped (server-only mutations)
- router.go: passes entryStore, adjustmentStore, settingsStore to SyncHandler
- settings_store.go: UUID PK, updated_at column, Upsert() for push path
- settings_service.go: generates UUID on create, sets updated_at on update
- settings_handler.go: ID params changed from int64 to string
- domain.go: Settings.ID string, Settings.UpdatedAt added
- client.ts: all mutation methods catch TypeError (offline) and fall back
  to Dexie write + outbox enqueue; crypto.randomUUID() for offline creates;
  Settings.id type changed to string
- db.ts: Dexie v3 — settings_history key path changed to string UUID;
  upgrade handler clears table for repopulation via pull
- sync.ts: real pushOutbox to POST /api/sync/push; pullChanges uses GET
  with ?since=N; 410 triggers coldStart() + retry; coldStart() wipes all
  tables and resets last_version
- 4 new Go store tests covering normal pull, stale client, empty prune,
  client-ahead-of-marker; all tests pass (store + service, 19 Vitest)
This commit is contained in:
2026-04-30 22:50:33 +02:00
parent 3214f48a6f
commit d8366f5c25
15 changed files with 864 additions and 144 deletions

View File

@@ -4,12 +4,21 @@ import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/wotra/wotra/internal/domain"
)
// SyncStore manages the sync_log and server_version.
// ErrSyncStale is returned when the client's since_version is behind the prune marker.
var ErrSyncStale = errors.New("sync state stale: full re-sync required")
// pruneEntity and pruneOp are sentinel values written as a prune marker row.
const pruneEntity = "_pruned"
const pruneOp = "marker"
// SyncStore manages the sync_log.
type SyncStore struct {
db *sql.DB
}
@@ -21,13 +30,19 @@ func NewSyncStore(db *sql.DB) *SyncStore {
type SyncChange struct {
Entity string `json:"entity"`
EntityID string `json:"entity_id"`
Op string `json:"op"` // "upsert" | "delete"
Op string `json:"op"` // "upsert" | "delete" | "marker"
Version int64 `json:"version"`
Payload string `json:"payload"`
}
// Pull returns all sync_log rows with version > sinceVersion.
// It calls Prune first with a 30-day TTL.
// If the client is behind a prune marker it returns ErrSyncStale.
func (s *SyncStore) Pull(ctx context.Context, sinceVersion int64) ([]SyncChange, int64, error) {
if err := s.Prune(ctx, 30*24*time.Hour); err != nil {
return nil, 0, err
}
rows, err := s.db.QueryContext(ctx,
`SELECT entity, entity_id, op, version, payload FROM sync_log
WHERE version > ? ORDER BY version ASC`, sinceVersion)
@@ -35,6 +50,7 @@ func (s *SyncStore) Pull(ctx context.Context, sinceVersion int64) ([]SyncChange,
return nil, 0, err
}
defer rows.Close()
var changes []SyncChange
var maxVersion int64 = sinceVersion
for rows.Next() {
@@ -42,6 +58,10 @@ func (s *SyncStore) Pull(ctx context.Context, sinceVersion int64) ([]SyncChange,
if err := rows.Scan(&c.Entity, &c.EntityID, &c.Op, &c.Version, &c.Payload); err != nil {
return nil, 0, err
}
// First row with entity="_pruned" means client is stale.
if c.Entity == pruneEntity {
return nil, 0, ErrSyncStale
}
if c.Version > maxVersion {
maxVersion = c.Version
}
@@ -50,6 +70,49 @@ func (s *SyncStore) Pull(ctx context.Context, sinceVersion int64) ([]SyncChange,
return changes, maxVersion, rows.Err()
}
// Prune deletes sync_log rows older than ttl and inserts a prune marker at the
// version boundary so stale clients can detect they need a full re-sync.
func (s *SyncStore) Prune(ctx context.Context, ttl time.Duration) error {
cutoff := time.Now().Add(-ttl).UnixMilli()
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback() //nolint:errcheck
// Find max version among rows that will be pruned (excluding existing markers).
var maxPruned sql.NullInt64
err = tx.QueryRowContext(ctx,
`SELECT MAX(version) FROM sync_log WHERE logged_at < ? AND entity != ?`,
cutoff, pruneEntity).Scan(&maxPruned)
if err != nil {
return err
}
if !maxPruned.Valid {
// Nothing to prune.
return tx.Commit()
}
// Delete old rows (but not the existing marker, if any).
if _, err = tx.ExecContext(ctx,
`DELETE FROM sync_log WHERE logged_at < ? AND entity != ?`,
cutoff, pruneEntity); err != nil {
return err
}
// Insert (or replace) the prune marker at the boundary version.
now := time.Now().UnixMilli()
if _, err = tx.ExecContext(ctx,
`INSERT OR REPLACE INTO sync_log (entity, entity_id, op, version, payload, logged_at)
VALUES (?, ?, ?, ?, '{}', ?)`,
pruneEntity, pruneEntity, pruneOp, maxPruned.Int64, now); err != nil {
return err
}
return tx.Commit()
}
// nextVersion returns the next monotonic version number.
func (s *SyncStore) nextVersion(ctx context.Context) (int64, error) {
var max sql.NullInt64
@@ -96,6 +159,21 @@ func (s *SyncStore) LogClosedWeek(ctx context.Context, w *domain.ClosedWeek) err
return s.log(ctx, "closed_weeks", w.WeekKey, "upsert", string(payload))
}
// LogSettings appends a settings upsert to the sync log.
func (s *SyncStore) LogSettings(ctx context.Context, set *domain.Settings) error {
payload, err := json.Marshal(set)
if err != nil {
return err
}
return s.log(ctx, "settings_history", set.ID, "upsert", string(payload))
}
// LogSettingsDelete appends a settings delete to the sync log.
func (s *SyncStore) LogSettingsDelete(ctx context.Context, id string) error {
payload := fmt.Sprintf(`{"id":%q}`, id)
return s.log(ctx, "settings_history", id, "delete", payload)
}
// LogBalanceAdjustment appends a balance_adjustment upsert to the sync log.
func (s *SyncStore) LogBalanceAdjustment(ctx context.Context, a *domain.BalanceAdjustment) error {
payload, err := json.Marshal(a)
@@ -116,8 +194,9 @@ func (s *SyncStore) log(ctx context.Context, entity, entityID, op, payload strin
if err != nil {
return err
}
now := time.Now().UnixMilli()
_, err = s.db.ExecContext(ctx,
`INSERT INTO sync_log (entity, entity_id, op, version, payload) VALUES (?, ?, ?, ?, ?)`,
entity, entityID, op, version, payload)
`INSERT INTO sync_log (entity, entity_id, op, version, payload, logged_at) VALUES (?, ?, ?, ?, ?, ?)`,
entity, entityID, op, version, payload, now)
return err
}