split out replication into own tool
authorDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 9 Nov 2010 16:51:08 +0000 (17:51 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 9 Nov 2010 16:55:05 +0000 (17:55 +0100)
This change also involves using angular REST API for replication
as opposed to hitting CouchDB directly so it's more general

README
angular-replicate.pl [new file with mode: 0755]

diff --git a/README b/README
index dd7aa13..0ab9326 100644 (file)
--- a/README
+++ b/README
@@ -25,9 +25,14 @@ At it's current stage it provides support for angular $resource get, query and $
 http://angularjs.org/Service:$resource
 
 
 http://angularjs.org/Service:$resource
 
 
-Server also supports replication using same API which can be triggered with
+Replication of data between instances using angular REST API can be done with:
 
 
- http://localhost:3000/_replicate?from=http://dpavlin.getangular.com/data/conference/
+       # create local CouchDB database
+       curl -X PUT http://localhost:5984/test
+
+       ./angular-replicate.pl \
+               http://dpavlin.getangular.com/data/conference \
+               http://localhost:3000/data/test
 
 Replication is currently good only for initial import of data since it doesn't
 support incremental replication and dies if data is allready present.
 
 Replication is currently good only for initial import of data since it doesn't
 support incremental replication and dies if data is allready present.
diff --git a/angular-replicate.pl b/angular-replicate.pl
new file mode 100755 (executable)
index 0000000..ad8c807
--- /dev/null
@@ -0,0 +1,44 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+
+use Mojo::Client;
+use Data::Dump qw(dump);
+
+use lib 'common/mojo/lib';
+
+my ( $from, $to ) = @ARGV;
+
+die "usage: $0 http://from/data/database/ http://to/data/database/\n"
+unless $from && $to;
+
+my $client = Mojo::Client->new;
+
+my $got = $client->get( $from )->res->json;
+warn "# from $from ",dump($got);
+
+my $database = $got->{name};
+my $entities = $got->{entities};
+
+sub _url_entity {
+       my ($url,$entity) = @_;
+       $url =~ s{/?$}{/}; # add slash at end
+       $url .= $entity;
+       warn "URL $url\n";
+       return $url;
+}
+
+if ( $database && $entities ) {
+       foreach my $entity ( keys %$entities ) {
+               my $all = $client->get( _url_entity( $from => $entity ) )->res->json;
+               warn "## all = ",dump($all);
+               warn "# fetched ", $#$all + 1, " $entity entities from $from";
+               foreach my $e ( @$all ) {
+                       delete $e->{_id}; # sanitize data from older implementation
+                       my $json = Mojo::JSON->new->encode( $e );
+                       my $response = $client->post( _url_entity( $to => $entity ), $json )->res->body;
+                       warn "# replicated $entity\n$json\n",dump($response);
+               }
+       }
+}
+