Diff/Role/Common.pm

filter: Statementstmt 91.9% 80 / 87 Branchbran 69.6% 39 / 56 Conditioncond 54.5% 18 / 33 MC/DCmcdc 26.3% 5 / 19 Subroutinesub 100% 15 / 15 Podpod n/a totaltotal 74.7% 157 / 210 CC 43 SCAR 35.2
CC
43
Cov
78%
_is_eq
72.5
_info_diff
9.2
_coerce_schema_data
8.0
CRAP 33.6
↑ Summary Diff/State.pm »
1 package DBIx::Class::Schema::Diff::Role::Common;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Types::Standard qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use List::MoreUtils qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Array::Diff;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Path::Class qw(file);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 31 ▶sub _types_list { qw(
Subroutine _types_list: called CC=1 SCAR=0.0
15 columns
16 relationships
17 constraints
18 table_name
19 isa
20 )}
21  
22 #has '__types_list', is => 'ro', lazy => 1, default => sub {
23 # my $self = shift;
24 # my @list = qw(
25 # columns
26 # relationships
27 # constraints
28 # table_name
29 # isa
30 # );
31 # $self->split_db_schema_from_table_name and push @list, 'db_schema';
32 # \@list
33 #}, isa => ArrayRef;
34 #
35 #sub _types_list { @{(shift)->__types_list} }
36 #
37 #
38 #has 'split_db_schema_from_table_name',
39 # is => 'ro',
40 # is => Bool,
41 # default => sub { 0 };
42 #
43 #has 'null_db_schema_value',
44 # is => 'ro',
45 # isa => Str,
46 # default => sub { '<null>' };
47  
48  
49  
50 # Adapted from Hash::Diff, but heavily modified and specific to
51 # the unique needs of this module...
52 sub _info_diff {
53 7953 ▶ my ($self, $old, $new) = @_;
Subroutine _info_diff: called CC=9 SCAR=22.2
54
55 7953 my %old_keys = map {$_=>1} keys %$old;
56  
57 7953 my $nh = {};
58  
59 7953 for my $k (keys %$new) {
60 24189 ▶ if (exists $old->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $old->{$k}) { } 24189 0 24189
61 24189 delete $old_keys{$k};
62 24189 ▶ if(ref $new->{$k} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $new->{$k} eq 'HASH') { } 3795 20394 24189
63 3795 ▶ if(ref $old->{$k} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $old->{$k} eq 'HASH') { } 3795 0 3795
64 3795 ▶ my $diff = $self->_info_diff($old->{$k},$new->{$k}) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $diff = $self->_info_diff($old->{$k}, $new->{$k}) or next 3773 22 3795
65 22 $nh->{$k} = $diff;
66 }
67 else {
68 0 $nh->{$k} = $new->{$k};
69 }
70 }
71 else {
72 # Test if the non hash values are determined to be "equal"
73 20394 ▶ $nh->{$k} = $new->{$k} unless ($self->_is_eq($old->{$k},$new->{$k}));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_eq($old->{$k}, $new->{$k}) 68 20326 20394
74 }
75 }
76 else {
77 0 $nh->{$k} = $new->{$k};
78 }
79 }
80
81 # fill back in any left over, old keys (i.e. weren't in $new):
82 # TODO: track these separately
83 7953 $nh->{$_} = $old->{$_} for (keys %old_keys);
84  
85 7953 ▶ return undef unless (keys %$nh > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$nh > 0 7863 90 7953
86 90 return $nh;
87 }
88  
89 # test non-hash
90 # Note: since 'SchemaData' was introduced (Github Issue #1) most of
91 # this logic is now redundant/not needed...
92 sub _is_eq {
93 21472 ▶ my ($self, $old, $new) = @_;
Subroutine _is_eq: called CC=24 SCAR=42.8
94
95 # if both undef, they are equal:
96 21472 ▶ return 1 if(!defined $old && !defined $new);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $old or defined $new 0 21472 21472
Condition33%1/3
defined $old or defined $new
l!l&&r!l&&!r
2147200
MC/DC0%0/2
defined $old or defined $new
defined $old defined $new
Truth table33%1/3
A or B
A= defined $oldB= defined $new
ABresult
1X1
011
000
97
98 21472 my ($o_ref,$n_ref) = (ref $old,ref $new);
99
100 # one is a ref and the other isn't, obviously not equal:
101 21472 ▶ return 0 if ($n_ref && !$o_ref || $o_ref && !$n_ref);
Branch: 1 branch
BranchTrueFalseTotal
if $n_ref and not $o_ref or $o_ref and not $n_ref 0 21472 21472
Condition55%5/9
$n_ref and not $o_ref or $o_ref and not $n_ref
l!l&&r!l&&!r
0021472
$n_ref and not $o_ref
!ll&&!rl&&r
208676050
$o_ref and not $n_ref
!ll&&!rl&&r
208676050
MC/DC0%0/4
$n_ref and not $o_ref or $o_ref and not $n_ref
$n_ref not $o_ref $o_ref not $n_ref
Truth table28%2/7
A and not(B) or C and not(D)
A= $n_refB= not $o_refC= $o_refD= not $n_ref
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
102
103 # both refs:
104 21472 ▶ if($o_ref && $n_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o_ref and $n_ref) 605 20867 21472
Condition66%2/3
$o_ref and $n_ref
!ll&&!rl&&r
208670605
MC/DC50%1/2
$o_ref and $n_ref
$o_ref $n_ref
Truth table66%2/3
A and B
A= $o_refB= $n_ref
ABresult
0X0
100
111
105 # If they are not the same kind of ref, they obviously aren't equal:
106 605 ▶ return 0 unless ($o_ref eq $n_ref);
Branch: 1 branch
BranchTrueFalseTotal
unless $o_ref eq $n_ref 0 605 605
107
108 605 ▶ if($n_ref eq 'CODE') {
Branch: 5 branches
BranchTrueFalseTotal
if ($n_ref eq 'CODE') { } 0 605 605
elsif ($n_ref eq 'SCALAR' or $n_ref eq 'REF') { } 0 605 605
elsif ($n_ref eq 'ARRAY') { } 605 0 605
elsif ($n_ref eq 'HASH') { } 0 0 0
elsif (builtin::blessed($new)) { } 0 0 0
109 # We can't tell the difference between CodeRefs, but we don't want
110 # those cases to show up as changed, so we call them equal:
111 0 return 1;
112 }
113 ▶ elsif($n_ref eq 'SCALAR' || $n_ref eq 'REF') {
Condition33%1/3
$n_ref eq "SCALAR" or $n_ref eq "REF"
l!l&&r!l&&!r
00605
MC/DC0%0/2
$n_ref eq "SCALAR" or $n_ref eq "REF"
$n_ref eq "SCALAR" $n_ref eq "REF"
Truth table33%1/3
A or B
A= $n_ref eq "SCALAR"B= $n_ref eq "REF"
ABresult
1X1
011
000
114 # For ScalarRefs, compare their referants:
115 0 return $self->_is_eq($$old,$$new);
116 }
117 elsif($n_ref eq 'ARRAY') {
118 # If they don't have the same number of elements, they aren't equal:
119 605 ▶ return 0 unless (scalar @$new == scalar @$old);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$new == scalar @$old 11 594 605
120
121 # If they are both empty, they are equal:
122 594 ▶ return 1 if (scalar @$new == 0 && scalar @$old == 0);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$new == 0 and scalar @$old == 0 0 594 594
Condition33%1/3
scalar @$new == 0 and scalar @$old == 0
!ll&&!rl&&r
59400
MC/DC0%0/2
scalar @$new == 0 and scalar @$old == 0
scalar @$new == 0 scalar @$old == 0
Truth table33%1/3
A and B
A= scalar @$new == 0B= scalar @$old == 0
ABresult
0X0
100
111
123
124 # iterate both sides:
125 594 my $i = 0;
126 594 for my $n_el (@$new) {
127 1078 my $o_el = $old->[$i++];
128 # Return 0 as soon as the first element is not equal:
129 1078 ▶ return 0 unless ($self->_is_eq($o_el,$n_el));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_eq($o_el, $n_el) 22 1056 1078
130 }
131
132 # If we made it here, then all the elements were equal above:
133 572 return 1;
134 }
135 elsif($n_ref eq 'HASH') {
136 # This case will only be called by us for HashRef elements of ArrayRef
137 # (case above). The main _info_diff() function handles HashRef's itself.
138 # Also note that from this point it is a true/false equality -- there
139 # is no more selective merging of hashes, showing only different keys
140 #
141 # If the hashes are equal, the diff should be undef:
142 0 ▶ return $self->_info_diff($old,$new) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->_info_diff($old, $new) ? : 0 0 0
143 }
144 elsif(blessed $new) {
145 # If this is an object reference, just compare the classes, since we don't
146 # know how to compare object data and won't try:
147 0 return $self->_is_eq(blessed($old),blessed($new));
148 }
149 else {
150 0 die "Unexpected ref type '$n_ref'";
151 }
152 }
153  
154 # simple scalar value comparison:
155 20867 ▶ return (defined $old && defined $new && "$old" eq "$new");
Condition50%3/6
defined $old && defined $new && "$old" eq "$new"
!ll&&!rl&&r
05720810
defined $old and defined $new
!ll&&!rl&&r
0020867
MC/DC33%1/3
defined $old && defined $new && "$old" eq "$new"
defined $old defined $new "$old" eq "$new"
Truth table50%2/4
A and B && C
A= defined $oldB= defined $newC= "$old" eq "$new"
ABCresult
0XX0
10X0
1100
1111
156 }
157  
158  
159 sub _coerce_list_hash {
160 $_[0] && ! ref($_[0]) ? { $_[0] => 1 } :
161 7 ▶ ref($_[0]) eq 'ARRAY' ? { map {$_=>1} @{$_[0]} } : $_[0];
Subroutine _coerce_list_hash: called CC=4 SCAR=13.9
Branch: 2 branches
BranchTrueFalseTotal
$_[0] && !ref($_[0]) ? : 5 2 7
ref $_[0] eq 'ARRAY' ? : 1 1 2
Condition66%2/3
$_[0] and not ref $_[0]
!ll&&!rl&&r
025
MC/DC50%1/2
$_[0] and not ref $_[0]
$_[0] not ref $_[0]
Truth table66%2/3
A and not(B)
A= $_[0]B= not ref $_[0]
ABresult
0X0
100
111
162 }
163  
164  
165 sub _coerce_schema_diff {
166 100 ▶ blessed $_[0] ? $_[0] : DBIx::Class::Schema::Diff::Schema->new($_[0]);
Subroutine _coerce_schema_diff: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) ? : 76 24 100
167 }
168  
169  
170 sub _coerce_schema_data {
171 48 ▶ my ($v) = @_;
Subroutine _coerce_schema_data: called CC=8 SCAR=20.8
172 48 my $rt = ref($v);
173 48 ▶ if($rt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rt) { } 32 16 48
174 32 ▶ if(blessed($v) && blessed($v) eq 'DBIx::Class::Schema::Diff::SchemaData') {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($v) and builtin::blessed($v) eq 'DBIx::Class::Schema::Diff::SchemaData') { } 1 31 32
elsif ($rt eq 'HASH') { } 2 29 31
Condition100%3/3
builtin::blessed($v) and builtin::blessed($v) eq "DBIx::Class::Schema::Diff::SchemaData"
!ll&&!rl&&r
2291
MC/DC100%2/2
builtin::blessed($v) and builtin::blessed($v) eq "DBIx::Class::Schema::Diff::SchemaData"
builtin::blessed($v) builtin::blessed($v) eq "DBIx::Class::Schema::Diff::SchemaData"
Truth table100%3/3
A and B
A= builtin::blessed($v)B= builtin::blessed($v) eq "DBIx::Class::Schema::Diff::SchemaData"
ABresult
0X0
100
111
175 1 return $v;
176 }
177 elsif($rt eq 'HASH') {
178 2 return DBIx::Class::Schema::Diff::SchemaData->new({ data => $v });
179 }
180 else {
181 # Assume all other ref types are schema instances:
182 29 return DBIx::Class::Schema::Diff::SchemaData->new({ schema => $v });
183 }
184 }
185 else {
186 16 ▶ unless(Module::Runtime::is_module_name($v)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Module::Runtime::is_module_name($v)) 4 12 16
187 4 my $file = file($v)->absolute;
188 4 ▶ if(-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file) 4 0 4
189 # Assume it is a json file and try to decode it:
190 4 local $/;
191 4 ▶ open( my $fh, '<', $file ) or die "Could not open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Could not open ${file}: $!" 0 4 4
192 4 my $json_text = <$fh>;
193 4 close $fh;
194 4 my $data = JSON::decode_json($json_text);
195 4 return DBIx::Class::Schema::Diff::SchemaData->new({ data => $data });
196 }
197 }
198 12 return DBIx::Class::Schema::Diff::SchemaData->new({ schema => $v });
199 }
200 }
201  
202  
203 5 1;
204  
205  
206 __END__
207  
208 =pod
209  
210 =head1 NAME
211  
212 DBIx::Class::Schema::Diff::Schema::Role::Common - common role for DBIx::Class::Schema::Diff
213  
214 =head1 DESCRIPTION
215  
216 This role/class is used internally by L<DBIx::Class::Schema::Diff> and is not meant to be called directly.
217  
218 Please refer to the main L<DBIx::Class::Schema::Diff> documentation for more info.
219  
220 =head1 AUTHOR
221  
222 Henry Van Styn <vanstyn@cpan.org>
223  
224 =head1 COPYRIGHT AND LICENSE
225  
226 This software is copyright (c) 2014 by IntelliTree Solutions llc.
227  
228 This is free software; you can redistribute it and/or modify it under
229 the same terms as the Perl 5 programming language system itself.
230  
231 =cut
↑ Summary Diff/State.pm »