File Coverage

blib/lib/Yancy/Command/backend/copy.pm
Criterion Covered Total %
statement 17 18 94.4
branch 1 2 50.0
condition 2 5 40.0
subroutine 3 3 100.0
pod 1 1 100.0
total 24 29 82.7


line stmt bran cond sub pod time code
1             package Yancy::Command::backend::copy;
2             our $VERSION = '1.086';
3             # ABSTRACT: Copy data between backends
4              
5             #pod =head1 SYNOPSIS
6             #pod
7             #pod Usage: APPLICATION backend copy DEST_BACKEND SRC_SCHEMA [DEST_SCHEMA]
8             #pod
9             #pod ./myapp.pl backend copy sqlite:prod.db users
10             #pod ./myapp.pl backend copy sqlite:prod.db users new_users
11             #pod
12             #pod =head1 DESCRIPTION
13             #pod
14             #pod This command copies data from the application's backend to another backend.
15             #pod Use this to set up a new application from an existing application's data,
16             #pod copy data between environments, or migrate data to a different backend.
17             #pod
18             #pod =head1 SEE ALSO
19             #pod
20             #pod L
21             #pod
22             #pod =cut
23              
24 1     1   614 use Mojo::Base 'Mojolicious::Command';
  1         3  
  1         7  
25 1     1   714 use Yancy::Util qw( load_backend );
  1         3  
  1         373  
26              
27             has description => 'Copy data between backends';
28             has usage => sub { shift->extract_usage };
29              
30             sub run {
31 1     1 1 119 my ( $self, @argv ) = @_;
32              
33 1         4 my ( $dest_url, $source_schema, $dest_schema ) = @argv;
34 1   33     5 $dest_schema ||= $source_schema;
35 1         8 my $source_backend = $self->app->yancy->backend;
36 1         19 my $schema = $self->app->yancy->schema( $source_schema );
37 1   50     13 my $id_field = $schema->{'x-id-field'} || 'id';
38 1         7 my $dest_backend = load_backend( $dest_url, { $dest_schema => $schema } );
39 1         3 for my $item ( @{ $source_backend->list( $source_schema )->{items} } ) {
  1         28  
40             # XXX This is two round-trip requests to the database for every
41             # item. We could fetch the entire list to have known IDs, but
42             # that could be potentially thousands of rows. Either the
43             # backend list() method needs some kind of iterator version so
44             # as to not load the entire table before returning, or we need
45             # a way for list() to return a single column instead of the
46             # entire data set.
47 2 50       9 if ( $dest_backend->get( $dest_schema, $item->{ $id_field } ) ) {
48 0         0 $dest_backend->set( $dest_schema, $item->{ $id_field }, $item );
49             }
50             else {
51 2         8 $dest_backend->create( $dest_schema, $item );
52             }
53             }
54             }
55              
56             1;
57              
58             __END__