blib/lib/DBIx/ThinSQL/Deploy.pm

filter: Statementstmt 87.2% 185 / 212 Branchbran 63.8% 46 / 72 Conditioncond 47.3% 9 / 19 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 89.4% 17 / 19 Podpod 100% 10 / 10 totaltotal 78.1% 268 / 343 CC 49 SCAR 19.7
CC
49
Cov
79%
deploy_arrayref
13.3
_split_sql
9.1
run_dir
8.1
CRAP 7.1
« blib/lib/DBIx/ThinSQL.pm ↑ Summary blib/lib/DBIx/ThinSQL/Driver/Pg.pm »
1 package DBIx::ThinSQL::Deploy;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Log::Any qw/$log/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp qw/croak carp confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '0.0.49';
9  
10 41 ▶sub _split_sql {
Subroutine _split_sql: called CC=9 SCAR=22.1
11 41 my $input = shift;
12 41 my $end = '';
13 41 my $item = '';
14 41 my @items;
15  
16 41 $input =~ s/^\s*--.*\n//gm;
17 41 $input =~ s!/\*.*?\*/!!gsm;
18  
19 41 ▶ while ( $input =~ s/(.*\n)// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input =~ s/(.*\n)//) 325 41 366
20 325 my $try = $1;
21  
22 325 ▶ if ($end) {
Branch: 4 branches
BranchTrueFalseTotal
if ($end) { } 60 265 325
elsif ($try =~ /;/) { } 107 158 265
elsif ($try =~ /^\s*CREATE( OR REPLACE)? FUNCTION.*AS (\S*)/i) { } 0 158 158
elsif ($try =~ /^\s*CREATE TRIGGER/i) { } 6 152 158
23 60 ▶ if ( $try =~ m/$end/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($try =~ /$end/i) { } 6 54 60
24 6 $item .= $try;
25  
26 6 ▶ if ( $try =~ m/;/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($try =~ /;/) 6 0 6
27 6 $item =~ s/(^[\s\n]+)|(\s\n]+$)//;
28 6 push( @items, { sql => $item } );
29 6 $item = '';
30 }
31  
32 6 $end = '';
33 }
34 else {
35 54 $item .= $try;
36 }
37  
38 }
39 elsif ( $try =~ m/;/ ) {
40 107 $item .= $try;
41 107 $item =~ s/(^[\s\n]+)|(\s\n]+$)//;
42 107 push( @items, { sql => $item } );
43 107 $item = '';
44 }
45 elsif ( $try =~ m/^\s*CREATE( OR REPLACE)? FUNCTION.*AS (\S*)/i ) {
46 0 $end = $2;
47 0 $end =~ s/\$/\\\$/g;
48 0 $item .= $try;
49 }
50 elsif ( $try =~ m/^\s*CREATE TRIGGER/i ) {
51 6 $end = qr/(EXECUTE PROCEDURE)|(^END)/i;
52 6 $item .= $try;
53 }
54 else {
55 152 $item .= $try;
56 }
57 }
58  
59 41 foreach my $item (@items) {
60 113 $item->{sql} =~ s/;[\s\n]*$//;
61 }
62 41 return \@items;
63 }
64  
65 46 ▶sub _load_file {
Subroutine _load_file: called CC=4 SCAR=14.2
66 46 my $file = path(shift);
67 46 my $type = lc $file;
68  
69 46 $log->debug( '_load_file(' . $file . ')' );
70  
71 46 ▶ confess "fatal: missing extension/type: $file\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ s/.*\.(.+)$/$1/ 0 46 46
72 unless $type =~ s/.*\.(.+)$/$1/;
73  
74 46 ▶ if ( $type eq 'sql' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'sql') { } 41 5 46
elsif ($type eq 'pl') { } 5 0 5
75  
76 # TODO add file name to hashrefs
77 41 return _split_sql( $file->slurp_utf8 );
78 }
79 elsif ( $type eq 'pl' ) {
80 5 return [ { $type => $file->slurp_utf8 } ];
81 }
82  
83 0 die "Cannot load file of type '$type': $file";
84 }
85  
86 34 ▶sub run_arrayref {
Subroutine run_arrayref: called CC=5 SCAR=19.9
Pod documented
87 34 my $self = shift;
88 34 my $ref = shift;
89  
90 34 local $self->{ShowErrorStatement} = 1;
91 34 local $self->{RaiseError} = 1;
92  
93 34 $log->debug( 'running ' . scalar @$ref . ' statements' );
94 34 my $i = 1;
95  
96 34 foreach my $cmd (@$ref) {
97 100 ▶ if ( exists $cmd->{sql} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $cmd->{'sql'}) { } 100 0 100
elsif (exists $cmd->{'pl'}) { } 0 0 0
98 100 $self->do( $cmd->{sql} );
99 }
100 elsif ( exists $cmd->{pl} ) {
101 0 $log->debug( "-- _run_cmd\n" . $cmd->{pl} );
102 0 my $tmp = Path::Tiny->tempfile;
103 0 print $tmp $cmd->{pl};
104 0 ▶ system( $^X, $tmp->filename ) == 0 or die "system failed";
Branch: 1 branch
BranchTrueFalseTotal
system($^X, $tmp->filename) == 0 or die "system failed" 0 0 0
105 }
106 else {
107 0 confess "Missing 'sql' or 'pl' key";
108 }
109  
110 100 $i++;
111 }
112  
113 34 return scalar @$ref;
114 }
115  
116 0 ▶sub run_sql {
Subroutine run_sql: not called CC=1 SCAR=6.9
Pod documented
117 0 my $self = shift;
118 0 my $sql = shift;
119  
120 0 $log->debug("run_sql");
121 0 $self->run_arrayref( _split_sql($sql) );
122 }
123  
124 34 ▶sub run_file {
Subroutine run_file: called CC=2 SCAR=7.1
Pod documented
125 34 my $self = shift;
126 34 my $file = shift;
127  
128 34 $log->debug("run_file($file)");
129 34 my $result = eval { $self->run_arrayref( _load_file($file) ) };
130 34 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 34 34
131 0 die "$file\n" . $@;
132 }
133 34 return $result;
134 }
135  
136 6 ▶sub run_dir {
Subroutine run_dir: called CC=7 SCAR=21.0
Pod documented
137 6 my $self = shift;
138 6 ▶ my $dir = path(shift) || confess 'deploy_dir($dir)';
Condition50%1/2
path(shift()) || confess('deploy_dir($dir)')
l!l
60
MC/DC0%0/1
path(shift()) || confess('deploy_dir($dir)')
path(shift())
Truth table50%1/2
A
A= path(shift())
Aresult
11
00
139  
140 6 ▶ confess "directory not found: $dir" unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 6 6
141 6 $log->debug("run_dir($dir)");
142  
143 6 my @files;
144 6 my $iter = $dir->iterator;
145 6 ▶ while ( my $file = $iter->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = &$iter()) 6 6 12
146 6 ▶ push( @files, $file )
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /.+\.((sql)|(pl))$/ and -f $file 6 0 6
Condition33%1/3
$file =~ /.+\.((sql)|(pl))$/ and -f $file
!ll&&!rl&&r
006
MC/DC0%0/2
$file =~ /.+\.((sql)|(pl))$/ and -f $file
$file =~ /.+\.((sql)|(pl))$/ -f $file
Truth table33%1/3
A and B
A= $file =~ /.+\.((sql)|(pl))$/B= -f $file
ABresult
0X0
100
111
147 if $file =~ m/.+\.((sql)|(pl))$/ and -f $file;
148 }
149  
150 0 $self->run_file($_) for sort { $a->stringify cmp $b->stringify } @files;
151 }
152  
153 6 ▶sub _setup_deploy {
Subroutine _setup_deploy: called CC=3 SCAR=14.2
154 6 my $self = shift;
155 6 my $driver = $self->{Driver}->{Name};
156  
157 6 $log->debug("_setup_deploy");
158  
159 6 ▶ if ( defined &static::find ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &static::find) 0 6 6
160 0 my $src = 'auto/share/dist/DBIx-ThinSQL/Deploy/' . $driver . '.sql';
161 0 ▶ my $sql = static::find($src)
Branch: 1 branch
BranchTrueFalseTotal
my $sql = static::find($src) or croak('Driver not supported for deploy: ' . $driver) 0 0 0
162 or croak 'Driver not supported for deploy: ' . $driver;
163 0 return $self->run_sql($sql);
164 }
165  
166 6 return $self->run_dir( $self->share_dir->child( 'Deploy', $driver ) );
167 }
168  
169 12 ▶sub last_deploy_id {
Subroutine last_deploy_id: called CC=3 SCAR=11.0
Pod documented
170 12 my $self = shift;
171 12 ▶ my $app = shift || 'default';
Condition100%2/2
shift() || 'default'
l!l
66
MC/DC100%1/1
shift() || 'default'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
172  
173 12 my $sth = $self->table_info( '%', '%', '_deploy' );
174 12 ▶ return 0 unless ( @{ $sth->fetchall_arrayref } );
Branch: 1 branch
BranchTrueFalseTotal
unless @{$sth->fetchall_arrayref;} 2 10 12
175  
176 10 return $self->selectrow_array(
177 'SELECT COALESCE(MAX(seq),0) FROM _deploy WHERE app=?',
178 undef, $app );
179 }
180  
181 6 ▶sub deploy_arrayref {
Subroutine deploy_arrayref: called CC=13 SCAR=25.9
Pod documented
182 6 my $self = shift;
183 6 my $ref = shift;
184 6 ▶ my $app = shift || 'default';
Condition50%1/2
shift() || 'default'
l!l
06
MC/DC0%0/1
shift() || 'default'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
185  
186 6 ▶ confess 'deploy(ARRAYREF)' unless ref $ref eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ref eq "ARRAY" 0 6 6
187 6 local $self->{ShowErrorStatement} = 1;
188 6 local $self->{RaiseError} = 1;
189  
190 6 my @current =
191 $self->selectrow_array( 'SELECT COUNT(app) from _deploy WHERE app=?',
192 undef, $app );
193  
194 6 ▶ unless ( $current[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($current[0]) 2 4 6
195 2 $self->do( '
196 INSERT INTO _deploy(app)
197 VALUES(?)
198 ', undef, $app );
199 }
200  
201 6 my $latest_change_id = $self->last_deploy_id($app);
202 6 $log->debug( 'Current Change ID:', $latest_change_id );
203 6 $log->debug( 'Requested Change ID:', scalar @$ref );
204  
205 6 ▶ die "Requested Change ID("
Branch: 1 branch
BranchTrueFalseTotal
if @$ref < $latest_change_id 0 6 6
206 . ( scalar @$ref )
207 . ") is less than current: $latest_change_id"
208 if @$ref < $latest_change_id;
209  
210 6 my $count = 0;
211 6 foreach my $cmd (@$ref) {
212 18 $count++;
213 18 ▶ next unless ( $count > $latest_change_id );
Branch: 1 branch
BranchTrueFalseTotal
unless $count > $latest_change_id 10 8 18
214  
215 exists $cmd->{sql}
216 || exists $cmd->{pl}
217 8 ▶ || confess "Missing 'sql' or 'pl' key for id " . $count;
Branch: 1 branch
BranchTrueFalseTotal
exists $cmd->{"sql"} or exists $cmd->{"pl"} or confess(q[Missing 'sql' or 'pl' key for id ] . $count) 8 0 8
Condition66%2/3
exists $cmd->{"sql"} or exists $cmd->{"pl"}
l!l&&r!l&&!r
530
MC/DC0%0/2
exists $cmd->{"sql"} or exists $cmd->{"pl"}
exists $cmd->{"sql"} exists $cmd->{"pl"}
Truth table66%2/3
A or B
A= exists $cmd->{"sql"}B= exists $cmd->{"pl"}
ABresult
1X1
011
000
218  
219 8 ▶ if ( exists $cmd->{sql} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cmd->{"sql"}) 5 3 8
220 5 $log->debug("-- change #$count\n");
221 5 eval { $self->do( $cmd->{sql} ); };
222 5 ▶ die $cmd->{sql} . $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
223 $self->do( "
224 UPDATE
225 _deploy
226 SET
227 type = ?,
228 data = ?
229 WHERE
230 app = ?
231 ",
232 5 undef, 'sql', $cmd->{sql}, $app );
233 }
234  
235 8 ▶ if ( exists $cmd->{pl} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cmd->{"pl"}) 3 5 8
236 3 $log->debug( "# change #$count\n" . $cmd->{pl} );
237 3 my $tmp = Path::Tiny->tempfile;
238 3 $tmp->spew_utf8( $cmd->{pl} );
239  
240 # TODO stop and restart the transaction (if any) around
241 # this
242 3 ▶ system( $^X, $tmp ) == 0 or die "system failed";
Branch: 1 branch
BranchTrueFalseTotal
system($^X, $tmp) == 0 or die "system failed" 3 0 3
243 $self->do( "
244 UPDATE
245 _deploy
246 SET
247 type = ?,
248 data = ?
249 WHERE
250 app = ?
251 ",
252 3 undef, 'pl', $cmd->{pl}, $app );
253 }
254 }
255 6 $log->debug( 'Deployed to Change ID:', $count );
256 6 return ( $latest_change_id, $count );
257 }
258  
259 0 ▶sub deploy_sql {
Subroutine deploy_sql: not called CC=2 SCAR=17.9
Pod documented
260 0 my $self = shift;
261 0 my $sql = shift;
262 0 ▶ my $app = shift || 'default';
Condition0%0/2
shift() || 'default'
l!l
00
MC/DC0%0/1
shift() || 'default'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
263  
264 0 $log->debug("deploy_sql($app)");
265 0 $self->_setup_deploy;
266 0 $self->deploy_arrayref( _split_sql($sql), $app );
267 }
268  
269 2 ▶sub deploy_file {
Subroutine deploy_file: called CC=1 SCAR=0.0
Pod documented
270 2 my $self = shift;
271 2 my $file = shift;
272 2 my $app = shift;
273 2 $log->debug("deploy_file($file)");
274 2 $self->_setup_deploy;
275 2 $self->deploy_arrayref( _load_file($file), $app );
276 }
277  
278 4 ▶sub deploy_dir {
Subroutine deploy_dir: called CC=6 SCAR=18.5
Pod documented
279 4 my $self = shift;
280 4 ▶ my $dir = path(shift) || confess 'deploy_dir($dir)';
Condition50%1/2
path(shift()) || confess('deploy_dir($dir)')
l!l
40
MC/DC0%0/1
path(shift()) || confess('deploy_dir($dir)')
path(shift())
Truth table50%1/2
A
A= path(shift())
Aresult
11
00
281 4 my $app = shift;
282  
283 4 ▶ confess "directory not found: $dir" unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 4 4
284 4 $log->debug("deploy_dir($dir)");
285 4 $self->_setup_deploy;
286  
287 4 my @files;
288 4 my $iter = $dir->iterator;
289 4 ▶ while ( my $file = $iter->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = &$iter()) 10 4 14
290 10 ▶ if ( $file =~ m/.+\.((sql)|(pl))$/ and -f $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /.+\.((sql)|(pl))$/ and -f $file) { } 10 0 10
Condition33%1/3
$file =~ /.+\.((sql)|(pl))$/ and -f $file
!ll&&!rl&&r
0010
MC/DC0%0/2
$file =~ /.+\.((sql)|(pl))$/ and -f $file
$file =~ /.+\.((sql)|(pl))$/ -f $file
Truth table33%1/3
A and B
A= $file =~ /.+\.((sql)|(pl))$/B= -f $file
ABresult
0X0
100
111
291 10 push( @files, $file );
292 }
293 else {
294 0 warn "Cannot deploy file: $file";
295 }
296 }
297  
298 10 my @items = map { @{ _load_file($_) } }
299 4 sort { $a->stringify cmp $b->stringify } @files;
300  
301 4 $self->deploy_arrayref( \@items, $app );
302 }
303  
304 1 ▶sub deployed_table_info {
Subroutine deployed_table_info: called CC=5 SCAR=17.1
Pod documented
305 1 my $self = shift;
306 1 my $dbschema = shift;
307 1 my $driver = $self->{Driver}->{Name};
308  
309 1 ▶ if ( !$dbschema ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbschema) 1 0 1
310 1 ▶ if ( $driver eq 'SQLite' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($driver eq 'SQLite') { } 1 0 1
elsif ($driver eq 'Pg') { } 0 0 0
311 1 $dbschema = 'main';
312 }
313 elsif ( $driver eq 'Pg' ) {
314 0 $dbschema = 'public';
315 }
316 else {
317 0 $dbschema = '%';
318 }
319 }
320  
321 1 my $sth = $self->table_info( '%', $dbschema, '%',
322 "'TABLE','VIEW','GLOBAL TEMPORARY','LOCAL TEMPORARY'" );
323  
324 1 my %tables;
325  
326 1 ▶ while ( my $table = $sth->fetchrow_arrayref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $table = $sth->fetchrow_arrayref) 4 1 5
327 4 my $sth2 = $self->column_info( '%', '%', $table->[2], '%' );
328 4 $tables{ $table->[2] } = $sth2->fetchall_arrayref;
329 }
330  
331 1 return \%tables;
332 }
333  
334 {
335 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
336 2 *{'DBIx::ThinSQL::db::last_deploy_id'} = \&last_deploy_id;
337 2 *{'DBIx::ThinSQL::db::_split_sql'} = \&_split_sql;
338 2 *{'DBIx::ThinSQL::db::_load_file'} = \&_load_file;
339 2 *{'DBIx::ThinSQL::db::run_sql'} = \&run_sql;
340 2 *{'DBIx::ThinSQL::db::run_arrayref'} = \&run_arrayref;
341 2 *{'DBIx::ThinSQL::db::run_file'} = \&run_file;
342 2 *{'DBIx::ThinSQL::db::run_dir'} = \&run_dir;
343 2 *{'DBIx::ThinSQL::db::_setup_deploy'} = \&_setup_deploy;
344 2 *{'DBIx::ThinSQL::db::deploy_arrayref'} = \&deploy_arrayref;
345 2 *{'DBIx::ThinSQL::db::deploy_sql'} = \&deploy_sql;
346 2 *{'DBIx::ThinSQL::db::deploy_file'} = \&deploy_file;
347 2 *{'DBIx::ThinSQL::db::deploy_dir'} = \&deploy_dir;
348 2 *{'DBIx::ThinSQL::db::deployed_table_info'} = \&deployed_table_info;
349 }
350  
351 2 1;
352  
353 __END__
« blib/lib/DBIx/ThinSQL.pm ↑ Summary blib/lib/DBIx/ThinSQL/Driver/Pg.pm »