4.6. View recommendations¶
Here are some tips and tricks for working with CouchDB’s (JavaScript-based) views.
4.6.1. Deploying a view change in a live environment¶
It is possible to change the definition of a view, build the index, then make
those changes go live without causing downtime for your application. The trick
to making this work is that CouchDB’s JavaScript view index files are based on
the contents of the design document - not its name, _id
or revision. This
means that two design documents with identical view code will share the same
on-disk view index files.
Here is a worked example, assuming your /db/_design/ddoc
needs to be updated.
Upload the old design doc to
/db/_design/ddoc-old
(or copy the document) if you want an easy way to rollback in case of problems. Theddoc-old
document will reference the same view indexes already built for_design/ddoc
.Upload the updated design doc to
/db/_design/ddoc-new
.Query a view in the new design document to trigger secondary index generation. You can track the indexing progress via the
/_active_tasks
endpoint, or through the Fauxton web interface.When the index is done being built, re-upload the updated design document to
/db/_design/ddoc
(or copy the document). Theddoc
document will now reference the same view indexes already built for_design/ddoc-new
.Delete
/db/_design/ddoc-new
and/or/db/_design/ddoc-old
at your discretion. Don’t forget to trigger Views cleanup to reclaim disk space after deletingddoc-old
.
The COPY HTTP verb can be used to copy the design document with a single command:
curl -X COPY <URL of source design document> -H "Destination: <ID of destination design document>"