fix(api): read the DEFAULT partition count before maintenance, not after

Merged
SA
3mo ago
+41-20

Follow-up to #6749, from a CodeRabbit comment I merged before reading.

maintain_webhook_payload_partitions() throws when webhook_payloads_default holds rows for a day it's trying to create. My route counted the DEFAULT partition after calling it — so in the one case that count exists to explain, it never ran, and the operator got an opaque 500.

Counted first now, logged if non-empty, and returned alongside the error so a failing run still says what to drain.

Reproduced on a copy of production

DROP TABLE ingest.webhook_payloads_20260827;
INSERT ... received_at '2026-08-27'   -- lands in DEFAULT
SELECT * FROM ingest.maintain_webhook_payload_partitions();

ERROR: updated partition constraint for default partition
       "webhook_payloads_default" would be violated by some row
CONTEXT: SQL statement "CREATE TABLE ingest.webhook_payloads_20260827 ..."

Then the documented recovery — drain, recreate, reinsert, in one transaction:

default_now_empty: 0
maintenance_ok:    runs clean

Testing

Lint and typecheck pass; the failure and the recovery both executed against a real production copy rather than a fixture.

https://claude.ai/code/session_014D6wif8HDRCXEFf5Ku4w7V


Summary by cubic

Reads the default-partition row count before partition maintenance and returns it with failures. Previously we counted after calling maintain, so failures returned an opaque 500; now we guard the count and include defaultRows (number or null) in the error, while successful runs still report created/dropped counts.

Operational notes

  • On failure, the JSON response includes defaultRows. Drain ingest.webhook_payloads_default and rerun the job.
  • No schema or function changes; only call order and error handling.

Summary by CodeRabbit

  • Bug Fixes
    • Improved partition maintenance error handling and reporting.
    • Maintenance failures now return a clear error response with relevant row-count details.
    • Added visibility into the number of rows in the default partition before maintenance runs.
    • Maintenance results now consistently include partition changes and default-partition row information, including when the row count cannot be determined.