- Notifications
You must be signed in to change notification settings - Fork929
chore(coderd/database): enforce agent name unique within workspace build#18052
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
fb3cf7b
3c47f69
043005a
e51c8be
aae7049
58b8a2f
f88f17b
74ab8f2
0615eec
585b311
5164413
d6edbb5
479681c
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
The CI will fail for this as there are test failures for insights.
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -2,36 +2,41 @@ CREATE OR REPLACE FUNCTION check_workspace_agent_name_unique() | ||
RETURNS TRIGGER AS $$ | ||
DECLARE | ||
provisioner_job_id uuid; | ||
workspace_id_var uuid; | ||
agents_with_name int; | ||
BEGIN | ||
-- Find the provisioner job and workspace the agent is | ||
-- being inserted into. | ||
SELECT INTO provisioner_job_id, workspace_id_var | ||
DanielleMaywood marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
provisioner_jobs.id, workspaces.id | ||
FROM workspace_resources | ||
JOIN provisioner_jobs ON provisioner_jobs.id = workspace_resources.job_id | ||
JOIN workspace_builds ON workspace_builds.job_id = provisioner_jobs.id | ||
DanielleMaywood marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
JOIN workspaces ON workspaces.id = workspace_builds.workspace_id | ||
DanielleMaywood marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
WHERE workspace_resources.id = NEW.resource_id; | ||
-- If there is no workspace or provisioner job attached to the agent, | ||
-- we will allow the insert to happen as there is no need to guarantee | ||
-- uniqueness. | ||
IF workspace_id_var IS NULL OR provisioner_job_id IS NULL THEN | ||
RETURN NEW; | ||
END IF; | ||
-- Count how many agents in this provisioner job already have | ||
-- the given agent name. | ||
SELECT COUNT(*) INTO agents_with_name | ||
DanielleMaywood marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
FROM workspace_agents | ||
JOIN workspace_resources ON workspace_resources.id = workspace_agents.resource_id | ||
JOIN provisioner_jobs ON provisioner_jobs.id = workspace_resources.job_id | ||
JOIN workspace_builds ON workspace_builds.job_id = provisioner_jobs.id | ||
WHERE provisioner_jobs.id = provisioner_job_id | ||
AND workspace_builds.workspace_id = workspace_id_var | ||
AND workspace_agents.name = NEW.name | ||
AND workspace_agents.id != NEW.id; | ||
-- If there's already an agent with this name, raise an error | ||
IF agents_with_name > 0 THEN | ||
RAISE EXCEPTION 'workspace agent name "%" already exists in thisprovisioner job', NEW.name | ||
USING ERRCODE = 'unique_violation'; | ||
END IF; | ||
Uh oh!
There was an error while loading.Please reload this page.