开发者

How can you simulate a conflict in CouchDB without using replication?

开发者 https://www.devze.com 2023-04-09 04:22 出处:网络
I\'d like to write a unit test for my app t开发者_C百科hat simulates a conflict during replication. Is there a way to simulate a conflict using only a single CouchDB database and server?I assume you w

I'd like to write a unit test for my app t开发者_C百科hat simulates a conflict during replication. Is there a way to simulate a conflict using only a single CouchDB database and server?


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}'


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消