I have some entity classes in my application. I want to save all changes to this entities and some happened events in a history table. Problem is that entity classes are different, build with different primitives and different relation between them.
For example in a shopping application there can be: User, Item, Transaction, Watch. I want to be able to show some activity log to user like this:
- 16:00 You've bought "Lord of the Rings" in a transaction 2468,
- 15:30 You've added "Hobbit" to your watchlist,
- 15:00 Watched item's "Silmarillion" has dropped price from 15 to 12,50 euro,
- 14:30 You've changed your name from "Tomasz" to "Tom".
There are many things involved in this log:
- new Transaction entity 2468 with Item "LotR",
- new Watch entity linked with Item "Hobbit" and my account,
- updated Item price (but both old and new prices are stored),
- updated User name (both old and new stored).
My main question is how to keep track of these data?
- Should I have as many tables as many entity tables to keep their changes? UserVersions, ItemVersions etc?
- Should I query all Version tables, join and sort results to generate this log?
- Or maybe there should be one table Versions with columns "Entity", "OldValue", "NewValue" - what about constraints and foreign keys? Isn't it too dirty solution?
- Is there design pattern for this?
- Finally if you know - how does Facebook does this? :)