-
Notifications
You must be signed in to change notification settings - Fork 943
feat: enable soft delete for organizations #16584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
606a5fa
609890f
31e5eb2
608661b
54039cf
9516695
9a26df6
f1f25e1
b202254
1bf3742
41e943b
9fe8da1
6929988
eb88091
10d1564
56260b0
5968da1
70bf5e3
50074d1
eed03fe
891cbae
22d27f8
dc5fc46
9466ff7
ae2bf08
041630c
607311f
7291c02
b9b7a14
9af7cb5
aee6834
74ef3de
4c70aaa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,44 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_organization_name_lower ON organizations U | |
|
||
ALTER TABLE ONLY organizations | ||
DROP CONSTRAINT IF EXISTS organizations_name; | ||
|
||
CREATE FUNCTION protect_provisioned_organizations() | ||
RETURNS TRIGGER AS | ||
$$ | ||
DECLARE | ||
workspace_count int; | ||
template_count int; | ||
BEGIN | ||
workspace_count := ( | ||
SELECT count(*) as count FROM workspaces | ||
WHERE | ||
workspaces.organization_id = OLD.id | ||
AND workspaces.deleted = false | ||
); | ||
|
||
template_count := ( | ||
SELECT count(*) as count FROM templates | ||
WHERE | ||
templates.organization_id = OLD.id | ||
AND templates.deleted = false | ||
); | ||
|
||
-- Fail the deletion if one of the following: | ||
-- * the organization has 1 or more workspaces | ||
-- * the organization has 1 or more templates | ||
IF (workspace_count + template_count) > 0 THEN | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it feels like such a long error message if this is combined into one exception. In the future the FE should let the user know what resources exist before deleting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can improve the error handling in golang up front in the future and improve the UX on failures. |
||
RAISE EXCEPTION 'cannot delete organization: organization has % workspaces and % templates that must be deleted first', workspace_count, template_count; | ||
END IF; | ||
|
||
-- add more cases to fail a delete | ||
|
||
RETURN OLD; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
-- Trigger to protect organizations from being soft deleted with existing resources | ||
CREATE TRIGGER protect_provisioned_organizations | ||
BEFORE UPDATE ON organizations | ||
FOR EACH ROW | ||
WHEN (NEW.deleted = true AND OLD.deleted = false) | ||
EXECUTE FUNCTION protect_provisioned_organizations(); |
Uh oh!
There was an error while loading. Please reload this page.