Skip to content

fix: Sort workspace by name by created_at #2214

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 4 commits into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Next Next commit
fix: Sort workspace by name by created_at
Fix bug where deleting workspaces with the same name returns the
oldest deleted workspace
  • Loading branch information
Emyrk committed Jun 9, 2022
commit f56b73d9aaecfb875e63822baf50a505dd6d93fb
1 change: 1 addition & 0 deletions coderd/database/queries.sql.go

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

3 changes: 2 additions & 1 deletion coderd/database/queries/workspaces.sql
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ FROM
WHERE
owner_id = @owner_id
AND deleted = @deleted
AND LOWER("name") = LOWER(@name);
AND LOWER("name") = LOWER(@name)
ORDER BY created_at DESC;

-- name: GetWorkspaceOwnerCountsByTemplateIDs :many
SELECT
Expand Down
6 changes: 2 additions & 4 deletions coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,16 @@ func (api *API) workspace(rw http.ResponseWriter, r *http.Request) {
return
}

// The `deleted` query parameter (which defaults to `false`) MUST match the
// `Deleted` field on the workspace otherwise you will get a 410 Gone.
var (
deletedStr = r.URL.Query().Get("deleted")
deletedStr = r.URL.Query().Get("include_deleted")
showDeleted = false
)
if deletedStr != "" {
var err error
showDeleted, err = strconv.ParseBool(deletedStr)
if err != nil {
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
Message: fmt.Sprintf("Invalid boolean value %q for \"deleted\" query param.", deletedStr),
Message: fmt.Sprintf("Invalid boolean value %q for \"include_deleted\" query param.", deletedStr),
Validations: []httpapi.Error{
{Field: "deleted", Detail: "Must be a valid boolean"},
},
Expand Down
14 changes: 5 additions & 9 deletions codersdk/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ type CreateWorkspaceBuildRequest struct {
}

type WorkspaceOptions struct {
Deleted bool `json:"deleted,omitempty"`
IncludeDeleted bool `json:"deleted,omitempty"`
}

// asRequestOption returns a function that can be used in (*Client).Request.
// It modifies the request query parameters.
func (o WorkspaceOptions) asRequestOption() requestOption {
return func(r *http.Request) {
q := r.URL.Query()
if o.Deleted {
q.Set("deleted", "true")
if o.IncludeDeleted {
q.Set("include_deleted", "true")
}
r.URL.RawQuery = q.Encode()
}
Expand All @@ -62,7 +62,7 @@ func (c *Client) Workspace(ctx context.Context, id uuid.UUID) (Workspace, error)
// DeletedWorkspace returns a single workspace that was deleted.
func (c *Client) DeletedWorkspace(ctx context.Context, id uuid.UUID) (Workspace, error) {
o := WorkspaceOptions{
Deleted: true,
IncludeDeleted: true,
}
return c.getWorkspace(ctx, id, o.asRequestOption())
}
Expand Down Expand Up @@ -258,12 +258,8 @@ func (c *Client) Workspaces(ctx context.Context, filter WorkspaceFilter) ([]Work
return workspaces, json.NewDecoder(res.Body).Decode(&workspaces)
}

type WorkspaceByOwnerAndNameParams struct {
IncludeDeleted bool `json:"include_deleted,omitempty"`
}

// WorkspaceByOwnerAndName returns a workspace by the owner's UUID and the workspace's name.
func (c *Client) WorkspaceByOwnerAndName(ctx context.Context, owner string, name string, params WorkspaceByOwnerAndNameParams) (Workspace, error) {
func (c *Client) WorkspaceByOwnerAndName(ctx context.Context, owner string, name string, params WorkspaceOptions) (Workspace, error) {
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/users/%s/workspace/%s", owner, name), nil, func(r *http.Request) {
q := r.URL.Query()
q.Set("include_deleted", fmt.Sprintf("%t", params.IncludeDeleted))
Expand Down
2 changes: 1 addition & 1 deletion site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export const getWorkspaces = async (filter?: TypesGen.WorkspaceFilter): Promise<
export const getWorkspaceByOwnerAndName = async (
username = "me",
workspaceName: string,
params?: TypesGen.WorkspaceByOwnerAndNameParams,
params?: TypesGen.WorkspaceOptions,
): Promise<TypesGen.Workspace> => {
const response = await axios.get<TypesGen.Workspace>(`/api/v2/users/${username}/workspace/${workspaceName}`, {
params,
Expand Down
5 changes: 0 additions & 5 deletions site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,11 +464,6 @@ export interface WorkspaceBuildsRequest extends Pagination {
readonly WorkspaceID: string
}

// From codersdk/workspaces.go:261:6
export interface WorkspaceByOwnerAndNameParams {
readonly include_deleted?: boolean
}

// From codersdk/workspaces.go:219:6
export interface WorkspaceFilter {
readonly organization_id?: string
Expand Down