blib/lib/DBIx/Compare/ContentChecksum/mysql.pm

filter: Statementstmt 10.6% 13 / 122 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 27.2% 3 / 11 Podpod n/a totaltotal 7.7% 16 / 206 CC 32 SCAR 43.5
CC
32
Cov
7%
compare
156.0
compare_fields_checksum
110.0
field_checksum
30.0
CRAP 77.3
↑ Summary
1 package DBIx::Compare::ContentChecksum::mysql;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '1.2';
8  
9 1 require DBIx::Compare;
10  
11 { package compare_mysql_checksum;
12  
13 1 our @ISA = qw( db_comparison );
14
15 0 ▶ sub compare {
Subroutine compare: not called CC=12 SCAR=50.5
16 0 my $self = shift;
17
18 0 my $tables = $self->compare_table_lists;
19 0 my $tfields = $self->compare_table_fields;
20 0 my $rows = $self->compare_row_counts;
21 0 my $fields = $self->compare_fields_checksum;
22  
23 0 ▶ if ($tables && $tfields && $rows && $fields){
Branch: 1 branch
BranchTrueFalseTotal
if ($tables and $tfields and $rows and $fields) { } 0 0 0
Condition0%0/9
$tables and $tfields and $rows and $fields
!ll&&!rl&&r
000
$tables and $tfields and $rows
!ll&&!rl&&r
000
$tables and $tfields
!ll&&!rl&&r
000
MC/DC0%0/4
$tables and $tfields and $rows and $fields
$tables $tfields $rows $fields
Truth table0%0/5
A and B and C and D
A= $tablesB= $tfieldsC= $rowsD= $fields
ABCDresult
0XXX0
10XX0
110X0
11100
11111
24 0 return 1;
25 0 warn "No differences were found\n";
26 } else {
27 0 ▶ unless ($tables){
Branch: 1 branch
BranchTrueFalseTotal
unless ($tables) 0 0 0
28 0 warn "Table Lists are different\n".
29 "\tComparing the common tables...\n";
30 }
31 0 ▶ unless ($rows){
Branch: 1 branch
BranchTrueFalseTotal
unless ($rows) 0 0 0
32 0 warn "Row counts in some tables are different\n".
33 "\tComparing the content of tables with the same row count...\n";
34 }
35 0 ▶ unless ($tfields){
Branch: 1 branch
BranchTrueFalseTotal
unless ($tfields) 0 0 0
36 0 warn "Table fields are different\n".
37 "\tComparing the common fields...\n";
38 }
39 0 ▶ unless ($fields){
Branch: 1 branch
BranchTrueFalseTotal
unless ($fields) 0 0 0
40 0 warn "Data are different in some tables\n";
41 }
42 0 my $hDiffs = $self->get_differences;
43 0 ▶ if (%$hDiffs){
Branch: 1 branch
BranchTrueFalseTotal
if (%$hDiffs) 0 0 0
44 0 ▶ while (my ($type,$aErrors) = each %$hDiffs){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$hDiffs) 0 0 0
45 0 warn "$type:\n";
46 0 for my $error (@$aErrors){
47 0 warn "\t$error\n";
48 }
49 }
50 }
51 0 return;
52 }
53 }
54 0 ▶ sub compare_fields_checksum {
Subroutine compare_fields_checksum: not called CC=10 SCAR=47.0
55 0 my $self = shift;
56 0 my $aTables;
57 0 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
58 0 $aTables = [shift];
59 } else {
60 0 $aTables = $self->similar_tables;
61 }
62 0 my ($dbh1,$dbh2) = $self->get_dbh;
63 0 my $same = 1;
64 0 TABLE:for my $table (@$aTables){
65 0 my @aErrors = ();
66 0 my @aFields = $self->field_list($table);
67 0 FIELD:for my $field (@aFields){
68 0 my ($string1,$string2) = $self->field_checksum($table,$field);
69 0 ▶ unless ((($string1 && $string2) && ($string1 eq $string2)) || (!$string1 && !$string2)){
Branch: 1 branch
BranchTrueFalseTotal
unless ($string1 and $string2 and $string1 eq $string2 or not $string1 || $string2) 0 0 0
Condition0%0/12
$string1 and $string2 and $string1 eq $string2 or not $string1 || $string2
l!l&&r!l&&!r
000
$string1 and $string2 and $string1 eq $string2
!ll&&!rl&&r
000
$string1 and $string2
!ll&&!rl&&r
000
$string1 || $string2
l!l&&r!l&&!r
000
MC/DC0%0/5
$string1 and $string2 and $string1 eq $string2 or not $string1 || $string2
$string1 $string2 $string1 eq $string2 $string1 $string2
Truth table0%0/10
A and B and C or not(D || E)
A= $string1B= $string2C= $string1 eq $string2D= $string1E= $string2
ABCDEresult
111XX1
0XX001
10X001
110001
0XX1X0
0XX010
10X1X0
10X010
1101X0
110010
70 0 push(@aErrors, $field);
71 }
72 }
73 0 ▶ if (@aErrors){
Branch: 1 branch
BranchTrueFalseTotal
if (@aErrors) 0 0 0
74 0 $self->add_errors("Table $table fields",@aErrors);
75 0 $same = undef;
76 }
77 }
78 0 return $same;
79 }
80 0 ▶ sub field_checksum {
Subroutine field_checksum: not called CC=5 SCAR=34.0
81 0 my $self = shift;
82 0 my ($table,$field) = @_;
83 0 my @aDBH = $self->get_dbh;
84 0 my @aChecksum;
85 0 for my $dbh (@aDBH){
86 0 my $statement = "select md5(group_concat($field";
87 0 my $statement_desc = $statement;
88 0 ▶ if (my $primary_keys = $self->get_primary_keys($table,$dbh)){
Branch: 1 branch
BranchTrueFalseTotal
if (my $primary_keys = $self->get_primary_keys($table, $dbh)) 0 0 0
89 0 $statement .= " order by $primary_keys";
90 # make the descending keys
91 0 $statement_desc .= " order by $primary_keys desc";
92 0 $statement_desc =~ s/,/ desc,/g; # descend compound keys
93 }
94 0 $statement .= ")) from $table";
95 0 $statement_desc .= ")) from $table";
96 0 my $checksum = $self->fetch_singlefield($statement,$dbh);
97 0 ▶ unless (($statement eq $statement_desc)||!$checksum){ # i.e. no primary key, or $checksum = null
Branch: 1 branch
BranchTrueFalseTotal
unless ($statement eq $statement_desc or not $checksum) 0 0 0
Condition0%0/3
$statement eq $statement_desc or not $checksum
l!l&&r!l&&!r
000
MC/DC0%0/2
$statement eq $statement_desc or not $checksum
$statement eq $statement_desc not $checksum
Truth table0%0/3
A or not(B)
A= $statement eq $statement_descB= not $checksum
ABresult
1X1
011
000
98 0 my $checksum2 = $self->fetch_singlefield($statement_desc,$dbh);
99 0 $checksum .= $checksum2;
100 }
101 0 push (@aChecksum,$checksum);
102 }
103 0 return @aChecksum;
104 }
105 0 ▶ sub do_group_concat_max_len {
Subroutine do_group_concat_max_len: not called CC=2 SCAR=17.9
106 0 my $self = shift;
107 0 my $concat_length = shift;
108 0 $self->{ _group_concat_max_len } = $concat_length;
109 0 my ($dbh1,$dbh2) = $self->get_dbh;
110 0 eval {
111 0 $dbh1->do("set group_concat_max_len = $concat_length");
112 0 $dbh2->do("set group_concat_max_len = $concat_length");
113 };
114 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
115 0 die "DBIx::Compare::ContentChecksum::mysql ERROR: Could not 'set group_concat_max_len'\n".$@;
116 }
117 }
118 0 ▶ sub group_concat_max_len {
Subroutine group_concat_max_len: not called CC=3 SCAR=24.8
119 0 my $self = shift;
120 0 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
121 0 $self->do_group_concat_max_len(shift);
122 } else {
123 0 ▶ if (defined $self->{ _group_concat_max_len }){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_group_concat_max_len'}) { } 0 0 0
124 0 return $self->{ _group_concat_max_len };
125 } else {
126 0 return $self->mysql_group_concat_max_len; # mysql default
127 }
128 }
129 }
130 0 ▶ sub mysql_group_concat_max_len {
Subroutine mysql_group_concat_max_len: not called CC=1 SCAR=6.9
131 0 my $self = shift;
132 0 my ($dbh1,$dbh2) = $self->get_dbh;
133 0 my @aLength1 = $self->fetchlist_singlerow("show variables like 'group_concat_max_len'",$dbh1);
134 0 my @aLength2 = $self->fetchlist_singlerow("show variables like 'group_concat_max_len'",$dbh2);
135 0 return ($aLength1[1],$aLength2[1]);
136 }
137 0 ▶ sub fetchlist_singlerow {
Subroutine fetchlist_singlerow: not called CC=3 SCAR=24.8
138 0 my $self = shift;
139 0 my $statement = shift;
140 0 my $dbh = shift;
141 0 my @aResult_Row;
142 0 eval {
143 0 my $sth = $dbh->prepare($statement);
144 0 $sth->execute();
145 0 @aResult_Row = $sth->fetchrow_array();
146 0 $sth->finish();
147 };
148 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
149 0 die $@;
150 } else {
151 0 ▶ if (@aResult_Row){
Branch: 1 branch
BranchTrueFalseTotal
if (@aResult_Row) { } 0 0 0
152 0 return @aResult_Row;
153 } else {
154 0 return;
155 }
156 }
157 }
158 0 ▶ sub fetch_singlefield {
Subroutine fetch_singlefield: not called CC=3 SCAR=24.8
159 0 my $self = shift;
160 0 my $statement = shift;
161 0 my $dbh = shift;
162 0 my $value;
163 0 eval {
164 0 my $sth = $dbh->prepare($statement);
165 0 $sth->execute();
166 0 $sth->bind_columns(undef, \$value);
167 0 $sth->fetch();
168 ### looking for mysql warnings
169 0 ▶ if ($sth->{mysql_warning_count}){
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->{"mysql_warning_count"}) 0 0 0
170 0 die "*** warning ***\n\t$statement\n";
171 }
172 0 $sth->finish();
173 };
174 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
175 0 die $@;
176 } else {
177 0 return $value;
178 }
179 }
180  
181 }
182  
183  
184 1 1;
185  
186 __END__
↑ Summary