An example would set the context right, the example below captures the various states of the entity, which needs to be reverted(rolled back) .
State 1 – Recorded on 01-Mar-2010
Column1 Column2
Data1 0.56
State 2 – Recorded on 02-Mar-2010
Column1 Column2
Data1 0.57
State 3 – Recorded on 03-Mar-2010
Column1 Column2
Data1 0.58
User notices that state3 is not what he intended to be in, decides to revert back to state2.
One approach that I can think of, without modifying the entity is via “auditing” all the inserts/updates, as below, the rollback information captures the data just before the updates/modifications on the entity, so that it can be applied in an order when you need to revert.Please note that changing the entity’s schema, is not an option.
Rollback – R1 recorded on 01-Mar-2010
Column1 Column2
Data1 0.56
Rollback – R2 Recorded on 02-Mar-2010
Column1 Column2
Data1 0.56
Rollback – R3 Recorded on 03-Mar-2010
Column1 Column2
Data1 0.57
So, to get to state2 , we would start with rollback information R1,apply R2 onto it.
Is there a better approach to achieve this ?
Thanks for your time.
For each table in your schema, create a new auditing table in a different schema with two additional columns:
validFromandvalidTo.When you insert/update a row, you need to make two changes in the audit table:
(no insert if you delete a row in the original table).
If you need to go back to a certain state, you can select the row in the audit table with the same primary key (PK) and which lies in the time range
[ validFrom, validTo )or wherevalidTo is noneand simply copy the row into the original table.Next, you must delete all rows in the original table which don’t exist at that time in the audit table.