Skip to content

Commit d8aa52c

Browse files
committed
fix: allow same custom role name for different orgs
The existing idx_custom_roles_name_lower index prevents that
1 parent e4a06f8 commit d8aa52c

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

coderd/database/check_constraint.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dump.sql

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- Restore the original unique constraint (name only, no organization_id).
2+
DROP INDEX IF EXISTS idx_custom_roles_name_lower_organization_id;
3+
4+
ALTER TABLE custom_roles DROP CONSTRAINT IF EXISTS organization_id_not_zero;
5+
6+
CREATE UNIQUE INDEX idx_custom_roles_name_lower ON custom_roles USING btree (LOWER(name));
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-- Fix the unique index in `custom_roles` to allow the same role name
2+
-- in different organizations. The original index only covered name,
3+
-- but names don't have to be unique across different organizations.
4+
--
5+
-- Note: after fixing it, we end up with an almost-replica of the
6+
-- existing `custom_roles_unique_key` constraint. That's unfortunate,
7+
-- but since we can't define a constraint on an expression (e.g. lower()),
8+
-- we'll have to keep both of them.
9+
DROP INDEX IF EXISTS idx_custom_roles_name_lower;
10+
11+
-- Use `COALESCE` to handle `NULL` organization_id. Site-wide custom
12+
-- roles are currently not used, but that can change in the future and
13+
-- this will become necessary. And there are no performance implications.
14+
--
15+
-- Note: Using `NULLS NOT DISTINCT` instead of `COALESCE` here would
16+
-- limit us to PG15+.
17+
18+
-- Paranoia check.
19+
UPDATE custom_roles SET organization_id = NULL WHERE organization_id = '00000000-0000-0000-0000-000000000000';
20+
21+
ALTER TABLE custom_roles
22+
ADD CONSTRAINT organization_id_not_zero
23+
CHECK (organization_id <> '00000000-0000-0000-0000-000000000000'::uuid);
24+
25+
CREATE UNIQUE INDEX idx_custom_roles_name_lower_organization_id ON custom_roles USING btree (
26+
LOWER(name),
27+
COALESCE(organization_id, '00000000-0000-0000-0000-000000000000'::uuid)
28+
);

coderd/database/unique_constraint.go

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

0 commit comments

Comments
 (0)