Monday, February 04, 2013

CouchDB keep Design Documents with views formatted

Unix Power Tools work. In many cases from command line you can practically do anything you want way faster and consuming way less resources than from a GUI. When it comes to automation a GUI can't compete so there are even scenarios where you really have no option other than scripting it.

In a NoSQL DB you probably have no schema. I will not argue here about structured/semistructured/unstructured choices. That is a subject for a bigger discussion for which my simple answer is "- it depends". But I will argue here that your Views in CouchDB or whatever you call the map/reduce functions in your favorite noSQL must be kept in a version control repository.

I will also argue that any migrations applied to Views should automatically generate the latest version of them and should be committed to your version control system.

I should not have to say it but all of the above should be automated for sure.

Storing a compressed or minified source code (in couchdb that would be javascript) does not make sense to me so I would vote for storing the extracted views correctly formatted. A one-liner will be enough to achieve this. Note that below curl uses k(to ignore ssl fake certificate of this integration environment), s (to avoid showing the progress) and X (to specify the method in this case a GET). Python formats the output.
$ curl -ksX GET " "| python -mjson.tool
Granted this is still not that great as the javascript functions do not get formatted but yet better than the original output of the command without the python help.

To use the design document later on in couchDB you need to PUT it in the couchdb server database, however the "_rev" attribute must be removed first. Again nothing Unix Power Tools won't be able to do. Here is how to do a correct backup:
#extract design document from db
curl -ksX GET "" | python -mjson.tool | sed '/\"_rev\"/d' > mydb-couchdb-design-Document.json
Here is how you would put the design document back into a couchdb database (restore). Note how we need the revision to be able to delete and then after deleted we just PUT our saved copy. We can probably do the same with just one POST instead of DELETE+PUT but you get the idea about the power of unix tools here.
$ revision=`curl -ksX GET "" | sed 's/.*_rev\":\"\([^\"]*\).*/\1/g'`
$ curl -ksX DELETE "$revision"
$ curl -ksX PUT "" --data-binary @mydb-couchdb-design-Document.json
Now you can keep your views in version control and should you need a blank database it is just a matter of creating it and running the previous code to create the views:
#extract design document from db
curl -ksX GET "" | python -mjson.tool | sed '/\"_rev\"/d' > mydb-couchdb-design-Document.json
#delete database
curl -ksX DELETE ""
#create database
curl -ksX PUT ""
#put the design document in db
630  curl -ksX PUT "" --data-binary @mydb-couchdb-design-Document.json

No comments: