|
1 | 1 | CREATE OR REPLACEFUNCTIONcheck_workspace_agent_name_unique()
|
2 | 2 | RETURNS TRIGGERAS $$
|
3 | 3 | DECLARE
|
4 |
| -workspace_id_var uuid; |
| 4 | +workspace_build_id uuid; |
5 | 5 | existing_countinteger;
|
6 | 6 | BEGIN
|
7 |
| --- Get theworkspace_id for this agent by following the relationship chain: |
8 |
| --- workspace_agents -> workspace_resources -> provisioner_jobs -> workspace_builds -> workspaces |
9 |
| -SELECTwb.workspace_id INTOworkspace_id_var |
| 7 | +-- Get theworkspace_build.id for this agent by following the relationship chain: |
| 8 | +-- workspace_agents -> workspace_resources -> provisioner_jobs -> workspace_builds |
| 9 | +SELECTwb.id INTOworkspace_build_id |
10 | 10 | FROM workspace_resources wr
|
11 | 11 | JOIN provisioner_jobs pjONwr.job_id=pj.id
|
12 | 12 | JOIN workspace_builds wbONpj.id=wb.job_id
|
13 | 13 | WHEREwr.id=NEW.resource_id;
|
14 | 14 |
|
15 |
| --- If we couldn't find aworkspace_id, allow the insert (might be a template import or other edge case) |
16 |
| - IFworkspace_id_var ISNULL THEN |
| 15 | +-- If we couldn't find aworkspace_build_id, allow the insert (might be a template import or other edge case) |
| 16 | + IFworkspace_build_id ISNULL THEN |
17 | 17 | RETURN NEW;
|
18 | 18 | END IF;
|
19 | 19 |
|
20 |
| --- Check if there's already an agent with this namein this workspace |
| 20 | +-- Check if there's already an agent with this namefor this workspace build |
21 | 21 | SELECTCOUNT(*) INTO existing_count
|
22 | 22 | FROM workspace_agents wa
|
23 | 23 | JOIN workspace_resources wrONwa.resource_id=wr.id
|
24 | 24 | JOIN provisioner_jobs pjONwr.job_id=pj.id
|
25 | 25 | JOIN workspace_builds wbONpj.id=wb.job_id
|
26 |
| -WHEREwb.workspace_id=workspace_id_var |
| 26 | +WHEREwb.id=workspace_build_id |
27 | 27 | ANDwa.name=NEW.name
|
28 | 28 | ANDwa.id!=NEW.id;-- Exclude the current agent (for updates)
|
29 | 29 |
|
|