-
Notifications
You must be signed in to change notification settings - Fork 963
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fb3cf7b
chore(coderd/database): create db trigger to enforce agent name uniqu…
DanielleMaywood 3c47f69
fix: unique per build not workspace
DanielleMaywood 043005a
fix: unique per resource not build
DanielleMaywood e51c8be
fix: for child agents, ensure unique across provisioner job
DanielleMaywood aae7049
chore: fix migration number
DanielleMaywood 58b8a2f
chore: ID -> name
DanielleMaywood f88f17b
fix: listen to dean
DanielleMaywood 74ab8f2
fix: use unique name per agent
DanielleMaywood 0615eec
Merge branch 'main' into dm-workspace-agent-name-uniqueness
DanielleMaywood 585b311
chore: feedback
DanielleMaywood 5164413
chore: some changes
DanielleMaywood d6edbb5
chore: improve sql query
DanielleMaywood 479681c
chore: feedback
DanielleMaywood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 2 additions & 0 deletions
2
coderd/database/migrations/000332_workspace_agent_name_unique_trigger.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
DROP TRIGGER IF EXISTS workspace_agent_name_unique_trigger ON workspace_agents; | ||
DROP FUNCTION IF EXISTS check_workspace_agent_name_unique(); |
45 changes: 45 additions & 0 deletions
45
coderd/database/migrations/000332_workspace_agent_name_unique_trigger.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
CREATE OR REPLACE FUNCTION check_workspace_agent_name_unique() | ||
RETURNS TRIGGER AS $$ | ||
DECLARE | ||
workspace_build_id uuid; | ||
agents_with_name int; | ||
BEGIN | ||
-- Find the workspace build the workspace agent is being inserted into. | ||
SELECT workspace_builds.id INTO workspace_build_id | ||
FROM workspace_resources | ||
JOIN workspace_builds ON workspace_builds.job_id = workspace_resources.job_id | ||
WHERE workspace_resources.id = NEW.resource_id; | ||
|
||
-- If the agent doesn't have a workspace build, we'll allow the insert. | ||
IF workspace_build_id IS NULL THEN | ||
RETURN NEW; | ||
END IF; | ||
|
||
-- Count how many agents in this workspace build already have the given agent name. | ||
SELECT COUNT(*) INTO agents_with_name | ||
FROM workspace_agents | ||
JOIN workspace_resources ON workspace_resources.id = workspace_agents.resource_id | ||
JOIN workspace_builds ON workspace_builds.job_id = workspace_resources.job_id | ||
WHERE workspace_builds.id = workspace_build_id | ||
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 this workspace build', NEW.name | ||
USING ERRCODE = 'unique_violation'; | ||
END IF; | ||
|
||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE TRIGGER workspace_agent_name_unique_trigger | ||
BEFORE INSERT OR UPDATE OF name, resource_id ON workspace_agents | ||
FOR EACH ROW | ||
EXECUTE FUNCTION check_workspace_agent_name_unique(); | ||
|
||
COMMENT ON TRIGGER workspace_agent_name_unique_trigger ON workspace_agents IS | ||
'Use a trigger instead of a unique constraint because existing data may violate | ||
the uniqueness requirement. A trigger allows us to enforce uniqueness going | ||
forward without requiring a migration to clean up historical data.'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.