如何在不使用复制的情况下模拟 CouchDB 中的冲突?

发布于 2024-12-07 16:44:17 字数 67 浏览 0 评论 0原文

我想为我的应用程序编写一个单元测试来模拟复制期间的冲突。有没有办法仅使用单个 CouchDB 数据库和服务器来模拟冲突?

I'd like to write a unit test for my app that simulates a conflict during replication. Is there a way to simulate a conflict using only a single CouchDB database and server?

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

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

发布评论

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

评论(3

临走之时 2024-12-14 16:44:17

我假设您想要获取数据库中包含冲突的文档,而不是 409 冲突响应?

因此,在数据库中创建一个具有已知 _id 的文档:

$ curl http://localhost:5984/scratch/foo -X PUT -H "Content-Type: application/json" -d '{}'
{"ok":true,"id":"foo","rev":"1-967a00dff5e02add41819138abb3284d"}

然后使用带有 all_or_nothing: true 选项的批量文档 API 来更新带有故意错误或无 _rev 的同一文档,添加一些不同的文档属性以实现良好的测量:

$ curl http://localhost:5984/scratch/_bulk_docs -X POST -H "Content-Type: application/json" -d '{"all_or_nothing": true, "docs": [{"_id": "foo", "abc": 123}]}'
[{"id":"foo","rev":"1-15c813a2b4b312c6915821b01a1986c5"}]

然后您应该文档中存在冲突:

$ curl http://localhost:5984/scratch/foo?conflicts=true
{"_id":"foo","_rev":"1-967a00dff5e02add41819138abb3284d","_conflicts":["1-15c813a2b4b312c6915821b01a1986c5"]}

您还可以使用 ?new_edits=false 作为 描述CouchDB 提交者 Randall Leeds

$ curl http://localhost:5984/scratch?new_edits=false -X POST -H "Content-Type: application/json" -d '{"_id": "foo", "abc": 123}'

I assume you want to get a document containing a conflict in your database, rather than a 409 Conflict response?

So, create a document in the database with a known _id:

$ curl http://localhost:5984/scratch/foo -X PUT -H "Content-Type: application/json" -d '{}'
{"ok":true,"id":"foo","rev":"1-967a00dff5e02add41819138abb3284d"}

Then use the bulk docs API with the all_or_nothing: true option to update the same document with a deliberately bad or no _rev, adding some different document attributes for good measure:

$ curl http://localhost:5984/scratch/_bulk_docs -X POST -H "Content-Type: application/json" -d '{"all_or_nothing": true, "docs": [{"_id": "foo", "abc": 123}]}'
[{"id":"foo","rev":"1-15c813a2b4b312c6915821b01a1986c5"}]

You should then have a conflict in the document:

$ curl http://localhost:5984/scratch/foo?conflicts=true
{"_id":"foo","_rev":"1-967a00dff5e02add41819138abb3284d","_conflicts":["1-15c813a2b4b312c6915821b01a1986c5"]}

You can also perform a normal query with ?new_edits=false as described by CouchDB committer Randall Leeds.

$ curl http://localhost:5984/scratch?new_edits=false -X POST -H "Content-Type: application/json" -d '{"_id": "foo", "abc": 123}'
风月客 2024-12-14 16:44:17

提出问题后进一步 Google 了一下,答案似乎是使用批量文档 API 的全有或全无模式。

http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API

查看页面末尾附近。

Googled further after asking the question, and it looks like the answer is to use the all-or-nothing mode of the bulk document API.

http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API

Look near the end of the page.

﹂绝世的画 2024-12-14 16:44:17

只需发布两个具有相同 _id 属性的文档即可。这会产生冲突,因为第二个文档将不包含正确的 _rev 属性。请记住,您需要在每个后续帖子中包含最新的 _rev 属性,以便 CouchDB 知道您是最新的。

此外,您可以在同一服务器上创建两个数据库并在它们之间进行复制。

Just post two documents with the same _id attribute. This creates a conflict since the 2nd doc will not contain the proper _rev attribute. Remember, you need to include the latest _rev attribute in each subsequent post so that CouchDB knows you are up to date.

Also, you can create two databases on the same server and replicate between those.

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