| 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
MC/DC0%0/2
$dest_schema ||= $source_schema
$dest_schema
$source_schema
Truth table33%1/3
A ||= B
A= $dest_schemaB= $source_schema
| |||||||||||||||||||||
| 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'
MC/DC0%0/1
$schema->{'x-id-field'} || 'id'
$schema->{'x-id-field'}
Truth table50%1/2
A
A= $schema->{'x-id-field'}
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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__ | ||||||||||||||||||||