java-json-tools/json-patch

Revert an applied patch

Closed this issue · 1 comments

I'm like to unapply a patch to restore a document to its original form.

I am executing transformation steps and checkpointing at each stage, where I store a JsonPatch of the changes. Each checkpoint I also update the visible entity, so that the user sees progressive results (e.g. images while OCR'ing takes place). The checkpoint patches make it easy to debug when new steps are added.

If this workflow fails and has to restart, e.g. the server is restarted during a deployment, the job would be rescheduled. Ideally it would resume at the checkpoint, but sometimes it might be preferred to restart the workflow (e.g. bug in a prior step). But instead of keeping the original json, I'd like to rollback by reverting the checkpoints. Then I'd store the accumulated patch for an audit trail and purge the checkpoint history periodically.

This could all be done today by computing the reverse patch and storing it as well. It seems fairly natural that JsonPatch.unapply or JsonPatch.reverse() should be provided, though. I'd expect it to be easily computable for the patch to change the direction of each operation.

I may have been too optimistic. Some operations like replace are destructive and the original value isn't retained. I had only considered cases like add and remove which would be easy to reverse.