| 1 | package Mail::Decency::Core::ExportImport; | ||||||||||||
| 2 | |||||||||||||
| 3 | 11 | ▶ | use Moose::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | 11 | ▶ | use version 0.74; our $VERSION = qv( "v0.1.4" ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 11 | ▶ | use Data::Dumper; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 11 | ▶ | use Archive::Tar qw/ COMPRESS_GZIP /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 11 | ▶ | use File::Temp qw/ tempfile /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 11 | ▶ | use IO::File; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 11 | ▶ | use IO::YAML; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 11 | ▶ | use Carp qw/ croak confess /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | =head1 NAME | ||||||||||||
| 15 | |||||||||||||
| 16 | Mail::Decency::Core::ExportImport | ||||||||||||
| 17 | |||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||
| 19 | |||||||||||||
| 20 | Export / import module databases from/to CSV. | ||||||||||||
| 21 | |||||||||||||
| 22 | Can be used to deploy changes in a distributed environment. | ||||||||||||
| 23 | |||||||||||||
| 24 | =cut | ||||||||||||
| 25 | |||||||||||||
| 26 | |||||||||||||
| 27 | =head1 METHODS | ||||||||||||
| 28 | |||||||||||||
| 29 | =head2 export $export_file | ||||||||||||
| 30 | |||||||||||||
| 31 | Exports databases contents to a tarred compressed YAML (stream) file | ||||||||||||
| 32 | |||||||||||||
| 33 | =over | ||||||||||||
| 34 | |||||||||||||
| 35 | =item * $export_file | ||||||||||||
| 36 | |||||||||||||
| 37 | Path to an export file (gzip compressed tar). The file will be overwritten. | ||||||||||||
| 38 | "-" instead of a filename will print to STDOUT | ||||||||||||
| 39 | |||||||||||||
| 40 | =back | ||||||||||||
| 41 | |||||||||||||
| 42 | =cut | ||||||||||||
| 43 | |||||||||||||
| 44 | 0 | ▶ | sub export_database { | ||||||||||
|
Subroutine
export_database: not called CC=19 SCAR=59.4
Pod
undocumented
| |||||||||||||
| 45 | 0 | my ( $self, $export_file ) = @_; | |||||||||||
| 46 | |||||||||||||
| 47 | 0 | my ( $tar, @temp_files ); | |||||||||||
| 48 | 0 | my $stdout = $export_file eq '-'; | |||||||||||
| 49 | |||||||||||||
| 50 | # remove existing file | ||||||||||||
| 51 | 0 | ▶ | unless ( $stdout ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 52 | 0 | ▶ | unlink $export_file if -f $export_file; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 53 | 0 | ▶ | croak "Cannot remove export file '$export_file'" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 54 | if -f $export_file; | ||||||||||||
| 55 | |||||||||||||
| 56 | # create new tar | ||||||||||||
| 57 | 0 | $tar = Archive::Tar->new(); | |||||||||||
| 58 | } | ||||||||||||
| 59 | |||||||||||||
| 60 | # go throgh all modules | ||||||||||||
| 61 | 0 | foreach my $module( @{ $self->childs } ) { | |||||||||||
| 62 | 0 | ▶ | next unless $module->can( 'schema_definition' ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | |||||||||||||
| 64 | 0 | ▶ | while ( my ( $schema, $tables_ref ) = each %{ $module->schema_definition } ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 65 | 0 | ▶ | while ( my ( $table, $cols_ref ) = each %{ $tables_ref } ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | |||||||||||||
| 67 | # temp file | ||||||||||||
| 68 | 0 | my ( $th, $tn ); | |||||||||||
| 69 | 0 | ▶ | unless ( $stdout ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 70 | 0 | ( $th, $tn ) = tempfile( UNLINK => 0 ); | |||||||||||
| 71 | 0 | push @temp_files, $tn; | |||||||||||
| 72 | } | ||||||||||||
| 73 | else { | ||||||||||||
| 74 | 0 | $th = \*STDOUT; | |||||||||||
| 75 | } | ||||||||||||
| 76 | |||||||||||||
| 77 | 0 | ▶ | my $io = IO::YAML->new( $th ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 78 | or croak "Cannot file handle with YAML: $!"; | ||||||||||||
| 79 | 0 | $io->auto_terminate( 1 ); | |||||||||||
| 80 | |||||||||||||
| 81 | # get columns | ||||||||||||
| 82 | 0 | my @col_names = sort grep { !/^\-/ } keys %{ $cols_ref }; | |||||||||||
| 83 | |||||||||||||
| 84 | # get search result and read method | ||||||||||||
| 85 | 0 | my ( $handle, $read_meth ) | |||||||||||
| 86 | = $module->database->search_read( $schema => $table => {} ); | ||||||||||||
| 87 | |||||||||||||
| 88 | # read all records | ||||||||||||
| 89 | 0 | $| = 1; | |||||||||||
| 90 | 0 | ▶ | print STDERR "Exporting $schema / $table: " unless $stdout; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 91 | 0 | my $count = 0; | |||||||||||
| 92 | 0 | ▶ | while ( my $row_ref = $handle->$read_meth ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 93 | 0 | ▶ | print STDERR "." unless $stdout; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 94 | |||||||||||||
| 95 | # build row .. | ||||||||||||
| 96 | my $data_ref = { map { | ||||||||||||
| 97 | 0 | ▶ | ( $_ => $row_ref->{ $_ } || "" ) | ||||||||||
|
Condition0%0/2
$row_ref->{$_} || ''
MC/DC0%0/1
$row_ref->{$_} || ''
$row_ref->{$_}
Truth table0%0/2
A
A= $row_ref->{$_}
| |||||||||||||
| 98 | } @col_names }; | ||||||||||||
| 99 | |||||||||||||
| 100 | # print yaml .. | ||||||||||||
| 101 | 0 | ▶ | print $io $data_ref | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 102 | or confess "Could not print to YAML filehandle: $!"; | ||||||||||||
| 103 | |||||||||||||
| 104 | 0 | $count++; | |||||||||||
| 105 | } | ||||||||||||
| 106 | 0 | close $io; | |||||||||||
| 107 | |||||||||||||
| 108 | # add table to result | ||||||||||||
| 109 | 0 | ▶ | if ( $tn ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 110 | 0 | close $th; | |||||||||||
| 111 | 0 | $tar->add_files( $tn ); | |||||||||||
| 112 | 0 | $tn =~ s#^/##; | |||||||||||
| 113 | 0 | $tar->rename( $tn => "$module/${schema}_${table}.yml" ); | |||||||||||
| 114 | } | ||||||||||||
| 115 | |||||||||||||
| 116 | 0 | ▶ | print STDERR " Done ($count)\n\n" unless $stdout; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 117 | } | ||||||||||||
| 118 | } | ||||||||||||
| 119 | } | ||||||||||||
| 120 | |||||||||||||
| 121 | 0 | ▶ | unless ( $stdout ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 122 | # write to export | ||||||||||||
| 123 | 0 | $tar->write( $export_file, COMPRESS_GZIP ); | |||||||||||
| 124 | |||||||||||||
| 125 | # cleanup | ||||||||||||
| 126 | 0 | undef $tar; | |||||||||||
| 127 | 0 | unlink $_ for @temp_files; | |||||||||||
| 128 | } | ||||||||||||
| 129 | |||||||||||||
| 130 | 0 | return; | |||||||||||
| 131 | } | ||||||||||||
| 132 | |||||||||||||
| 133 | |||||||||||||
| 134 | =head2 import_database | ||||||||||||
| 135 | |||||||||||||
| 136 | =cut | ||||||||||||
| 137 | |||||||||||||
| 138 | 0 | ▶ | sub import_database { | ||||||||||
|
Subroutine
import_database: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||
| 139 | 0 | my ( $self, $import_file, $args_ref ) = @_; | |||||||||||
| 140 | 0 | ▶ | $args_ref ||= { replace => 0 }; | ||||||||||
|
Condition0%0/2
$args_ref ||= {"replace", 0}
MC/DC0%0/1
$args_ref ||= {"replace", 0}
$args_ref
Truth table0%0/2
A
A= $args_ref
| |||||||||||||
| 141 | |||||||||||||
| 142 | # open tar file | ||||||||||||
| 143 | 0 | ▶ | my $tar = Archive::Tar->new( $import_file, COMPRESS_GZIP ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 144 | or croak "Cannot open import file '$import_file' for read: $!\n"; | ||||||||||||
| 145 | |||||||||||||
| 146 | # go throgh all modules | ||||||||||||
| 147 | 0 | foreach my $module( @{ $self->childs } ) { | |||||||||||
| 148 | 0 | ▶ | next unless $module->can( 'schema_definition' ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 149 | |||||||||||||
| 150 | 0 | ▶ | while ( my ( $schema, $tables_ref ) = each %{ $module->schema_definition } ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 151 | 0 | ▶ | while ( my ( $table, $cols_ref ) = each %{ $tables_ref } ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 152 | |||||||||||||
| 153 | # determine file to be extracted | ||||||||||||
| 154 | 0 | my $file = "$module/${schema}_${table}.yml"; | |||||||||||
| 155 | |||||||||||||
| 156 | # if tar does not contain file -> ignore | ||||||||||||
| 157 | 0 | ▶ | next unless $tar->contains_file( $file ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 158 | |||||||||||||
| 159 | # create tempfile | ||||||||||||
| 160 | 0 | my ( $th, $tn ) = tempfile( UNLINK => 0, SUFFIX => ".yml" ); | |||||||||||
| 161 | |||||||||||||
| 162 | # extract tar | ||||||||||||
| 163 | 0 | $tar->extract_file( $file => $tn ); | |||||||||||
| 164 | |||||||||||||
| 165 | # read file | ||||||||||||
| 166 | 0 | reset $th; | |||||||||||
| 167 | 0 | my $io; | |||||||||||
| 168 | 0 | eval { | |||||||||||
| 169 | 0 | $io = IO::YAML->new( $th ); | |||||||||||
| 170 | }; | ||||||||||||
| 171 | 0 | ▶ | croak "Errro: $@" if $@; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 172 | 0 | $io->auto_load( 1 ); | |||||||||||
| 173 | |||||||||||||
| 174 | 0 | $| = 1; | |||||||||||
| 175 | 0 | print STDERR "Importing $schema / $table: "; | |||||||||||
| 176 | 0 | my $count = 0; | |||||||||||
| 177 | 0 | ▶ | while( my $row_ref = $io->next ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 178 | 0 | print STDERR "."; | |||||||||||
| 179 | 0 | $module->database->set( $schema => $table => $row_ref => $row_ref ); | |||||||||||
| 180 | 0 | $count++; | |||||||||||
| 181 | } | ||||||||||||
| 182 | 0 | print STDERR " Done ($count)\n\n"; | |||||||||||
| 183 | |||||||||||||
| 184 | # cleanup | ||||||||||||
| 185 | 0 | $io->close; | |||||||||||
| 186 | 0 | close $th; | |||||||||||
| 187 | 0 | unlink( $tn ); | |||||||||||
| 188 | } | ||||||||||||
| 189 | } | ||||||||||||
| 190 | } | ||||||||||||
| 191 | |||||||||||||
| 192 | } | ||||||||||||
| 193 | |||||||||||||
| 194 | |||||||||||||
| 195 | |||||||||||||
| 196 | |||||||||||||
| 197 | =head1 AUTHOR | ||||||||||||
| 198 | |||||||||||||
| 199 | Ulrich Kautz <uk@fortrabbit.de> | ||||||||||||
| 200 | |||||||||||||
| 201 | =head1 COPYRIGHT | ||||||||||||
| 202 | |||||||||||||
| 203 | Copyright (c) 2010 the L</AUTHOR> as listed above | ||||||||||||
| 204 | |||||||||||||
| 205 | =head1 LICENCSE | ||||||||||||
| 206 | |||||||||||||
| 207 | This library is free software and may be distributed under the same terms as perl itself. | ||||||||||||
| 208 | |||||||||||||
| 209 | =cut | ||||||||||||
| 210 | |||||||||||||
| 211 | |||||||||||||
| 212 | |||||||||||||
| 213 | |||||||||||||
| 214 | 11 | 1; | |||||||||||