CouchDB Merging Revisions (conflict)

345 views Asked by At

So I'm causing a conflict on purpose with the example of this site: http://guide.couchdb.org/draft/conflicts.html (Working with Conflicts).

Now there are two revisions and CouchDB decides with its own algorithm which revision to use but i would like to keep both revisions and merge them.

As an example: i got Phonenumber: 111 and Name: Jules on database A and Phonenumber: 222 and Name: Jules on database B. Is it possible to create a new document which keeps all information from the old and the new revision? Or a new field like "NewPhonenumber: 222" and the other fields Phonenumber: 111 and Name: Jules ?

I just want to keep both revisions no matter how.

I tried to write a View function but i just don't know how to grab the data from the conflicting database.

function(doc) { 
if(doc._conflicts)
emit(doc._id, doc);
emit(doc._id, {oldNumber: doc.phonenumber, newNumber: doc.phonenumber, name: doc.name});
}

How can I replace "oldNumber: doc.phonenumber" with the number from the old revision?

Thanks!

1

There are 1 answers

0
Ingo Radatz On

The property doc._conflicts contains one or more conflicted revisions of the doc - you have to iterate over the list and grab the value you want.

If you do this in the view your conflict resolving will never be stored. You have to send the resolved version of the doc as new revision to CouchDB.

You can request the doc with its conflicts by using the query param ?conflicts=true (more in the CouchDB documentation) and store your decision as new revision.