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
+13-6Lines changed: 13 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -3,8 +3,6 @@ ALTER TABLE organizations ADD COLUMN deleted boolean DEFAULT FALSE NOT NULL;
3
3
DROPINDEX IF EXISTS idx_organization_name;
4
4
DROPINDEX IF EXISTS idx_organization_name_lower;
5
5
6
-
CREATEUNIQUE INDEXIF NOT EXISTS idx_organization_name ON organizations USING btree (name)
7
-
where deleted = false;
8
6
CREATEUNIQUE INDEXIF NOT EXISTS idx_organization_name_lower ON organizations USING btree (lower(name))
9
7
where deleted = false;
10
8
@@ -18,6 +16,7 @@ DECLARE
18
16
workspace_count int;
19
17
template_count int;
20
18
group_count int;
19
+
member_count int;
21
20
BEGIN
22
21
workspace_count := (
23
22
SELECTcount(*) as count FROM workspaces
@@ -39,19 +38,27 @@ BEGIN
39
38
groups.organization_id=OLD.id
40
39
);
41
40
41
+
member_count := (
42
+
SELECTcount(*) as count FROM organization_members
43
+
WHERE
44
+
organization_members.organization_id=OLD.id
45
+
);
46
+
42
47
-- Fail the deletion if one of the following:
43
48
-- * the organization has 1 or more workspaces
44
49
-- * the organization has 1 or more templates
45
-
-- * the organization has 1 or more groups
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
+
46
53
IF (workspace_count + template_count) >0 THEN
47
54
RAISE EXCEPTION 'cannot delete organization: organization has % workspaces and % templates that must be deleted first', workspace_count, template_count;
48
55
END IF;
49
56
50
-
IF (group_count) >1 THEN
51
-
RAISE EXCEPTION 'cannot delete organization: organization has % groups that must be deleted first', group_count;
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;
0 commit comments