Permalink
Browse files

script to replicate data from a couchdb to another using the changes API

  • Loading branch information...
1 parent 1db3c9a commit a3bcd89e36296b97566be607bd300ffd9ddbacc3 @franckcuny franckcuny committed with Apr 25, 2010
Showing with 38 additions and 0 deletions.
  1. +38 −0 eg/replicate.pl
View
38 eg/replicate.pl
@@ -0,0 +1,38 @@
+use strict;
+use warnings;
+
+use 5.012;
+
+use AnyEvent::CouchDB;
+use AnyEvent::CouchDB::Stream;
+
+my ( $host_orig, $db_orig, $host_dest, $db_dest ) = @ARGV;
+
+my $cv = my $done = AE::cv;
+
+my $couch_orig = couch($host_orig);
+my $couchdb_orig = $couch_orig->db($db_orig);
+
+my $couch_dest = couch($host_dest);
+my $couchdb_dest = $couch_dest->db($db_dest);
+
+my $l = AnyEvent::CouchDB::Stream->new(
+ url => $host_orig,
+ database => $db_orig,
+ on_change => sub {
+ my $change = shift;
+ say "document "
+ . $change->{id}
+ . " with sequence "
+ . $change->{seq}
+ . " have been updated";
+ $couchdb_orig->open_doc( $change->{id} )->cb(
+ sub {
+ my $data = $_[0]->recv;
+ $couchdb_dest->save_doc($data);
+ }
+ );
+ },
+);
+
+$cv->recv;

0 comments on commit a3bcd89

Please sign in to comment.