---
id: w8/m4/t006
title: "Acceptance: seed old rows → compact → verify purge + correct historical totals"
worker: worker8
status: done
estimate: 30m
depends_on: [w8/m4/t004, w8/m4/t005]
---

## Objective

End-to-end verification that compaction actually bounds table growth without breaking historical queries: seed old `usage_hourly` rows, run compaction, confirm `usage_monthly` is populated, the hourly rows are gone, and `GET /v1/usage?period=<old-month>` still returns the pre-compaction totals exactly.

## Context

- Run against a real (or envtest-equivalent) control-plane Postgres instance, not mocks — this is a storage-integrity check.
- Compare totals before and after compaction for the same month to catch any aggregation drift.

## Steps

1. Seed `usage_hourly` rows for a month outside the (test-configured, short) hot window.
2. Record the pre-compaction `GET /v1/usage?period=<that-month>` response.
3. Run the compaction routine (t003/t004's entry point directly, not waiting for the daily tick).
4. Confirm: `usage_monthly` has the aggregated rows, `usage_hourly` no longer has rows for that month, and `GET /v1/usage?period=<that-month>` returns byte-identical totals to step 2.

## Files

- `lego/backend/internal/usage/service_acceptance_test.go` (extend, or add a sibling)

## Acceptance criteria

- [ ] Post-compaction totals match pre-compaction totals exactly for the compacted month.
- [ ] `usage_hourly` row count for the compacted month is zero after compaction.
- [ ] Re-running compaction over the same month is a no-op (no error, no duplicate rows, no total drift).

## Out of scope

- Broader unit-test coverage (t008, the standing closing task) — this is the one integration/acceptance pass.
