diff --git a/coderd/database/queries.sql.go b/coderd/database/queries.sql.go index aa8f9c542f..c4cc4246fd 100644 --- a/coderd/database/queries.sql.go +++ b/coderd/database/queries.sql.go @@ -11300,20 +11300,30 @@ func (q *sqlQuerier) UpdateWorkspaceDeletedByID(ctx context.Context, arg UpdateW const updateWorkspaceDormantDeletingAt = `-- name: UpdateWorkspaceDormantDeletingAt :one UPDATE - workspaces + workspaces SET - dormant_at = $2, - -- When a workspace is active we want to update the last_used_at to avoid the workspace going + dormant_at = $2, + -- When a workspace is active we want to update the last_used_at to avoid the workspace going -- immediately dormant. If we're transition the workspace to dormant then we leave it alone. - last_used_at = CASE WHEN $2::timestamptz IS NULL THEN now() at time zone 'utc' ELSE last_used_at END, - -- If dormant_at is null (meaning active) or the template-defined time_til_dormant_autodelete is 0 we should set - -- deleting_at to NULL else set it to the dormant_at + time_til_dormant_autodelete duration. - deleting_at = CASE WHEN $2::timestamptz IS NULL OR templates.time_til_dormant_autodelete = 0 THEN NULL ELSE $2::timestamptz + INTERVAL '1 milliseconds' * templates.time_til_dormant_autodelete / 1000000 END + last_used_at = CASE WHEN $2::timestamptz IS NULL THEN + now() at time zone 'utc' + ELSE + last_used_at + END, + -- If dormant_at is null (meaning active) or the template-defined time_til_dormant_autodelete is 0 we should set + -- deleting_at to NULL else set it to the dormant_at + time_til_dormant_autodelete duration. + deleting_at = CASE WHEN $2::timestamptz IS NULL OR templates.time_til_dormant_autodelete = 0 THEN + NULL + ELSE + $2::timestamptz + (INTERVAL '1 millisecond' * (templates.time_til_dormant_autodelete / 1000000)) + END FROM - templates + templates WHERE - workspaces.id = $1 -RETURNING workspaces.id, workspaces.created_at, workspaces.updated_at, workspaces.owner_id, workspaces.organization_id, workspaces.template_id, workspaces.deleted, workspaces.name, workspaces.autostart_schedule, workspaces.ttl, workspaces.last_used_at, workspaces.dormant_at, workspaces.deleting_at, workspaces.automatic_updates + workspaces.id = $1 + AND templates.id = workspaces.template_id +RETURNING + workspaces.id, workspaces.created_at, workspaces.updated_at, workspaces.owner_id, workspaces.organization_id, workspaces.template_id, workspaces.deleted, workspaces.name, workspaces.autostart_schedule, workspaces.ttl, workspaces.last_used_at, workspaces.dormant_at, workspaces.deleting_at, workspaces.automatic_updates ` type UpdateWorkspaceDormantDeletingAtParams struct { diff --git a/coderd/database/queries/workspaces.sql b/coderd/database/queries/workspaces.sql index 01a283d1bc..ac3a1fd86c 100644 --- a/coderd/database/queries/workspaces.sql +++ b/coderd/database/queries/workspaces.sql @@ -476,20 +476,30 @@ WHERE -- name: UpdateWorkspaceDormantDeletingAt :one UPDATE - workspaces + workspaces SET - dormant_at = $2, - -- When a workspace is active we want to update the last_used_at to avoid the workspace going + dormant_at = $2, + -- When a workspace is active we want to update the last_used_at to avoid the workspace going -- immediately dormant. If we're transition the workspace to dormant then we leave it alone. - last_used_at = CASE WHEN $2::timestamptz IS NULL THEN now() at time zone 'utc' ELSE last_used_at END, - -- If dormant_at is null (meaning active) or the template-defined time_til_dormant_autodelete is 0 we should set - -- deleting_at to NULL else set it to the dormant_at + time_til_dormant_autodelete duration. - deleting_at = CASE WHEN $2::timestamptz IS NULL OR templates.time_til_dormant_autodelete = 0 THEN NULL ELSE $2::timestamptz + INTERVAL '1 milliseconds' * templates.time_til_dormant_autodelete / 1000000 END + last_used_at = CASE WHEN $2::timestamptz IS NULL THEN + now() at time zone 'utc' + ELSE + last_used_at + END, + -- If dormant_at is null (meaning active) or the template-defined time_til_dormant_autodelete is 0 we should set + -- deleting_at to NULL else set it to the dormant_at + time_til_dormant_autodelete duration. + deleting_at = CASE WHEN $2::timestamptz IS NULL OR templates.time_til_dormant_autodelete = 0 THEN + NULL + ELSE + $2::timestamptz + (INTERVAL '1 millisecond' * (templates.time_til_dormant_autodelete / 1000000)) + END FROM - templates + templates WHERE - workspaces.id = $1 -RETURNING workspaces.*; + workspaces.id = $1 + AND templates.id = workspaces.template_id +RETURNING + workspaces.*; -- name: UpdateWorkspacesDormantDeletingAtByTemplateID :exec UPDATE workspaces