Skip to content

Instantly share code, notes, and snippets.

@nolanlawson
Created May 2, 2012 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nolanlawson/2575396 to your computer and use it in GitHub Desktop.
Save nolanlawson/2575396 to your computer and use it in GitHub Desktop.
Simple perl script to query all design docs in a CouchDB database. Useful as a cron job to force permanent views to reindex automatically.
#!/usr/bin/perl
# simple script to run a query on all the CouchDB saved views under the "projections" design
# document, in order to force CouchDB to re-index them.
#
# Usage:
#
# update_couchdb_views.pl couchdb_host couchdb_database
#
# e.g.
# update_couchdb_views.pl http://localhost:5984 honbot
#
# @author nolan
# script homepage: http://gist.github.com/2575396
#
use strict;
use warnings;
use AnyEvent::CouchDB;
use Data::Dumper;
my $USAGE =
"\nUSAGE:\nupdate_couchdb_views.pl couchdb_host couchdb_database\n\ne.g.\nupdate_couchdb_views.pl http://localhost:5984 honbot\n\n";
my $host = shift or die $USAGE;
my $database = shift or die $USAGE;
my $couch = couch($host);
my $db = $couch->db($database);
my %initial_opts = (
'startkey' => '_design/',
'endkey' => '_design0'
);
my $design_doc_names = $db->open_doc( "_all_docs", \%initial_opts )->recv;
foreach my $design_doc_name ( @{ $design_doc_names->{'rows'} } ) {
my $name = $design_doc_name->{'key'};
print "Requesting all design documents in $name\n";
my @recvd = $db->open_doc($name)->recv;
my $design_docs = $recvd[0]{"views"};
# grab the last document only
my %options = (
'limit' => 1,
'descending' => "false"
);
my $hash_ref = \%options;
$name =~ s/_design\///g;
foreach my $key ( keys %$design_docs ) {
print "Requesting from CouchDB: $name/$key...\n";
eval { print Dumper( $db->view( "$name/$key", $hash_ref )->recv ); };
if ($@) {
print "Exception: $@\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment