blib/lib/DBIO/Diff/Compare.pm

filter: Statementstmt 100% 110 / 110 Branchbran 88.4% 69 / 78 Conditioncond 76.2% 45 / 59 MC/DCmcdc 56.2% 18 / 32 Subroutinesub 100% 15 / 15 Podpod 100% 8 / 8 totaltotal 87.7% 265 / 302 CC 74 SCAR 27.8
CC
74
Cov
91%
changed_fields
29.4
column_change_lossy
17.8
changed_column_fields
11.1
CRAP 16.0
« blib/lib/DBIO/Cake.pm ↑ Summary blib/lib/DBIO/PopulateMore.pm »
1 package DBIO::Diff::Compare;
2 # ABSTRACT: Engine-agnostic comparison helpers for DBIO diff operations
3  
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 our $VERSION = '0.900007';
8  
9 5 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 our @EXPORT_OK = qw(
12 norm norm_type arr_differ changed_fields
13 changed_column_fields changed_index_fields changed_fk_fields
14 column_change_lossy
15 );
16  
17  
18 54 ▶sub norm {
Subroutine norm: called CC=2 SCAR=6.9
Pod documented
19 54 my ($v) = @_;
20 54 ▶ return defined $v ? $v : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 50 4 54
21 }
22  
23  
24 92 ▶sub norm_type {
Subroutine norm_type: called CC=2 SCAR=6.9
Pod documented
25 92 my ($t) = @_;
26 92 ▶ return '' unless defined $t;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t 1 91 92
27 91 $t =~ s/\s+/ /g;
28 91 $t =~ s/^\s+//;
29 91 $t =~ s/\s+$//;
30 91 return uc $t;
31 }
32  
33  
34 25 ▶sub arr_differ {
Subroutine arr_differ: called CC=6 SCAR=17.9
Pod documented
35 25 my ($a, $b) = @_;
36 25 ▶ $a = ref($a) eq 'ARRAY' ? $a : [];
Branch: 1 branch
BranchTrueFalseTotal
ref $a eq 'ARRAY' ? : 24 1 25
37 25 ▶ $b = ref($b) eq 'ARRAY' ? $b : [];
Branch: 1 branch
BranchTrueFalseTotal
ref $b eq 'ARRAY' ? : 25 0 25
38 25 ▶ return 1 if scalar(@$a) != scalar(@$b);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$a != scalar @$b 1 24 25
39 24 my @sa = sort @$a;
40 24 my @sb = sort @$b;
41 24 for my $i (0 .. $#sa) {
42 29 ▶ return 1 if $sa[$i] ne $sb[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $sa[$i] ne $sb[$i] 1 28 29
43 }
44 23 return 0;
45 }
46  
47 # Internal: stringify a scalar-or-arrayref into an ORDER-PRESERVING key, for
48 # 'dim' fields where order is significant ([precision, scale], ordered columns).
49 14 ▶sub _dim_key {
Subroutine _dim_key: called CC=3 SCAR=11.0
50 14 my ($v) = @_;
51 14 ▶ return join ',', map { defined $_ ? $_ : "\0" } @$v if ref($v) eq 'ARRAY';
Branch: 2 branches
BranchTrueFalseTotal
if ref $v eq "ARRAY" 4 10 14
defined $_ ? : 8 0 8
52 10 return norm($v);
53 }
54  
55  
56 49 ▶sub changed_fields {
Subroutine changed_fields: called CC=29 SCAR=33.8
Pod documented
57 49 my ($old, $new, %spec) = @_;
58 # desired_state is the always-on contract now (any non-bool field the target
59 # leaves undef is ignored); the key is accepted but vestigial. See DESCRIPTION.
60 49 delete $spec{desired_state};
61 49 ▶ $old ||= {};
Condition50%1/2
$old ||= {}
l!l
490
MC/DC0%0/1
$old ||= {}
$old
Truth table50%1/2
A
A= $old
Aresult
11
00
62 49 ▶ $new ||= {};
Condition50%1/2
$new ||= {}
l!l
490
MC/DC0%0/1
$new ||= {}
$new
Truth table50%1/2
A
A= $new
Aresult
11
00
63  
64 49 my @changed;
65  
66 # Desired-state contract: a non-bool field the target ($new) leaves undef is a
67 # "don't care" -- the desired state did not prescribe it, so whatever the live
68 # database reports for it is left alone. Skip it from comparison entirely.
69 122 ▶ my $skip = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
70 122 my ($field) = @_;
71 122 return !defined $new->{$field};
72 49 };
73  
74 49 ▶ for my $field (@{ $spec{scalar} || [] }) {
Condition100%2/2
$spec{'scalar'} || []
l!l
3019
MC/DC100%1/1
$spec{'scalar'} || []
$spec{'scalar'}
Truth table100%2/2
A
A= $spec{'scalar'}
Aresult
11
00
75 59 ▶ next if $skip->($field);
Branch: 1 branch
BranchTrueFalseTotal
if &$skip($field) 41 18 59
76 18 ▶ push @changed, $field if norm($old->{$field}) ne norm($new->{$field});
Branch: 1 branch
BranchTrueFalseTotal
if norm($old->{$field}) ne norm($new->{$field}) 9 9 18
77 }
78 49 ▶ for my $field (@{ $spec{type} || [] }) {
Condition100%2/2
$spec{'type'} || []
l!l
2227
MC/DC100%1/1
$spec{'type'} || []
$spec{'type'}
Truth table100%2/2
A
A= $spec{'type'}
Aresult
11
00
79 22 ▶ next if $skip->($field);
Branch: 1 branch
BranchTrueFalseTotal
if &$skip($field) 0 22 22
80 22 ▶ push @changed, $field if norm_type($old->{$field}) ne norm_type($new->{$field});
Branch: 1 branch
BranchTrueFalseTotal
if norm_type($old->{$field}) ne norm_type($new->{$field}) 1 21 22
81 }
82 49 ▶ for my $field (@{ $spec{bool} || [] }) {
Condition100%2/2
$spec{'bool'} || []
l!l
1732
MC/DC100%1/1
$spec{'bool'} || []
$spec{'bool'}
Truth table100%2/2
A
A= $spec{'bool'}
Aresult
11
00
83 17 ▶ push @changed, $field if (($old->{$field} // 0) <=> ($new->{$field} // 0));
Branch: 1 branch
BranchTrueFalseTotal
if ($old->{$field} // 0) <=> ($new->{$field} // 0) 6 11 17
Condition100%4/4
$old->{$field} // 0
l!l
152
$new->{$field} // 0
l!l
161
MC/DC100%1/1
$old->{$field} // 0
$old->{$field}
MC/DC100%1/1
$new->{$field} // 0
$new->{$field}
Truth table100%2/2
A
A= $old->{$field}
Aresult
11
00
Truth table100%2/2
A
A= $new->{$field}
Aresult
11
00
84 }
85 49 ▶ for my $field (@{ $spec{dim} || [] }) {
Condition100%2/2
$spec{'dim'} || []
l!l
2029
MC/DC100%1/1
$spec{'dim'} || []
$spec{'dim'}
Truth table100%2/2
A
A= $spec{'dim'}
Aresult
11
00
86 20 ▶ next if $skip->($field);
Branch: 1 branch
BranchTrueFalseTotal
if &$skip($field) 13 7 20
87 7 ▶ push @changed, $field if _dim_key($old->{$field}) ne _dim_key($new->{$field});
Branch: 1 branch
BranchTrueFalseTotal
if _dim_key($old->{$field}) ne _dim_key($new->{$field}) 7 0 7
88 }
89 49 ▶ for my $field (@{ $spec{array} || [] }) {
Condition100%2/2
$spec{'array'} || []
l!l
1732
MC/DC100%1/1
$spec{'array'} || []
$spec{'array'}
Truth table100%2/2
A
A= $spec{'array'}
Aresult
11
00
90 21 ▶ next if $skip->($field);
Branch: 1 branch
BranchTrueFalseTotal
if &$skip($field) 0 21 21
91 21 ▶ push @changed, $field if arr_differ($old->{$field}, $new->{$field});
Branch: 1 branch
BranchTrueFalseTotal
if arr_differ($old->{$field}, $new->{$field}) 0 21 21
92 }
93 49 ▶ for my $field (@{ $spec{mutual} || [] }) {
Condition100%2/2
$spec{'mutual'} || []
l!l
1732
MC/DC100%1/1
$spec{'mutual'} || []
$spec{'mutual'}
Truth table100%2/2
A
A= $spec{'mutual'}
Aresult
11
00
94 # Bilateral: a difference only when BOTH sides carry a value. undef on
95 # either side means that participant has no opinion (e.g. a driver that
96 # does not introspect index_type), so it is never a change -- a typeless
97 # driver produces no phantom diff. Compared as a type token. ADR 0052.
98 17 ▶ next unless defined $old->{$field} && defined $new->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $old->{$field} and defined $new->{$field} 11 6 17
Condition100%3/3
defined $old->{$field} and defined $new->{$field}
!ll&&!rl&&r
926
MC/DC100%2/2
defined $old->{$field} and defined $new->{$field}
defined $old->{$field} defined $new->{$field}
Truth table100%3/3
A and B
A= defined $old->{$field}B= defined $new->{$field}
ABresult
0X0
100
111
99 6 ▶ push @changed, $field if norm_type($old->{$field}) ne norm_type($new->{$field});
Branch: 1 branch
BranchTrueFalseTotal
if norm_type($old->{$field}) ne norm_type($new->{$field}) 4 2 6
100 }
101  
102 49 return @changed;
103 }
104  
105  
106 # Internal: true when a live default value is a real default. An absent one and
107 # a literal NULL (plain or as a \'NULL' ref) are none.
108 19 ▶sub _has_default {
Subroutine _has_default: called CC=5 SCAR=16.1
109 19 my ($v) = @_;
110 19 ▶ $v = $$v if ref($v) eq 'SCALAR' || ref($v) eq 'REF';
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "SCALAR" or ref $v eq "REF" 1 18 19
Condition66%2/3
ref $v eq "SCALAR" or ref $v eq "REF"
l!l&&r!l&&!r
1018
MC/DC50%1/2
ref $v eq "SCALAR" or ref $v eq "REF"
ref $v eq "SCALAR" ref $v eq "REF"
Truth table66%2/3
A or B
A= ref $v eq "SCALAR"B= ref $v eq "REF"
ABresult
1X1
011
000
111 19 ▶ return defined $v && uc(norm($v)) ne 'NULL' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $v && uc norm($v) ne 'NULL' ? : 3 16 19
Condition100%3/3
defined $v and uc norm($v) ne "NULL"
!ll&&!rl&&r
1423
MC/DC100%2/2
defined $v and uc norm($v) ne "NULL"
defined $v uc norm($v) ne "NULL"
Truth table100%3/3
A and B
A= defined $vB= uc norm($v) ne "NULL"
ABresult
0X0
100
111
112 }
113  
114 19 ▶sub changed_column_fields {
Subroutine changed_column_fields: called CC=11 SCAR=24.1
Pod documented
115 19 my ($old, $new) = @_;
116 19 ▶ $old ||= {};
Condition50%1/2
$old ||= {}
l!l
190
MC/DC0%0/1
$old ||= {}
$old
Truth table50%1/2
A
A= $old
Aresult
11
00
117 19 ▶ $new ||= {};
Condition50%1/2
$new ||= {}
l!l
190
MC/DC0%0/1
$new ||= {}
$new
Truth table50%1/2
A
A= $new
Aresult
11
00
118 19 my @changed = changed_fields($old, $new,
119 type => ['data_type'],
120 scalar => ['not_null', 'default_value'],
121 dim => ['size'],
122 desired_state => 1,
123 );
124 # A default the target no longer declares is a change: the live column
125 # carries one (a live DEFAULT NULL is none), the target leaves it undef.
126 # Placed where the scalar pass puts default_value, after not_null.
127 19 ▶ if (!defined $new->{default_value} && _has_default($old->{default_value})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $new->{"default_value"} and _has_default($old->{"default_value"})) 3 16 19
Condition66%2/3
not defined $new->{"default_value"} and _has_default($old->{"default_value"})
!ll&&!rl&&r
0163
MC/DC50%1/2
not defined $new->{"default_value"} and _has_default($old->{"default_value"})
not defined $new->{"default_value"} _has_default($old->{"default_value"})
Truth table66%2/3
not(A) and B
A= not defined $new->{"default_value"}B= _has_default($old->{"default_value"})
ABresult
0X0
100
111
128 3 my $at = (grep { $changed[$_] eq 'not_null' } 0 .. $#changed)[0];
129 3 ▶ splice @changed, defined $at ? $at + 1 : 0, 0, 'default_value';
Branch: 1 branch
BranchTrueFalseTotal
defined $at ? : 1 2 3
130 }
131 # is_auto_increment follows the desired-state rule (a target that leaves it
132 # undef prescribes nothing), but a prescribed flag compares as a bool: an
133 # explicit 0 equals a live column that carries no flag.
134 push @changed, 'is_auto_increment'
135 if defined $new->{is_auto_increment}
136 19 ▶ && (($old->{is_auto_increment} // 0) ? 1 : 0) != ($new->{is_auto_increment} ? 1 : 0);
Branch: 3 branches
BranchTrueFalseTotal
if defined $new->{"is_auto_increment"} and ($old->{"is_auto_increment"} // 0 ? 1 : 0) != ($new->{"is_auto_increment"} ? 1 : 0) 3 16 19
$old->{'is_auto_increment'} // 0 ? : 1 3 4
$new->{'is_auto_increment'} ? : 2 2 4
Condition100%5/5
defined $new->{"is_auto_increment"} and ($old->{"is_auto_increment"} // 0 ? 1 : 0) != ($new->{"is_auto_increment"} ? 1 : 0)
!ll&&!rl&&r
1513
$old->{"is_auto_increment"} // 0
l!l
22
MC/DC100%2/2
defined $new->{"is_auto_increment"} and ($old->{"is_auto_increment"} // 0 ? 1 : 0) != ($new->{"is_auto_increment"} ? 1 : 0)
defined $new->{"is_auto_increment"} $old->{"is_auto_increment"}
Truth table100%3/3
A and B
A= defined $new->{"is_auto_increment"}B= $old->{"is_auto_increment"}
ABresult
0X0
100
111
137 19 return @changed;
138 }
139  
140  
141 # Internal: the components of a size as a list of numbers, or undef when the
142 # value is absent or not numeric.
143 18 ▶sub _size_parts {
Subroutine _size_parts: called CC=7 SCAR=19.9
144 18 my ($v) = @_;
145 18 ▶ return undef unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 1 17 18
146 17 ▶ my @p = ref($v) eq 'ARRAY' ? @$v : split /\s*,\s*/, $v;
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 4 13 17
147 17 s/^\s+|\s+$//g for grep { defined } @p;
148 17 ▶ @p = grep { defined $_ && length $_ } @p;
Condition33%1/3
defined $_ && length $_
!ll&&!rl&&r
0026
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
149 17 ▶ return undef unless @p;
Branch: 1 branch
BranchTrueFalseTotal
unless @p 0 17 17
150 17 ▶ return undef if grep { !/\A[0-9]+\z/ } @p;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not /\A[0-9]+\z/;} @p 0 17 17
151 17 return \@p;
152 }
153  
154 17 ▶sub column_change_lossy {
Subroutine column_change_lossy: called CC=17 SCAR=28.8
Pod documented
155 17 my ($old, $new, $pairs) = @_;
156 17 ▶ $old ||= {};
Condition50%1/2
$old ||= {}
l!l
170
MC/DC0%0/1
$old ||= {}
$old
Truth table50%1/2
A
A= $old
Aresult
11
00
157 17 ▶ $new ||= {};
Condition50%1/2
$new ||= {}
l!l
170
MC/DC0%0/1
$new ||= {}
$new
Truth table50%1/2
A
A= $new
Aresult
11
00
158 17 ▶ $pairs ||= {};
Condition100%2/2
$pairs ||= {}
l!l
161
MC/DC100%1/1
$pairs ||= {}
$pairs
Truth table100%2/2
A
A= $pairs
Aresult
11
00
159  
160 17 my $ot = lc norm_type($old->{data_type});
161 17 ▶ my $nt = defined $new->{data_type} ? lc norm_type($new->{data_type}) : $ot;
Branch: 1 branch
BranchTrueFalseTotal
defined $new->{'data_type'} ? : 16 1 17
162  
163 17 ▶ if ($ot ne $nt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ot ne $nt) 5 12 17
164 5 ▶ return $pairs->{"$ot->$nt"} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$$pairs{"$ot->$nt"} ? : 2 3 5
165 }
166  
167 12 ▶ return 0 unless defined $new->{size}; # prescribes nothing
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new->{"size"} 3 9 12
168 9 my $np = _size_parts($new->{size});
169 9 my $op = _size_parts($old->{size});
170 9 ▶ return 0 if !defined $op && !defined $np && norm($old->{size}) eq norm($new->{size});
Branch: 1 branch
BranchTrueFalseTotal
if not defined $op || defined $np and norm($old->{"size"}) eq norm($new->{"size"}) 0 9 9
Condition50%3/6
not defined $op || defined $np and norm($old->{"size"}) eq norm($new->{"size"})
!ll&&!rl&&r
900
defined $op || defined $np
l!l&&r!l&&!r
810
MC/DC0%0/3
not defined $op || defined $np and norm($old->{"size"}) eq norm($new->{"size"})
defined $op defined $np norm($old->{"size"}) eq norm($new->{"size"})
Truth table50%2/4
not(A || B) and C
A= defined $opB= defined $npC= norm($old->{"size"}) eq norm($new->{"size"})
ABCresult
1XX0
01X0
0000
0011
171 9 ▶ return 1 unless defined $op && defined $np;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $op and defined $np 1 8 9
Condition66%2/3
defined $op and defined $np
!ll&&!rl&&r
108
MC/DC50%1/2
defined $op and defined $np
defined $op defined $np
Truth table66%2/3
A and B
A= defined $opB= defined $np
ABresult
0X0
100
111
172 8 ▶ return 1 if @$op != @$np;
Branch: 1 branch
BranchTrueFalseTotal
if @$op != @$np 1 7 8
173 7 for my $i (0 .. $#$np) {
174 10 ▶ return 1 if $i == 1 ? $np->[$i] != $op->[$i] : $np->[$i] < $op->[$i];
Branch: 2 branches
BranchTrueFalseTotal
if $i == 1 ? $np->[$i] != $op->[$i] : $np->[$i] < $op->[$i] 3 7 10
$i == 1 ? : 3 7 10
175 }
176 4 return 0;
177 }
178  
179  
180 12 ▶sub changed_index_fields {
Subroutine changed_index_fields: called CC=1 SCAR=0.0
Pod documented
181 12 my ($old, $new) = @_;
182 12 return changed_fields($old, $new,
183 bool => ['is_unique'],
184 array => ['columns'],
185 mutual => ['index_type'],
186 );
187 }
188  
189  
190 4 ▶sub changed_fk_fields {
Subroutine changed_fk_fields: called CC=1 SCAR=0.0
Pod documented
191 4 my ($old, $new) = @_;
192 4 return changed_fields($old, $new,
193 scalar => ['to_table', 'on_update', 'on_delete'],
194 array => ['from_columns', 'to_columns'],
195 );
196 }
197  
198 5 1;
199  
200 __END__
« blib/lib/DBIO/Cake.pm ↑ Summary blib/lib/DBIO/PopulateMore.pm »