Created
June 16, 2012 20:50
-
-
Save danielrichman/2942482 to your computer and use it in GitHub Desktop.
Refresh all couchdb views
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Copyright Daniel Richman 2012. License: GNU GPL 3 | |
import sys | |
import couchdbkit | |
def main(uri, db): | |
server = couchdbkit.Server(uri) | |
database = server[db] | |
for row in database.all_docs(startkey="_design/", endkey="_design0", | |
include_docs=True): | |
did = row["id"] | |
assert did.startswith("_design/") | |
design = did[len("_design/"):] | |
doc = row["doc"] | |
if "views" not in doc: | |
continue | |
for key in doc["views"]: | |
view = design + "/" + key | |
print "Refreshing view", view | |
if "reduce" in doc["views"][key]: | |
r = database.view(view, reduce=True, group=True, limit=1) | |
else: | |
r = database.view(view, limit=1) | |
# It's an iterator, and it's lazily loaded: | |
list(r) | |
if __name__ == "__main__": | |
if len(sys.argv) != 3: | |
sys.stderr.write("Usage: {0} uri database\n".format(sys.argv[0])) | |
sys.exit(1) | |
main(sys.argv[1], sys.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment