Add Optimistic locking that allows handling of concurrent update to entities. This will ensure that an update only occurs if the data has not changed since it was last read.
Controller Example:
// UpdateCommentType updates a comment type.
func (r *CommentTypeOps) UpdateCommentType(ctx context.Context, commentType ent.CommentType) (*ent.CommentType, error) {
// Retrieve the current state of the comment type.
current, err := r.client.CommentType.Get(ctx, commentType.ID)
if err != nil {
wrappedErr := eris.Wrap(err, "failed to retrieve comment type")
r.logger.WithField("error", wrappedErr).Error("failed to retrieve comment type")
return nil, wrappedErr
}
// Check if the version matches.
if current.Version != commentType.Version {
return nil, tools.NewValidationError("Comment type has been updated by another user. Please refresh and try again",
"syncError",
"name")
}
// Start building the update operation
updateOp := r.client.CommentType.UpdateOneID(commentType.ID).
SetStatus(commentType.Status).
SetDescription(commentType.Description).
SetName(commentType.Name).
SetSeverity(commentType.Severity).
SetVersion(commentType.Version + 1) // Increment the version
// Execute the update operation
updatedCommentType, err := updateOp.Save(ctx)
if err != nil {
return nil, err
}
return updatedCommentType, nil
}
Add Optimistic locking that allows handling of concurrent update to entities. This will ensure that an update only occurs if the data has not changed since it was last read.
Controller Example:
Entity Example:
Note: We can probably add the version to the
BaseMixin
so that all entities will have this.