blib/lib/DBIO/Diff/Op.pm

filter: Statementstmt 100% 118 / 118 Branchbran 85.7% 48 / 56 Conditioncond 75.5% 37 / 49 MC/DCmcdc 44.8% 13 / 29 Subroutinesub 100% 12 / 12 Podpod 100% 6 / 6 totaltotal 86.6% 234 / 270 CC 59 SCAR 29.7
CC
59
Cov
91%
diff_nested
34.9
_order_by_dependency
10.0
diff_toplevel
9.2
CRAP 19.5
« blib/lib/DBIO/Test/Util/UmaskGuard.pm ↑ Summary blib/lib/DBIO/Generate/Style/Moo.pm »
1 package DBIO::Diff::Op;
2 # ABSTRACT: Base class for DBIO driver diff operation objects
3  
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 our $VERSION = '0.900007';
8  
9 6 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 ▶use base 'Class::Accessor::Grouped';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 __PACKAGE__->mk_group_accessors(simple => 'action');
14  
15  
16 53 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
17 53 my ($class, %args) = @_;
18 53 return bless { %args }, $class;
19 }
20  
21  
22 4 ▶sub mk_diff_accessors {
Subroutine mk_diff_accessors: called CC=1 SCAR=0.0
Pod documented
23 4 my ($class, @names) = @_;
24 4 $class->mk_group_accessors(simple => @names);
25 }
26  
27 6 my %DEFAULT_PREFIX = (
28 create => '+', add => '+',
29 drop => '-',
30 alter => '~', modify => '~', change => '~',
31 );
32  
33  
34 6 ▶sub summary_prefix {
Subroutine summary_prefix: called CC=4 SCAR=13.9
Pod documented
35 6 my ($self, %map) = @_;
36 6 ▶ my $action = $self->action // '';
Condition50%1/2
$self->action // ""
l!l
60
MC/DC0%0/1
$self->action // ""
$self->action
Truth table50%1/2
A
A= $self->action
Aresult
11
00
37 6 ▶ return $map{$action} // $DEFAULT_PREFIX{$action} // '~';
Condition100%5/5
($map{$action} // $DEFAULT_PREFIX{$action}) // "~"
l!l
51
$map{$action} // $DEFAULT_PREFIX{$action}
l!l&&r!l&&!r
141
MC/DC100%2/2
($map{$action} // $DEFAULT_PREFIX{$action}) // "~"
$map{$action} $DEFAULT_PREFIX{$action}
Truth table100%3/3
A // B
A= $map{$action}B= $DEFAULT_PREFIX{$action}
ABresult
1X1
011
000
38 }
39  
40  
41 # Internal: stable topological order of @names; a name comes after the names
42 # it depends on. Names caught in a cycle follow alphabetically at the end.
43 18 ▶sub _order_by_dependency {
Subroutine _order_by_dependency: called CC=10 SCAR=23.0
44 18 my ($depends, @names) = @_;
45 18 @names = sort @names;
46 18 ▶ return @names unless $depends;
Branch: 1 branch
BranchTrueFalseTotal
unless $depends 6 12 18
47  
48 12 my %in = map { $_ => 1 } @names;
49 12 my %deps;
50 12 for my $n (@names) {
51 21 ▶ my $d = ref($depends) eq 'CODE' ? $depends->($n) : $depends->{$n};
Branch: 1 branch
BranchTrueFalseTotal
ref $depends eq 'CODE' ? : 4 17 21
52 21 ▶ $deps{$n} = [ grep { $_ ne $n && $in{$_} } @{ $d || [] } ];
Condition80%4/5
$_ ne $n && $in{$_}
!ll&&!rl&&r
0115
$d || []
l!l
174
MC/DC50%1/2
$_ ne $n && $in{$_}
$_ ne $n $in{$_}
MC/DC100%1/1
$d || []
$d
Truth table66%2/3
A && B
A= $_ ne $nB= $in{$_}
ABresult
0X0
100
111
Truth table100%2/2
A
A= $d
Aresult
11
00
53 }
54  
55 12 my (@ordered, %done);
56 12 my @rest = @names;
57 12 ▶ while (@rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest) 19 11 30
58 19 my $at;
59 19 for my $i (0 .. $#rest) {
60 32 ▶ next if grep { !$done{$_} } @{ $deps{ $rest[$i] } };
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $done{$_};} @{$deps{$rest[$i]};} 14 18 32
61 18 $at = $i;
62 18 last;
63 }
64 19 ▶ last unless defined $at;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $at 1 18 19
65 18 my ($n) = splice @rest, $at, 1;
66 18 $done{$n} = 1;
67 18 push @ordered, $n;
68 }
69 12 return (@ordered, @rest);
70 }
71  
72 9 ▶sub diff_toplevel {
Subroutine diff_toplevel: called CC=9 SCAR=22.2
Pod documented
73 9 my ($class, $source, $target, %cb) = @_;
74 9 ▶ $source ||= {};
Condition50%1/2
$source ||= {}
l!l
90
MC/DC0%0/1
$source ||= {}
$source
Truth table50%1/2
A
A= $source
Aresult
11
00
75 9 ▶ $target ||= {};
Condition50%1/2
$target ||= {}
l!l
90
MC/DC0%0/1
$target ||= {}
$target
Truth table50%1/2
A
A= $target
Aresult
11
00
76  
77 9 my @ops;
78 my @create = _order_by_dependency($cb{depends},
79 9 grep { !exists $source->{$_} } keys %$target);
80 9 for my $name (@create) {
81 18 ▶ push @ops, $cb{create}->($name) if $cb{create};
Branch: 1 branch
BranchTrueFalseTotal
if $cb{"create"} 18 0 18
82 }
83  
84 9 ▶ my $drop_depends = $cb{source_depends} || $cb{depends};
Condition100%3/3
$cb{'source_depends'} || $cb{'depends'}
l!l&&r!l&&!r
153
MC/DC100%2/2
$cb{'source_depends'} || $cb{'depends'}
$cb{'source_depends'} $cb{'depends'}
Truth table100%3/3
A || B
A= $cb{'source_depends'}B= $cb{'depends'}
ABresult
1X1
011
000
85 my @drop = _order_by_dependency($drop_depends,
86 9 grep { !exists $target->{$_} } keys %$source);
87 9 ▶ @drop = reverse @drop if $drop_depends;
Branch: 1 branch
BranchTrueFalseTotal
if $drop_depends 6 3 9
88 9 for my $name (@drop) {
89 13 ▶ push @ops, $cb{drop}->($name) if $cb{drop};
Branch: 1 branch
BranchTrueFalseTotal
if $cb{"drop"} 13 0 13
90 }
91 9 return @ops;
92 }
93  
94 # Internal: normalize a per-table member collection into { name => $member }.
95 # A collection that is already a hashref is returned as-is; an arrayref is
96 # keyed by the field named in $index_by.
97 20 ▶sub _members_by_key {
Subroutine _members_by_key: called CC=4 SCAR=13.9
98 20 my ($members, $index_by) = @_;
99 20 ▶ return {} unless defined $members;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $members 1 19 20
100 19 ▶ if (ref($members) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $members eq "HASH") 11 8 19
101 11 return $members;
102 }
103 8 my %by;
104 8 $by{ $_->{$index_by} } = $_ for @{ $members };
105 8 return \%by;
106 }
107  
108  
109 9 ▶sub diff_nested {
Subroutine diff_nested: called CC=32 SCAR=35.5
Pod documented
110 9 my ($class, $source, $target, %opt) = @_;
111 9 ▶ $source ||= {};
Condition50%1/2
$source ||= {}
l!l
90
MC/DC0%0/1
$source ||= {}
$source
Truth table50%1/2
A
A= $source
Aresult
11
00
112 9 ▶ $target ||= {};
Condition50%1/2
$target ||= {}
l!l
90
MC/DC0%0/1
$target ||= {}
$target
Truth table50%1/2
A
A= $target
Aresult
11
00
113  
114 9 my $index_by = $opt{index_by};
115 9 ▶ my $scope = $opt{scope} || 'both';
Condition50%1/2
$opt{'scope'} || 'both'
l!l
90
MC/DC0%0/1
$opt{'scope'} || 'both'
$opt{'scope'}
Truth table50%1/2
A
A= $opt{'scope'}
Aresult
11
00
116 9 my $skip = $opt{skip};
117 9 my $changed = $opt{changed_when};
118 9 ▶ my $src_tables = $opt{source_tables} || $source;
Condition66%2/3
$opt{'source_tables'} || $source
l!l&&r!l&&!r
360
MC/DC0%0/2
$opt{'source_tables'} || $source
$opt{'source_tables'} $source
Truth table66%2/3
A || B
A= $opt{'source_tables'}B= $source
ABresult
1X1
011
000
119 9 ▶ my $tgt_tables = $opt{target_tables} || $target;
Condition66%2/3
$opt{'target_tables'} || $target
l!l&&r!l&&!r
360
MC/DC0%0/2
$opt{'target_tables'} || $target
$opt{'target_tables'} $target
Truth table66%2/3
A || B
A= $opt{'target_tables'}B= $target
ABresult
1X1
011
000
120  
121 9 my @ops;
122  
123 # Under 'both' the tables of the source count too: a table that lost its
124 # last member is missing from the target hash, but its on_gone is due.
125 9 my %walk = map { $_ => 1 } keys %$target;
126 9 ▶ if ($scope eq 'both') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope eq "both") 7 2 9
127 7 $walk{$_} = 1 for keys %$source;
128 }
129  
130 9 for my $table (sort keys %walk) {
131 12 ▶ if ($scope eq 'both') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope eq "both") 10 2 12
132 10 ▶ next unless exists $src_tables->{$table} && exists $tgt_tables->{$table};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $src_tables->{$table} and exists $tgt_tables->{$table} 3 7 10
Condition100%3/3
exists $src_tables->{$table} and exists $tgt_tables->{$table}
!ll&&!rl&&r
127
MC/DC100%2/2
exists $src_tables->{$table} and exists $tgt_tables->{$table}
exists $src_tables->{$table} exists $tgt_tables->{$table}
Truth table100%3/3
A and B
A= exists $src_tables->{$table}B= exists $tgt_tables->{$table}
ABresult
0X0
100
111
133 }
134  
135 9 my $src = _members_by_key($source->{$table}, $index_by);
136 9 my $tgt = _members_by_key($target->{$table}, $index_by);
137  
138 9 for my $name (sort keys %$tgt) {
139 11 my $new = $tgt->{$name};
140 11 ▶ next if $skip && $skip->($new);
Branch: 1 branch
BranchTrueFalseTotal
if $skip and &$skip($new) 0 11 11
Condition66%2/3
$skip and &$skip($new)
!ll&&!rl&&r
830
MC/DC0%0/2
$skip and &$skip($new)
$skip &$skip($new)
Truth table66%2/3
A and B
A= $skipB= &$skip($new)
ABresult
0X0
100
111
141  
142 11 ▶ if (!exists $src->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $src->{$name}) 2 9 11
143 2 ▶ push @ops, $opt{on_new}->($table, $name, $new) if $opt{on_new};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"on_new"} 2 0 2
144 2 next;
145 }
146  
147 9 my $old = $src->{$name};
148 9 ▶ if (!$changed || $changed->($old, $new)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $changed or &$changed($old, $new)) 5 4 9
Condition66%2/3
not $changed or &$changed($old, $new)
l!l&&r!l&&!r
054
MC/DC50%1/2
not $changed or &$changed($old, $new)
not $changed &$changed($old, $new)
Truth table66%2/3
not(A) or B
A= not $changedB= &$changed($old, $new)
ABresult
1X1
011
000
149 5 ▶ push @ops, $opt{on_changed}->($table, $name, $old, $new) if $opt{on_changed};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"on_changed"} 5 0 5
150 }
151 }
152  
153 9 for my $name (sort keys %$src) {
154 15 my $old = $src->{$name};
155 15 ▶ next if $skip && $skip->($old);
Branch: 1 branch
BranchTrueFalseTotal
if $skip and &$skip($old) 1 14 15
Condition100%3/3
$skip and &$skip($old)
!ll&&!rl&&r
1131
MC/DC100%2/2
$skip and &$skip($old)
$skip &$skip($old)
Truth table100%3/3
A and B
A= $skipB= &$skip($old)
ABresult
0X0
100
111
156 14 ▶ next if exists $tgt->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $tgt->{$name} 9 5 14
157 5 ▶ push @ops, $opt{on_gone}->($table, $name, $old) if $opt{on_gone};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"on_gone"} 5 0 5
158 }
159 }
160  
161 9 ▶ if ($scope eq 'all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope eq "all") 2 7 9
162 2 for my $table (sort keys %$source) {
163 4 ▶ next if exists $target->{$table};
Branch: 1 branch
BranchTrueFalseTotal
if exists $target->{$table} 2 2 4
164 2 my $src = _members_by_key($source->{$table}, $index_by);
165 2 for my $name (sort keys %$src) {
166 2 my $old = $src->{$name};
167 2 ▶ next if $skip && $skip->($old);
Branch: 1 branch
BranchTrueFalseTotal
if $skip and &$skip($old) 0 2 2
Condition66%2/3
$skip and &$skip($old)
!ll&&!rl&&r
110
MC/DC0%0/2
$skip and &$skip($old)
$skip &$skip($old)
Truth table66%2/3
A and B
A= $skipB= &$skip($old)
ABresult
0X0
100
111
168 2 ▶ push @ops, $opt{on_gone}->($table, $name, $old) if $opt{on_gone};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"on_gone"} 2 0 2
169 }
170 }
171 }
172  
173 9 return @ops;
174 }
175  
176  
177 9 ▶sub should_emit_if_exists {
Subroutine should_emit_if_exists: called CC=5 SCAR=16.1
Pod documented
178 9 my ($storage) = @_;
179 9 ▶ return 0 unless $storage;
Branch: 1 branch
BranchTrueFalseTotal
unless $storage 1 8 9
180 8 ▶ return 0 unless Scalar::Util::blessed($storage)
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($storage) and $storage->can("_use_supports_if_exists") 2 6 8
Condition100%3/3
builtin::blessed($storage) and $storage->can("_use_supports_if_exists")
!ll&&!rl&&r
116
MC/DC100%2/2
builtin::blessed($storage) and $storage->can("_use_supports_if_exists")
builtin::blessed($storage) $storage->can("_use_supports_if_exists")
Truth table100%3/3
A and B
A= builtin::blessed($storage)B= $storage->can("_use_supports_if_exists")
ABresult
0X0
100
111
181 && $storage->can('_use_supports_if_exists');
182 6 ▶ return $storage->_use_supports_if_exists ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$storage->_use_supports_if_exists ? : 2 4 6
183 }
184  
185 6 1;
186  
187 __END__
« blib/lib/DBIO/Test/Util/UmaskGuard.pm ↑ Summary blib/lib/DBIO/Generate/Style/Moo.pm »