问题描述
我已经在线阅读了几篇文章以及关于为数据库驱动的应用程序创建审计跟踪的StackOverflow的几个答案。似乎最流行的解决方案是为有问题的表创建一个审计表,并使用触发器将审计记录插入审计表。
我可以看到如何对于一个数据包含在一个表中的简单实体,这将很有用。
包含子节点的聚合根如何?
示例:
订单是一个聚合根,包含许多订单行,每个订单行都有数据库中的自己的表。假设每个数据库中都有一个审核表,数据库中的原始表格更改时通过触发器接收更新:
tblOrders - > ;触发器 - > tblOrdersAudit
tblOrderLines - >触发器 - > tblOrderLinesAudit
现在,假设我们改变了一个订单,但是没有更改任何订单行。结果更新了tblOrders,并且触发器会将反映更改的新的审核记录插入到tblOrdersAudit中。但是,对tblOrderLine没有任何更改,因此在tblOrderLinesAudit中没有匹配的审核记录。
稍后我需要看到更早的状态订单,也许是回滚数据。我们如何配合审核记录?
如果您的回滚不会按表进行操作?
假定只有在t-1更新tblOrders之后,数据库才进行更改。在这种情况下
-
tblOrders
将被回滚到T- 1:来自审核的值将用于将tblOrders
返回到T-1中的位置。 -
tblOrdersLines
将被回滚到时间T-1:tblOrdersLineAudit
中没有条目,因此没有任何内容更新。
最后你的表格处于T-1的状态。 >
更多信息的链接很少 -
I have read several articles online as well as several answers on StackOverflow about creating an audit trail for a database driven application. It seems that the most popular solution is to create an audit table for the table in question and use triggers to insert an audit record into the audit table.
I can see how this would work well for simple entities whose data is contained in one table.
What about aggregate roots that contain children?
Example:
Order is an aggregate root containing many Order Lines, each with their own table in the database. Assume each also has an audit table in the database that receives updates via triggers when the original table is changed:
tblOrders --> Trigger --> tblOrdersAudit
tblOrderLines --> Trigger --> tblOrderLinesAudit
Now, suppose we change something about an Order, but make no changes to any of its Order Lines. tblOrders is updated as a result, and a trigger inserts a new audit record reflecting the changes to tblOrdersAudit. However, no changes have been made to tblOrderLines and as a result there is no matching audit record in tblOrderLinesAudit.
Some time later I need to see the an earlier state of the Order, perhaps to rollback the data. How do we match up the audit records?
In case of roll back wouldn't you be doing it per table basis? Assume only change ever made to the database was since time T-1 was updation of tblOrders. In this case
tblOrders
would be rolled back to time T-1: Values from audit will be used to bringtblOrders
back to how it was at T-1.tblOrdersLines
would be rolled back to time T-1: There is no entry intblOrdersLineAudit
and hence nothing will be updated.
At the end you have your tables are in the state they were at T-1.
Few links for more info -
- How to version control a record in a database
- Database history for client usage
- Using Triggers to Track Database Action History
这篇关于您如何为聚合根创建审计跟踪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!