Yancy/Command/backend/copy.pm

filter: Statementstmt 43.4% 10 / 23 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 50.0% 2 / 4 Podpod 100% 1 / 1 totaltotal 34.2% 13 / 38 CC 5 SCAR 29.5
CC
5
Cov
33%
run
30.0
__ANON__
1.1
BEGIN
1.0
CRAP 19.1
« Yancy/Plugin/Auth/Github.pm ↑ Summary Yancy/Plugin/Roles.pm »
1 package Yancy::Command::backend::copy;
2 2 our $VERSION = '1.078';
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<Yancy::Command::backend>
21 #pod
22 #pod =cut
23  
24 2 ▶use Mojo::Base 'Mojolicious::Command';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use Yancy::Util qw( load_backend );
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 2 has description => 'Copy data between backends';
28 0 ▶has usage => sub { shift->extract_usage };
Subroutine __ANON__: not called CC=1 SCAR=1.2
29  
30 sub run {
31 0 ▶ my ( $self, @argv ) = @_;
Subroutine run: not called CC=5 SCAR=34.0
Pod documented
32  
33 0 my ( $dest_url, $source_schema, $dest_schema ) = @argv;
34 0 ▶ $dest_schema ||= $source_schema;
Condition0%0/3
$dest_schema ||= $source_schema
l!l&&r!l&&!r
000
MC/DC0%0/2
$dest_schema ||= $source_schema
$dest_schema $source_schema
Truth table0%0/3
A ||= B
A= $dest_schemaB= $source_schema
ABresult
1X1
011
000
35 0 my $source_backend = $self->app->yancy->backend;
36 0 my $schema = $self->app->yancy->schema( $source_schema );
37 0 ▶ my $id_field = $schema->{'x-id-field'} || 'id';
Condition0%0/2
$schema->{'x-id-field'} || 'id'
l!l
00
MC/DC0%0/1
$schema->{'x-id-field'} || 'id'
$schema->{'x-id-field'}
Truth table0%0/2
A
A= $schema->{'x-id-field'}
Aresult
11
00
38 0 my $dest_backend = load_backend( $dest_url, { $dest_schema => $schema } );
39 0 for my $item ( @{ $source_backend->list( $source_schema )->{items} } ) {
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 0 ▶ if ( $dest_backend->get( $dest_schema, $item->{ $id_field } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dest_backend->get($dest_schema, $item->{$id_field})) { } 0 0 0
48 0 $dest_backend->set( $dest_schema, $item->{ $id_field }, $item );
49 }
50 else {
51 0 $dest_backend->create( $dest_schema, $item );
52 }
53 }
54 }
55  
56 2 1;
57  
58 __END__
« Yancy/Plugin/Auth/Github.pm ↑ Summary Yancy/Plugin/Roles.pm »