Is there a design pattern for merging duplicate database records?

后端 未结 1 1379
死守一世寂寞
死守一世寂寞 2021-01-02 13:24

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\".

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-02 13:39

    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_FAVORITE and MOVIE, I'd add a new table USER_FAVORITE_MOVIE_MERGE_COMMAND with columns:

    • id
    • date
    • user_id
    • old_favorite_movie_title
    • new_favorite_movie_title

    So your nightly cleanup script (or whatever) runs over the USER_FAVORITE table looking for non-standard movie titles. Each time it finds one, it corrects it and records the pertinent facts in the USER_FAVORITE_MOVIE_MERGE_COMMAND table.

    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 new with old.

    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?

    0 讨论(0)
提交回复
热议问题