fix(coderd): mark sub agent deletion via boolean instead of delete (#18411)

Deletion of data is uncommon in our database, so the introduction of sub agents
and the deletion of them introduced issues with foreign key assumptions, as can
be seen in coder/internal#685. We could have only addressed the specific case by
allowing cascade deletion of stats as well as handling in the stats collector,
but it's unclear how many more such edge-cases we could run into.

In this change, we mark the rows as deleted via boolean instead, and filter them
out in all relevant queries.

Fixes coder/internal#685
This commit is contained in:
Mathias Fredriksson
2025-06-19 16:32:51 +03:00
committed by GitHub
parent 68f21fa523
commit 511fd09582
13 changed files with 385 additions and 38 deletions
+7 -1
View File
@@ -303,6 +303,8 @@ WHERE
WHERE
workspace_resources.job_id = latest_build.provisioner_job_id AND
latest_build.transition = 'start'::workspace_transition AND
-- Filter out deleted sub agents.
workspace_agents.deleted = FALSE AND
@has_agent = (
CASE
WHEN workspace_agents.first_connected_at IS NULL THEN
@@ -846,7 +848,11 @@ LEFT JOIN LATERAL (
workspace_agents.name as agent_name,
job_id
FROM workspace_resources
JOIN workspace_agents ON workspace_agents.resource_id = workspace_resources.id
JOIN workspace_agents ON (
workspace_agents.resource_id = workspace_resources.id
-- Filter out deleted sub agents.
AND workspace_agents.deleted = FALSE
)
WHERE job_id = latest_build.job_id
) resources ON true
WHERE