Skip to content

Commit 067f912

Browse files
committed
Fix created_at edge case for pagination cursor in queries
1 parent 66af4ec commit 067f912

File tree

5 files changed

+50
-62
lines changed

5 files changed

+50
-62
lines changed

coderd/database/databasefake/databasefake.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,10 @@ func (q *fakeQuerier) GetUsers(_ context.Context, params database.GetUsersParams
186186
return a.CreatedAt.Before(b.CreatedAt)
187187
})
188188

189-
if params.AfterUser != uuid.Nil {
189+
if params.AfterID != uuid.Nil {
190190
found := false
191191
for i, v := range users {
192-
if v.ID == params.AfterUser {
192+
if v.ID == params.AfterID {
193193
// We want to return all users after index i.
194194
users = users[i+1:]
195195
found = true

coderd/database/queries.sql.go

Lines changed: 24 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/templateversions.sql

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,17 @@ WHERE
1212
WHEN @after_id :: uuid != '00000000-00000000-00000000-00000000' THEN (
1313
-- The pagination cursor is the last ID of the previous page.
1414
-- The query is ordered by the created_at field, so select all
15-
-- rows after the cursor. We also want to include any rows
16-
-- that share the created_at (super rare).
17-
created_at >= (
18-
SELECT
19-
created_at
20-
FROM
21-
template_versions
22-
WHERE
23-
id = @after_id
24-
)
25-
-- Omit the cursor from the final.
26-
AND id != @after_id
15+
-- rows after the cursor.
16+
(created_at, id) > (
17+
SELECT
18+
created_at, id
19+
FROM
20+
template_versions
21+
WHERE
22+
id = @after_id
2723
)
28-
ELSE true
24+
)
25+
ELSE true
2926
END
3027
ORDER BY
3128
-- Deterministic and consistent ordering of all rows, even if they share

coderd/database/queries/users.sql

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,20 @@ WHERE
7777
-- This allows using the last element on a page as effectively a cursor.
7878
-- This is an important option for scripts that need to paginate without
7979
-- duplicating or missing data.
80-
WHEN @after_user :: uuid != '00000000-00000000-00000000-00000000' THEN (
81-
-- The pagination cursor is the last user of the previous page.
82-
-- The query is ordered by the created_at field, so select all
83-
-- users after the cursor. We also want to include any users
84-
-- that share the created_at (super rare).
85-
created_at >= (
86-
SELECT
87-
created_at
88-
FROM
89-
users
90-
WHERE
91-
id = @after_user
92-
)
93-
-- Omit the cursor from the final.
94-
AND id != @after_user
80+
WHEN @after_id :: uuid != '00000000-00000000-00000000-00000000' THEN (
81+
-- The pagination cursor is the last ID of the previous page.
82+
-- The query is ordered by the created_at field, so select all
83+
-- rows after the cursor.
84+
(created_at, id) > (
85+
SELECT
86+
created_at, id
87+
FROM
88+
template_versions
89+
WHERE
90+
id = @after_id
9591
)
96-
ELSE true
92+
)
93+
ELSE true
9794
END
9895
-- Start filters
9996
-- Filter by name, email or username

coderd/users.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (api *api) users(rw http.ResponseWriter, r *http.Request) {
115115
}
116116

117117
users, err := api.Database.GetUsers(r.Context(), database.GetUsersParams{
118-
AfterUser: paginationParams.AfterID,
118+
AfterID: paginationParams.AfterID,
119119
OffsetOpt: int32(paginationParams.Offset),
120120
LimitOpt: int32(paginationParams.Limit),
121121
Search: searchName,

0 commit comments

Comments
 (0)