Search code examples
couchdb

CouchDB replication from per-user databases to master database


I've that system arch: master database named master and users databases named user1 and user2. I'm replicating a document from master database to both user1 and user2 dbs. Then users separatelly modify that document in their DBs (for example user1 adds tags:[1, 2] and user2 adds tags:[3, 4] fields). Now I'd like to replicate from users databases back to master and merge newly added tags field so its result into tags:[1,2,3,4]. Also, I may probably need to run some logic on replication that will make resulted tags to be tags:[1,2,4].

What is the right approach?


Solution

  • You don't say how you might want to drop tag "3" in your final result, so it's hard to give a full answer.

    There is a lot of good information about various ways to handle just this sort of scenario in the CouchDB Documentation. Designing how you store the data and how you use the stored data to resolve conflicts which includes the following "Suggested code to fetch a document with conflict resolution"

    1. GET docid?conflicts=true
    2. For each member in the _conflicts array: GET docid?rev=xxx If any errors occur at this stage, restart from step 1. (There could be a race where someone else has already resolved this conflict and deleted that rev)
    3. Perform application-specific merging
    4. Write _bulk_docs with an update to the first rev and deletes of the other revs.

    It also notes "This could either be done on every read (in which case you could replace all calls to GET in your application with calls to a library which does the above), or as part of your sweeper code." And it goes on to give a full example in Ruby.

    Does that answer all your questions?