如何为聚合根创建审计跟踪?

发布于 2024-11-07 00:41:49 字数 586 浏览 0 评论 0原文

我在线阅读了几篇文章以及 StackOverflow 上的几个关于为数据库驱动的应用程序创建审计跟踪的答案。似乎最流行的解决方案是为有问题的表创建一个审计表,并使用触发器将审计记录插入到审计表中。

我可以看到这对于数据包含在一个表中的简单实体如何有效。

包含子项的聚合根又如何呢?

示例:

订单是包含许多订单行的聚合根,每个订单行在数据库中都有自己的表。假设每个数据库中都有一个审计表,当原始表发生更改时,该表通过触发器接收更新:

tblOrders --> Trigger --> tblOrdersAudit
tblOrderLines --> Trigger --> tblOrderLinesAudit

现在,假设我们更改了某个订单的某些内容,但不对其任何订单行进行任何更改。结果 tblOrders 被更新,并且触发器插入反映 tblOrdersAudit 更改的新审计记录。然而,tblOrderLines 没有发生任何更改,因此 tblOrderLinesAudit 中没有匹配的审计记录。

一段时间后,我需要查看订单的早期状态,也许是为了回滚数据。我们如何匹配审计记录?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄人书 2024-11-14 00:41:49

如果回滚,您不会按表进行操作吗?
假设自 T-1 更新 tblOrders 以来对数据库进行的唯一更改。在这种情况下,

  1. tblOrders 将回滚到时间 T-1:审计中的值将用于将 tblOrders 恢复到 T-1 时的状态。

  2. tblOrdersLines 将回滚到时间 T-1:tblOrdersLineAudit 中没有任何条目,因此不会更新任何内容。

最后,您的表将处于 T-1 时的状态。

有关更多信息的链接 -

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

  1. tblOrders would be rolled back to time T-1: Values from audit will be used to bring tblOrders back to how it was at T-1.

  2. tblOrdersLines would be rolled back to time T-1: There is no entry in tblOrdersLineAudit 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 -

怼怹恏 2024-11-14 00:41:49

一段时间后,我需要查看订单的早期状态,也许是为了回滚数据。我们如何匹配审核记录?

并不容易,正如您正确识别的那样。

就个人而言,当我需要重新访问快照时,我会存储整个聚合的副本。换句话说,在插入/更新/删除订单或订单行或任何其他关联表时,我会记录该订单+每个订单行+其他关联表中的每个相关行。

从存储的角度来看,它效率不高(尽管我倾向于存储每个事务的最终快照,而不是每次更改),从性能的角度来看,它也不是理想的,但它完成了工作......

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?

Not easy, as you correctly identified.

Personally, I store a copy of the whole aggregate when I need to revisit snapshots. Put another way, on insert/update/delete to orders or order lines or any other associated tables, I log that order + each order line + each related line in other associated tables.

It's not efficient from a storage standpoint (even though I tend to store the final snapshot per transaction, rather than each change), nor is it ideal from a performance standpoint, but it gets the job done...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文