|
1 | 1 | CREATE OR REPLACE FUNCTION check_workspace_agent_name_unique()
|
2 | 2 | RETURNS TRIGGER AS $$
|
3 | 3 | DECLARE
|
4 |
| - workspace_id_var uuid; |
| 4 | + workspace_build_id uuid; |
5 | 5 | existing_count integer;
|
6 | 6 | 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 |
10 | 10 | FROM workspace_resources wr
|
11 | 11 | JOIN provisioner_jobs pj ON wr.job_id = pj.id
|
12 | 12 | JOIN workspace_builds wb ON pj.id = wb.job_id
|
13 | 13 | WHERE wr.id = NEW.resource_id;
|
14 | 14 |
|
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 |
17 | 17 | RETURN NEW;
|
18 | 18 | END IF;
|
19 | 19 |
|
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 |
21 | 21 | SELECT COUNT(*) INTO existing_count
|
22 | 22 | FROM workspace_agents wa
|
23 | 23 | JOIN workspace_resources wr ON wa.resource_id = wr.id
|
24 | 24 | JOIN provisioner_jobs pj ON wr.job_id = pj.id
|
25 | 25 | 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 |
27 | 27 | AND wa.name = NEW.name
|
28 | 28 | AND wa.id != NEW.id; -- Exclude the current agent (for updates)
|
29 | 29 |
|
|
0 commit comments