Skip to content

feat: Implement unified pagination and add template versions support #1308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
475dcf9
feat: Implement pagination for template versions
mafredri May 5, 2022
01a52d1
Use codersdk.Pagination in users endpoint
mafredri May 5, 2022
d1478a9
Avoid user sort side-effect in databasefake
mafredri May 5, 2022
7a36610
Return sql.ErrNoRows for GetUsers in databasefake
mafredri May 5, 2022
61c60c6
Sync codepaths in databasefake
mafredri May 5, 2022
62fa273
Fix test with better handling of sql.ErrNoRows in coderd
mafredri May 5, 2022
878d633
Remove an unused require.NoError
mafredri May 5, 2022
5c840fd
Return empty list for sql.ErrNoRows in coderd users endpoint
mafredri May 5, 2022
07e590c
Add t.Parallel() to sub tests
mafredri May 5, 2022
be8ba6c
Reinit tt due to parallel test
mafredri May 5, 2022
5ab9ad5
Remove unused variable
mafredri May 5, 2022
e2a3741
Fix copy pasta in query comments
mafredri May 5, 2022
61410a5
Move ParsePagination from httpapi to coderd and unexport, return code…
mafredri May 5, 2022
8a67746
codersdk: Create requestOption type
mafredri May 5, 2022
55028d2
codersdk: Add test for Pagination.asRequestOption
mafredri May 5, 2022
ea2371e
coderd: Handle http response errors in parsePagination
mafredri May 5, 2022
66af4ec
codersdk: Use parallel test
mafredri May 5, 2022
067f912
Fix created_at edge case for pagination cursor in queries
mafredri May 5, 2022
b8be7a8
Fix copy paste issue
mafredri May 5, 2022
13fc406
Run make gen
mafredri May 10, 2022
aab400c
feat: Add support for json omitempty and embedded structs in apitypin…
mafredri May 10, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix created_at edge case for pagination cursor in queries
  • Loading branch information
mafredri committed May 10, 2022
commit 067f912a8fab01f08f48a9a99218b2e58470c9c1
4 changes: 2 additions & 2 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ func (q *fakeQuerier) GetUsers(_ context.Context, params database.GetUsersParams
return a.CreatedAt.Before(b.CreatedAt)
})

if params.AfterUser != uuid.Nil {
if params.AfterID != uuid.Nil {
found := false
for i, v := range users {
if v.ID == params.AfterUser {
if v.ID == params.AfterID {
// We want to return all users after index i.
users = users[i+1:]
found = true
Expand Down
54 changes: 24 additions & 30 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 10 additions & 13 deletions coderd/database/queries/templateversions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,17 @@ WHERE
WHEN @after_id :: uuid != '00000000-00000000-00000000-00000000' THEN (
-- The pagination cursor is the last ID of the previous page.
-- The query is ordered by the created_at field, so select all
-- rows after the cursor. We also want to include any rows
-- that share the created_at (super rare).
created_at >= (
SELECT
created_at
FROM
template_versions
WHERE
id = @after_id
)
-- Omit the cursor from the final.
AND id != @after_id
-- rows after the cursor.
(created_at, id) > (
SELECT
created_at, id
FROM
template_versions
WHERE
id = @after_id
)
ELSE true
)
ELSE true
END
ORDER BY
-- Deterministic and consistent ordering of all rows, even if they share
Expand Down
29 changes: 13 additions & 16 deletions coderd/database/queries/users.sql
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,20 @@ WHERE
-- This allows using the last element on a page as effectively a cursor.
-- This is an important option for scripts that need to paginate without
-- duplicating or missing data.
WHEN @after_user :: uuid != '00000000-00000000-00000000-00000000' THEN (
-- The pagination cursor is the last user of the previous page.
-- The query is ordered by the created_at field, so select all
-- users after the cursor. We also want to include any users
-- that share the created_at (super rare).
created_at >= (
SELECT
created_at
FROM
users
WHERE
id = @after_user
)
-- Omit the cursor from the final.
AND id != @after_user
WHEN @after_id :: uuid != '00000000-00000000-00000000-00000000' THEN (
-- The pagination cursor is the last ID of the previous page.
-- The query is ordered by the created_at field, so select all
-- rows after the cursor.
(created_at, id) > (
SELECT
created_at, id
FROM
template_versions
WHERE
id = @after_id
)
ELSE true
)
ELSE true
END
-- Start filters
-- Filter by name, email or username
Expand Down
2 changes: 1 addition & 1 deletion coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (api *api) users(rw http.ResponseWriter, r *http.Request) {
}

users, err := api.Database.GetUsers(r.Context(), database.GetUsersParams{
AfterUser: paginationParams.AfterID,
AfterID: paginationParams.AfterID,
OffsetOpt: int32(paginationParams.Offset),
LimitOpt: int32(paginationParams.Limit),
Search: searchName,
Expand Down