Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ad0b191

Browse files
author
Claude
committed
fix: Improve error message when deleting organization with resources
This fixes issue #477 where the error message when deleting an organization that still has resources incorrectly references deleted resources with a zero value. Now, only resources that actually exist will be mentioned in the error message.
1 parent cb19fd4 commit ad0b191

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

coderd/database/migrations/000296_organization_soft_delete.up.sql

+20-1
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,27 @@ BEGIN
5858
-- * the organization has 1 or more members other than the organization owner
5959
-- * the organization has 1 or more provisioner keys
6060

61+
-- Only create error message for resources that actually exist
6162
IF (workspace_count + template_count + provisioner_keys_count) > 0 THEN
62-
RAISE EXCEPTION 'cannot delete organization: organization has % workspaces, % templates, and % provisioner keys that must be deleted first', workspace_count, template_count, provisioner_keys_count;
63+
DECLARE
64+
error_message text := 'cannot delete organization: organization has ';
65+
error_parts text[] := '{}';
66+
BEGIN
67+
IF workspace_count > 0 THEN
68+
error_parts := array_append(error_parts, workspace_count || ' workspaces');
69+
END IF;
70+
71+
IF template_count > 0 THEN
72+
error_parts := array_append(error_parts, template_count || ' templates');
73+
END IF;
74+
75+
IF provisioner_keys_count > 0 THEN
76+
error_parts := array_append(error_parts, provisioner_keys_count || ' provisioner keys');
77+
END IF;
78+
79+
error_message := error_message || array_to_string(error_parts, ', ') || ' that must be deleted first';
80+
RAISE EXCEPTION '%', error_message;
81+
END;
6382
END IF;
6483

6584
IF (group_count) > 1 THEN

0 commit comments

Comments
 (0)