fix(pagination): return adjacent page for reverse cursor pagination#3023
Open
abhay-codes07 wants to merge 1 commit into
Open
fix(pagination): return adjacent page for reverse cursor pagination#3023abhay-codes07 wants to merge 1 commit into
abhay-codes07 wants to merge 1 commit into
Conversation
|
✅ No security or compliance issues detected. Reviewed everything up to 8613676. Security Overview
Detected Code Changes
|
Contributor
|
@cubic review |
Contributor
@topher-lo I have started the AI code review. It will take a few minutes to complete. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Checklist
uv run pytest tests)?pre-commit run --all-files)?Description
Fixes reverse (
reverse=true, i.e. "previous page") cursor pagination in three list endpoints. The reference implementations already in the codebase (TableEditorService.list_rows,WorkflowsManagementService.list_workflows) do it in four steps: flip the scan direction, fetchlimit + 1, compute cursors from scan order, then reverse the items and swap cursors/flags. Each of the fixed endpoints was missing a different piece:CasesService.search_cases: flipped the cursor comparison but not theORDER BY, soLIMITkept the first rows of the whole backward prefix — "previous page" returned page 1 from any page 3 or deeper. It also never swappedhas_more/has_previousand its reverse branch overwrotenext_cursorwhile leavingprev_cursorasnull. Now uses the canonical scan-flip + reverse + swap.TablesService.list_rows(the search-style method behindsearch_rows): same missingORDER BYflip; the post-fetch reversal/swap was already there, so it reversed the wrong window. Now flips the scan direction to match.CaseTableRowsService.list_rows: had two problems. Its reverse branch appended ascending sort keys to a statement already built with.order_by(created_at.desc(), id.desc())— SQLAlchemy keeps the first sort keys, so the intended flip never took effect and the same wrong window came back. It also computed cursors from the already-reversed items and then swapped them again, so "next" pointed backward and "prev" pointed forward. The reverse branch now replaces the ORDER BY (order_by(None)first), and cursors are computed from scan order before the reversal, like the siblings.Forward pagination is untouched in all three (the reverse handling is a no-op when
reverseis false).Related Issues
Fixes #3022
Screenshots / Recordings
N/A (API behavior fix)
Steps to QA
The new regression tests page three deep and then walk back — the existing reverse tests only went back from page 2, where "previous page" and "page 1" coincide, which is why the bug never surfaced:
test_search_cases_reverse_pagination_returns_adjacent_page: back from page 3 returns page 2 (previously page 1), flags are oriented in display direction, and the swapped cursors round-trip forward to page 3 and back to page 1.test_list_rows_reverse_pagination_returns_adjacent_page(tables): same walk over 7 rows withlimit=2.test_list_rows_reverse_pagination_cursors_point_forward_and_back(case table rows): after going back a page,next_cursorreturns to page 3 andprev_cursorcontinues to page 1 (previously each pointed the other way).All pre-existing pagination tests in the three files pass unchanged.
Summary by cubic
Fixes reverse cursor pagination so “previous page” returns the adjacent page with correct cursors and flags across
CasesService.search_cases,TablesService.list_rows, andCaseTableRowsService.list_rows. Forward pagination is unchanged.limit + 1, compute cursors from scan order, then reverse items and swapnext/prevandhas_more/has_previous. Added regression tests that walk back from page 3 to page 2.CasesService.search_casesandTablesService.list_rows: now flipORDER BYfor reverse scans; case search also fixes swapped/overwritten cursors and flags.CaseTableRowsService.list_rows: replaces (not appends) the baseORDER BYwhen reversing; computes cursors before reversal and swaps correctly sonextpoints forward andprevpoints back.Written for commit 8613676. Summary will update on new commits.