mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
chore: remove excess join in GetQuotaConsumedForUser query (#15338)
Filter is applied in original workspace query. We do not need to join `workspaces` twice. Use build_number instead of `created_at` for determining the last build.
This commit is contained in:
@ -288,6 +288,15 @@ func WorkspaceBuild(t testing.TB, db database.Store, orig database.WorkspaceBuil
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if orig.DailyCost > 0 {
|
||||
err = db.UpdateWorkspaceBuildCostByID(genCtx, database.UpdateWorkspaceBuildCostByIDParams{
|
||||
ID: buildID,
|
||||
DailyCost: orig.DailyCost,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
build, err = db.GetWorkspaceBuildByID(genCtx, buildID)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -6746,25 +6746,19 @@ FROM
|
||||
INNER JOIN
|
||||
workspaces on wb.workspace_id = workspaces.id
|
||||
WHERE
|
||||
-- Only return workspaces that match the user + organization.
|
||||
-- Quotas are calculated per user per organization.
|
||||
NOT workspaces.deleted AND
|
||||
workspaces.owner_id = $1 AND
|
||||
workspaces.organization_id = $2
|
||||
ORDER BY
|
||||
wb.workspace_id,
|
||||
wb.created_at DESC
|
||||
wb.build_number DESC
|
||||
)
|
||||
SELECT
|
||||
coalesce(SUM(daily_cost), 0)::BIGINT
|
||||
FROM
|
||||
workspaces
|
||||
INNER JOIN latest_builds ON
|
||||
latest_builds.workspace_id = workspaces.id
|
||||
WHERE
|
||||
NOT deleted AND
|
||||
-- We can likely remove these conditions since we check above.
|
||||
-- But it does not hurt to be defensive and make sure future query changes
|
||||
-- do not break anything.
|
||||
workspaces.owner_id = $1 AND
|
||||
workspaces.organization_id = $2
|
||||
latest_builds
|
||||
`
|
||||
|
||||
type GetQuotaConsumedForUserParams struct {
|
||||
|
@ -28,23 +28,17 @@ FROM
|
||||
INNER JOIN
|
||||
workspaces on wb.workspace_id = workspaces.id
|
||||
WHERE
|
||||
-- Only return workspaces that match the user + organization.
|
||||
-- Quotas are calculated per user per organization.
|
||||
NOT workspaces.deleted AND
|
||||
workspaces.owner_id = @owner_id AND
|
||||
workspaces.organization_id = @organization_id
|
||||
ORDER BY
|
||||
wb.workspace_id,
|
||||
wb.created_at DESC
|
||||
wb.build_number DESC
|
||||
)
|
||||
SELECT
|
||||
coalesce(SUM(daily_cost), 0)::BIGINT
|
||||
FROM
|
||||
workspaces
|
||||
INNER JOIN latest_builds ON
|
||||
latest_builds.workspace_id = workspaces.id
|
||||
WHERE
|
||||
NOT deleted AND
|
||||
-- We can likely remove these conditions since we check above.
|
||||
-- But it does not hurt to be defensive and make sure future query changes
|
||||
-- do not break anything.
|
||||
workspaces.owner_id = @owner_id AND
|
||||
workspaces.organization_id = @organization_id
|
||||
latest_builds
|
||||
;
|
||||
|
Reference in New Issue
Block a user