fix(coderd/database): match workspace requester by owner ID when sorting (#32188)

GetWorkspaces sorted the requester's favorite and own workspaces first
by matching the owner's username. Usernames are unique only among
non-deleted users, and a deleted user keeps theirs, so a user who took a
deleted user's username saw that user's deleted workspaces and favorites
sorted as their own when listing deleted workspaces. Match by owner_id
instead, as the sort did before #22290.

The username form most likely worked around the sqlc CTE bug: with the
EXISTS (tasks) filter the query had then, sqlc without the coder/sqlc
fix rejected the bare owner_id as ambiguous. #29085 removed that filter,
so the owner_id form generates with either sqlc.
This commit is contained in:
Mathias Fredriksson authored and GitHub committed 2026-11-17 13:41:57 +02:00
1 parent 4866d0fb1f
commit c3ff770e8a
3 files changed
+51 -4

No files matched your search

+2 -2
View File
@@ -398,9 +398,9 @@ WHERE
filtered_workspaces fw
ORDER BY
-- Favorited workspaces should show up first only for their owner.
CASE WHEN favorite AND owner_username = (SELECT users.username FROM users WHERE users.id = @requester_id) THEN 0 ELSE 1 END ASC,
CASE WHEN favorite AND owner_id = @requester_id THEN 0 ELSE 1 END ASC,
-- Workspaces you own should show up first.
CASE WHEN owner_username = (SELECT users.username FROM users WHERE users.id = @requester_id) THEN 0 ELSE 1 END ASC,
CASE WHEN owner_id = @requester_id THEN 0 ELSE 1 END ASC,
-- Running workspaces should show up first.
(latest_build_completed_at IS NOT NULL AND
latest_build_canceled_at IS NULL AND