2026-07-19 12:41:38 +02:00
|
|
|
package store
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-25 13:17:17 +02:00
|
|
|
// ResetAllSyncedData deletes every synced activity for userID (cascading to its laps,
|
2026-07-19 12:41:38 +02:00
|
|
|
// activity_samples, and kind_assignments via ON DELETE CASCADE) and rewinds
|
|
|
|
|
// the backfill watermark to its initial state, so a subsequent Backfill
|
|
|
|
|
// starts a genuinely fresh pull instead of thinking history is already
|
|
|
|
|
// covered. Workout kinds (the user's taxonomy) are left untouched.
|
2026-07-25 13:17:17 +02:00
|
|
|
func (db *DB) ResetAllSyncedData(ctx context.Context, userID int64) error {
|
2026-07-19 12:41:38 +02:00
|
|
|
tx, err := db.BeginTx(ctx, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("begin reset tx: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
2026-07-25 13:17:17 +02:00
|
|
|
if _, err := tx.ExecContext(ctx, `DELETE FROM activities WHERE user_id = ?`, userID); err != nil {
|
|
|
|
|
return fmt.Errorf("delete activities for user %d: %w", userID, err)
|
2026-07-19 12:41:38 +02:00
|
|
|
}
|
2026-07-25 13:17:17 +02:00
|
|
|
if _, err := tx.ExecContext(ctx, `UPDATE sync_state SET earliest_synced_date = NULL, backfill_complete = 0 WHERE user_id = ?`, userID); err != nil {
|
|
|
|
|
return fmt.Errorf("reset sync state for user %d: %w", userID, err)
|
2026-07-19 12:41:38 +02:00
|
|
|
}
|
|
|
|
|
return tx.Commit()
|
|
|
|
|
}
|