couchdbtouchdb

Migrate between two CouchDB designs


Using TouchDB-iOS we have an iOS app that has a local CouchDB document store that replicates to a cloudant CouchDB server. We have a couple of users running this app, resulting in a bunch of TouchDB database replica's out there.

When we started with the app we were new to CouchDB (we still are). We designed a relationship so that document of type A would have a property saying: this is a string describing a comma-separated list of id's that are documents of type B.

So using the Employee/Employer example, it would be that Employer had a property called employeeIds which would be "1,7,8,10". If Employee 10 would quit this list would be updated to "1,7,8".

Problem was that when on another instance of the app, on another phone let's say, employee 7 would quit the list would there be updated to "1,8,10", causing conflicts when replicating.

So we're thinking a better idea would be to have an employerId in the Employee document property. If an employee quits, we just set his employerId to empty. There will be a lot less conflicts like that, right?

The problem I'm facing now is with having multiple apps out there, how do I migrate all the CouchDB databases from the first design to the second design.

Do I need to retire all old apps or is there a fail-safe way to migrate all the apps over to the new design without breaking existing apps and while minimising conflicts? How should I best handle this case?


Solution

  • There are basically two scenarios:

    If your apps are only doing queries on the database (through views), you could simply update your views to work with "old-style" an "new-style" docs equally. You could then update your documents in the background (eg by following the _changes feed) and eventually remove support for old-style docs again.

    If you apps use the structure of the apps, then there will probably be no way around updating the apps as well. Otherwise you'd need to have some proxy in between that translates between old and new style documents, eg

    CouchDB with new-style docs  <-- proxy application --> apps with old-style docs
    

    You could, of course update your app to handle both old and new-style docs, so that you can transition your documents gradually.

    If you have to re-design access to CouchDB, you might consider update handlers to make future changes more transparent.