| 1 | package App::DoubleUp; | ||||||||||||||
| 2 | 6 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 6 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 6 | our $VERSION = '0.4.2'; | |||||||||||||
| 5 | |||||||||||||||
| 6 | 6 | ▶ | use 5.010; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | |||||||||||||||
| 9 | 6 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 6 | ▶ | use DBI; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 6 | ▶ | use YAML; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 6 | ▶ | use File::Slurp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 6 | ▶ | use SQL::SplitStatement; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 6 | ▶ | use File::Spec::Functions 'catfile'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 6 | ▶ | use IO::Handle; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | |||||||||||||||
| 17 | 8 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||
| 18 | 8 | my ($klass, $args) = @_; | |||||||||||||
| 19 | |||||||||||||||
| 20 | 8 | my $self = bless {}, $klass; | |||||||||||||
| 21 | |||||||||||||||
| 22 | 8 | ▶ | if (!$args->{config_file}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 23 | 3 | for ('.', $ENV{HOME}) { | |||||||||||||
| 24 | 3 | my $filename = catfile($_, '.doubleuprc'); | |||||||||||||
| 25 | 3 | ▶ | if (-e $filename) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 26 | 3 | $args->{config_file} = $filename; | |||||||||||||
| 27 | 3 | last; | |||||||||||||
| 28 | } | ||||||||||||||
| 29 | } | ||||||||||||||
| 30 | } | ||||||||||||||
| 31 | 8 | $self->{config_file} = $args->{config_file}; | |||||||||||||
| 32 | |||||||||||||||
| 33 | 8 | $self->{config} = $self->load_config($self->config_file); | |||||||||||||
| 34 | |||||||||||||||
| 35 | 8 | return $self; | |||||||||||||
| 36 | } | ||||||||||||||
| 37 | |||||||||||||||
| 38 | 10 | ▶ | sub config_file { | ||||||||||||
|
Subroutine
config_file: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 39 | 10 | my $self = shift; | |||||||||||||
| 40 | 10 | return $self->{config_file}; | |||||||||||||
| 41 | } | ||||||||||||||
| 42 | |||||||||||||||
| 43 | 8 | ▶ | sub load_config { | ||||||||||||
|
Subroutine
load_config: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 44 | 8 | my ($self, $filename) = @_; | |||||||||||||
| 45 | 8 | return YAML::LoadFile($filename); | |||||||||||||
| 46 | } | ||||||||||||||
| 47 | |||||||||||||||
| 48 | 2 | ▶ | sub source { | ||||||||||||
|
Subroutine
source: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 49 | 2 | my ($self) = @_; | |||||||||||||
| 50 | 2 | return $self->{config}{source}; | |||||||||||||
| 51 | } | ||||||||||||||
| 52 | |||||||||||||||
| 53 | 4 | ▶ | sub process_args { | ||||||||||||
|
Subroutine
process_args: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 54 | 4 | my ($self, @args) = @_; | |||||||||||||
| 55 | |||||||||||||||
| 56 | 4 | $self->{command} = shift @args; | |||||||||||||
| 57 | |||||||||||||||
| 58 | 4 | ▶ | if ($self->{command} eq 'import1') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | 1 | $self->{db} = [shift @args]; | |||||||||||||
| 60 | 1 | $self->{command} = 'import'; | |||||||||||||
| 61 | } | ||||||||||||||
| 62 | |||||||||||||||
| 63 | 4 | $self->{files} = \@args; | |||||||||||||
| 64 | |||||||||||||||
| 65 | 4 | return; | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | |||||||||||||||
| 68 | 0 | ▶ | sub process_files { | ||||||||||||
|
Subroutine
process_files: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 69 | 0 | my ($self, $files) = @_; | |||||||||||||
| 70 | |||||||||||||||
| 71 | 0 | my @querys; | |||||||||||||
| 72 | |||||||||||||||
| 73 | 0 | local $/ = ";\n"; | |||||||||||||
| 74 | |||||||||||||||
| 75 | 0 | for my $filename (@$files) { | |||||||||||||
| 76 | 0 | push @querys, $self->split_sql_file($filename); | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | |||||||||||||||
| 79 | 0 | return @querys; | |||||||||||||
| 80 | } | ||||||||||||||
| 81 | 1 | ▶ | sub split_sql_file { | ||||||||||||
|
Subroutine
split_sql_file: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 82 | 1 | my ($self, $filename) = @_; | |||||||||||||
| 83 | 1 | my $splitter = SQL::SplitStatement->new(); | |||||||||||||
| 84 | 1 | return $splitter->split(scalar read_file($filename)); | |||||||||||||
| 85 | } | ||||||||||||||
| 86 | |||||||||||||||
| 87 | 0 | ▶ | sub db_prepare { | ||||||||||||
|
Subroutine
db_prepare: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 88 | 0 | my ($db, $query) = @_; | |||||||||||||
| 89 | 0 | my $stmt = $db->prepare($query); | |||||||||||||
| 90 | 0 | return $stmt; | |||||||||||||
| 91 | } | ||||||||||||||
| 92 | |||||||||||||||
| 93 | 0 | ▶ | sub db_flatarray { | ||||||||||||
|
Subroutine
db_flatarray: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 94 | 0 | my ($db, $query, @args) = @_; | |||||||||||||
| 95 | 0 | my $stmt = db_prepare($db, $query); | |||||||||||||
| 96 | 0 | $stmt->execute(@args); | |||||||||||||
| 97 | 0 | my @vals; | |||||||||||||
| 98 | 0 | ▶ | while (my $row = $stmt->fetchrow_arrayref) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 99 | 0 | push @vals, $row->[0]; | |||||||||||||
| 100 | } | ||||||||||||||
| 101 | 0 | return @vals; | |||||||||||||
| 102 | } | ||||||||||||||
| 103 | |||||||||||||||
| 104 | 2 | ▶ | sub list_of_schemata { | ||||||||||||
|
Subroutine
list_of_schemata: called CC=3 SCAR=13.5
Pod
undocumented
| |||||||||||||||
| 105 | 2 | my ($self) = @_; | |||||||||||||
| 106 | 2 | my $source = $self->source; | |||||||||||||
| 107 | 2 | ▶ | if ($source->{type} eq 'config') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 108 | 2 | return @{ $source->{databases} }; | |||||||||||||
| 109 | } | ||||||||||||||
| 110 | elsif ($source->{type} eq 'database') { | ||||||||||||||
| 111 | 0 | my $db = $self->connect_to_db('dbi:mysql:information_schema', $self->credentials); | |||||||||||||
| 112 | 0 | return db_flatarray($db, $source->{schemata_sql}); | |||||||||||||
| 113 | } | ||||||||||||||
| 114 | } | ||||||||||||||
| 115 | |||||||||||||||
| 116 | 1 | ▶ | sub credentials { | ||||||||||||
|
Subroutine
credentials: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 117 | 1 | my $self = shift; | |||||||||||||
| 118 | 1 | return @{$self->{config}{credentials}}; | |||||||||||||
| 119 | } | ||||||||||||||
| 120 | |||||||||||||||
| 121 | 0 | ▶ | sub connect_to_db { | ||||||||||||
|
Subroutine
connect_to_db: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 122 | 0 | my ($self, $dsn, $user, $password) = @_; | |||||||||||||
| 123 | 0 | ▶ | return DBI->connect($dsn, $user, $password, { RaiseError => 1, PrintError => 0 }) || croak "Error while connecting to '$dsn'"; | ||||||||||||
|
Condition0%0/2
'DBI'->connect($dsn, $user, $password, {'RaiseError', 1, 'PrintError', 0}) || croak("Error while connecting to '${dsn}'")
MC/DC0%0/1
'DBI'->connect($dsn, $user, $password, {'RaiseError', 1, 'PrintError', 0}) || croak("Error while connecting to '${dsn}'")
'DBI'->connect($dsn, $user, $password, {'RaiseError', 1, 'PrintError', 0})
Truth table0%0/2
A
A= 'DBI'->connect($dsn, $user, $password, {'RaiseError', 1, 'PrintError', 0})
| |||||||||||||||
| 124 | } | ||||||||||||||
| 125 | |||||||||||||||
| 126 | 0 | ▶ | sub process_querys_for_one_db { | ||||||||||||
|
Subroutine
process_querys_for_one_db: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 127 | 0 | my ($self, $db, $querys) = @_; | |||||||||||||
| 128 | |||||||||||||||
| 129 | 0 | for my $q (@$querys) { | |||||||||||||
| 130 | 0 | ▶ | if ($self->process_one_query($db, $q)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 131 | 0 | print '.'; | |||||||||||||
| 132 | } | ||||||||||||||
| 133 | else { | ||||||||||||||
| 134 | 0 | print '!'; | |||||||||||||
| 135 | } | ||||||||||||||
| 136 | } | ||||||||||||||
| 137 | 0 | return; | |||||||||||||
| 138 | } | ||||||||||||||
| 139 | |||||||||||||||
| 140 | 0 | ▶ | sub process_one_query { | ||||||||||||
|
Subroutine
process_one_query: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 141 | 0 | my ($self, $db, $q) = @_; | |||||||||||||
| 142 | |||||||||||||||
| 143 | 0 | eval { | |||||||||||||
| 144 | 0 | $db->do($q); | |||||||||||||
| 145 | }; | ||||||||||||||
| 146 | 0 | ▶ | if ($@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 147 | 0 | return; | |||||||||||||
| 148 | } | ||||||||||||||
| 149 | 0 | return 1; | |||||||||||||
| 150 | } | ||||||||||||||
| 151 | |||||||||||||||
| 152 | 5 | ▶ | sub command { | ||||||||||||
|
Subroutine
command: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 153 | 5 | my $self = shift; | |||||||||||||
| 154 | 5 | return $self->{command}; | |||||||||||||
| 155 | } | ||||||||||||||
| 156 | |||||||||||||||
| 157 | 3 | ▶ | sub database_names { | ||||||||||||
|
Subroutine
database_names: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 158 | 3 | my $self = shift; | |||||||||||||
| 159 | 3 | ▶ | $self->{db} //= [ $self->list_of_schemata ]; | ||||||||||||
|
Condition100%2/2
$self->{"db"} //= [$self->list_of_schemata]
MC/DC100%1/1
$self->{"db"} //= [$self->list_of_schemata]
$self->{"db"}
Truth table100%2/2
A
A= $self->{"db"}
| |||||||||||||||
| 160 | 3 | return $self->{db}; | |||||||||||||
| 161 | } | ||||||||||||||
| 162 | |||||||||||||||
| 163 | 2 | ▶ | sub files { | ||||||||||||
|
Subroutine
files: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 164 | 2 | my $self = shift; | |||||||||||||
| 165 | 2 | return $self->{files}; | |||||||||||||
| 166 | } | ||||||||||||||
| 167 | |||||||||||||||
| 168 | 1 | ▶ | sub run { | ||||||||||||
|
Subroutine
run: called CC=3 SCAR=19.2
Pod
undocumented
| |||||||||||||||
| 169 | 1 | my ($self) = @_; | |||||||||||||
| 170 | |||||||||||||||
| 171 | 1 | STDOUT->autoflush(1); | |||||||||||||
| 172 | |||||||||||||||
| 173 | 1 | given ($self->command) { | |||||||||||||
| 174 | 1 | when ('version') { | |||||||||||||
| 175 | 1 | say "doubleup version $VERSION"; | |||||||||||||
| 176 | } | ||||||||||||||
| 177 | 0 | when ('listdb') { | |||||||||||||
| 178 | 0 | my @db = $self->list_of_schemata(); | |||||||||||||
| 179 | 0 | for (@db) { | |||||||||||||
| 180 | 0 | say; | |||||||||||||
| 181 | } | ||||||||||||||
| 182 | } | ||||||||||||||
| 183 | 0 | when ('import') { | |||||||||||||
| 184 | 0 | my @querys = $self->process_files($self->files); | |||||||||||||
| 185 | |||||||||||||||
| 186 | 0 | for my $schema (@{ $self->database_names }) { | |||||||||||||
| 187 | 0 | my $dsn = 'dbi:mysql:'.$schema; | |||||||||||||
| 188 | 0 | say "DB: $schema"; | |||||||||||||
| 189 | 0 | my $db = $self->connect_to_db($dsn, $self->credentials); | |||||||||||||
| 190 | 0 | $self->process_querys_for_one_db($db, \@querys); | |||||||||||||
| 191 | 0 | say ''; | |||||||||||||
| 192 | } | ||||||||||||||
| 193 | } | ||||||||||||||
| 194 | 0 | when (undef) { | |||||||||||||
| 195 | 0 | $self->usage; | |||||||||||||
| 196 | } | ||||||||||||||
| 197 | 0 | default { | |||||||||||||
| 198 | 0 | say "Unknown command: $_"; | |||||||||||||
| 199 | 0 | $self->usage; | |||||||||||||
| 200 | } | ||||||||||||||
| 201 | } | ||||||||||||||
| 202 | 1 | return; | |||||||||||||
| 203 | |||||||||||||||
| 204 | } | ||||||||||||||
| 205 | 0 | ▶ | sub usage { | ||||||||||||
|
Subroutine
usage: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 206 | 0 | my $self = shift; | |||||||||||||
| 207 | 0 | say "Usage: doubleup [command] [options]"; | |||||||||||||
| 208 | 0 | say ""; | |||||||||||||
| 209 | 0 | say "List of commands"; | |||||||||||||
| 210 | 0 | say ""; | |||||||||||||
| 211 | 0 | say " listdb list of schemata"; | |||||||||||||
| 212 | 0 | say " import [filename] import a file into each db"; | |||||||||||||
| 213 | 0 | say " import1 [db] [filename] import a file into one db"; | |||||||||||||
| 214 | 0 | say " version show version"; | |||||||||||||
| 215 | 0 | say ""; | |||||||||||||
| 216 | 0 | return; | |||||||||||||
| 217 | } | ||||||||||||||
| 218 | |||||||||||||||
| 219 | 6 | 1; | |||||||||||||
| 220 | |||||||||||||||
| 221 | =head1 NAME | ||||||||||||||
| 222 | |||||||||||||||
| 223 | App::DoubleApp - Import SQL files into MySQL | ||||||||||||||
| 224 | |||||||||||||||
| 225 | =head1 SYNOPSIS | ||||||||||||||
| 226 | |||||||||||||||
| 227 | $ doubleup listdb | ||||||||||||||
| 228 | ww_test1 | ||||||||||||||
| 229 | ww_test2 | ||||||||||||||
| 230 | ww_test3 | ||||||||||||||
| 231 | ww_test4 | ||||||||||||||
| 232 | $ doubleup import1 ww_test db/01_base.sql | ||||||||||||||
| 233 | . | ||||||||||||||
| 234 | $ doubleup import db/02_upgrade.sql | ||||||||||||||
| 235 | .... | ||||||||||||||
| 236 | |||||||||||||||
| 237 | =head1 DESCRIPTION | ||||||||||||||
| 238 | |||||||||||||||
| 239 | Import SQL files into a DBI compatible database. | ||||||||||||||
| 240 | |||||||||||||||
| 241 | =head1 AUTHOR | ||||||||||||||
| 242 | |||||||||||||||
| 243 | Peter Stuifzand E<lt>peter@stuifzand.euE<gt> | ||||||||||||||
| 244 | |||||||||||||||
| 245 | =head1 COPYRIGHT | ||||||||||||||
| 246 | |||||||||||||||
| 247 | Copyright 2013- Peter Stuifzand | ||||||||||||||
| 248 | |||||||||||||||
| 249 | =head1 LICENSE | ||||||||||||||
| 250 | |||||||||||||||
| 251 | This library is free software; you can redistribute it and/or modify | ||||||||||||||
| 252 | it under the same terms as Perl itself. | ||||||||||||||
| 253 | |||||||||||||||
| 254 | =head1 SEE ALSO | ||||||||||||||
| 255 | |||||||||||||||
| 256 | =cut | ||||||||||||||