Skip to content

Commit 01c31b4

Browse files
authored
fix: Adjust pagination limit to be zero-based (coder#2663)
There isn't a use-case for querying a limit of zero. Using -1 led to issues when using default parameters for querying.
1 parent 95e854d commit 01c31b4

File tree

7 files changed

+14
-16
lines changed

7 files changed

+14
-16
lines changed

coderd/coderdtest/coderdtest.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ func AwaitWorkspaceAgents(t *testing.T, client *codersdk.Client, build uuid.UUID
396396
}
397397
}
398398
return true
399-
}, 5*time.Second, 25*time.Millisecond)
399+
}, 15*time.Second, 50*time.Millisecond)
400400
return resources
401401
}
402402

coderd/database/queries.sql.go

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/templateversions.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ ORDER BY
2929
-- a timestamp. This is to ensure consistent pagination.
3030
(created_at, id) ASC OFFSET @offset_opt
3131
LIMIT
32-
-- A null limit means "no limit", so -1 means return all
33-
NULLIF(@limit_opt :: int, -1);
32+
-- A null limit means "no limit", so 0 means return all
33+
NULLIF(@limit_opt :: int, 0);
3434

3535
-- name: GetTemplateVersionByJobID :one
3636
SELECT

coderd/database/queries/users.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ ORDER BY
126126
-- a timestamp. This is to ensure consistent pagination.
127127
(created_at, id) ASC OFFSET @offset_opt
128128
LIMIT
129-
-- A null limit means "no limit", so -1 means return all
130-
NULLIF(@limit_opt :: int, -1);
129+
-- A null limit means "no limit", so 0 means return all
130+
NULLIF(@limit_opt :: int, 0);
131131

132132
-- name: UpdateUserStatus :one
133133
UPDATE

coderd/database/queries/workspacebuilds.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ END
6868
ORDER BY
6969
build_number desc OFFSET @offset_opt
7070
LIMIT
71-
-- A null limit means "no limit", so -1 means return all
72-
NULLIF(@limit_opt :: int, -1);
71+
-- A null limit means "no limit", so 0 means return all
72+
NULLIF(@limit_opt :: int, 0);
7373

7474
-- name: GetLatestWorkspaceBuildByWorkspaceID :one
7575
SELECT

coderd/pagination.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func parsePagination(w http.ResponseWriter, r *http.Request) (p codersdk.Paginat
1717
params := codersdk.Pagination{
1818
AfterID: parser.UUID(queryParams, uuid.Nil, "after_id"),
1919
// Limit default to "-1" which returns all results
20-
Limit: parser.Int(queryParams, -1, "limit"),
20+
Limit: parser.Int(queryParams, 0, "limit"),
2121
Offset: parser.Int(queryParams, 0, "offset"),
2222
}
2323
if len(parser.Errors) > 0 {

coderd/pagination_internal_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,13 @@ func TestPagination(t *testing.T) {
7777
ExpectedParams: codersdk.Pagination{
7878
AfterID: uuid.Nil,
7979
Offset: 150,
80-
Limit: -1,
8180
},
8281
},
8382
{
8483
Name: "ValidAfterID",
8584
AfterID: "5f2005fc-acc4-4e5e-a7fa-be017359c60b",
8685
ExpectedParams: codersdk.Pagination{
8786
AfterID: uuid.MustParse("5f2005fc-acc4-4e5e-a7fa-be017359c60b"),
88-
Limit: -1,
8987
},
9088
},
9189
}

0 commit comments

Comments
 (0)