@@ -494,11 +494,24 @@ func (api *API) patchWorkspace(rw http.ResponseWriter, r *http.Request) {
494
494
name = req .Name
495
495
}
496
496
497
- n , err := api .Database .UpdateWorkspace (r .Context (), database.UpdateWorkspaceParams {
497
+ err := api .Database .UpdateWorkspace (r .Context (), database.UpdateWorkspaceParams {
498
498
ID : workspace .ID ,
499
499
Name : name ,
500
500
})
501
501
if err != nil {
502
+ // The query protects against updating deleted workspaces and
503
+ // the existence of the workspace is checked in the request,
504
+ // the only conclusion we can make is that we're trying to
505
+ // update a deleted workspace.
506
+ //
507
+ // We could do this check earlier but since we're not in a
508
+ // transaction, it's pointless.
509
+ if errors .Is (err , sql .ErrNoRows ) {
510
+ httpapi .Write (rw , http .StatusMethodNotAllowed , codersdk.Response {
511
+ Message : fmt .Sprintf ("Workspace %q is deleted and cannot be updated." , workspace .Name ),
512
+ })
513
+ return
514
+ }
502
515
// Check if the name was already in use.
503
516
if database .IsUniqueViolation (err , database .UniqueWorkspacesOwnerIDLowerIdx ) {
504
517
httpapi .Write (rw , http .StatusConflict , codersdk.Response {
@@ -516,18 +529,6 @@ func (api *API) patchWorkspace(rw http.ResponseWriter, r *http.Request) {
516
529
})
517
530
return
518
531
}
519
- // If no rows were affected, the workspace must have been deleted
520
- // between the start of the request and the query updating the
521
- // workspace name.
522
- //
523
- // We could do this check earlier but we'd have to start a
524
- // transaction to ensure consistency.
525
- if n == 0 {
526
- httpapi .Write (rw , http .StatusMethodNotAllowed , codersdk.Response {
527
- Message : fmt .Sprintf ("Workspace %q is deleted and cannot be updated." , workspace .Name ),
528
- })
529
- return
530
- }
531
532
532
533
rw .WriteHeader (http .StatusNoContent )
533
534
}
0 commit comments