Skip to content

feat(sabnzbd): paginate history via a single UNION query (no schema change) - #788

Merged
javi11 merged 1 commit into
mainfrom
session/sabnzbd-history-pagination-8e7819
Jul 21, 2026
Merged

feat(sabnzbd): paginate history via a single UNION query (no schema change)#788
javi11 merged 1 commit into
mainfrom
session/sabnzbd-history-pagination-8e7819

Conversation

@javi11

@javi11 javi11 commented Jul 20, 2026

Copy link
Copy Markdown
Owner

What

Fix pagination of the SABnzbd-compatible mode=history API by moving the merge + dedup + pagination into one SQL query over the existing tables. No schema change, no migration.

Why

The handler merged three sources — completed rows from import_queue, a 7-day window of import_history, and failed rows from import_queue — deduped and sliced them in memory over hardcoded caps (2000 / 1000). So clients (Sonarr/Radarr) could never page past the caps, and noofslots didn't reflect the true total.

How

  • New repo methods ListSABnzbdHistory + CountSABnzbdHistory: a UNION ALL of import_queue(completed) + import_history + import_queue(failed), deduped by an anti-join on import_history.nzb_id = import_queue.id (a completed import can live in both tables — the live queue row wins, the history copy is dropped), ORDER BY completed_at DESC, id DESC, real LIMIT/OFFSET, and an exact COUNT. Portable across SQLite + Postgres (no window functions, no basename/regexp; ? and FALSE handled by the dialect layer).
  • Handler bulk path rewritten to use them; limit capped at 1000.
  • SAB-history reports paths that do not exist on disk → ARR queues fill with FileNotFoundException #596 preserved — only rows sourced from the live completed queue are flipped to Failed when their reported path is missing on disk.
  • nzo_ids reconciliation path unchanged.

What does NOT change

  • No new tables/columns, no migration.
  • Failures keep living in import_queue with their existing FailedItemRetentionHours lifecycle (default 24h) — same as before.
  • Completed/failed queue rows, retention config, the UI, and Stremio are all untouched.

Behavior note

Dedup shifts from filename-based to queue-id-based (import_history.nzb_id). This is more correct than the old name dedup (which was partly ineffective because queue paths carry a {id}- prefix that import_history.nzb_name strips): a completed import in both tables now reliably shows once, and two genuinely distinct jobs that share a filename both show (as real SABnzbd does). The old fixed history window (7 days) is gone — pagination now spans the full history.

Test plan

  • go test ./internal/database/... ./internal/api/... — green; go vet clean; go build ./... clean
  • New tests: dedup across queue+history (single row, correct source), failed-item inclusion, skip_arr exclusion, category filter, paged enumeration with no overlap/gaps and newest-first ordering, start beyond total
  • Manual: mode=history with start/limit (no page overlap, constant noofslots across pages)

…hange)

The mode=history handler merged completed-queue + a 7-day import_history
window + failed-queue and paginated in memory over hardcoded caps
(2000/1000), so clients couldn't page past the caps and noofslots was wrong.

Move the merge + dedup + pagination into one SQL query over the existing
tables — no migration, no schema change:

- add ListSABnzbdHistory + CountSABnzbdHistory (UNION ALL of
  import_queue[completed] + import_history + import_queue[failed]), deduped by
  the import_history.nzb_id = import_queue.id anti-join so a completed import
  present in both tables is counted once; ORDER BY completed_at DESC, id DESC
  with real LIMIT/OFFSET and an exact COUNT
- rewrite the handler bulk path to use them; cap limit at 1000
- #596 preserved: only rows sourced from the live completed queue are flipped
  to Failed on a missing path
- nzo_ids reconciliation path and failed-item retention are unchanged

Failed items keep living in import_queue with their existing
FailedItemRetentionHours lifecycle; nothing about failures or retention
changes. Adds repository tests for dedup, sources, pagination and category.
@javi11
javi11 force-pushed the session/sabnzbd-history-pagination-8e7819 branch from e9798bf to 3a30266 Compare July 20, 2026 11:20
@javi11 javi11 changed the title feat(sabnzbd): single-source history with real pagination, failures & delete-with-library feat(sabnzbd): paginate history via a single UNION query (no schema change) Jul 20, 2026
@javi11
javi11 merged commit a346a43 into main Jul 21, 2026
2 checks passed
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