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/000306_update_protect_deleting_organization_function.down.sql
+19-19Lines changed: 19 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -3,14 +3,14 @@ DROP TRIGGER IF EXISTS protect_deleting_organizations ON organizations;
3
3
4
4
-- Revert the function to its original implementation
5
5
CREATE OR REPLACEFUNCTIONprotect_deleting_organizations()
6
-
RETURNS TRIGGER AS
6
+
RETURNS TRIGGER AS
7
7
$$
8
8
DECLARE
9
9
workspace_count int;
10
-
template_count int;
11
-
group_count int;
12
-
member_count int;
13
-
provisioner_keys_count int;
10
+
template_count int;
11
+
group_count int;
12
+
member_count int;
13
+
provisioner_keys_count int;
14
14
BEGIN
15
15
workspace_count := (
16
16
SELECTcount(*) as count FROM workspaces
@@ -19,50 +19,50 @@ BEGIN
19
19
ANDworkspaces.deleted= false
20
20
);
21
21
22
-
template_count := (
22
+
template_count := (
23
23
SELECTcount(*) as count FROM templates
24
24
WHERE
25
25
templates.organization_id=OLD.id
26
26
ANDtemplates.deleted= false
27
27
);
28
28
29
-
group_count := (
29
+
group_count := (
30
30
SELECTcount(*) as count FROM groups
31
31
WHERE
32
32
groups.organization_id=OLD.id
33
33
);
34
34
35
-
member_count := (
35
+
member_count := (
36
36
SELECTcount(*) as count FROM organization_members
37
37
WHERE
38
38
organization_members.organization_id=OLD.id
39
39
);
40
40
41
-
provisioner_keys_count := (
42
-
Selectcount(*) as count FROM provisioner_keys
43
-
WHERE
44
-
provisioner_keys.organization_id=OLD.id
45
-
);
41
+
provisioner_keys_count := (
42
+
Selectcount(*) as count FROM provisioner_keys
43
+
WHERE
44
+
provisioner_keys.organization_id=OLD.id
45
+
);
46
46
47
47
-- Fail the deletion if one of the following:
48
48
-- * the organization has 1 or more workspaces
49
-
-- * the organization has 1 or more templates
50
-
-- * the organization has 1 or more groups other than "Everyone" group
51
-
-- * the organization has 1 or more members other than the organization owner
52
-
-- * the organization has 1 or more provisioner keys
49
+
-- * the organization has 1 or more templates
50
+
-- * the organization has 1 or more groups other than "Everyone" group
51
+
-- * the organization has 1 or more members other than the organization owner
52
+
-- * the organization has 1 or more provisioner keys
53
53
54
54
IF (workspace_count + template_count + provisioner_keys_count) >0 THEN
55
55
RAISE EXCEPTION 'cannot delete organization: organization has % workspaces, % templates, and % provisioner keys that must be deleted first', workspace_count, template_count, provisioner_keys_count;
56
56
END IF;
57
57
58
-
IF (group_count) >1 THEN
58
+
IF (group_count) >1 THEN
59
59
RAISE EXCEPTION 'cannot delete organization: organization has % groups that must be deleted first', group_count -1;
60
60
END IF;
61
61
62
62
-- Allow 1 member to exist, because you cannot remove yourself. You can
63
63
-- remove everyone else. Ideally, we only omit the member that matches
64
64
-- the user_id of the caller, however in a trigger, the caller is unknown.
65
-
IF (member_count) >1 THEN
65
+
IF (member_count) >1 THEN
66
66
RAISE EXCEPTION 'cannot delete organization: organization has % members that must be deleted first', member_count -1;
0 commit comments