允许用户使用 SQLAlchemy 从数据库审计跟踪回滚

发布于 2024-08-06 04:43:23 字数 1316 浏览 2 评论 0原文

我开始将 SQLAlchemy 用于一个新项目,我计划在该项目中实施类似于针对以下问题提出的审计跟踪:

由于我将拥有“有趣”对象的完整历史记录,因此我正在考虑允许用户回滚到给定版本,从而使他们能够无限制地撤消。

使用 SQLAlchemy 可以以干净的方式完成此操作吗?

在内部 API(业务逻辑和 ORM)中公开此功能的正确方法是什么?

我是在 user.rollback(ver=42) 的道路上走来的。

I'm starting to use SQLAlchemy for a new project where I was planning to implement an audit trail similar to the one proposed on these questions:

As I will have the full history of the "interesting" objects, I was thinking in allowing users to rollback to a given version, giving them the possibility to have unlimited undo.

Would this be possible to be done in a clean way with SQLAlchemy?

What would be the correct way to expose this feature in the internal API (business logic and ORM)?

I was something along the ways of user.rollback(ver=42).

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

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

发布评论

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

评论(1

夢归不見 2024-08-13 04:43:23

虽然我没有专门使用过 SQLAlchemy,但我可以为您提供一些可以在任何 ORM 中轻松实现的一般提示:

  • 将版本化项目分成两个表,例如 DocumentDocumentVersion. Document 存储在版本之间永远不会更改的信息,而 DocumentVersion 存储会更改的信息。
  • 为每个 DocumentVersion 提供一个“父”引用。为同一个表创建一个外键,指向文档的先前版本。
  • 通过将文档中的引用更新为“当前”版本来回滚到以前的版本。不要从链的底部删除版本。
  • 当他们回滚后创建新版本时,它将创建另一个版本分支。

例如,创建A、B、C,回滚到B,创建D、E:

(A)
 |
(B)
 | \
(C) (D)
     |
    (E)

Although I haven't used SQLAlchemy specifically, I can give you some general tips that can be easily implemented in any ORM:

  • Separate out the versioned item into two tables, say Document and DocumentVersion. Document stores information that will never change between versions, and DocumentVersion stores information that does change.
  • Give each DocumentVersion a "parent" reference. Make a foreign key to the same table, pointing to the previous version of the document.
  • Roll back to previous versions by updating a reference from Document to the "current" version. Don't delete versions from the bottom of the chain.
  • When they make newer versions after rolling back, it will create another branch of versions.

Example, create A, B, C, rollback to B, create D, E:

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