Skip to content

Commit 8e305d5

Browse files
authored
feat(store): role names are unique over live rows only (#1961)
1 parent f09054e commit 8e305d5

5 files changed

Lines changed: 71 additions & 6 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE roles ADD CONSTRAINT roles_org_id_name_key UNIQUE (org_id, name);
2+
DROP INDEX IF EXISTS uq_roles_org_id_name_live;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CREATE UNIQUE INDEX IF NOT EXISTS uq_roles_org_id_name_live ON roles (org_id, name) WHERE deleted_at IS NULL;
2+
ALTER TABLE roles DROP CONSTRAINT IF EXISTS roles_org_id_name_key;

‎internal/store/postgres/postgres.go‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,9 @@ func fromLive(table string) *goqu.SelectDataset {
4040
return dialect.From(table).Where(live(table))
4141
}
4242

43-
// liveConflictTarget builds the ON CONFLICT target for a unique index that
44-
// covers only live rows. For example "urn" renders as
45-
// ON CONFLICT (urn) WHERE (deleted_at IS NULL). Postgres uses such an index only
46-
// when the clause names its condition. goqu wraps the target in parentheses as
47-
// is, which is why the string ends open.
43+
// liveConflictTarget is the ON CONFLICT target for a unique index over live
44+
// rows. Postgres matches such an index only when the target repeats its WHERE
45+
// clause. goqu wraps the target in parentheses as is, so the string ends open.
4846
func liveConflictTarget(columns string) string {
4947
return columns + ") WHERE (deleted_at IS NULL"
5048
}

‎internal/store/postgres/role_repository.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,13 @@ func (r RoleRepository) Upsert(ctx context.Context, rl role.Role) (role.Role, er
132132
"state": rl.State,
133133
"metadata": marshaledMetadata,
134134
"scopes": pq.Array(rl.Scopes),
135-
}).OnConflict(goqu.DoUpdate("org_id, name", goqu.Record{
135+
}).OnConflict(goqu.DoUpdate(liveConflictTarget("org_id, name"), goqu.Record{
136136
"title": rl.Title,
137137
"permissions": marshaledPermissions,
138138
"state": rl.State,
139139
"metadata": marshaledMetadata,
140140
"scopes": pq.Array(rl.Scopes),
141+
"updated_at": goqu.L("now()"),
141142
})).Returning(&Role{}).ToSQL()
142143
if err != nil {
143144
return role.Role{}, fmt.Errorf("%w: %s", errQuery, err)

‎internal/store/postgres/role_repository_test.go‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,68 @@ func (s *RoleRepositoryTestSuite) TestCreate() {
190190
}
191191
})
192192
}
193+
194+
s.Run("should create a new row when the name belongs to a soft-deleted role", func() {
195+
deleted := s.roles[3]
196+
if _, err := s.client.ExecContext(s.ctx, "UPDATE roles SET deleted_at = now() WHERE id = $1", deleted.ID); err != nil {
197+
s.T().Fatal(err)
198+
}
199+
200+
got, err := s.repository.Upsert(s.ctx, role.Role{
201+
Name: deleted.Name,
202+
Title: "Test Title",
203+
Permissions: []string{"user"},
204+
OrgID: s.orgID,
205+
Metadata: metadata.Metadata{},
206+
})
207+
s.Assert().NoError(err)
208+
if got.ID == deleted.ID {
209+
s.T().Fatalf("got the deleted row %s back, expected a new row", deleted.ID)
210+
}
211+
212+
var rows int
213+
if err := s.client.QueryRowxContext(s.ctx, "SELECT count(*) FROM roles WHERE org_id = $1 AND name = $2", s.orgID, deleted.Name).Scan(&rows); err != nil {
214+
s.T().Fatal(err)
215+
}
216+
if rows != 2 {
217+
s.T().Fatalf("got %d rows named %s, expected the deleted row and the new one", rows, deleted.Name)
218+
}
219+
})
220+
221+
s.Run("should return conflict when the id belongs to a soft-deleted role", func() {
222+
deleted := s.roles[4]
223+
if _, err := s.client.ExecContext(s.ctx, "UPDATE roles SET deleted_at = now() WHERE id = $1", deleted.ID); err != nil {
224+
s.T().Fatal(err)
225+
}
226+
227+
_, err := s.repository.Upsert(s.ctx, role.Role{
228+
ID: deleted.ID,
229+
Name: "role with a reused id",
230+
OrgID: s.orgID,
231+
Metadata: metadata.Metadata{},
232+
})
233+
s.Assert().ErrorIs(err, role.ErrConflict)
234+
})
235+
236+
s.Run("should update the live role and move updated_at when the name is taken", func() {
237+
before, err := s.repository.Get(s.ctx, s.roles[2].ID)
238+
if err != nil {
239+
s.T().Fatal(err)
240+
}
241+
242+
got, err := s.repository.Upsert(s.ctx, role.Role{
243+
Name: before.Name,
244+
Title: "changed",
245+
Permissions: before.Permissions,
246+
Scopes: before.Scopes,
247+
OrgID: s.orgID,
248+
Metadata: metadata.Metadata{},
249+
})
250+
s.Assert().NoError(err)
251+
s.Assert().Equal(before.ID, got.ID)
252+
s.Assert().Equal("changed", got.Title)
253+
s.Assert().True(got.UpdatedAt.After(before.UpdatedAt))
254+
})
193255
}
194256

195257
func (s *RoleRepositoryTestSuite) TestList() {

0 commit comments

Comments
 (0)