Skip to content

Commit 2417efc

Browse files
fix: unique per build not workspace
1 parent 97e0d44 commit 2417efc

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

coderd/database/dump.sql

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

coderd/database/migrations/000330_workspace_agent_name_unique_trigger.up.sql

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
CREATE OR REPLACE FUNCTION check_workspace_agent_name_unique()
22
RETURNS TRIGGER AS $$
33
DECLARE
4-
workspace_id_var uuid;
4+
workspace_build_id uuid;
55
existing_count integer;
66
BEGIN
7-
-- Get the workspace_id for this agent by following the relationship chain:
8-
-- workspace_agents -> workspace_resources -> provisioner_jobs -> workspace_builds -> workspaces
9-
SELECT wb.workspace_id INTO workspace_id_var
7+
-- Get the workspace_build.id for this agent by following the relationship chain:
8+
-- workspace_agents -> workspace_resources -> provisioner_jobs -> workspace_builds
9+
SELECT wb.id INTO workspace_build_id
1010
FROM workspace_resources wr
1111
JOIN provisioner_jobs pj ON wr.job_id = pj.id
1212
JOIN workspace_builds wb ON pj.id = wb.job_id
1313
WHERE wr.id = NEW.resource_id;
1414

15-
-- If we couldn't find a workspace_id, allow the insert (might be a template import or other edge case)
16-
IF workspace_id_var IS NULL THEN
15+
-- If we couldn't find a workspace_build_id, allow the insert (might be a template import or other edge case)
16+
IF workspace_build_id IS NULL THEN
1717
RETURN NEW;
1818
END IF;
1919

20-
-- Check if there's already an agent with this name in this workspace
20+
-- Check if there's already an agent with this name for this workspace build
2121
SELECT COUNT(*) INTO existing_count
2222
FROM workspace_agents wa
2323
JOIN workspace_resources wr ON wa.resource_id = wr.id
2424
JOIN provisioner_jobs pj ON wr.job_id = pj.id
2525
JOIN workspace_builds wb ON pj.id = wb.job_id
26-
WHERE wb.workspace_id = workspace_id_var
26+
WHERE wb.id = workspace_build_id
2727
AND wa.name = NEW.name
2828
AND wa.id != NEW.id; -- Exclude the current agent (for updates)
2929

0 commit comments

Comments
 (0)