Skip to content

Commit 9d03e04

Browse files
authored
fix: workspaces query to correctly user username from users table (#15305)
The subquery on the users table was incorrectly using the username from the `workspaces` table, not the `users` table. This passed `sqlc-vet` because the column did exist in the query, it just was not the correct one.
1 parent 4849b4d commit 9d03e04

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

coderd/database/queries.sql.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/workspaces.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ WHERE
233233
-- Filter by owner_name
234234
AND CASE
235235
WHEN @owner_username :: text != '' THEN
236-
workspaces.owner_id = (SELECT id FROM users WHERE lower(owner_username) = lower(@owner_username) AND deleted = false)
236+
workspaces.owner_id = (SELECT id FROM users WHERE lower(users.username) = lower(@owner_username) AND deleted = false)
237237
ELSE true
238238
END
239239
-- Filter by template_name

coderd/workspaces_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,39 @@ func TestWorkspaceFilterManual(t *testing.T) {
13131313
require.NoError(t, err)
13141314
require.Len(t, res.Workspaces, 0)
13151315
})
1316+
t.Run("Owner", func(t *testing.T) {
1317+
t.Parallel()
1318+
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
1319+
user := coderdtest.CreateFirstUser(t, client)
1320+
otherUser, _ := coderdtest.CreateAnotherUser(t, client, user.OrganizationID, rbac.RoleOwner())
1321+
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
1322+
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
1323+
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
1324+
1325+
// Add a non-matching workspace
1326+
coderdtest.CreateWorkspace(t, otherUser, template.ID)
1327+
1328+
workspaces := []codersdk.Workspace{
1329+
coderdtest.CreateWorkspace(t, client, template.ID),
1330+
coderdtest.CreateWorkspace(t, client, template.ID),
1331+
}
1332+
1333+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
1334+
defer cancel()
1335+
1336+
sdkUser, err := client.User(ctx, codersdk.Me)
1337+
require.NoError(t, err)
1338+
1339+
// match owner name
1340+
res, err := client.Workspaces(ctx, codersdk.WorkspaceFilter{
1341+
FilterQuery: fmt.Sprintf("owner:%s", sdkUser.Username),
1342+
})
1343+
require.NoError(t, err)
1344+
require.Len(t, res.Workspaces, len(workspaces))
1345+
for _, found := range res.Workspaces {
1346+
require.Equal(t, found.OwnerName, sdkUser.Username)
1347+
}
1348+
})
13161349
t.Run("IDs", func(t *testing.T) {
13171350
t.Parallel()
13181351
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy