Skip to content

Commit b60282d

Browse files
committed
Fix created_at edge case for pagination cursor in queries
1 parent 6b7dec3 commit b60282d

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
@@ -69,23 +69,20 @@ WHERE
6969
-- This allows using the last element on a page as effectively a cursor.
7070
-- This is an important option for scripts that need to paginate without
7171
-- duplicating or missing data.
72-
WHEN @after_user :: uuid != '00000000-00000000-00000000-00000000' THEN (
73-
-- The pagination cursor is the last user of the previous page.
74-
-- The query is ordered by the created_at field, so select all
75-
-- users after the cursor. We also want to include any users
76-
-- that share the created_at (super rare).
77-
created_at >= (
78-
SELECT
79-
created_at
80-
FROM
81-
users
82-
WHERE
83-
id = @after_user
84-
)
85-
-- Omit the cursor from the final.
86-
AND id != @after_user
72+
WHEN @after_id :: uuid != '00000000-00000000-00000000-00000000' THEN (
73+
-- The pagination cursor is the last ID of the previous page.
74+
-- The query is ordered by the created_at field, so select all
75+
-- rows after the cursor.
76+
(created_at, id) > (
77+
SELECT
78+
created_at, id
79+
FROM
80+
template_versions
81+
WHERE
82+
id = @after_id
8783
)
88-
ELSE true
84+
)
85+
ELSE true
8986
END
9087
-- Start filters
9188
-- 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)