For example, let’s say I had a social networking site for movie fans. Some people list “Rocky” as their favorite movie, others list “Rocky 1”, other still “Rocky I”. The obvious thing is to merge the three together and update the associated tables. However, for every obvious solution there’s a design pattern that’s 1) more complicated and 2) has some extra benefits. Is there a design pattern for merging duplicate database records? Specifically, something that provides auditability or reversibility?
Share
As soon as you as you say “reversibility” I think Command Pattern.
The typical example is to support Undo style behaviour but I think this would be a good fit for auditability as well – especially as the individual “steps” (for want of a better word) are so small and easily represented (e.g.
{Merged "Rocky I" -> "Rocky" }).How would I get the Command pattern to actually work for your scenario?
Well, keeping this very much in the RDBMS arena rather than OO modelling, assuming you’ve already got tables
USER_FAVORITEandMOVIE, I’d add a new tableUSER_FAVORITE_MOVIE_MERGE_COMMANDwith columns:iddateuser_idold_favorite_movie_titlenew_favorite_movie_titleSo your nightly cleanup script (or whatever) runs over the
USER_FAVORITEtable looking for non-standard movie titles. Each time it finds one, it corrects it and records the pertinent facts in theUSER_FAVORITE_MOVIE_MERGE_COMMANDtable.Your audit trail is right there, and if you ever need to reverse the cleanup job, “play back” the rows in reverse chronological order, replacing
newwithold.Notice how you’ve got both reversibility and auditability both in the temporal sense (e.g. last night’s batch run went weird at 2.12am, let’s roll back all the work done after that) and in the per-user sense.
Is this the sort of thing you’re after?