lib/Test2/Harness/UI/Sync.pm

filter: Statementstmt 6.9% 27 / 390 Branchbran 0.0% 0 / 126 Conditioncond 0.0% 0 / 54 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 18.1% 8 / 44 Podpod 39.2% 11 / 28 totaltotal 6.8% 46 / 673 CC 98 SCAR 47.0
CC
98
Cov
5%
sync
306.0
read_sync
306.0
write_sync
210.0
CRAP 109.4
« lib/App/Yath/Command/recent.pm ↑ Summary lib/Test2/Harness/UI.pm »
1 package Test2::Harness::UI::Sync;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Scope::Guard;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Test2::Harness::Util::JSON qw/encode_json decode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Test2::Harness::UI::UUID qw/uuid_inflate gen_uuid/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 our $VERSION = '0.000150';
12  
13 3 ▶use Test2::Harness::UI::Util::HashBase;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 0 ▶sub run_delta {
Subroutine run_delta: not called CC=7 SCAR=40.3
Pod documented
16 0 my $self = shift;
17 0 my ($dbh_a, $dbh_b) = @_;
18  
19 0 my ($a_runs, $a_all) = $self->_delta_runs($dbh_a);
20 0 my ($b_runs, $b_all) = $self->_delta_runs($dbh_b);
21  
22 # A run that exists on the other side in any state must not be synced,
23 # otherwise we try to insert a duplicate run_id. Only the list of runs to
24 # copy is filtered by status.
25 0 my %all_a = map { ($_ => 1) } @$a_all;
26 0 my %all_b = map { ($_ => 1) } @$b_all;
27 0 my %map_a = map { ($_ => 1) } @$a_runs;
28 0 my %map_b = map { ($_ => 1) } @$b_runs;
29  
30 0 for my $run_id (@$a_runs) {
31 0 ▶ next if $map_b{$run_id} || !$all_b{$run_id};
Branch: 1 branch
BranchTrueFalseTotal
if $map_b{$run_id} or not $all_b{$run_id} 0 0 0
Condition0%0/3
$map_b{$run_id} or not $all_b{$run_id}
l!l&&r!l&&!r
000
MC/DC0%0/2
$map_b{$run_id} or not $all_b{$run_id}
$map_b{$run_id} not $all_b{$run_id}
Truth table0%0/3
A or not(B)
A= $map_b{$run_id}B= not $all_b{$run_id}
ABresult
1X1
011
000
32 0 warn "Run '$run_id' is finished in database A, but is pending, running, or broken in database B, not syncing it.\n";
33 }
34  
35 0 for my $run_id (@$b_runs) {
36 0 ▶ next if $map_a{$run_id} || !$all_a{$run_id};
Branch: 1 branch
BranchTrueFalseTotal
if $map_a{$run_id} or not $all_a{$run_id} 0 0 0
Condition0%0/3
$map_a{$run_id} or not $all_a{$run_id}
l!l&&r!l&&!r
000
MC/DC0%0/2
$map_a{$run_id} or not $all_a{$run_id}
$map_a{$run_id} not $all_a{$run_id}
Truth table0%0/3
A or not(B)
A= $map_a{$run_id}B= not $all_a{$run_id}
ABresult
1X1
011
000
37 0 warn "Run '$run_id' is finished in database B, but is pending, running, or broken in database A, not syncing it.\n";
38 }
39  
40 return {
41 0 missing_in_a => [grep { !$all_a{$_} } @$b_runs],
42 0 missing_in_b => [grep { !$all_b{$_} } @$a_runs],
43 };
44 }
45  
46 0 ▶sub _delta_runs {
Subroutine _delta_runs: not called CC=4 SCAR=30.0
47 0 my $self = shift;
48 0 my ($in) = @_;
49  
50 0 ▶ return ($in, $in) if ref($in) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $in eq "ARRAY" 0 0 0
51  
52 0 my $runs = $self->get_runs($in);
53 0 ▶ return ($runs, $runs) if $in =~ m/\.jsonl$/ && -f $in;
Branch: 1 branch
BranchTrueFalseTotal
if $in =~ /\.jsonl$/ and -f $in 0 0 0
Condition0%0/3
$in =~ /\.jsonl$/ and -f $in
!ll&&!rl&&r
000
MC/DC0%0/2
$in =~ /\.jsonl$/ and -f $in
$in =~ /\.jsonl$/ -f $in
Truth table0%0/3
A and B
A= $in =~ /\.jsonl$/B= -f $in
ABresult
0X0
100
111
54  
55 0 return ($runs, $self->get_runs($in, all => 1));
56 }
57  
58 0 ▶sub sync {
Subroutine sync: not called CC=17 SCAR=57.2
Pod documented
59 0 my $self = shift;
60 0 my %params = @_;
61  
62 0 ▶ my $from_dbh = $params{from_dbh} or croak "Need a DBH to pull data (from_dbh)";
Branch: 1 branch
BranchTrueFalseTotal
my $from_dbh = $params{"from_dbh"} or croak("Need a DBH to pull data (from_dbh)") 0 0 0
63 0 ▶ my $to_dbh = $params{to_dbh} or croak "Need a DBH to push data TO (to_dbh)";
Branch: 1 branch
BranchTrueFalseTotal
my $to_dbh = $params{"to_dbh"} or croak("Need a DBH to push data TO (to_dbh)") 0 0 0
64 0 ▶ my $run_ids = $params{run_ids} or croak "Need a list of run_id's to sync";
Branch: 1 branch
BranchTrueFalseTotal
my $run_ids = $params{"run_ids"} or croak("Need a list of run_id's to sync") 0 0 0
65  
66 0 ▶ my $name = $params{name} // "$from_dbh -> $to_dbh";
Condition0%0/2
$params{"name"} // "$from_dbh -> $to_dbh"
l!l
00
MC/DC0%0/1
$params{"name"} // "$from_dbh -> $to_dbh"
$params{"name"}
Truth table0%0/2
A
A= $params{"name"}
Aresult
11
00
67 0 ▶ my $skip = $params{skip} // {};
Condition0%0/2
$params{"skip"} // {}
l!l
00
MC/DC0%0/1
$params{"skip"} // {}
$params{"skip"}
Truth table0%0/2
A
A= $params{"skip"}
Aresult
11
00
68 0 ▶ my $cache = $params{cache} // {};
Condition0%0/2
$params{"cache"} // {}
l!l
00
MC/DC0%0/1
$params{"cache"} // {}
$params{"cache"}
Truth table0%0/2
A
A= $params{"cache"}
Aresult
11
00
69 0 ▶ my $debug = $params{debug} // 0;
Condition0%0/2
$params{"debug"} // 0
l!l
00
MC/DC0%0/1
$params{"debug"} // 0
$params{"debug"}
Truth table0%0/2
A
A= $params{"debug"}
Aresult
11
00
70  
71 0 ▶ my $from_uuidf = $params{from_uuid_format} // 'binary';
Condition0%0/2
$params{"from_uuid_format"} // "binary"
l!l
00
MC/DC0%0/1
$params{"from_uuid_format"} // "binary"
$params{"from_uuid_format"}
Truth table0%0/2
A
A= $params{"from_uuid_format"}
Aresult
11
00
72 0 ▶ my $to_uuidf = $params{to_uuid_format} // 'binary';
Condition0%0/2
$params{"to_uuid_format"} // "binary"
l!l
00
MC/DC0%0/1
$params{"to_uuid_format"} // "binary"
$params{"to_uuid_format"}
Truth table0%0/2
A
A= $params{"to_uuid_format"}
Aresult
11
00
73  
74 0 my ($rh, $wh);
75 0 ▶ pipe($rh, $wh) or die "Could not open pipe: $!";
Branch: 1 branch
BranchTrueFalseTotal
pipe $rh, $wh or die "Could not open pipe: $!" 0 0 0
76 0 $wh->autoflush(1);
77  
78 0 ▶ my $pid = fork // die "Could not fork: $!";
Condition0%0/2
fork // die("Could not fork: $!")
l!l
00
MC/DC0%0/1
fork // die("Could not fork: $!")
fork
Truth table0%0/2
A
A= fork
Aresult
11
00
79 0 ▶ unless ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 0 0
80 0 close($wh);
81  
82 # Every handle open at fork time, including from_dbh and to_dbh, was
83 # inherited from the parent and still belongs to it. Destroying them
84 # here would close the parent's connections, so the loader works on
85 # its own connection to the destination instead.
86 0 $self->_inactivate_inherited_handles();
87  
88 0 ▶ my $guard = Scope::Guard->new(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
89 0 warn "Scope Leak";
90 0 exit 255;
91 0 });
92  
93 0 my $ok = eval {
94 0 ▶ my $dbh = $to_dbh->clone() or die "Could not connect to the destination database: " . $DBI::errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $to_dbh->clone or die 'Could not connect to the destination database: ' . $DBI::errstr 0 0 0
95 0 $dbh->{InactiveDestroy} = 0; # clone() copies it from the inherited handle
96  
97 0 $self->read_sync(
98 dbh => $dbh,
99 run_ids => $run_ids,
100 rh => $rh,
101 cache => $cache,
102 debug => $debug,
103 uuidf => $to_uuidf,
104 );
105  
106 0 $dbh->disconnect();
107 0 1;
108 };
109 0 my $err = $@;
110  
111 0 $guard->dismiss();
112  
113 0 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
114 0 warn "[Loader] $name failed: $err";
115 0 exit 255;
116 }
117  
118 0 exit 0;
119 }
120  
121 0 close($rh);
122  
123 0 my ($ok, $err);
124 {
125 # If the loader dies early our writes fail with EPIPE instead of the
126 # signal killing us, so the loader's failure can still be reported.
127 0 local $SIG{PIPE} = 'IGNORE';
128  
129 0 $ok = eval {
130 0 $self->write_sync(
131 dbh => $from_dbh,
132 run_ids => $run_ids,
133 wh => $wh,
134 skip => $skip,
135 debug => $debug,
136 uuidf => $from_uuidf,
137 );
138 0 1;
139 };
140 0 $err = $@;
141  
142 0 close($wh);
143 }
144  
145 0 ▶ die "Loader exited badly" if $self->wait_on($pid => "[Loader] $name");
Branch: 1 branch
BranchTrueFalseTotal
if $self->wait_on($pid, "[Loader] $name") 0 0 0
146 0 ▶ die $err unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
147  
148 0 return;
149 }
150  
151 0 ▶sub _inactivate_inherited_handles {
Subroutine _inactivate_inherited_handles: not called CC=4 SCAR=30.0
152 0 my %drivers = DBI->installed_drivers();
153 0 for my $drh (values %drivers) {
154 0 ▶ for my $dbh (grep { defined } @{$drh->{ChildHandles} // []}) {
Condition0%0/2
$drh->{"ChildHandles"} // []
l!l
00
MC/DC0%0/1
$drh->{"ChildHandles"} // []
$drh->{"ChildHandles"}
Truth table0%0/2
A
A= $drh->{"ChildHandles"}
Aresult
11
00
155 0 $dbh->{InactiveDestroy} = 1;
156 }
157 }
158 }
159  
160 0 ▶sub wait_on {
Subroutine wait_on: not called CC=5 SCAR=34.0
Pod undocumented
161 0 my $self = shift;
162 0 my ($pid, $desc) = @_;
163  
164 0 my $out = 0;
165  
166 0 my $check = waitpid($pid, 0);
167 0 my $exit = $?;
168 0 ▶ if ($check != $pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check != $pid) 0 0 0
169 0 warn "$desc waitpid failed: $check (exit: $?)";
170 0 ▶ return $exit || 1;
Condition0%0/2
$exit || 1
l!l
00
MC/DC0%0/1
$exit || 1
$exit
Truth table0%0/2
A
A= $exit
Aresult
11
00
171 }
172  
173 0 ▶ return 0 unless $exit;
Branch: 1 branch
BranchTrueFalseTotal
unless $exit 0 0 0
174  
175 0 ▶ if (my $sig = $exit & 127) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sig = $exit & 127) { } 0 0 0
176 0 warn "$desc was killed by signal $sig\n";
177 }
178 else {
179 0 warn "$desc exited badly: " . ($exit >> 8) . "\n";
180 }
181  
182 0 return $exit;
183 }
184  
185 0 ▶sub get_runs {
Subroutine get_runs: not called CC=3 SCAR=24.8
Pod documented
186 0 my $self = shift;
187 0 my ($dbh_or_file, %params) = @_;
188  
189 0 ▶ return $self->_get_jsonl_runs($dbh_or_file)
Branch: 1 branch
BranchTrueFalseTotal
if $dbh_or_file =~ /\.jsonl$/ and -f $dbh_or_file 0 0 0
Condition0%0/3
$dbh_or_file =~ /\.jsonl$/ and -f $dbh_or_file
!ll&&!rl&&r
000
MC/DC0%0/2
$dbh_or_file =~ /\.jsonl$/ and -f $dbh_or_file
$dbh_or_file =~ /\.jsonl$/ -f $dbh_or_file
Truth table0%0/3
A and B
A= $dbh_or_file =~ /\.jsonl$/B= -f $dbh_or_file
ABresult
0X0
100
111
190 if $dbh_or_file =~ m/\.jsonl$/ && -f $dbh_or_file;
191  
192 0 return $self->_get_dbh_runs($dbh_or_file, %params);
193 }
194  
195 0 ▶sub _get_jsonl_runs {
Subroutine _get_jsonl_runs: not called CC=4 SCAR=30.0
196 0 my $self = shift;
197 0 my ($file) = @_;
198  
199 0 my $runs = [];
200  
201 0 ▶ open(my $fh, '<', $file) or croak "Could not open file '$file' for reading: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or croak("Could not open file '${file}' for reading: $!") 0 0 0
202 0 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
203 # We only care about lines that start with this, the format does not allow this to false-positive
204 0 ▶ next unless $line =~ m/\{"run":\{.*"run_id":"([^"]+)"/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\{"run":\{.*"run_id":"([^"]+)"/ 0 0 0
205 0 push @$runs => $1;
206 }
207 0 close($fh);
208  
209 0 return $runs;
210 }
211  
212 0 ▶sub _get_dbh_runs {
Subroutine _get_dbh_runs: not called CC=4 SCAR=30.0
213 0 my $self = shift;
214 0 my ($dbh, %params) = @_;
215  
216 0 ▶ my $where = $params{all} ? '' : "WHERE status NOT IN ('pending', 'running', 'broken')";
Branch: 1 branch
BranchTrueFalseTotal
$params{'all'} ? : 0 0 0
217  
218 0 my $sth = $dbh->prepare(<<" EOT");
219 SELECT run_id
220 FROM runs
221 $where
222 ORDER BY added ASC
223 EOT
224  
225 0 ▶ $sth->execute() or die "MySQL Error: " . $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die 'MySQL Error: ' . $dbh->errstr 0 0 0
226  
227 0 my @out;
228  
229 0 ▶ while (my $run = $sth->fetchrow_arrayref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $run = $sth->fetchrow_arrayref) 0 0 0
230 0 push @out => uuid_inflate($run->[0])->string;
231 }
232  
233 0 return \@out;
234 }
235  
236 0 ▶sub table_list { qw/runs run_fields jobs job_fields events binaries reporting coverage/ }
Subroutine table_list: not called CC=1 SCAR=6.9
Pod documented
237  
238 0 ▶sub write_sync {
Subroutine write_sync: not called CC=14 SCAR=53.5
Pod documented
239 0 my $self = shift;
240 0 my %params = @_;
241  
242 0 ▶ my $dbh = $params{dbh} or croak "'dbh' is required";
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $params{"dbh"} or croak("'dbh' is required") 0 0 0
243 0 ▶ my $run_ids = $params{run_ids} or croak "'run_ids' must be an arrayref of run ids";
Branch: 1 branch
BranchTrueFalseTotal
my $run_ids = $params{"run_ids"} or croak("'run_ids' must be an arrayref of run ids") 0 0 0
244 0 ▶ my $wh = $params{wh} or croak "'wh' is required and must be a writable filehandle";
Branch: 1 branch
BranchTrueFalseTotal
my $wh = $params{"wh"} or croak("'wh' is required and must be a writable filehandle") 0 0 0
245 0 ▶ my $uuidf = $params{uuidf} // 'binary';
Condition0%0/2
$params{"uuidf"} // "binary"
l!l
00
MC/DC0%0/1
$params{"uuidf"} // "binary"
$params{"uuidf"}
Truth table0%0/2
A
A= $params{"uuidf"}
Aresult
11
00
246 0 ▶ my $skip = $params{skip} // {};
Condition0%0/2
$params{"skip"} // {}
l!l
00
MC/DC0%0/1
$params{"skip"} // {}
$params{"skip"}
Truth table0%0/2
A
A= $params{"skip"}
Aresult
11
00
247 0 ▶ my $debug = $params{debug} // 0;
Condition0%0/2
$params{"debug"} // 0
l!l
00
MC/DC0%0/1
$params{"debug"} // 0
$params{"debug"}
Truth table0%0/2
A
A= $params{"debug"}
Aresult
11
00
248  
249 0 my @to_dump;
250 0 for my $table ($self->table_list) {
251 0 ▶ next if $skip->{$table};
Branch: 1 branch
BranchTrueFalseTotal
if $skip->{$table} 0 0 0
252 0 push @to_dump => "render_${table}";
253 }
254  
255 0 $wh->autoflush(1);
256  
257 0 STDOUT->autoflush(1);
258  
259 0 my $total = @$run_ids;
260 0 my $counter = 0;
261 0 my $subcount = 0;
262 0 for my $run_id (@$run_ids) {
263 0 my $run_uuid = uuid_inflate($run_id)->$uuidf;
264 0 my @args = ($dbh, $run_uuid, $skip);
265  
266 0 for my $meth (@to_dump) {
267 0 for my $item ($self->$meth(@args)) {
268 0 $subcount++;
269 0 my ($key) = keys(%$item);
270 0 my $line = encode_json($item);
271 0 ▶ print $wh $line, "\n" or die "Could not write to the loader: $!";
Branch: 1 branch
BranchTrueFalseTotal
print $wh $line, "\n" or die "Could not write to the loader: $!" 0 0 0
272 }
273 }
274  
275 0 $counter++;
276 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
277 0 print " Dumped run $counter/$total: $run_id\n";
278 }
279 }
280  
281 0 return;
282 }
283  
284 0 ▶sub read_sync {
Subroutine read_sync: not called CC=17 SCAR=57.2
Pod documented
285 0 my $self = shift;
286 0 my %params = @_;
287  
288 0 ▶ my $dbh = $params{dbh} or croak "'dbh' is required";
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $params{"dbh"} or croak("'dbh' is required") 0 0 0
289 0 ▶ my $run_ids = $params{run_ids} or croak "'run_ids' must be an arrayref of run ids";
Branch: 1 branch
BranchTrueFalseTotal
my $run_ids = $params{"run_ids"} or croak("'run_ids' must be an arrayref of run ids") 0 0 0
290 0 ▶ my $rh = $params{rh} or croak "'rh' is required and must be a readable filehandle";
Branch: 1 branch
BranchTrueFalseTotal
my $rh = $params{"rh"} or croak("'rh' is required and must be a readable filehandle") 0 0 0
291 0 ▶ my $uuidf = $params{uuidf} // 'binary';
Condition0%0/2
$params{"uuidf"} // "binary"
l!l
00
MC/DC0%0/1
$params{"uuidf"} // "binary"
$params{"uuidf"}
Truth table0%0/2
A
A= $params{"uuidf"}
Aresult
11
00
292 0 ▶ my $cache = $params{cache} // {};
Condition0%0/2
$params{"cache"} // {}
l!l
00
MC/DC0%0/1
$params{"cache"} // {}
$params{"cache"}
Truth table0%0/2
A
A= $params{"cache"}
Aresult
11
00
293 0 ▶ my $debug = $params{debug} // 0;
Condition0%0/2
$params{"debug"} // 0
l!l
00
MC/DC0%0/1
$params{"debug"} // 0
$params{"debug"}
Truth table0%0/2
A
A= $params{"debug"}
Aresult
11
00
294  
295 0 my $auto_commit = $dbh->{AutoCommit};
296 0 $dbh->{AutoCommit} = 0;
297  
298 0 my %include = map {($_ => 1)} @$run_ids;
299 0 my $total = @$run_ids;
300 0 my $counter = 0;
301 0 my $last_run_id;
302 my $broken;
303  
304 0 ▶ my $report = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
305 0 ▶ return unless $debug && $last_run_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $debug and $last_run_id 0 0 0
Condition0%0/3
$debug and $last_run_id
!ll&&!rl&&r
000
MC/DC0%0/2
$debug and $last_run_id
$debug $last_run_id
Truth table0%0/3
A and B
A= $debugB= $last_run_id
ABresult
0X0
100
111
306  
307 0 ▶ if ($broken) {
Branch: 1 branch
BranchTrueFalseTotal
if ($broken) { } 0 0 0
308 0 print " BROKEN run $counter/$total: $last_run_id\n";
309 }
310 else {
311 0 print "Imported run $counter/$total: $last_run_id\n";
312 }
313 0 };
314  
315 0 ▶ while (my $line = <$rh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $rh)) 0 0 0
316 0 my $data = decode_json($line);
317  
318 0 my ($type, @bad) = keys %$data;
319 0 ▶ die "Invalid data!" if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 0 0 0
320  
321 0 ▶ if ($type eq 'run') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "run") 0 0 0
322 0 $dbh->commit();
323 0 $dbh->{AutoCommit} = 0;
324  
325 0 $report->();
326  
327 0 $broken = undef;
328 0 my $new_run_id = $data->{$type}->{run_id};
329  
330 0 ▶ if ($new_run_id && !$include{$new_run_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_run_id and not $include{$new_run_id}) 0 0 0
Condition0%0/3
$new_run_id and not $include{$new_run_id}
!ll&&!rl&&r
000
MC/DC0%0/2
$new_run_id and not $include{$new_run_id}
$new_run_id not $include{$new_run_id}
Truth table0%0/3
A and not(B)
A= $new_run_idB= not $include{$new_run_id}
ABresult
0X0
100
111
331 0 $last_run_id = undef;
332 0 next;
333 }
334  
335 # The run may have been added to the destination after the delta
336 # was calculated, never overwrite or duplicate it.
337 0 ▶ if ($new_run_id && $self->run_exists($dbh, $uuidf, $new_run_id)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_run_id and $self->run_exists($dbh, $uuidf, $new_run_id)) 0 0 0
Condition0%0/3
$new_run_id and $self->run_exists($dbh, $uuidf, $new_run_id)
!ll&&!rl&&r
000
MC/DC0%0/2
$new_run_id and $self->run_exists($dbh, $uuidf, $new_run_id)
$new_run_id $self->run_exists($dbh, $uuidf, $new_run_id)
Truth table0%0/3
A and B
A= $new_run_idB= $self->run_exists($dbh, $uuidf, $new_run_id)
ABresult
0X0
100
111
338 0 warn "Run '$new_run_id' already exists in the destination database, skipping it.\n";
339 0 $last_run_id = undef;
340 0 next;
341 }
342  
343 0 $last_run_id = $new_run_id;
344 0 $counter++;
345 }
346  
347 0 ▶ next if $broken;
Branch: 1 branch
BranchTrueFalseTotal
if $broken 0 0 0
348 0 ▶ next unless $last_run_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $last_run_id 0 0 0
349  
350 0 my $method = "import_$type";
351  
352 0 ▶ next if eval {
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $self->$method("dbh", $dbh, "item", $data->{$type}, "uuidf", $uuidf, "cache", $cache); 1 } } 0 0 0
353 0 $self->$method(dbh => $dbh, item => $data->{$type}, uuidf => $uuidf, cache => $cache);
354 0 1;
355 };
356  
357 0 $dbh->rollback();
358 0 $broken = $last_run_id;
359 }
360  
361 0 $dbh->commit();
362 0 $report->();
363  
364 0 $dbh->{AutoCommit} = $auto_commit;
365  
366 0 return;
367 }
368  
369 0 ▶sub run_exists {
Subroutine run_exists: not called CC=3 SCAR=24.8
Pod documented
370 0 my $self = shift;
371 0 my ($dbh, $uuidf, $run_id) = @_;
372  
373 0 my $sth = $dbh->prepare("SELECT 1 FROM runs WHERE run_id = ?");
374 0 ▶ $sth->execute(uuid_inflate($run_id)->$uuidf) or die "MySQL Error: " . $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(uuid_inflate($run_id)->$uuidf) or die 'MySQL Error: ' . $dbh->errstr 0 0 0
375 0 ▶ return $sth->fetchrow_arrayref() ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$sth->fetchrow_arrayref ? : 0 0 0
376 }
377  
378 0 ▶sub get_or_create_id {
Subroutine get_or_create_id: not called CC=3 SCAR=24.8
Pod documented
379 0 my $self = shift;
380 0 my ($cache, $dbh, $uuidf, $table, $field, $via_field, $via_value) = @_;
381  
382 0 ▶ return undef unless $via_value;
Branch: 1 branch
BranchTrueFalseTotal
unless $via_value 0 0 0
383  
384 0 ▶ return $cache->{$table}{$via_field}{$via_value}{$field} //= $self->_get_or_create_id(@_);
Condition0%0/3
$cache->{$table}{$via_field}{$via_value}{$field} //= $self->_get_or_create_id(@_)
l!l&&r!l&&!r
000
MC/DC0%0/2
$cache->{$table}{$via_field}{$via_value}{$field} //= $self->_get_or_create_id(@_)
$cache->{$table}{$via_field}{$via_value}{$field} $self->_get_or_create_id(@_)
Truth table0%0/3
A //= B
A= $cache->{$table}{$via_field}{$via_value}{$field}B= $self->_get_or_create_id(@_)
ABresult
1X1
011
000
385 }
386  
387 0 ▶sub _get_or_create_id {
Subroutine _get_or_create_id: not called CC=3 SCAR=24.8
388 0 my $self = shift;
389 0 my ($cache, $dbh, $uuidf, $table, $field, $via_field, $via_value) = @_;
390  
391 0 my $sql = "SELECT $field FROM $table WHERE $via_field = ?";
392  
393 0 my $sth = $dbh->prepare($sql);
394 0 ▶ $sth->execute($via_value) or die "MySQL Error: " . $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($via_value) or die 'MySQL Error: ' . $dbh->errstr 0 0 0
395 0 ▶ if ($sth->rows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->rows) 0 0 0
396 0 my $row = $sth->fetchrow_hashref();
397 0 return uuid_inflate($row->{$field})->string;
398 }
399  
400 0 my $uuid = gen_uuid();
401 0 $self->insert($dbh, $uuidf, $table, {$field => $uuid, $via_field => $via_value});
402 0 return $uuid->string;
403 }
404  
405 0 ▶sub insert {
Subroutine insert: not called CC=3 SCAR=24.8
Pod documented
406 0 my $self = shift;
407 0 my ($dbh, $uuidf, $table, $data) = @_;
408  
409 0 _fix_uuids($uuidf => $data);
410  
411 0 my $sql = "INSERT INTO $table(";
412 0 my (@fields, @vars);
413 0 for my $key (keys %$data) {
414 0 push @fields => $key;
415 0 push @vars => $data->{$key};
416 }
417 0 $sql .= join(',', @fields) . ') VALUES(' . join(',', map { '?' } @vars) . ')';
418  
419 0 my $sth = $dbh->prepare($sql);
420 0 ▶ $sth->execute(@vars) or die "Insert failed: " . $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(@vars) or die 'Insert failed: ' . $dbh->errstr 0 0 0
421 }
422  
423 0 ▶sub stringify_uuids {
Subroutine stringify_uuids: not called CC=1 SCAR=6.9
Pod documented
424 0 my ($in) = @_;
425 0 _fix_uuids(string => $in);
426 }
427  
428 0 ▶sub binarify_uuids {
Subroutine binarify_uuids: not called CC=1 SCAR=6.9
Pod documented
429 0 my ($in) = @_;
430 0 _fix_uuids(binary => $in);
431 }
432  
433 3 my @ID_FIELDS = qw{
434 coverage_id
435 coverage_manager_id
436 event_id
437 job_field_id
438 job_id
439 job_key
440 parent_id
441 project_id
442 reporting_id
443 run_field_id
444 run_id
445 source_file_id
446 source_sub_id
447 test_file_id
448 user_id
449 };
450  
451 0 ▶sub _fix_uuids {
Subroutine _fix_uuids: not called CC=9 SCAR=45.0
452 0 my ($method => $in) = @_;
453 0 ▶ return unless $in;
Branch: 1 branch
BranchTrueFalseTotal
unless $in 0 0 0
454 0 my $type = ref($in);
455  
456 0 ▶ if (!$type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 0 0 0
457 0 return uuid_inflate($in)->$method;
458 }
459 0 ▶ if ($type eq 'Test2::Harness::UI::UUID') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "Test2::Harness::UI::UUID") 0 0 0
460 0 return $in->$method;
461 }
462 0 ▶ if ($type eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 0 0 0
elsif ($type eq 'ARRAY') { } 0 0 0
463 # Cannot do all_id or _key fields, some are not uuids...
464 # This is a list of safe ones
465 0 for my $key (@ID_FIELDS) {
466 0 ▶ next unless exists $in->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $in->{$key} 0 0 0
467 0 $in->{$key} = _fix_uuids($method, $in->{$key});
468 }
469 }
470 elsif($type eq 'ARRAY') {
471 0 _fix_uuids($method, $_) for @$in;
472 }
473 else {
474 0 die "Unsupported type '$type' '$in'";
475 }
476  
477 0 return $in;
478 }
479  
480 0 ▶sub render_runs {
Subroutine render_runs: not called CC=3 SCAR=24.8
Pod undocumented
481 0 my $self = shift;
482 0 my ($dbh, $run_id, $skip) = @_;
483  
484 0 my $sth = $dbh->prepare(<<" EOT");
485 SELECT
486 run_id, status, worker_id, error, added, duration, mode, buffer,
487 passed, failed, retried, concurrency, parameters, has_coverage,
488 users.username, projects.name as project_name
489 FROM runs
490 JOIN users USING(user_id)
491 JOIN projects USING(project_id)
492 WHERE run_id = ?
493 EOT
494  
495 0 ▶ $sth->execute($run_id) or die "MySQL Error: " . $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($run_id) or die 'MySQL Error: ' . $dbh->errstr 0 0 0
496  
497 0 my $run = stringify_uuids($sth->fetchrow_hashref());
498 0 ▶ delete $run->{has_coverage} if $skip->{coverage};
Branch: 1 branch
BranchTrueFalseTotal
if $skip->{"coverage"} 0 0 0
499  
500 0 return {run => $run};
501 }
502  
503 0 ▶sub render_run_fields {
Subroutine render_run_fields: not called CC=2 SCAR=17.9
Pod undocumented
504 0 my $self = shift;
505 0 my ($dbh, $run_id) = @_;
506  
507 0 my $sth = $dbh->prepare(<<" EOT");
508 SELECT run_id, run_field_id, name, data, details, raw, link
509 FROM run_fields
510 WHERE run_id = ?
511 EOT
512 0 ▶ $sth->execute($run_id) or die "MySQL Error: " . $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($run_id) or die 'MySQL Error: ' . $dbh->errstr 0 0 0
513 0 my $run_fields = stringify_uuids($sth->fetchall_arrayref({}));
514  
515 0 return map { +{run_field => $_} } @$run_fields;
516 }
517  
518 0 ▶sub render_jobs {
Subroutine render_jobs: not called CC=2 SCAR=17.9
Pod undocumented
519 0 my $self = shift;
520 0 my ($dbh, $run_id) = @_;
521  
522 0 my $sth = $dbh->prepare(<<" EOT");
523 SELECT
524 run_id, job_key, job_id, job_try, job_ord, is_harness_out, status, parameters, fields, name, fail, retry,
525 exit_code, launch, start, ended, duration, pass_count, fail_count,
526 test_files.filename
527 FROM jobs
528 JOIN test_files USING (test_file_id)
529 WHERE run_id = ?
530 EOT
531  
532 0 ▶ $sth->execute($run_id) or die "MySQL Error: " . $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($run_id) or die 'MySQL Error: ' . $dbh->errstr 0 0 0
533 0 my $jobs = stringify_uuids($sth->fetchall_arrayref({}));
534 0 return map { +{job => $_} } @$jobs;
535 }
536  
537 0 ▶sub render_job_fields {
Subroutine render_job_fields: not called CC=2 SCAR=17.9
Pod undocumented
538 0 my $self = shift;
539 0 my ($dbh, $run_id) = @_;
540  
541 0 my $sth = $dbh->prepare(<<" EOT");
542 SELECT job_field_id, job_key, job_fields.name as name, data, details, raw, link
543 FROM job_fields
544 JOIN jobs USING(job_key)
545 WHERE run_id = ?
546 EOT
547  
548 0 ▶ $sth->execute($run_id) or die "MySQL Error: " . $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($run_id) or die 'MySQL Error: ' . $dbh->errstr 0 0 0
549 0 my $job_fields = stringify_uuids($sth->fetchall_arrayref({}));
550 0 return map { +{job_field => $_} } @$job_fields;
551 }
552  
553 0 ▶sub render_events {
Subroutine render_events: not called CC=2 SCAR=17.9
Pod undocumented
554 0 my $self = shift;
555 0 my ($dbh, $run_id) = @_;
556  
557 0 my $sth = $dbh->prepare(<<" EOT");
558 SELECT events.*
559 FROM events
560 JOIN jobs USING(job_key)
561 WHERE run_id = ?
562 AND is_subtest = TRUE
563 EOT
564  
565 0 ▶ $sth->execute($run_id) or die "MySQL Error: " . $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($run_id) or die 'MySQL Error: ' . $dbh->errstr 0 0 0
566 0 my $events = stringify_uuids($sth->fetchall_arrayref({}));
567 0 return map { +{event => $_} } @$events;
568 }
569  
570 0 ▶sub render_binaries {
Subroutine render_binaries: not called CC=2 SCAR=17.9
Pod undocumented
571 0 my $self = shift;
572 0 my ($dbh, $run_id) = @_;
573  
574 0 my $sth = $dbh->prepare(<<" EOT");
575 SELECT binaries.*
576 FROM binaries
577 JOIN events USING(event_id)
578 JOIN jobs USING(job_key)
579 WHERE run_id = ?
580 AND is_subtest = TRUE
581 EOT
582  
583 0 ▶ $sth->execute($run_id) or die "MySQL Error: " . $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($run_id) or die 'MySQL Error: ' . $dbh->errstr 0 0 0
584 0 my $binaries = stringify_uuids($sth->fetchall_arrayref({}));
585 0 return map { +{binary => $_} } @$binaries;
586 }
587  
588 0 ▶sub render_reporting {
Subroutine render_reporting: not called CC=2 SCAR=17.9
Pod undocumented
589 0 my $self = shift;
590 0 my ($dbh, $run_id) = @_;
591  
592 0 my $sth = $dbh->prepare(<<" EOT");
593 SELECT
594 reporting_id, run_id, run_ord, job_try, subtest, duration, fail, pass, retry, abort, job_key, event_id,
595 projects.name AS project_name,
596 users.username AS username,
597 test_files.filename AS filename
598 FROM reporting
599 JOIN projects USING(project_id)
600 JOIN users USING(user_id)
601 JOIN test_files USING(test_file_id)
602 WHERE run_id = ?
603 EOT
604 0 ▶ $sth->execute($run_id) or die "MySQL Error: " . $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($run_id) or die 'MySQL Error: ' . $dbh->errstr 0 0 0
605 0 my $reporting = stringify_uuids($sth->fetchall_arrayref({}));
606 0 return map { +{reporting => $_} } @$reporting;
607 }
608  
609 0 ▶sub render_coverage {
Subroutine render_coverage: not called CC=2 SCAR=17.9
Pod undocumented
610 0 my $self = shift;
611 0 my ($dbh, $run_id) = @_;
612  
613 0 my $sth = $dbh->prepare(<<" EOT");
614 SELECT
615 coverage_id, run_id, job_key, metadata,
616 test_files.filename AS test_file,
617 source_files.filename AS source_file,
618 source_subs.subname AS source_sub,
619 coverage_manager.package AS coverage_manager
620 FROM coverage
621 JOIN test_files USING(test_file_id)
622 JOIN source_files USING(source_file_id)
623 JOIN source_subs USING(source_sub_id)
624 JOIN coverage_manager USING(coverage_manager_id)
625 WHERE run_id = ?
626 EOT
627  
628 0 ▶ $sth->execute($run_id) or die "MySQL Error: " . $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($run_id) or die 'MySQL Error: ' . $dbh->errstr 0 0 0
629 0 my $coverage = stringify_uuids($sth->fetchall_arrayref({}));
630 0 return map { +{coverage => $_} } @$coverage;
631 }
632  
633 0 ▶sub import_run {
Subroutine import_run: not called CC=1 SCAR=6.9
Pod undocumented
634 0 my $self = shift;
635 0 my %params = @_;
636  
637 0 my $dbh = $params{dbh};
638 0 my $uuidf = $params{uuidf};
639 0 my $cache = $params{cache};
640 0 my $run = $params{item};
641  
642 0 $run->{user_id} = $self->get_or_create_id($cache, $dbh, $uuidf, 'users' => 'user_id', username => delete $run->{username});
643 0 $run->{project_id} = $self->get_or_create_id($cache, $dbh, $uuidf, 'projects' => 'project_id', name => delete $run->{project_name});
644  
645 0 $self->insert($dbh, $uuidf, runs => $run);
646 }
647  
648 0 ▶sub import_run_field {
Subroutine import_run_field: not called CC=1 SCAR=6.9
Pod undocumented
649 0 my $self = shift;
650 0 my %params = @_;
651  
652 0 my $dbh = $params{dbh};
653 0 my $uuidf = $params{uuidf};
654 0 my $run_field = $params{item};
655  
656 0 $self->insert($dbh, $uuidf, run_fields => $run_field);
657 }
658  
659 0 ▶sub import_job {
Subroutine import_job: not called CC=1 SCAR=6.9
Pod undocumented
660 0 my $self = shift;
661 0 my %params = @_;
662  
663 0 my $dbh = $params{dbh};
664 0 my $uuidf = $params{uuidf};
665 0 my $cache = $params{cache};
666 0 my $job = $params{item};
667  
668 0 $job->{test_file_id} = $self->get_or_create_id($cache, $dbh, $uuidf, 'test_files' => 'test_file_id', filename => delete $job->{filename});
669  
670 0 $self->insert($dbh, $uuidf, jobs => $job);
671 }
672  
673 0 ▶sub import_job_field {
Subroutine import_job_field: not called CC=1 SCAR=6.9
Pod undocumented
674 0 my $self = shift;
675 0 my %params = @_;
676  
677 0 my $dbh = $params{dbh};
678 0 my $uuidf = $params{uuidf};
679 0 my $job_field = $params{item};
680  
681 0 $self->insert($dbh, $uuidf, job_fields => $job_field);
682 }
683  
684 0 ▶sub import_event {
Subroutine import_event: not called CC=1 SCAR=6.9
Pod undocumented
685 0 my $self = shift;
686 0 my %params = @_;
687  
688 0 my $dbh = $params{dbh};
689 0 my $uuidf = $params{uuidf};
690 0 my $event = $params{item};
691  
692 0 $self->insert($dbh, $uuidf, events => $event);
693 }
694  
695 0 ▶sub import_binary {
Subroutine import_binary: not called CC=1 SCAR=6.9
Pod undocumented
696 0 my $self = shift;
697 0 my %params = @_;
698  
699 0 my $dbh = $params{dbh};
700 0 my $uuidf = $params{uuidf};
701 0 my $binary = $params{item};
702  
703 0 $self->insert($dbh, $uuidf, binaries => $binary);
704 }
705  
706 0 ▶sub import_reporting {
Subroutine import_reporting: not called CC=1 SCAR=6.9
Pod undocumented
707 0 my $self = shift;
708 0 my %params = @_;
709  
710 0 my $dbh = $params{dbh};
711 0 my $uuidf = $params{uuidf};
712 0 my $cache = $params{cache};
713 0 my $reporting = $params{item};
714  
715 0 $reporting->{project_id} = $self->get_or_create_id($cache, $dbh, $uuidf, 'projects' => 'project_id', name => delete $reporting->{project_name});
716 0 $reporting->{user_id} = $self->get_or_create_id($cache, $dbh, $uuidf, 'users' => 'user_id', username => delete $reporting->{username});
717 0 $reporting->{test_file_id} = $self->get_or_create_id($cache, $dbh, $uuidf, 'test_files' => 'test_file_id', filename => delete $reporting->{filename});
718  
719 0 $self->insert($dbh, $uuidf, reporting => $reporting);
720 }
721  
722 0 ▶sub import_coverage {
Subroutine import_coverage: not called CC=1 SCAR=6.9
Pod undocumented
723 0 my $self = shift;
724 0 my %params = @_;
725  
726 0 my $dbh = $params{dbh};
727 0 my $uuidf = $params{uuidf};
728 0 my $cache = $params{cache};
729 0 my $coverage = $params{item};
730  
731 0 $coverage->{test_file_id} = $self->get_or_create_id($cache, $dbh, $uuidf, 'test_files' => 'test_file_id', filename => delete $coverage->{test_file});
732 0 $coverage->{source_file_id} = $self->get_or_create_id($cache, $dbh, $uuidf, 'source_files' => 'source_file_id', filename => delete $coverage->{source_file});
733 0 $coverage->{source_sub_id} = $self->get_or_create_id($cache, $dbh, $uuidf, 'source_subs' => 'source_sub_id', subname => delete $coverage->{source_sub});
734 0 $coverage->{coverage_manager_id} = $self->get_or_create_id($cache, $dbh, $uuidf, 'coverage_manager' => 'coverage_manager_id', package => delete $coverage->{coverage_manager});
735  
736 0 $self->insert($dbh, $uuidf, coverage => $coverage);
737 }
738  
739 3 1;
740  
741 __END__
« lib/App/Yath/Command/recent.pm ↑ Summary lib/Test2/Harness/UI.pm »