conflicting documents on sync

245 views
Skip to first unread message

Dominique Legault

unread,
Oct 28, 2014, 4:56:30 PM10/28/14
to mobile-c...@googlegroups.com
I'm using phonegap with Android and I'm having an issue where I insert a document in my local database and the document already exists in the sync gateway by another user. The sync gateway doesn't pull the document because the user is not the owner of the document. The changes feed on the client that created the conflicting document doesn't get reported of the conflict or deletion, even if I do a get on the local document in the changes feed everything looks normal. If I log out (local db gets deleted) and log back in (sync with gateway) the document isn't there because the sync gateway ignored the revision.
How can I notify the user of the conflicting document, and resolve it through the client ?

This is what I use to check for a conflict on the document.
                    //get the document to see if there was a conflict.
                    if( typeof change.doc._rev != 'undefined' )
                    config.db.get([change.doc._id, { "rev":change.doc._rev, "conflicts": true, "deleted_conflicts" : true } ], function(error, doc) {
                        if(error) { return log ("Error getting revision:" + JSON.stringify( error ) ) }
                        log( "Changed Document" + JSON.stringify( doc ) )
                    } )

is there a way to detect when the replication has finished doing the _bulk_docs post for changes in the local db ?

Jens Alfke

unread,
Oct 30, 2014, 2:43:36 PM10/30/14
to mobile-c...@googlegroups.com

On Oct 28, 2014, at 1:56 PM, Dominique Legault <deefac...@gmail.com> wrote:

I'm using phonegap with Android and I'm having an issue where I insert a document in my local database and the document already exists in the sync gateway by another user. The sync gateway doesn't pull the document because the user is not the owner of the document.

Could you describe this in more detail, maybe with an example? I'm not sure exactly what situation you're describing. Thanks.

—Jens

Dominique Legault

unread,
Oct 30, 2014, 7:29:41 PM10/30/14
to mobile-c...@googlegroups.com
I think I've got a little more details into the issue.

The problem I'm facing is when I insert a document that has a conflict with another users document. because the user that inserted the conflicting document doesn't have access to the document they can't resolve the conflict with the document.

I'm not getting an error returned either. the couchbase lite db on the android just keeps the conflicting document and nothing is returned that I can parse to indicate an error to the user.

I've recently changed the sync-gateway json to give access to deleted documents, but I don't think it would resolve this particular issue I'm having.

here is an example.

User A, inserts a document
{ "_id" : "mydocument", "key": "value", "type": "mytype", "username":"usera"}

document is synced to sync gateway.

User B, inserts document with same id
{ "_id" : "mydocument", "key": "value", "type": "mytype", "username":"userb"}


the document inserts in to the local db, but when it tries to sync to the sync-gateway it rejects it because there is already a document with the same id.

I don't see anything show up in the User B changes feed.
if I render the view of the User B document it still exists until I log out and delete db, then it's gone, when I log in and resync with the gateway the document isn't there.

here is an example sync-gateway function
function(doc, olddoc) {
   
var channelDelimiter = "@";
   
if ( (olddoc != null && olddoc.type == "mytype" ) || (olddoc == null && doc.type == "mytype") ) {
       
if (olddoc == null) {
             
if (!doc.username || !doc.key ) {
                   
throw({forbidden: "Missing required properties"});
             
}
             channel
(doc.type + channelDelimiter + doc.key);
             access
(doc.username, doc.type + channelDelimiter + doc.key);
       
} else {
             
if (doc._deleted) {
                   requireUser
(olddoc.username);
                   channel
(olddoc.type + channelDelimiter + olddoc.key);
                   access
(olddoc.username, olddoc.type + channelDelimiter + olddoc.key);
             
} else {
                   
if (!doc.username || !doc.key ) {
                           
throw({forbidden: "Missing required properties"});
                   
}
                   
if (olddoc.username != doc.username || olddoc.key != doc.key){
                           
throw({forbidden: "unmutable properties"});
                   
}
                   requireUser
(olddoc.username);
                   channel
(doc.type + channelDelimiter + doc.key);
                   access
(doc.username, doc.type + channelDelimiter + doc.key);
             
}
       
}
   
}
)

What would be the correct way to resolve document id conflicts ?
Is there any way for the client to see if a document conflicted ?

Traun Leyden

unread,
Jan 8, 2015, 8:12:39 PM1/8/15
to mobile-c...@googlegroups.com
Have you seen the Document conflicts FAQ?

--
You received this message because you are subscribed to the Google Groups "Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mobile-couchbase/89aa2f73-cef4-4a41-a2e0-251e8e1d0534%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages