Yancy/Command/backend/copy.pm

filter: Statementstmt 91.3% 21 / 23 Branchbran 50.0% 1 / 2 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 75.0% 3 / 4 Podpod 100% 1 / 1 totaltotal 73.6% 28 / 38 CC 5 SCAR 13.4
CC
5
Cov
80%
run
5.5
__ANON__
1.1
BEGIN
1.0
CRAP 3.8
« Yancy/Plugin/Auth/Github.pm ↑ Summary Yancy.pm »
1 package Yancy::Command::backend::copy;
2 2 our $VERSION = '1.088';
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 1 ▶sub run {
Subroutine run: called CC=5 SCAR=17.0
Pod documented
31 1 my ( $self, @argv ) = @_;
32  
33 1 my ( $dest_url, $source_schema, $dest_schema ) = @argv;
34 1 ▶ $dest_schema ||= $source_schema;
Condition33%1/3
$dest_schema ||= $source_schema
l!l&&r!l&&!r
100
MC/DC0%0/2
$dest_schema ||= $source_schema
$dest_schema $source_schema
Truth table33%1/3
A ||= B
A= $dest_schemaB= $source_schema
ABresult
1X1
011
000
35 1 my $source_backend = $self->app->yancy->backend;
36 1 my $schema = $self->app->yancy->schema( $source_schema );
37 1 ▶ my $id_field = $schema->{'x-id-field'} || 'id';
Condition50%1/2
$schema->{'x-id-field'} || 'id'
l!l
01
MC/DC0%0/1
$schema->{'x-id-field'} || 'id'
$schema->{'x-id-field'}
Truth table50%1/2
A
A= $schema->{'x-id-field'}
Aresult
11
00
38 1 my $dest_backend = load_backend( $dest_url, { $dest_schema => $schema } );
39 1 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 2 ▶ if ( $dest_backend->get( $dest_schema, $item->{ $id_field } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dest_backend->get($dest_schema, $item->{$id_field})) { } 0 2 2
48 0 $dest_backend->set( $dest_schema, $item->{ $id_field }, $item );
49 }
50 else {
51 2 $dest_backend->create( $dest_schema, $item );
52 }
53 }
54 }
55  
56 2 1;
57  
58 __END__
« Yancy/Plugin/Auth/Github.pm ↑ Summary Yancy.pm »