You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: coderd/database/migrations/000296_organization_soft_delete.up.sql
+21-2Lines changed: 21 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,7 @@ DECLARE
17
17
template_count int;
18
18
group_count int;
19
19
member_count int;
20
+
provisioner_keys_count int;
20
21
BEGIN
21
22
workspace_count := (
22
23
SELECTcount(*) as count FROM workspaces
@@ -44,6 +45,12 @@ BEGIN
44
45
organization_members.organization_id=OLD.id
45
46
);
46
47
48
+
provisioner_keys_count := (
49
+
Selectcount(*) as count FROM provisioner_keys
50
+
WHERE
51
+
provisioner_keys.organization_id=OLD.id
52
+
)
53
+
47
54
-- Fail the deletion if one of the following:
48
55
-- * the organization has 1 or more workspaces
49
56
-- * the organization has 1 or more templates
@@ -54,8 +61,20 @@ BEGIN
54
61
RAISE EXCEPTION 'cannot delete organization: organization has % workspaces and % templates that must be deleted first', workspace_count, template_count;
55
62
END IF;
56
63
57
-
IF (group_count + member_count) >2 THEN
58
-
RAISE EXCEPTION 'cannot delete organization: organization has % groups and % members that must be deleted first', group_count -1, member_count -1;
64
+
IF (group_count) >1 THEN
65
+
RAISE EXCEPTION 'cannot delete organization: organization has % groups that must be deleted first', group_count -1;
66
+
END IF;
67
+
68
+
IF (provisioner_keys_count) >0 THEN
69
+
RAISE EXCEPTION 'cannot delete organization: organization has % provisioner keys that must be deleted first', provisioner_keys_count;
70
+
END IF;
71
+
72
+
-- If member count > 1
73
+
-- Allow 1 member to exist, because you cannot remove yourself. You can
74
+
-- remove everyone else. Ideally, we only omit the member that matches
75
+
-- the user_id of the caller, however in a trigger, the caller is unknown.
76
+
IF (member_count) >1 THEN
77
+
RAISE EXCEPTION 'cannot delete organization: organization has % members that must be deleted first', member_count -1;
0 commit comments