Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b539ddc

Browse files
committed
chore: adding clarity around soft-deletion constraints
Signed-off-by: Danny Kopping <[email protected]>
1 parent 9e8bf7e commit b539ddc

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

coderd/database/queries.sql.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/prebuilds.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ FROM templates t
4141
INNER JOIN template_version_presets tvp ON tvp.template_version_id = tv.id
4242
INNER JOIN organizations o ON o.id = t.organization_id
4343
WHERE tvp.desired_instances IS NOT NULL -- Consider only presets that have a prebuild configuration.
44+
-- AND NOT t.deleted -- We don't exclude deleted templates because there's no constraint in the DB preventing a soft deletion on a template while workspaces are running.
4445
AND (t.id = sqlc.narg('template_id')::uuid OR sqlc.narg('template_id') IS NULL);
4546

4647
-- name: GetRunningPrebuiltWorkspaces :many
@@ -71,6 +72,7 @@ FROM workspace_latest_builds wlb
7172
-- prebuilds that are still building.
7273
INNER JOIN templates t ON t.active_version_id = wlb.template_version_id
7374
WHERE wlb.job_status IN ('pending'::provisioner_job_status, 'running'::provisioner_job_status)
75+
-- AND NOT t.deleted -- We don't exclude deleted templates because there's no constraint in the DB preventing a soft deletion on a template while workspaces are running.
7476
GROUP BY t.id, wpb.template_version_id, wpb.transition, wlb.template_version_preset_id;
7577

7678
-- GetPresetsBackoff groups workspace builds by preset ID.

enterprise/coderd/prebuilds/reconcile_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,15 @@ func TestPrebuildReconciliation(t *testing.T) {
294294
templateDeleted: []bool{false},
295295
},
296296
{
297-
name: "delete prebuilds for deleted templates",
297+
// Templates can be soft-deleted (`deleted=true`) or hard-deleted (row is removed).
298+
// On the former there is *no* DB constraint to prevent soft deletion, so we have to ensure that if somehow
299+
// the template was soft-deleted any running prebuilds will be removed.
300+
// On the latter there is a DB constraint to prevent row deletion if any workspaces reference the deleting template.
301+
name: "soft-deleted templates MAY have prebuilds",
298302
prebuildLatestTransitions: []database.WorkspaceTransition{database.WorkspaceTransitionStart},
299303
prebuildJobStatuses: []database.ProvisionerJobStatus{database.ProvisionerJobStatusSucceeded},
300304
templateVersionActive: []bool{true, false},
305+
shouldCreateNewPrebuild: ptr.To(false),
301306
shouldDeleteOldPrebuild: ptr.To(true),
302307
templateDeleted: []bool{true},
303308
},

0 commit comments

Comments
 (0)