Sims/Runner.pm

filter: Statementstmt 96.0% 194 / 202 Branchbran 85.0% 51 / 60 Conditioncond 73.9% 17 / 23 MC/DCmcdc 57.1% 8 / 14 Subroutinesub 92.1% 35 / 38 Podpod 0.0% 0 / 21 totaltotal 85.2% 305 / 358 CC 47 SCAR 20.6
CC
47
Cov
92%
__ANON__
20.1
are_columns_equal
8.7
convert_backreference
8.3
CRAP 7.9
« Sims/Item.pm ↑ Summary Sims/Types.pm »
1 package DBIx::Class::Sims::Runner;
2  
3 54 ▶use 5.010_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 54 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 54 ▶use DDP;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 54 ▶use Data::Compare qw( Compare );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 54 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 54 ▶use DBIx::Class::Sims::Item;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 54 ▶use DBIx::Class::Sims::Source;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 668 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
16 668 my $class = shift;
17 668 my $self = bless {@_}, $class;
18 668 $self->initialize;
19 668 return $self;
20 }
21  
22 668 ▶sub initialize {
Subroutine initialize: called CC=2 SCAR=6.9
Pod undocumented
23 668 my $self = shift;
24  
25 668 $self->{sources} = {};
26 668 foreach my $name ( $self->schema->sources ) {
27 $self->{sources}{$name} = DBIx::Class::Sims::Source->new(
28 name => $name,
29 runner => $self,
30 812 constraints => $self->{constraints}{$name},
31 );
32 }
33  
34 668 $self->{created} = {};
35 668 $self->{duplicates} = {};
36  
37 668 $self->{create_stack} = [];
38 668 $self->{child_requested} = [];
39  
40 668 return;
41 }
42  
43 872 ▶sub has_item {
Subroutine has_item: called CC=4 SCAR=13.9
Pod undocumented
44 872 my $self = shift;
45 872 my ($item) = @_;
46  
47 872 foreach my $comp (@{$self->{create_stack}}) {
48 115 ▶ next unless $item->source_name eq $comp->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $item->source_name eq $comp->[0] 108 7 115
49 7 ▶ next unless Compare($item->spec, $comp->[1]);
Branch: 1 branch
BranchTrueFalseTotal
unless Compare($item->spec, $comp->[1]) 6 1 7
50 1 return 1;
51 }
52 871 return;
53 }
54 871 ▶sub add_item {
Subroutine add_item: called CC=1 SCAR=0.0
Pod undocumented
55 871 my $self = shift;
56 871 my ($item) = @_;
57 871 push @{$self->{create_stack}}, [
58 $item->source_name, MyCloner::clone($item->spec),
59 ];
60 }
61 760 ▶sub remove_item {
Subroutine remove_item: called CC=1 SCAR=0.0
Pod undocumented
62 760 my $self = shift;
63 760 my ($item) = @_;
64 760 pop @{$self->{create_stack}};
65 }
66  
67 8 ▶sub parent { shift->{parent} }
Subroutine parent: called CC=1 SCAR=0.0
Pod undocumented
68 6042 ▶sub schema { shift->{schema} }
Subroutine schema: called CC=1 SCAR=0.0
Pod undocumented
69 708 ▶sub driver { shift->schema->storage->dbh->{Driver}{Name} }
Subroutine driver: called CC=1 SCAR=0.0
Pod undocumented
70 708 ▶sub is_oracle { shift->driver eq 'Oracle' }
Subroutine is_oracle: called CC=1 SCAR=0.0
Pod undocumented
71 0 ▶sub datetime_parser { shift->schema->storage->datetime_parser }
Subroutine datetime_parser: not called CC=1 SCAR=6.9
Pod undocumented
72  
73 2 ▶sub are_columns_equal {
Subroutine are_columns_equal: called CC=8 SCAR=21.6
Pod undocumented
74 2 my $self = shift;
75 2 my ($source, $row, $compare) = @_;
76 2 foreach my $c ($source->columns) {
77 7 ▶ next if $c->is_in_fk;
Branch: 1 branch
BranchTrueFalseTotal
if $c->is_in_fk 3 4 7
78 4 my $col = $c->name;
79  
80 4 ▶ next if !exists($row->{$col}) && !exists($compare->{$col});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $row->{$col} or exists $compare->{$col} 2 2 4
Condition66%2/3
exists $row->{$col} or exists $compare->{$col}
l!l&&r!l&&!r
202
MC/DC50%1/2
exists $row->{$col} or exists $compare->{$col}
exists $row->{$col} exists $compare->{$col}
Truth table66%2/3
A or B
A= exists $row->{$col}B= exists $compare->{$col}
ABresult
1X1
011
000
81 2 ▶ return unless exists($row->{$col}) && exists($compare->{$col});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $row->{$col} and exists $compare->{$col} 0 2 2
Condition33%1/3
exists $row->{$col} and exists $compare->{$col}
!ll&&!rl&&r
002
MC/DC0%0/2
exists $row->{$col} and exists $compare->{$col}
exists $row->{$col} exists $compare->{$col}
Truth table33%1/3
A and B
A= exists $row->{$col}B= exists $compare->{$col}
ABresult
0X0
100
111
82 2 ▶ return if $compare->{$col} ne $row->{$col};
Branch: 1 branch
BranchTrueFalseTotal
if $compare->{$col} ne $row->{$col} 0 2 2
83 }
84 2 return 1;
85 }
86 {
87 54 my %added_by;
88 45 ▶ sub add_child {
Subroutine add_child: called CC=8 SCAR=20.9
Pod undocumented
89 45 my $self = shift;
90 45 my ($opts) = @_;
91 45 my ($source, $fkcol, $child, $adder, $trace) = @{$opts}{qw(
92 source fkcol child adder trace
93 )};
94  
95 # If $child has the same keys (other than parent columns) as another row
96 # added by a different parent table, then set the foreign key for this
97 # parent in the existing row.
98 45 foreach my $compare (@{$self->{spec}{$source->name}}) {
99 # Don't mess with the specs we were provided.
100 # next if ref($compare) eq 'HASH';
101  
102 # Handle the case of a child added with its trace.
103 17 ▶ if ( ref($compare) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $compare eq "ARRAY") 12 5 17
104 12 $compare = $compare->[0];
105 }
106  
107 17 ▶ next if exists $added_by{$adder} && exists $added_by{$adder}{$compare};
Branch: 1 branch
BranchTrueFalseTotal
if exists $added_by{$adder} and exists $added_by{$adder}{$compare} 15 2 17
Condition66%2/3
exists $added_by{$adder} and exists $added_by{$adder}{$compare}
!ll&&!rl&&r
0215
MC/DC50%1/2
exists $added_by{$adder} and exists $added_by{$adder}{$compare}
exists $added_by{$adder} exists $added_by{$adder}{$compare}
Truth table66%2/3
A and B
A= exists $added_by{$adder}B= exists $added_by{$adder}{$compare}
ABresult
0X0
100
111
108 2 ▶ next if exists $compare->{$fkcol};
Branch: 1 branch
BranchTrueFalseTotal
if exists $compare->{$fkcol} 0 2 2
109 2 ▶ if ($self->are_columns_equal($source, $child, $compare)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->are_columns_equal($source, $child, $compare)) 2 0 2
110 2 $compare->{$fkcol} = $child->{$fkcol};
111 2 return;
112 }
113 }
114  
115 43 push @{$self->{spec}{$source->name}}, [ $child, $trace ];
116 43 ▶ $added_by{$adder} //= {};
Condition100%2/2
$added_by{$adder} //= {}
l!l
3211
MC/DC100%1/1
$added_by{$adder} //= {}
$added_by{$adder}
Truth table100%2/2
A
A= $added_by{$adder}
Aresult
11
00
117 43 $added_by{$adder}{$child} = !!1;
118 43 $self->add_pending($source->name);
119 }
120 }
121  
122 {
123 # The "pending" structure exists because of t/parent_child_parent.t - q.v. the
124 # comments on the toposort->add_dependencies element.
125 54 my %pending;
126 43 ▶ sub add_pending { $pending{$_[1]} = undef; }
Subroutine add_pending: called CC=1 SCAR=0.0
Pod undocumented
127 571 ▶ sub has_pending { keys %pending != 0; }
Subroutine has_pending: called CC=1 SCAR=0.0
Pod undocumented
128 627 ▶ sub delete_pending { delete $pending{$_[1]}; }
Subroutine delete_pending: called CC=1 SCAR=0.0
Pod undocumented
129 15 ▶ sub clear_pending { %pending = (); }
Subroutine clear_pending: called CC=1 SCAR=0.0
Pod undocumented
130 }
131  
132 54 ▶sub backref_name {
Subroutine backref_name: called CC=1 SCAR=0.0
Pod undocumented
133 17 my $self = shift;
134 17 my ($item, $colname) = @_;
135 17 return $item->source->name . '->' . $colname;
136 }
137  
138 17 ▶sub convert_backreference {
Subroutine convert_backreference: called CC=8 SCAR=21.2
Pod undocumented
139 17 my $self = shift;
140 17 my ($backref_name, $proto, $default_method) = @_;
141  
142 17 my ($table, $idx, $methods) = ($proto =~ /(.+)\[(\d+)\](?:\.(.+))?$/);
143 17 ▶ unless ($table && defined $idx) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table and defined $idx) 1 16 17
Condition66%2/3
$table and defined $idx
!ll&&!rl&&r
1016
MC/DC50%1/2
$table and defined $idx
$table defined $idx
Truth table66%2/3
A and B
A= $tableB= defined $idx
ABresult
0X0
100
111
144 1 die "Unsure what to do about $backref_name => $proto\n";
145 }
146 16 ▶ unless (exists $self->{rows}{$table}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"rows"}{$table}) 0 16 16
147 0 die "No rows in $table to reference\n";
148 }
149 16 ▶ unless (exists $self->{rows}{$table}[$idx]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"rows"}{$table}[$idx]) 0 16 16
150 0 die "Not enough ($idx) rows in $table to reference\n";
151 }
152  
153 16 ▶ if ($methods) {
Branch: 2 branches
BranchTrueFalseTotal
if ($methods) { } 7 9 16
elsif ($default_method) { } 8 1 9
154 7 my @chain = split '\.', $methods;
155 7 my $obj = $self->{rows}{$table}[$idx];
156 7 $obj = $obj->$_ for @chain;
157 7 return $obj;
158 }
159 elsif ($default_method) {
160 8 return $self->{rows}{$table}[$idx]->$default_method;
161 }
162 else {
163 1 die "No method to call at $backref_name => $proto\n";
164 }
165 }
166  
167 2261 ▶sub call_hook {
Subroutine call_hook: called CC=2 SCAR=6.9
Pod undocumented
168 2261 my $self = shift;
169 2261 my $phase = shift;
170  
171 2261 ▶ return unless exists $self->{hooks}{$phase};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"hooks"}{$phase} 2240 21 2261
172 21 return $self->{hooks}{$phase}->(@_);
173 }
174  
175 19 ▶sub ensure_children {
Subroutine ensure_children: called CC=1 SCAR=0.0
Pod undocumented
176 19 my $self = shift;
177 19 my ($parent, $rel, $count) = @_;
178  
179 19 push @{$self->{child_requested}}, [ $parent, $rel, $count ];
180 }
181  
182 666 ▶sub run {
Subroutine run: called CC=1 SCAR=0.0
Pod undocumented
183 666 my $self = shift;
184  
185 666 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
186 666 ▶ return $self->schema->txn_do(sub {
Subroutine __ANON__: called CC=20 SCAR=30.0
187 # DateTime objects print too big in SIMS_DEBUG mode, so provide a
188 # good way for DDP to print them nicely.
189 54 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
190 0 ▶ local *{'DateTime::_data_printer'} = sub { shift->iso8601 }
Subroutine __ANON__: not called CC=1 SCAR=1.2
191 666 ▶ unless DateTime->can('_data_printer');
Branch: 1 branch
BranchTrueFalseTotal
unless "DateTime"->can("_data_printer") 666 0 666
192  
193 666 $self->{traces} = [];
194 $self->{ids} = {
195 666 find => 1,
196 made => 1,
197 seen => 1,
198 };
199  
200 666 $self->{rows} = {};
201 666 my %still_to_use = map { $_ => 1 } keys %{$self->{spec}};
202 666 while (1) {
203 681 foreach my $name ( @{$self->{toposort}} ) {
204 843 ▶ next unless $self->{spec}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"spec"}{$name} 106 737 843
205 737 delete $still_to_use{$name};
206  
207 737 ▶ while ( my $proto = shift @{$self->{spec}{$name}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $proto = shift @{$$self{"spec"}{$name};}) 779 627 1406
208 # This is the case of a child added via add_child()
209 779 ▶ if ( ref($proto) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto eq 'ARRAY') { } 31 748 779
210 31 ($proto, my $trace) = @{$proto};
211 31 push @{$self->{traces}}, $trace;
212 }
213 else {
214 748 push @{$self->{traces}}, {
215 table => $name,
216 spec => MyCloner::clone($proto),
217 748 seen => $self->{ids}{seen}++,
218 parent => 0,
219 };
220 }
221  
222 779 ▶ $proto->{__META__} //= {};
Condition100%2/2
$proto->{"__META__"} //= {}
l!l
44735
MC/DC100%1/1
$proto->{"__META__"} //= {}
$proto->{"__META__"}
Truth table100%2/2
A
A= $proto->{"__META__"}
Aresult
11
00
223 779 $proto->{__META__}{create} = 1;
224  
225 my $item = DBIx::Class::Sims::Item->new(
226 runner => $self,
227 source => $self->{sources}{$name},
228 spec => $proto,
229 779 trace => $self->{traces}[-1],
230 );
231  
232 779 ▶ if ($self->{allow_pk_set_value}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"allow_pk_set_value"}) 21 758 779
233 21 $item->set_allow_pk_to(1);
234 }
235  
236 779 my $row = $item->create;
237  
238 669 ▶ if ($self->{initial_spec}{$name}{$item->spec}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"initial_spec"}{$name}{$item->spec}) 627 42 669
239 627 ▶ push @{$self->{rows}{$name} //= []}, $row;
Condition100%2/2
$self->{"rows"}{$name} //= []
l!l
45582
MC/DC100%1/1
$self->{"rows"}{$name} //= []
$self->{"rows"}{$name}
Truth table100%2/2
A
A= $self->{"rows"}{$name}
Aresult
11
00
240 }
241 }
242  
243 627 $self->delete_pending($name);
244 }
245  
246 571 foreach my $item ( @{$self->{child_requested}} ) {
247 19 my ($parent, $rel, $count) = @{$item};
248 # Look at $parent's children via $rel and compare to $count.
249 # If we're short, then $self->add_child().
250 19 my $name = $rel->name;
251  
252 # TODO: If the rel is a single accessor, then the constraints should
253 # be capped to 1 and an error thrown otherwise.
254 19 ▶ my $num_children = $rel->is_single_accessor
Branch: 2 branches
BranchTrueFalseTotal
$rel->is_single_accessor ? : 1 18 19
$parent->row->$name ? : 1 0 1
255 ? ($parent->row->$name ? 1 : 0)
256 : $parent->row->$name->count;
257 19 ▶ while ( $count > $num_children ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > $num_children) 19 19 38
258 19 my $child = {};
259 19 ▶ ($child->{__META__} //= {})->{allow_pk_set_value} = 1;
Condition50%1/2
$child->{"__META__"} //= {}
l!l
019
MC/DC0%0/1
$child->{"__META__"} //= {}
$child->{"__META__"}
Truth table50%1/2
A
A= $child->{"__META__"}
Aresult
11
00
260  
261 19 my @inverse = $parent->source->find_inverse_relationships(
262 $rel->target, $rel->foreign_fk_col,
263 );
264 19 ▶ $child->{$rel->foreign_fk_col} = @inverse == 0
Branch: 1 branch
BranchTrueFalseTotal
@inverse == 0 ? : 0 19 19
265 ? $parent->row->get_column($rel->self_fk_col)
266 : $parent->row;
267  
268 $self->add_child({
269 adder => $parent->source_name,
270 source => $rel->target,
271 fkcol => $rel->foreign_fk_col,
272 child => $child,
273 trace => {
274 table => $rel->target->name,
275 spec => MyCloner::clone($child),
276 seen => $parent->{runner}{ids}{seen}++,
277 parent => $parent->{trace}{seen},
278 19 via => 'add_child',
279 },
280 });
281  
282 19 $count--;
283 }
284 }
285 571 $self->{child_requested} = [];
286  
287 571 ▶ last unless $self->has_pending();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_pending 556 15 571
288 15 $self->clear_pending();
289 }
290  
291 # Things were passed in, but don't exist in the schema.
292 556 ▶ if (!$self->{ignore_unknown_tables} && %still_to_use) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"ignore_unknown_tables"} and %still_to_use) 2 554 556
Condition100%3/3
not $self->{"ignore_unknown_tables"} and %still_to_use
!ll&&!rl&&r
15532
MC/DC100%2/2
not $self->{"ignore_unknown_tables"} and %still_to_use
not $self->{"ignore_unknown_tables"} %still_to_use
Truth table100%3/3
not(A) and B
A= not $self->{"ignore_unknown_tables"}B= %still_to_use
ABresult
0X0
100
111
293 2 my $msg = "The following names are in the spec, but not the schema:\n";
294 2 $msg .= join ',', sort keys %still_to_use;
295 2 $msg .= "\n";
296 2 die $msg;
297 }
298  
299 554 ▶ if ( $self->{object_trace} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"object_trace"}) 7 547 554
300 54 ▶ use JSON::MaybeXS qw( encode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
301 7 open my $fh, '>', $self->{object_trace};
302 print $fh encode_json({
303 objects => $self->{traces},
304 7 });
305 7 close $fh;
306 }
307  
308 554 return $self->{rows};
309 666 });
310 112 ▶ } catch {
Subroutine __ANON__: called CC=2 SCAR=7.1
311 112 my $e = $_;
312  
313 112 ▶ if ( $self->{object_trace} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"object_trace"}) 1 111 112
314 1 open my $fh, '>', $self->{object_trace};
315  
316 # Try our hardest to write out in JSON. If that doesn't work, write it
317 # out in DDP.
318 1 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
319 54 ▶ use JSON::MaybeXS qw( encode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
320 print $fh encode_json({
321 objects => $self->{traces},
322 1 });
323 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=1.7
324 0 my $e2 = $_;
325  
326 54 ▶ use DDP;
Subroutine BEGIN: called CC=1 SCAR=0.0
327 0 my %x = ( objects => $self->{traces} );
328 0 print $fh np(%x);
329  
330 0 warn "Couldn't write out in JSON ($e2). Using DDP instead\n";
331 1 };
332  
333 1 close $fh;
334 }
335  
336 112 die $e;
337 666 };
338 }
339  
340 54 1;
341 __END__
« Sims/Item.pm ↑ Summary Sims/Types.pm »