Skip to content

Commit 54db7ab

Browse files
committed
chore: update trigger function
1 parent 5949406 commit 54db7ab

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

coderd/database/dump.sql

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ $$
1717
DECLARE
1818
workspace_count int;
1919
template_count int;
20+
group_count int;
2021
BEGIN
2122
workspace_count := (
2223
SELECT count(*) as count FROM workspaces
@@ -32,14 +33,23 @@ BEGIN
3233
AND templates.deleted = false
3334
);
3435

36+
group_count := (
37+
SELECT count(*) as count FROM groups
38+
WHERE
39+
groups.organization_id = OLD.id
40+
);
41+
3542
-- Fail the deletion if one of the following:
3643
-- * the organization has 1 or more workspaces
3744
-- * the organization has 1 or more templates
45+
-- * the organization has 1 or more groups
3846
IF (workspace_count + template_count) > 0 THEN
3947
RAISE EXCEPTION 'cannot delete organization: organization has % workspaces and % templates that must be deleted first', workspace_count, template_count;
4048
END IF;
4149

42-
-- add more cases to fail a delete
50+
IF (group_count) > 1 THEN
51+
RAISE EXCEPTION 'cannot delete organization: organization has % groups that must be deleted first', group_count;
52+
END IF;
4353

4454
RETURN OLD;
4555
END;

0 commit comments

Comments
 (0)