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:
@@ -19,7 +19,7 @@ func NewSettingsStore(db *sql.DB) *SettingsStore {
|
||||
// Current returns the most recent settings effective on or before the given day key.
|
||||
func (s *SettingsStore) Current(ctx context.Context, asOfDayKey string) (*domain.Settings, error) {
|
||||
row := s.db.QueryRowContext(ctx,
|
||||
`SELECT id, effective_from, hours_per_week, workdays_mask, timezone, created_at
|
||||
`SELECT id, effective_from, hours_per_week, workdays_mask, timezone, created_at, updated_at
|
||||
FROM settings_history
|
||||
WHERE effective_from <= ?
|
||||
ORDER BY effective_from DESC, id DESC
|
||||
@@ -30,7 +30,7 @@ func (s *SettingsStore) Current(ctx context.Context, asOfDayKey string) (*domain
|
||||
// Latest returns the most recently created settings row.
|
||||
func (s *SettingsStore) Latest(ctx context.Context) (*domain.Settings, error) {
|
||||
row := s.db.QueryRowContext(ctx,
|
||||
`SELECT id, effective_from, hours_per_week, workdays_mask, timezone, created_at
|
||||
`SELECT id, effective_from, hours_per_week, workdays_mask, timezone, created_at, updated_at
|
||||
FROM settings_history
|
||||
ORDER BY effective_from DESC, id DESC
|
||||
LIMIT 1`)
|
||||
@@ -40,7 +40,7 @@ func (s *SettingsStore) Latest(ctx context.Context) (*domain.Settings, error) {
|
||||
// History returns all settings rows ordered by effective_from DESC.
|
||||
func (s *SettingsStore) History(ctx context.Context) ([]*domain.Settings, error) {
|
||||
rows, err := s.db.QueryContext(ctx,
|
||||
`SELECT id, effective_from, hours_per_week, workdays_mask, timezone, created_at
|
||||
`SELECT id, effective_from, hours_per_week, workdays_mask, timezone, created_at, updated_at
|
||||
FROM settings_history ORDER BY effective_from DESC, id DESC`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -49,7 +49,7 @@ func (s *SettingsStore) History(ctx context.Context) ([]*domain.Settings, error)
|
||||
var result []*domain.Settings
|
||||
for rows.Next() {
|
||||
var s domain.Settings
|
||||
if err := rows.Scan(&s.ID, &s.EffectiveFrom, &s.HoursPerWeek, &s.WorkdaysMask, &s.Timezone, &s.CreatedAt); err != nil {
|
||||
if err := rows.Scan(&s.ID, &s.EffectiveFrom, &s.HoursPerWeek, &s.WorkdaysMask, &s.Timezone, &s.CreatedAt, &s.UpdatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, &s)
|
||||
@@ -59,30 +59,41 @@ func (s *SettingsStore) History(ctx context.Context) ([]*domain.Settings, error)
|
||||
|
||||
// Insert inserts a new settings row.
|
||||
func (s *SettingsStore) Insert(ctx context.Context, set *domain.Settings) error {
|
||||
res, err := s.db.ExecContext(ctx,
|
||||
`INSERT INTO settings_history (effective_from, hours_per_week, workdays_mask, timezone, created_at)
|
||||
VALUES (?, ?, ?, ?, ?)`,
|
||||
set.EffectiveFrom, set.HoursPerWeek, set.WorkdaysMask, set.Timezone, set.CreatedAt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
id, _ := res.LastInsertId()
|
||||
set.ID = id
|
||||
return nil
|
||||
_, err := s.db.ExecContext(ctx,
|
||||
`INSERT INTO settings_history (id, effective_from, hours_per_week, workdays_mask, timezone, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
||||
set.ID, set.EffectiveFrom, set.HoursPerWeek, set.WorkdaysMask, set.Timezone, set.CreatedAt, set.UpdatedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
// Update overwrites an existing settings row by ID.
|
||||
func (s *SettingsStore) Update(ctx context.Context, set *domain.Settings) error {
|
||||
_, err := s.db.ExecContext(ctx,
|
||||
`UPDATE settings_history
|
||||
SET effective_from=?, hours_per_week=?, workdays_mask=?, timezone=?
|
||||
SET effective_from=?, hours_per_week=?, workdays_mask=?, timezone=?, updated_at=?
|
||||
WHERE id=?`,
|
||||
set.EffectiveFrom, set.HoursPerWeek, set.WorkdaysMask, set.Timezone, set.ID)
|
||||
set.EffectiveFrom, set.HoursPerWeek, set.WorkdaysMask, set.Timezone, set.UpdatedAt, set.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
// Upsert inserts or replaces a settings row (used by sync push; last updated_at wins).
|
||||
func (s *SettingsStore) Upsert(ctx context.Context, set *domain.Settings) error {
|
||||
_, err := s.db.ExecContext(ctx,
|
||||
`INSERT INTO settings_history (id, effective_from, hours_per_week, workdays_mask, timezone, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
effective_from=excluded.effective_from,
|
||||
hours_per_week=excluded.hours_per_week,
|
||||
workdays_mask=excluded.workdays_mask,
|
||||
timezone=excluded.timezone,
|
||||
updated_at=excluded.updated_at
|
||||
WHERE excluded.updated_at > settings_history.updated_at`,
|
||||
set.ID, set.EffectiveFrom, set.HoursPerWeek, set.WorkdaysMask, set.Timezone, set.CreatedAt, set.UpdatedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete removes a settings row by ID.
|
||||
func (s *SettingsStore) Delete(ctx context.Context, id int64) error {
|
||||
func (s *SettingsStore) Delete(ctx context.Context, id string) error {
|
||||
_, err := s.db.ExecContext(ctx, `DELETE FROM settings_history WHERE id=?`, id)
|
||||
return err
|
||||
}
|
||||
@@ -95,16 +106,16 @@ func (s *SettingsStore) Count(ctx context.Context) (int, error) {
|
||||
}
|
||||
|
||||
// GetByID returns a single settings row by ID.
|
||||
func (s *SettingsStore) GetByID(ctx context.Context, id int64) (*domain.Settings, error) {
|
||||
func (s *SettingsStore) GetByID(ctx context.Context, id string) (*domain.Settings, error) {
|
||||
row := s.db.QueryRowContext(ctx,
|
||||
`SELECT id, effective_from, hours_per_week, workdays_mask, timezone, created_at
|
||||
`SELECT id, effective_from, hours_per_week, workdays_mask, timezone, created_at, updated_at
|
||||
FROM settings_history WHERE id=?`, id)
|
||||
return scanSettings(row)
|
||||
}
|
||||
|
||||
func scanSettings(row *sql.Row) (*domain.Settings, error) {
|
||||
var s domain.Settings
|
||||
err := row.Scan(&s.ID, &s.EffectiveFrom, &s.HoursPerWeek, &s.WorkdaysMask, &s.Timezone, &s.CreatedAt)
|
||||
err := row.Scan(&s.ID, &s.EffectiveFrom, &s.HoursPerWeek, &s.WorkdaysMask, &s.Timezone, &s.CreatedAt, &s.UpdatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user