- 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)
124 lines
4.4 KiB
Go
124 lines
4.4 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
|
|
"github.com/wotra/wotra/internal/domain"
|
|
)
|
|
|
|
// SettingsStore handles persistence for settings history.
|
|
type SettingsStore struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
func NewSettingsStore(db *sql.DB) *SettingsStore {
|
|
return &SettingsStore{db: db}
|
|
}
|
|
|
|
// 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, updated_at
|
|
FROM settings_history
|
|
WHERE effective_from <= ?
|
|
ORDER BY effective_from DESC, id DESC
|
|
LIMIT 1`, asOfDayKey)
|
|
return scanSettings(row)
|
|
}
|
|
|
|
// 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, updated_at
|
|
FROM settings_history
|
|
ORDER BY effective_from DESC, id DESC
|
|
LIMIT 1`)
|
|
return scanSettings(row)
|
|
}
|
|
|
|
// 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, updated_at
|
|
FROM settings_history ORDER BY effective_from DESC, id DESC`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
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, &s.UpdatedAt); err != nil {
|
|
return nil, err
|
|
}
|
|
result = append(result, &s)
|
|
}
|
|
return result, rows.Err()
|
|
}
|
|
|
|
// Insert inserts a new settings row.
|
|
func (s *SettingsStore) Insert(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 (?, ?, ?, ?, ?, ?, ?)`,
|
|
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=?, updated_at=?
|
|
WHERE 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 string) error {
|
|
_, err := s.db.ExecContext(ctx, `DELETE FROM settings_history WHERE id=?`, id)
|
|
return err
|
|
}
|
|
|
|
// Count returns the total number of settings rows.
|
|
func (s *SettingsStore) Count(ctx context.Context) (int, error) {
|
|
var n int
|
|
err := s.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM settings_history`).Scan(&n)
|
|
return n, err
|
|
}
|
|
|
|
// GetByID returns a single settings row by ID.
|
|
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, 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, &s.UpdatedAt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &s, nil
|
|
}
|