您将如何在您喜欢的数据库范例中为您的模型实现版本控制系统?

发布于 2024-08-17 06:59:29 字数 770 浏览 11 评论 0原文

我发现模型的 RCS 是在数据持久性背景下需要解决的一个有趣的问题。它们是使用 django ORM 来实现此目的的几种解决方案 django-reversionAuditTrail 每个都提出了自己的方法来做到这一点。

这是我想要进行修订的模型(采用 django-model-like 格式):

class Page(Model):

    title = CharField()
    content = TextField()
    tags = ManyToMany(Tag)
    authors = ManyToMany(Author)
  • 每个修订版都应该用日期修订号、< strong>评论和进行修改的用户

您将如何在您喜欢的数据库(Mongo、neo4j、CouchDb、GAE 数据存储)中执行此操作?

请在每篇文章中仅发布一个 RCS 模型示例。

我并不是要求提供完整的代码(也许解释就足够了?),但足以了解如何在每种数据库类型中解决此问题。

I found out that RCS for models is an interesting problem to solve in the context of data persistence. They are several solution using the django ORM to achieve this django-reversion and AuditTrail each of which propose their own way to do it.

Here is the model (in django-model-like format) which I would like to have revisions :

class Page(Model):

    title = CharField()
    content = TextField()
    tags = ManyToMany(Tag)
    authors = ManyToMany(Author)
  • Each revision should be annotated with a date, a revision number, a comment and the user that did the modification.

How would you do it in you preferred db (Mongo, neo4j, CouchDb, GAE Datastore) ?

Please post only one example of RCS models per post.

I'm not asking for a complete code (maybe an explanation is enough?) but enough to see how this problem can be tackled in each db type.

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

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

发布评论

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

评论(2

失与倦" 2024-08-24 06:59:29

首先,如果您使用的是 CouchDB,请不要使用 _rev 字段。

为什么?压缩数据库时,旧版本会丢失。

Compaction重写数据库文件,
删除过时的文档修订
并删除了文档。

CouchDB wiki - 压缩页面

有几种可能的解决方案:

  1. 保留当前和旧版本在同一个数据库中。添加额外的修订字段以确定当前修订版本和旧修订版本之间的差异。
  2. 将旧版本存储在单独的数据库中。当新修订版添加到“当前”数据库时,可以删除旧修订版文档并将其插入到“修订版”数据库中。

哪一个最好?这取决于您的数据将如何被访问。如果您可以独立于当前修订版查询旧修订版,那么将文档存储在两个不同的数据库中将为您带来一些性能优势。

First of all, if you are using CouchDB, do not use the _rev field.

Why? Old revisions are lost when a database is compacted.

Compaction rewrites the database file,
removing outdated document revisions
and deleted documents.

CouchDB wiki - Compaction page

There are a couple possible solutions:

  1. Keep current and old revisions in the same database. Add an extra revision field to determine the difference between current and old revisions.
  2. Store old revisions in a separate database. When a new revision is added to the "current" database, the old revision document can be deleted and inserted into the "revisions" database.

Which one is best? It depends on how your data is going to be accessed. If you can query the old revisions independently from the current revisions, then storing the document in 2 different databases will give you some performance benefits.

岁月苍老的讽刺 2024-08-24 06:59:29

在 CouchDB 中,这相当简单。数据库中的每个项目都有一个 _id 和一个 _rev。因此您不需要单独的修订号。那时我可能会这样做。为每个项目分配一个系统版本号。该编号将是指向另一个数据库记录的链接,其中包含该修订的日期、注释和用户。

示例:

正在跟踪的项目:

{
     _id: "1231223klkj123",
     _rev: "4-1231223klkj123",
     systemRev: "192hjk8fhkj123",
     foo: "bar",
     fooarray: ["bar1", "bar2", bar3"]
}

然后创建一个单独的修订记录:

{
    _id: "192hjk8fhkj123",
    _rev: "2-192hjk8fhkj123",
    user: "John", 
    comment: "What I did yesterday",
    date: "1/1/2010",
    tags: ["C# edits", "bug fixes"]
}

对我来说,这看起来相当优雅......

In CouchDB this is rather straightforward. Every item in the DB has a _id and a _rev. So you don't need a separate revision number. I would probably do this then. Assign every item a systemrev number. This number would be a link to another DB record containing the date, comment and user for that revision.

Examples:

item being tracked:

{
     _id: "1231223klkj123",
     _rev: "4-1231223klkj123",
     systemRev: "192hjk8fhkj123",
     foo: "bar",
     fooarray: ["bar1", "bar2", bar3"]
}

And then create a separate revision record:

{
    _id: "192hjk8fhkj123",
    _rev: "2-192hjk8fhkj123",
    user: "John", 
    comment: "What I did yesterday",
    date: "1/1/2010",
    tags: ["C# edits", "bug fixes"]
}

To me it seems pretty elegant....

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