Skip to content

Commit

Permalink
fix: batch identity created event
Browse files Browse the repository at this point in the history
  • Loading branch information
hperl committed Sep 18, 2024
1 parent d72f456 commit f1c381c
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions persistence/sql/identity/persister_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ func (p *IdentityPersister) CreateIdentities(ctx context.Context, identities ...
}
}

if err := p.Transaction(ctx, func(ctx context.Context, tx *pop.Connection) error {
return p.Transaction(ctx, func(ctx context.Context, tx *pop.Connection) error {
conn := &batch.TracerConnection{
Tracer: p.r.Tracer(ctx),
Connection: tx,
Expand Down Expand Up @@ -645,32 +645,37 @@ func (p *IdentityPersister) CreateIdentities(ctx context.Context, identities ...
if len(failedIdentityIDs) > 0 {
partialErr := &identity.CreateIdentitiesError{}
failedIDs := make([]uuid.UUID, 0, len(failedIdentityIDs))
succeededIDs := make([]uuid.UUID, 0, len(identities))

for _, ident := range identities {
if _, ok := failedIdentityIDs[ident.ID]; ok {
partialErr.AddFailedIdentity(ident, sqlcon.ErrUniqueViolation)
failedIDs = append(failedIDs, ident.ID)
} else {
succeededIDs = append(succeededIDs, ident.ID)
}
}
// Manually roll back by deleting the identities that were inserted before the
// error occurred.
if err := p.DeleteIdentities(ctx, failedIDs); err != nil {
return sqlcon.HandleError(err)
}
// Wrap the partial error with the first error that occurred, so that the caller
// can continue to handle the error either as a partial error or a full error.

// Partial success: report succeeded identities as created.
for _, identID := range succeededIDs {
span.AddEvent(events.NewIdentityCreated(ctx, identID))
}

return partialErr
} else {
// No failures: report all identities as created.
for _, ident := range identities {
span.AddEvent(events.NewIdentityCreated(ctx, ident.ID))
}
}

return nil
}); err != nil {
return err
}

for _, ident := range identities {
span.AddEvent(events.NewIdentityCreated(ctx, ident.ID))
}

return nil
})
}

func (p *IdentityPersister) HydrateIdentityAssociations(ctx context.Context, i *identity.Identity, expand identity.Expandables) (err error) {
Expand Down

0 comments on commit f1c381c

Please sign in to comment.