feat: add pagination to getWorkspaces (#4521)

This commit is contained in:
Garrett Delfosse
2022-10-13 12:41:13 -04:00
committed by GitHub
parent 574e5d37c7
commit 459ee4e66a
8 changed files with 114 additions and 13 deletions
@@ -695,6 +695,19 @@ func (q *fakeQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg database.
workspaces = append(workspaces, workspace)
}
if arg.Offset > 0 {
if int(arg.Offset) > len(workspaces) {
return []database.Workspace{}, nil
}
workspaces = workspaces[arg.Offset:]
}
if arg.Limit > 0 {
if int(arg.Limit) > len(workspaces) {
return workspaces, nil
}
workspaces = workspaces[:arg.Limit]
}
return workspaces, nil
}
+7 -1
View File
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/lib/pq"
@@ -164,8 +165,11 @@ type workspaceQuerier interface {
// This code is copied from `GetWorkspaces` and adds the authorized filter WHERE
// clause.
func (q *sqlQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspacesParams, authorizedFilter rbac.AuthorizeFilter) ([]Workspace, error) {
// In order to properly use ORDER BY, OFFSET, and LIMIT, we need to inject the
// authorizedFilter between the end of the where clause and those statements.
filter := strings.Replace(getWorkspaces, "-- @authorize_filter", fmt.Sprintf(" AND %s", authorizedFilter.SQLString(rbac.NoACLConfig())), 1)
// The name comment is for metric tracking
query := fmt.Sprintf("-- name: GetAuthorizedWorkspaces :many\n%s AND %s", getWorkspaces, authorizedFilter.SQLString(rbac.NoACLConfig()))
query := fmt.Sprintf("-- name: GetAuthorizedWorkspaces :many\n%s", filter)
rows, err := q.db.QueryContext(ctx, query,
arg.Deleted,
arg.Status,
@@ -174,6 +178,8 @@ func (q *sqlQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspa
arg.TemplateName,
pq.Array(arg.TemplateIds),
arg.Name,
arg.Offset,
arg.Limit,
)
if err != nil {
return nil, xerrors.Errorf("get authorized workspaces: %w", err)
+15
View File
@@ -5553,6 +5553,17 @@ WHERE
name ILIKE '%' || $7 || '%'
ELSE true
END
-- Authorize Filter clause will be injected below in GetAuthorizedWorkspaces
-- @authorize_filter
ORDER BY
last_used_at DESC
LIMIT
CASE
WHEN $9 :: integer > 0 THEN
$9
END
OFFSET
$8
`
type GetWorkspacesParams struct {
@@ -5563,6 +5574,8 @@ type GetWorkspacesParams struct {
TemplateName string `db:"template_name" json:"template_name"`
TemplateIds []uuid.UUID `db:"template_ids" json:"template_ids"`
Name string `db:"name" json:"name"`
Offset int32 `db:"offset_" json:"offset_"`
Limit int32 `db:"limit_" json:"limit_"`
}
func (q *sqlQuerier) GetWorkspaces(ctx context.Context, arg GetWorkspacesParams) ([]Workspace, error) {
@@ -5574,6 +5587,8 @@ func (q *sqlQuerier) GetWorkspaces(ctx context.Context, arg GetWorkspacesParams)
arg.TemplateName,
pq.Array(arg.TemplateIds),
arg.Name,
arg.Offset,
arg.Limit,
)
if err != nil {
return nil, err
+11
View File
@@ -132,6 +132,17 @@ WHERE
name ILIKE '%' || @name || '%'
ELSE true
END
-- Authorize Filter clause will be injected below in GetAuthorizedWorkspaces
-- @authorize_filter
ORDER BY
last_used_at DESC
LIMIT
CASE
WHEN @limit_ :: integer > 0 THEN
@limit_
END
OFFSET
@offset_
;
-- name: GetWorkspaceByOwnerIDAndName :one