From a88cacc1e9f0515e04233efff349befcda44e510 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Tue, 9 Nov 2010 17:51:08 +0100 Subject: [PATCH] split out replication into own tool This change also involves using angular REST API for replication as opposed to hitting CouchDB directly so it's more general --- README | 9 +++++++-- angular-replicate.pl | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100755 angular-replicate.pl diff --git a/README b/README index dd7aa13..0ab9326 100644 --- 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 -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. diff --git a/angular-replicate.pl b/angular-replicate.pl new file mode 100755 index 0000000..ad8c807 --- /dev/null +++ b/angular-replicate.pl @@ -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); + } + } +} + -- 2.20.1