Skip to content

Commit 10d1564

Browse files
committed
fix: drop index add members check to trigger
1 parent eb88091 commit 10d1564

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

coderd/database/dump.sql

Lines changed: 13 additions & 4 deletions
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.down.sql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
ALTER TABLE organizations DROP COLUMN deleted;
22

3-
DROP INDEX IF EXISTS idx_organization_name;
43
DROP INDEX IF EXISTS idx_organization_name_lower;
54

65
CREATE UNIQUE INDEX IF NOT EXISTS idx_organization_name ON organizations USING btree (name);

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ ALTER TABLE organizations ADD COLUMN deleted boolean DEFAULT FALSE NOT NULL;
33
DROP INDEX IF EXISTS idx_organization_name;
44
DROP INDEX IF EXISTS idx_organization_name_lower;
55

6-
CREATE UNIQUE INDEX IF NOT EXISTS idx_organization_name ON organizations USING btree (name)
7-
where deleted = false;
86
CREATE UNIQUE INDEX IF NOT EXISTS idx_organization_name_lower ON organizations USING btree (lower(name))
97
where deleted = false;
108

@@ -18,6 +16,7 @@ DECLARE
1816
workspace_count int;
1917
template_count int;
2018
group_count int;
19+
member_count int;
2120
BEGIN
2221
workspace_count := (
2322
SELECT count(*) as count FROM workspaces
@@ -39,19 +38,27 @@ BEGIN
3938
groups.organization_id = OLD.id
4039
);
4140

41+
member_count := (
42+
SELECT count(*) as count FROM organization_members
43+
WHERE
44+
organization_members.organization_id = OLD.id
45+
);
46+
4247
-- Fail the deletion if one of the following:
4348
-- * the organization has 1 or more workspaces
4449
-- * 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+
4653
IF (workspace_count + template_count) > 0 THEN
4754
RAISE EXCEPTION 'cannot delete organization: organization has % workspaces and % templates that must be deleted first', workspace_count, template_count;
4855
END IF;
4956

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;
5259
END IF;
5360

54-
RETURN OLD;
61+
RETURN NEW;
5562
END;
5663
$$ LANGUAGE plpgsql;
5764

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy