Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
DanielleMaywood merged 13 commits intomainfromdm-workspace-agent-name-uniqueness
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
13 commits
Select commitHold shift + click to select a range
fb3cf7b
chore(coderd/database): create db trigger to enforce agent name uniqu…
DanielleMaywoodMay 27, 2025
3c47f69
fix: unique per build not workspace
DanielleMaywoodMay 27, 2025
043005a
fix: unique per resource not build
DanielleMaywoodMay 27, 2025
e51c8be
fix: for child agents, ensure unique across provisioner job
DanielleMaywoodMay 27, 2025
aae7049
chore: fix migration number
DanielleMaywoodMay 27, 2025
58b8a2f
chore: ID -> name
DanielleMaywoodMay 27, 2025
f88f17b
fix: listen to dean
DanielleMaywoodMay 27, 2025
74ab8f2
fix: use unique name per agent
DanielleMaywoodMay 28, 2025
0615eec
Merge branch 'main' into dm-workspace-agent-name-uniqueness
DanielleMaywoodMay 28, 2025
585b311
chore: feedback
DanielleMaywoodMay 28, 2025
5164413
chore: some changes
DanielleMaywoodMay 28, 2025
d6edbb5
chore: improve sql query
DanielleMaywoodMay 28, 2025
479681c
chore: feedback
DanielleMaywoodMay 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
fix: listen to dean
The CI will fail for this as there are test failures for insights.
  • Loading branch information
@DanielleMaywood
DanielleMaywood committedMay 27, 2025
commitf88f17beae4b8b65eb96f6e372e0e39f6e67a1d4
55 changes: 30 additions & 25 deletionscoderd/database/dump.sql
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,36 +2,41 @@ CREATE OR REPLACE FUNCTION check_workspace_agent_name_unique()
RETURNS TRIGGER AS $$
DECLARE
provisioner_job_id uuid;
agents_with_name integer;
workspace_id_var uuid;
agents_with_name int;
BEGIN
IF NEW.parent_id IS NULL THEN
-- Count how many agents in this resource already have
-- the given agent name.
SELECT COUNT(*) INTO agents_with_name
FROM workspace_agents
WHERE workspace_agents.resource_id = NEW.resource_id
AND workspace_agents.name = NEW.name
AND workspace_agents.id != NEW.id;
ELSE
SELECT provisioner_jobs.id INTO provisioner_job_id
FROM workspace_resources
JOIN provisioner_jobs ON provisioner_jobs.id = workspace_resources.job_id
WHERE workspace_resources.id = NEW.resource_id;
-- Find the provisioner job and workspace the agent is
-- being inserted into.
SELECT INTO provisioner_job_id, workspace_id_var
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
JOIN workspaces ON workspaces.id = workspace_builds.workspace_id
WHERE workspace_resources.id = NEW.resource_id;

-- Count how many agents in this provisioner job 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 provisioner_jobs ON provisioner_jobs.id = workspace_resources.job_id
WHERE provisioner_jobs.id = provisioner_job_id
AND workspace_agents.name = NEW.name
AND workspace_agents.id != NEW.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
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 thisworkspace resource', NEW.name
RAISE EXCEPTION 'workspace agent name "%" already exists in thisprovisioner job', NEW.name
USING ERRCODE = 'unique_violation';
END IF;

Expand Down
6 changes: 3 additions & 3 deletionscoderd/database/querier_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4781,7 +4781,7 @@ func TestWorkspaceAgentNameUniqueTrigger(t *testing.T) {
var pqErr *pq.Error
require.True(t, errors.As(err, &pqErr))
require.Equal(t, pq.ErrorCode("23505"), pqErr.Code) // unique_violation
require.Contains(t, pqErr.Message, `workspace agent name "duplicate-agent" already exists in thisworkspace resource`)
require.Contains(t, pqErr.Message, `workspace agent name "duplicate-agent" already exists in thisprovisioner job`)
})

t.Run("DuplicateNamesInSameProvisionerJob", func(t *testing.T) {
Expand DownExpand Up@@ -4812,7 +4812,7 @@ func TestWorkspaceAgentNameUniqueTrigger(t *testing.T) {
var pqErr *pq.Error
require.True(t, errors.As(err, &pqErr))
require.Equal(t, pq.ErrorCode("23505"), pqErr.Code) // unique_violation
require.Contains(t, pqErr.Message, `workspace agent name "duplicate-agent" already exists in thisworkspace resource`)
require.Contains(t, pqErr.Message, `workspace agent name "duplicate-agent" already exists in thisprovisioner job`)
})

t.Run("DuplicateChildNamesOverMultipleResources", func(t *testing.T) {
Expand DownExpand Up@@ -4857,7 +4857,7 @@ func TestWorkspaceAgentNameUniqueTrigger(t *testing.T) {
var pqErr *pq.Error
require.True(t, errors.As(err, &pqErr))
require.Equal(t, pq.ErrorCode("23505"), pqErr.Code) // unique_violation
require.Contains(t, pqErr.Message, `workspace agent name "child-agent" already exists in thisworkspace resource`)
require.Contains(t, pqErr.Message, `workspace agent name "child-agent" already exists in thisprovisioner job`)
})

t.Run("SameNamesInDifferentWorkspaces", func(t *testing.T) {
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp