Skip to content

Add DB retention/pruning to stop the database growing forever - #21

Open
ashleigh-byte wants to merge 2 commits into
Role1776:mainfrom
ashleigh-byte:db-retention-pruning
Open

Add DB retention/pruning to stop the database growing forever#21
ashleigh-byte wants to merge 2 commits into
Role1776:mainfrom
ashleigh-byte:db-retention-pruning

Conversation

@ashleigh-byte

Copy link
Copy Markdown
Contributor

metrics/device_scans/speedtest rows accumulate indefinitely today -- existing queries only ever read a 24h window, but nothing ever deletes older rows, so a long-running install's DB grows without bound.

  • New RETENTION_DAYS config (default 90 days)
  • DB.prune_old_data() deletes metrics older than the retention window (cascading to their linked speedtest row via existing ON DELETE CASCADE), then removes any device_scans rows left orphaned by that cascade -- device_scans has no timestamp of its own, but every scan is created in the same transaction as exactly one metric via speedtest, so "no linked speedtest row remains" is an equivalent age check
  • Runs once per detailed-report cycle (not every cycle -- no need to hit the DB with a cleanup pass every SLEEP_TIME tick)

Test plan:

  • Unit-tested prune_old_data: old rows deleted and cascade correctly removes orphaned device_scans, recent rows survive untouched, idempotent re-run is a no-op, retention-day boundary (91 days deleted vs 89 days kept for a 90-day window)
  • Verified get_metrics/get_metrics_with_device_counts still return correct results post-prune
  • Unit-tested RETENTION_DAYS config validation (default, override, negative value rejected)

metrics/device_scans/speedtest rows accumulate indefinitely today --
existing queries only ever read a 24h window, but nothing ever deletes
older rows, so a long-running install's DB grows without bound.

- New RETENTION_DAYS config (default 90 days)
- DB.prune_old_data() deletes metrics older than the retention window
  (cascading to their linked speedtest row via existing ON DELETE
  CASCADE), then removes any device_scans rows left orphaned by that
  cascade -- device_scans has no timestamp of its own, but every scan
  is created in the same transaction as exactly one metric via
  speedtest, so "no linked speedtest row remains" is an equivalent age
  check
- Runs once per detailed-report cycle (not every cycle -- no need to
  hit the DB with a cleanup pass every SLEEP_TIME tick)

Test plan:
- Unit-tested prune_old_data: old rows deleted and cascade correctly
  removes orphaned device_scans, recent rows survive untouched,
  idempotent re-run is a no-op, retention-day boundary (91 days
  deleted vs 89 days kept for a 90-day window)
- Verified get_metrics/get_metrics_with_device_counts still return
  correct results post-prune
- Unit-tested RETENTION_DAYS config validation (default, override,
  negative value rejected)
Adds RETENTION_DAYS to the .env variables table and notes the
automatic pruning behavior in the Features & Workflow overview.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant