Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: John Schaeffer <[email protected]>
Signed-off-by: Bailin He <[email protected]>
  • Loading branch information
bailinhe and jnschaeffer committed May 6, 2024
1 parent b72d193 commit d85ed39
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
4 changes: 1 addition & 3 deletions internal/query/relations.go
Original file line number Diff line number Diff line change
Expand Up @@ -1252,9 +1252,7 @@ func (e *engine) rollbackUpdates(ctx context.Context, updates []*pb.Relationship
var op pb.RelationshipUpdate_Operation

switch u.Operation {
case pb.RelationshipUpdate_OPERATION_CREATE:
fallthrough
case pb.RelationshipUpdate_OPERATION_TOUCH:
case pb.RelationshipUpdate_OPERATION_CREATE, pb.RelationshipUpdate_OPERATION_TOUCH:
op = pb.RelationshipUpdate_OPERATION_DELETE
case pb.RelationshipUpdate_OPERATION_DELETE:
op = pb.RelationshipUpdate_OPERATION_TOUCH
Expand Down
42 changes: 21 additions & 21 deletions internal/query/roles_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,26 @@ func (e *engine) DeleteRoleV2(ctx context.Context, roleResource types.Resource)
ctx, span := e.tracer.Start(ctx, "engine.DeleteRoleV2")
defer span.End()

dbCtx, err := e.store.BeginContext(ctx)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())

return err
}

err = e.store.LockRoleForUpdate(dbCtx, roleResource.ID)
if err != nil {
sErr := fmt.Errorf("failed to lock role: %s: %w", roleResource.ID, err)

span.RecordError(sErr)
span.SetStatus(codes.Error, sErr.Error())

logRollbackErr(e.logger, e.store.RollbackContext(dbCtx))

return err
}

// find all the bindings for the role
findBindingsFilter := &pb.RelationshipFilter{
ResourceType: e.namespaced(e.rbac.RoleBindingResource.Name),
Expand All @@ -362,7 +382,7 @@ func (e *engine) DeleteRoleV2(ctx context.Context, roleResource types.Resource)
},
}

bindings, err := e.readRelationships(ctx, findBindingsFilter)
bindings, err := e.readRelationships(dbCtx, findBindingsFilter)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
Expand All @@ -379,26 +399,6 @@ func (e *engine) DeleteRoleV2(ctx context.Context, roleResource types.Resource)
return err
}

dbCtx, err := e.store.BeginContext(ctx)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())

return err
}

err = e.store.LockRoleForUpdate(dbCtx, roleResource.ID)
if err != nil {
sErr := fmt.Errorf("failed to lock role: %s: %w", roleResource.ID, err)

span.RecordError(sErr)
span.SetStatus(codes.Error, sErr.Error())

logRollbackErr(e.logger, e.store.RollbackContext(dbCtx))

return err
}

dbRole, err := e.store.GetRoleByID(dbCtx, roleResource.ID)
if err != nil {
span.RecordError(err)
Expand Down
2 changes: 1 addition & 1 deletion internal/storage/rolebinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (e *engine) ListResourceRoleBindings(ctx context.Context, resourceID gidx.P

rows, err := db.QueryContext(ctx, `
SELECT id, resource_id, created_by, updated_by, created_at, updated_at
FROM rolebindings WHERE resource_id = $1
FROM rolebindings WHERE resource_id = $1 ORDER BY created_at ASC
`, resourceID.String(),
)
if err != nil {
Expand Down

0 comments on commit d85ed39

Please sign in to comment.