Diff.pm

filter: Statementstmt 6.5% 17 / 261 Branchbran 0.0% 0 / 144 Conditioncond 0.0% 0 / 104 MC/DCmcdc 0.0% 0 / 57 Subroutinesub 21.7% 5 / 23 Podpod 100% 5 / 5 totaltotal 4.5% 27 / 594 CC 121 SCAR 58.1
CC
121
Cov
3%
_diff_indices
812.0
_diff_fields
600.0
_diff_primary_key
210.0
CRAP 332.7
↑ Summary Diff/Table.pm »
1 package MySQL::Diff;
2  
3 =head1 NAME
4  
5 MySQL::Diff - Generates a database upgrade instruction set
6  
7 =head1 SYNOPSIS
8  
9 use MySQL::Diff;
10  
11 my $md = MySQL::Diff->new( %options );
12 my $db1 = $md->register_db($ARGV[0], 1);
13 my $db2 = $md->register_db($ARGV[1], 2);
14 my $diffs = $md->diff();
15  
16 =head1 DESCRIPTION
17  
18 Generates the SQL instructions required to upgrade the first database to match
19 the second.
20  
21 =cut
22  
23 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 2 our $VERSION = '0.60';
27  
28 # ------------------------------------------------------------------------------
29 # Libraries
30  
31 2 ▶use MySQL::Diff::Database;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use MySQL::Diff::Utils qw(debug debug_level debug_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 # ------------------------------------------------------------------------------
37  
38 =head1 METHODS
39  
40 =head2 Constructor
41  
42 =over 4
43  
44 =item new( %options )
45  
46 Instantiate the objects, providing the command line options for database
47 access and process requirements.
48  
49 =back
50  
51 =cut
52  
53 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
54 0 my $class = shift;
55 0 my %hash = @_;
56 0 my $self = {};
57 0 ▶ bless $self, ref $class || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
58  
59 0 $self->{opts} = \%hash;
60  
61 0 ▶ if($hash{debug}) { debug_level($hash{debug}) ; delete $hash{debug}; }
Branch: 1 branch
BranchTrueFalseTotal
if ($hash{"debug"}) 0 0 0
62 0 ▶ if($hash{debug_file}) { debug_file($hash{debug_file}) ; delete $hash{debug_file}; }
Branch: 1 branch
BranchTrueFalseTotal
if ($hash{"debug_file"}) 0 0 0
63  
64 0 debug(3,"\nconstructing new MySQL::Diff");
65  
66 0 return $self;
67 }
68  
69 =head2 Public Methods
70  
71 Fuller documentation will appear here in time :)
72  
73 =over 4
74  
75 =item * register_db($name,$inx)
76  
77 Reference the database, and setup a connection. The name can be an already
78 existing 'MySQL::Diff::Database' database object. The index can be '1' or '2',
79 and refers both to the order of the diff, and to the host, port, username and
80 password arguments that have been supplied.
81  
82 =cut
83  
84 0 ▶sub register_db {
Subroutine register_db: not called CC=4 SCAR=30.0
Pod documented
85 0 my ($self, $name, $inx) = @_;
86 0 ▶ return unless $inx == 1 || $inx == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $inx == 1 or $inx == 2 0 0 0
Condition0%0/3
$inx == 1 or $inx == 2
l!l&&r!l&&!r
000
MC/DC0%0/2
$inx == 1 or $inx == 2
$inx == 1 $inx == 2
Truth table0%0/3
A or B
A= $inx == 1B= $inx == 2
ABresult
1X1
011
000
87  
88 0 ▶ my $db = ref $name eq 'MySQL::Diff::Database' ? $name : $self->_load_database($name,$inx);
Branch: 1 branch
BranchTrueFalseTotal
ref $name eq 'MySQL::Diff::Database' ? : 0 0 0
89 0 $self->{databases}[$inx-1] = $db;
90 0 return $db;
91 }
92  
93 =item * db1()
94  
95 =item * db2()
96  
97 Return the first and second databases registered via C<register_db()>.
98  
99 =cut
100  
101 0 ▶sub db1 { shift->{databases}->[0] }
Subroutine db1: not called CC=1 SCAR=6.9
Pod documented
102 0 ▶sub db2 { shift->{databases}->[1] }
Subroutine db2: not called CC=1 SCAR=6.9
Pod documented
103  
104 =item * diff()
105  
106 Performs the diff, returning a string containing the commands needed to change
107 the schema of the first database into that of the second.
108  
109 =back
110  
111 =cut
112  
113 0 ▶sub diff {
Subroutine diff: not called CC=10 SCAR=47.0
Pod documented
114 0 my $self = shift;
115 0 my @changes;
116 0 my %used_tables = ();
117  
118 0 debug(1, "\ncomparing databases");
119  
120 0 for my $table1 ($self->db1->tables()) {
121 0 my $name = $table1->name();
122 0 $used_tables{'-- '. $name} = 1;
123 0 debug(4, "table 1 $name = ".Dumper($table1));
124 0 debug(2,"looking at tables called '$name'");
125 0 ▶ if (my $table2 = $self->db2->table_by_name($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $table2 = $self->db2->table_by_name($name)) { } 0 0 0
126 0 debug(3,"comparing tables called '$name'");
127 0 push @changes, $self->_diff_tables($table1, $table2);
128 } else {
129 0 debug(3,"table '$name' dropped");
130 push @changes, "DROP TABLE $name;\n\n"
131 0 ▶ unless $self->{opts}{'only-both'} || $self->{opts}{'keep-old-tables'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"opts"}{"only-both"} or $self->{"opts"}{"keep-old-tables"} 0 0 0
Condition0%0/3
$self->{"opts"}{"only-both"} or $self->{"opts"}{"keep-old-tables"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"opts"}{"only-both"} or $self->{"opts"}{"keep-old-tables"}
$self->{"opts"}{"only-both"} $self->{"opts"}{"keep-old-tables"}
Truth table0%0/3
A or B
A= $self->{"opts"}{"only-both"}B= $self->{"opts"}{"keep-old-tables"}
ABresult
1X1
011
000
132 }
133 }
134  
135 0 for my $table2 ($self->db2->tables()) {
136 0 my $name = $table2->name();
137 0 $used_tables{'-- '. $name} = 1;
138 0 debug(4, "table 2 $name = ".Dumper($table2));
139 0 ▶ if (! $self->db1->table_by_name($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->db1->table_by_name($name)) 0 0 0
140 0 debug(3,"table '$name' added");
141 0 debug(4,"table '$name' added '".$table2->def()."'");
142 push @changes, $table2->def() . "\n"
143 0 ▶ unless $self->{opts}{'only-both'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"opts"}{"only-both"} 0 0 0
144 }
145 }
146  
147 0 debug(4,join '', @changes);
148  
149 0 my $out = '';
150 0 ▶ if (@changes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@changes) 0 0 0
151 0 ▶ if (!$self->{opts}{'list-tables'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'opts'}{'list-tables'}) { } 0 0 0
152 0 $out .= $self->_diff_banner();
153 }
154 else {
155 0 $out .= "-- TABLES LIST \n";
156 0 $out .= join "\n", keys %used_tables;
157 0 $out .= "\n-- END OF TABLES LIST \n";
158 }
159 0 $out .= join '', @changes;
160 }
161 0 return $out;
162 }
163  
164 # ------------------------------------------------------------------------------
165 # Private Methods
166  
167 0 ▶sub _diff_banner {
Subroutine _diff_banner: not called CC=3 SCAR=24.8
168 0 my ($self) = @_;
169  
170 0 my $summary1 = $self->db1->summary();
171 0 my $summary2 = $self->db2->summary();
172  
173 my $opt_text =
174 join ', ',
175 0 ▶ map { $self->{opts}{$_} eq '1' ? $_ : "$_=$self->{opts}{$_}" }
Branch: 1 branch
BranchTrueFalseTotal
$self->{'opts'}{$_} eq '1' ? : 0 0 0
176 0 keys %{$self->{opts}};
177 0 ▶ $opt_text = "## Options: $opt_text\n" if $opt_text;
Branch: 1 branch
BranchTrueFalseTotal
if $opt_text 0 0 0
178  
179 0 my $now = scalar localtime();
180 0 return <<EOF;
181 ## mysqldiff $VERSION
182 ##
183 ## Run on $now
184 $opt_text##
185 ## --- $summary1
186 ## +++ $summary2
187  
188 EOF
189 }
190  
191 0 ▶sub _diff_tables {
Subroutine _diff_tables: not called CC=2 SCAR=17.9
192 0 my $self = shift;
193 0 my @changes = (
194 $self->_diff_fields(@_),
195 $self->_diff_indices(@_),
196 $self->_diff_primary_key(@_),
197 $self->_diff_foreign_key(@_),
198 $self->_diff_options(@_)
199 );
200  
201 0 ▶ $changes[-1] =~ s/\n*$/\n/ if (@changes);
Branch: 1 branch
BranchTrueFalseTotal
if @changes 0 0 0
202 0 return @changes;
203 }
204  
205 0 ▶sub _diff_fields {
Subroutine _diff_fields: not called CC=24 SCAR=64.0
206 0 my ($self, $table1, $table2) = @_;
207  
208 0 my $name1 = $table1->name();
209  
210 0 my $fields1 = $table1->fields;
211 0 my $fields2 = $table2->fields;
212  
213 0 ▶ return () unless $fields1 || $fields2;
Branch: 1 branch
BranchTrueFalseTotal
unless $fields1 or $fields2 0 0 0
Condition0%0/3
$fields1 or $fields2
l!l&&r!l&&!r
000
MC/DC0%0/2
$fields1 or $fields2
$fields1 $fields2
Truth table0%0/3
A or B
A= $fields1B= $fields2
ABresult
1X1
011
000
214  
215 0 my @changes;
216
217 0 ▶ if($fields1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields1) 0 0 0
218 0 for my $field (keys %$fields1) {
219 0 debug(3,"table1 had field '$field'");
220 0 my $f1 = $fields1->{$field};
221 0 my $f2 = $fields2->{$field};
222 0 ▶ if ($fields2 && $f2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fields2 and $f2) { } 0 0 0
elsif (not $self->{'opts'}{'keep-old-columns'}) { } 0 0 0
Condition0%0/3
$fields2 and $f2
!ll&&!rl&&r
000
MC/DC0%0/2
$fields2 and $f2
$fields2 $f2
Truth table0%0/3
A and B
A= $fields2B= $f2
ABresult
0X0
100
111
223 0 ▶ if ($self->{opts}{tolerant}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opts"}{"tolerant"}) 0 0 0
224 0 for ($f1, $f2) {
225 0 s/ COLLATE [\w_]+//gi;
226 }
227 }
228 0 ▶ if ($f1 ne $f2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f1 ne $f2) 0 0 0
229 0 ▶ if (not $self->{opts}{tolerant} or
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"opts"}{"tolerant"} or not $f1 =~ /$f2\(\d+,\d+\)/ and $f1 ne "$f2 DEFAULT '' NOT NULL" and $f1 ne "$f2 NOT NULL") 0 0 0
Condition0%0/9
not $self->{"opts"}{"tolerant"} or not $f1 =~ /$f2\(\d+,\d+\)/ and $f1 ne "$f2 DEFAULT '' NOT NULL" and $f1 ne "$f2 NOT NULL"
l!l&&r!l&&!r
000
not $f1 =~ /$f2\(\d+,\d+\)/ and $f1 ne "$f2 DEFAULT '' NOT NULL" and $f1 ne "$f2 NOT NULL"
!ll&&!rl&&r
000
not $f1 =~ /$f2\(\d+,\d+\)/ and $f1 ne "$f2 DEFAULT '' NOT NULL"
!ll&&!rl&&r
000
MC/DC0%0/4
not $self->{"opts"}{"tolerant"} or not $f1 =~ /$f2\(\d+,\d+\)/ and $f1 ne "$f2 DEFAULT '' NOT NULL" and $f1 ne "$f2 NOT NULL"
not $self->{"opts"}{"tolerant"} not $f1 =~ /$f2\(\d+,\d+\)/ $f1 ne "$f2 DEFAULT '' NOT NULL" $f1 ne "$f2 NOT NULL"
Truth table0%0/5
not(A) or not(B) and C and D
A= not $self->{"opts"}{"tolerant"}B= not $f1 =~ /$f2\(\d+,\d+\)/C= $f1 ne "$f2 DEFAULT '' NOT NULL"D= $f1 ne "$f2 NOT NULL"
ABCDresult
1XXX1
01111
00XX0
010X0
01100
230 (($f1 !~ m/$f2\(\d+,\d+\)/) and
231 ($f1 ne "$f2 DEFAULT '' NOT NULL") and
232 ($f1 ne "$f2 NOT NULL") ))
233 {
234 0 debug(3,"field '$field' changed");
235  
236 0 my $change = "ALTER TABLE $name1 CHANGE COLUMN $field $field $f2;";
237 0 ▶ $change .= " # was $f1" unless $self->{opts}{'no-old-defs'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"opts"}{"no-old-defs"} 0 0 0
238 0 $change .= "\n";
239 0 push @changes, $change;
240 }
241 }
242 } elsif (!$self->{opts}{'keep-old-columns'}) {
243 0 debug(3,"field '$field' removed");
244 0 my $change = "ALTER TABLE $name1 DROP COLUMN $field;";
245 0 ▶ $change .= " # was $fields1->{$field}" unless $self->{opts}{'no-old-defs'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"opts"}{"no-old-defs"} 0 0 0
246 0 $change .= "\n";
247 0 push @changes, $change;
248 }
249 }
250 }
251  
252 0 ▶ if($fields2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields2) 0 0 0
253 0 for my $field (keys %$fields2) {
254 0 ▶ unless($fields1 && $fields1->{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fields1 and $fields1->{$field}) 0 0 0
Condition0%0/3
$fields1 and $fields1->{$field}
!ll&&!rl&&r
000
MC/DC0%0/2
$fields1 and $fields1->{$field}
$fields1 $fields1->{$field}
Truth table0%0/3
A and B
A= $fields1B= $fields1->{$field}
ABresult
0X0
100
111
255 0 debug(3,"field '$field' added");
256 0 my $changes = "ALTER TABLE $name1 ADD COLUMN $field $fields2->{$field}";
257 0 ▶ if ($table2->is_auto_inc($field)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table2->is_auto_inc($field)) 0 0 0
258 0 ▶ if ($table2->isa_primary($field)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($table2->isa_primary($field)) { } 0 0 0
elsif ($table2->is_unique($field)) { } 0 0 0
259 0 $changes .= ' PRIMARY KEY';
260 } elsif ($table2->is_unique($field)) {
261 0 $changes .= ' UNIQUE KEY';
262 }
263 }
264 0 push @changes, "$changes;\n";
265 }
266 }
267 }
268  
269 0 return @changes;
270 }
271  
272 0 ▶sub _diff_indices {
Subroutine _diff_indices: not called CC=28 SCAR=67.0
273 0 my ($self, $table1, $table2) = @_;
274  
275 0 my $name1 = $table1->name();
276  
277 0 my $indices1 = $table1->indices();
278 0 my $indices2 = $table2->indices();
279  
280 0 ▶ return () unless $indices1 || $indices2;
Branch: 1 branch
BranchTrueFalseTotal
unless $indices1 or $indices2 0 0 0
Condition0%0/3
$indices1 or $indices2
l!l&&r!l&&!r
000
MC/DC0%0/2
$indices1 or $indices2
$indices1 $indices2
Truth table0%0/3
A or B
A= $indices1B= $indices2
ABresult
1X1
011
000
281  
282 0 my @changes;
283  
284 0 ▶ if($indices1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indices1) 0 0 0
285 0 for my $index (keys %$indices1) {
286 0 debug(3,"table1 had index '$index'");
287 0 ▶ my $old_type = $table1->is_unique($index) ? 'UNIQUE' :
Branch: 2 branches
BranchTrueFalseTotal
$table1->is_unique($index) ? : 0 0 0
$table1->is_fulltext($index) ? : 0 0 0
288 $table1->is_fulltext($index) ? 'FULLTEXT INDEX' : 'INDEX';
289  
290 0 ▶ if ($indices2 && $indices2->{$index}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indices2 and $indices2->{$index}) { } 0 0 0
Condition0%0/3
$indices2 and $indices2->{$index}
!ll&&!rl&&r
000
MC/DC0%0/2
$indices2 and $indices2->{$index}
$indices2 $indices2->{$index}
Truth table0%0/3
A and B
A= $indices2B= $indices2->{$index}
ABresult
0X0
100
111
291 0 ▶ if( ($indices1->{$index} ne $indices2->{$index}) or
Branch: 1 branch
BranchTrueFalseTotal
if ($indices1->{$index} ne $indices2->{$index} or ($table1->is_unique($index) xor $table2->is_unique($index)) or ($table1->is_fulltext($index) xor $table2->is_fulltext($index))) 0 0 0
Condition0%0/14
$indices1->{$index} ne $indices2->{$index} or ($table1->is_unique($index) xor $table2->is_unique($index)) or ($table1->is_fulltext($index) xor $table2->is_fulltext($index))
l!l&&r!l&&!r
000
$indices1->{$index} ne $indices2->{$index} or ($table1->is_unique($index) xor $table2->is_unique($index))
l!l&&r!l&&!r
000
$table1->is_unique($index) xor $table2->is_unique($index)
l&&rl&&!r!l&&r!l&&!r
0000
$table1->is_fulltext($index) xor $table2->is_fulltext($index)
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/5
$indices1->{$index} ne $indices2->{$index} or ($table1->is_unique($index) xor $table2->is_unique($index)) or ($table1->is_fulltext($index) xor $table2->is_fulltext($index))
$indices1->{$index} ne $indices2->{$index} $table1->is_unique($index) $table2->is_unique($index) $table1->is_fulltext($index) $table2->is_fulltext($index)
Truth table0%0/11
A or B xor C or D xor E
A= $indices1->{$index} ne $indices2->{$index}B= $table1->is_unique($index)C= $table2->is_unique($index)D= $table1->is_fulltext($index)E= $table2->is_fulltext($index)
ABCDEresult
1XXXX1
010XX1
001XX1
011101
011011
000101
000011
011110
011000
000110
000000
292 ($table1->is_unique($index) xor $table2->is_unique($index)) or
293 ($table1->is_fulltext($index) xor $table2->is_fulltext($index)) )
294 {
295 0 debug(3,"index '$index' changed");
296 0 ▶ my $new_type = $table2->is_unique($index) ? 'UNIQUE' :
Branch: 2 branches
BranchTrueFalseTotal
$table2->is_unique($index) ? : 0 0 0
$table2->is_fulltext($index) ? : 0 0 0
297 $table2->is_fulltext($index) ? 'FULLTEXT INDEX' : 'INDEX';
298  
299 0 my $changes = "ALTER TABLE $name1 DROP INDEX $index;";
300 $changes .= " # was $old_type ($indices1->{$index})"
301 0 ▶ unless $self->{opts}{'no-old-defs'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"opts"}{"no-old-defs"} 0 0 0
302 0 $changes .= "\nALTER TABLE $name1 ADD $new_type $index ($indices2->{$index});\n";
303 0 push @changes, $changes;
304 }
305 } else {
306 0 debug(3,"index '$index' removed");
307 0 ▶ my $auto = _check_for_auto_col($table2, $indices1->{$index}, 1) || '';
Condition0%0/2
_check_for_auto_col($table2, $indices1->{$index}, 1) || ''
l!l
00
MC/DC0%0/1
_check_for_auto_col($table2, $indices1->{$index}, 1) || ''
_check_for_auto_col($table2, $indices1->{$index}, 1)
Truth table0%0/2
A
A= _check_for_auto_col($table2, $indices1->{$index}, 1)
Aresult
11
00
308 0 ▶ my $changes = $auto ? _index_auto_col($table1, $indices1->{$index}) : '';
Branch: 1 branch
BranchTrueFalseTotal
$auto ? : 0 0 0
309 0 $changes .= "ALTER TABLE $name1 DROP INDEX $index;";
310 $changes .= " # was $old_type ($indices1->{$index})"
311 0 ▶ unless $self->{opts}{'no-old-defs'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"opts"}{"no-old-defs"} 0 0 0
312 0 $changes .= "\n";
313 0 push @changes, $changes;
314 }
315 }
316 }
317  
318 0 ▶ if($indices2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indices2) 0 0 0
319 0 for my $index (keys %$indices2) {
320 0 ▶ next if($indices1 && $indices1->{$index});
Branch: 1 branch
BranchTrueFalseTotal
if $indices1 and $indices1->{$index} 0 0 0
Condition0%0/3
$indices1 and $indices1->{$index}
!ll&&!rl&&r
000
MC/DC0%0/2
$indices1 and $indices1->{$index}
$indices1 $indices1->{$index}
Truth table0%0/3
A and B
A= $indices1B= $indices1->{$index}
ABresult
0X0
100
111
321 next if(
322 0 ▶ !$table2->isa_primary($index) &&
Branch: 1 branch
BranchTrueFalseTotal
if not $table2->isa_primary($index) and $table2->is_unique($index) and _key_covers_auto_col($table2, $index) 0 0 0
Condition0%0/6
not $table2->isa_primary($index) and $table2->is_unique($index) and _key_covers_auto_col($table2, $index)
!ll&&!rl&&r
000
not $table2->isa_primary($index) and $table2->is_unique($index)
!ll&&!rl&&r
000
MC/DC0%0/3
not $table2->isa_primary($index) and $table2->is_unique($index) and _key_covers_auto_col($table2, $index)
not $table2->isa_primary($index) $table2->is_unique($index) _key_covers_auto_col($table2, $index)
Truth table0%0/4
not(A) and B and C
A= not $table2->isa_primary($index)B= $table2->is_unique($index)C= _key_covers_auto_col($table2, $index)
ABCresult
0XX0
10X0
1100
1111
323 $table2->is_unique($index) &&
324 _key_covers_auto_col($table2, $index)
325 );
326 0 debug(3,"index '$index' added");
327 0 ▶ my $new_type = $table2->is_unique($index) ? 'UNIQUE' : 'INDEX';
Branch: 1 branch
BranchTrueFalseTotal
$table2->is_unique($index) ? : 0 0 0
328 0 push @changes, "ALTER TABLE $name1 ADD $new_type $index ($indices2->{$index});\n";
329 }
330 }
331  
332 0 return @changes;
333 }
334  
335 0 ▶sub _diff_primary_key {
Subroutine _diff_primary_key: not called CC=14 SCAR=53.5
336 0 my ($self, $table1, $table2) = @_;
337  
338 0 my $name1 = $table1->name();
339  
340 0 my $primary1 = $table1->primary_key();
341 0 my $primary2 = $table2->primary_key();
342  
343 0 ▶ return () unless $primary1 || $primary2;
Branch: 1 branch
BranchTrueFalseTotal
unless $primary1 or $primary2 0 0 0
Condition0%0/3
$primary1 or $primary2
l!l&&r!l&&!r
000
MC/DC0%0/2
$primary1 or $primary2
$primary1 $primary2
Truth table0%0/3
A or B
A= $primary1B= $primary2
ABresult
1X1
011
000
344  
345 0 my @changes;
346
347 0 ▶ if ($primary1 && ! $primary2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($primary1 and not $primary2) 0 0 0
Condition0%0/3
$primary1 and not $primary2
!ll&&!rl&&r
000
MC/DC0%0/2
$primary1 and not $primary2
$primary1 not $primary2
Truth table0%0/3
A and not(B)
A= $primary1B= not $primary2
ABresult
0X0
100
111
348 0 debug(3,"primary key '$primary1' dropped");
349 0 my $changes = _index_auto_col($table2, $primary1);
350 0 $changes .= "ALTER TABLE $name1 DROP PRIMARY KEY;";
351 0 ▶ $changes .= " # was $primary1" unless $self->{opts}{'no-old-defs'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"opts"}{"no-old-defs"} 0 0 0
352 0 return ( "$changes\n" );
353 }
354  
355 0 ▶ if (! $primary1 && $primary2) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $primary1 and $primary2) 0 0 0
Condition0%0/3
not $primary1 and $primary2
!ll&&!rl&&r
000
MC/DC0%0/2
not $primary1 and $primary2
not $primary1 $primary2
Truth table0%0/3
not(A) and B
A= not $primary1B= $primary2
ABresult
0X0
100
111
356 0 debug(3,"primary key '$primary2' added");
357 0 ▶ return () if _key_covers_auto_col($table2, $primary2);
Branch: 1 branch
BranchTrueFalseTotal
if _key_covers_auto_col($table2, $primary2) 0 0 0
358 0 return ("ALTER TABLE $name1 ADD PRIMARY KEY $primary2;\n");
359 }
360  
361 0 ▶ if ($primary1 ne $primary2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($primary1 ne $primary2) 0 0 0
362 0 debug(3,"primary key changed");
363 0 ▶ my $auto = _check_for_auto_col($table2, $primary1) || '';
Condition0%0/2
_check_for_auto_col($table2, $primary1) || ''
l!l
00
MC/DC0%0/1
_check_for_auto_col($table2, $primary1) || ''
_check_for_auto_col($table2, $primary1)
Truth table0%0/2
A
A= _check_for_auto_col($table2, $primary1)
Aresult
11
00
364 0 ▶ my $changes = $auto ? _index_auto_col($table2, $auto) : '';
Branch: 1 branch
BranchTrueFalseTotal
$auto ? : 0 0 0
365 0 $changes .= "ALTER TABLE $name1 DROP PRIMARY KEY;";
366 0 ▶ $changes .= " # was $primary1" unless $self->{opts}{'no-old-defs'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"opts"}{"no-old-defs"} 0 0 0
367 0 $changes .= "\nALTER TABLE $name1 ADD PRIMARY KEY $primary2;\n";
368 0 ▶ $changes .= "ALTER TABLE $name1 DROP INDEX $auto;\n" if($auto);
Branch: 1 branch
BranchTrueFalseTotal
if $auto 0 0 0
369 0 push @changes, $changes;
370 }
371  
372 0 return @changes;
373 }
374  
375 0 ▶sub _diff_foreign_key {
Subroutine _diff_foreign_key: not called CC=14 SCAR=53.5
376 0 my ($self, $table1, $table2) = @_;
377  
378 0 my $name1 = $table1->name();
379  
380 0 my $fks1 = $table1->foreign_key();
381 0 my $fks2 = $table2->foreign_key();
382  
383 0 ▶ return () unless $fks1 || $fks2;
Branch: 1 branch
BranchTrueFalseTotal
unless $fks1 or $fks2 0 0 0
Condition0%0/3
$fks1 or $fks2
l!l&&r!l&&!r
000
MC/DC0%0/2
$fks1 or $fks2
$fks1 $fks2
Truth table0%0/3
A or B
A= $fks1B= $fks2
ABresult
1X1
011
000
384  
385 0 my @changes;
386
387 0 ▶ if($fks1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fks1) 0 0 0
388 0 for my $fk (keys %$fks1) {
389 0 debug(1,"$name1 has fk '$fk'");
390  
391 0 ▶ if ($fks2 && $fks2->{$fk}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fks2 and $fks2->{$fk}) { } 0 0 0
Condition0%0/3
$fks2 and $fks2->{$fk}
!ll&&!rl&&r
000
MC/DC0%0/2
$fks2 and $fks2->{$fk}
$fks2 $fks2->{$fk}
Truth table0%0/3
A and B
A= $fks2B= $fks2->{$fk}
ABresult
0X0
100
111
392 0 ▶ if($fks1->{$fk} ne $fks2->{$fk})
Branch: 1 branch
BranchTrueFalseTotal
if ($fks1->{$fk} ne $fks2->{$fk}) 0 0 0
393 {
394 0 debug(1,"foreign key '$fk' changed");
395 0 my $changes = "ALTER TABLE $name1 DROP FOREIGN KEY $fk;";
396 $changes .= " # was CONSTRAINT $fk $fks1->{$fk}"
397 0 ▶ unless $self->{opts}{'no-old-defs'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"opts"}{"no-old-defs"} 0 0 0
398 0 $changes .= "\nALTER TABLE $name1 ADD CONSTRAINT $fk FOREIGN KEY $fks2->{$fk};\n";
399 0 push @changes, $changes;
400 }
401 } else {
402 0 debug(1,"foreign key '$fk' removed");
403 0 my $changes .= "ALTER TABLE $name1 DROP FOREIGN KEY $fk;";
404 $changes .= " # was CONSTRAINT $fk $fks1->{$fk}"
405 0 ▶ unless $self->{opts}{'no-old-defs'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"opts"}{"no-old-defs"} 0 0 0
406 0 $changes .= "\n";
407 0 push @changes, $changes;
408 }
409 }
410 }
411  
412 0 ▶ if($fks2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fks2) 0 0 0
413 0 for my $fk (keys %$fks2) {
414 0 ▶ next if($fks1 && $fks1->{$fk});
Branch: 1 branch
BranchTrueFalseTotal
if $fks1 and $fks1->{$fk} 0 0 0
Condition0%0/3
$fks1 and $fks1->{$fk}
!ll&&!rl&&r
000
MC/DC0%0/2
$fks1 and $fks1->{$fk}
$fks1 $fks1->{$fk}
Truth table0%0/3
A and B
A= $fks1B= $fks1->{$fk}
ABresult
0X0
100
111
415 0 debug(1, "foreign key '$fk' added");
416 0 push @changes, "ALTER TABLE $name1 ADD CONSTRAINT $fk FOREIGN KEY $fks2->{$fk};\n";
417 }
418 }
419  
420 0 return @changes;
421 }
422  
423 # If we're about to drop a composite (multi-column) index, we need to
424 # check whether any of the columns in the composite index are
425 # auto_increment; if so, we have to add an index for that
426 # auto_increment column *before* dropping the composite index, since
427 # auto_increment columns must always be indexed.
428 0 ▶sub _check_for_auto_col {
Subroutine _check_for_auto_col: not called CC=6 SCAR=37.4
429 0 my ($table, $fields, $primary) = @_;
430  
431 0 my @fields = _fields_from_key($fields);
432  
433 0 for my $field (@fields) {
434 0 ▶ next if($table->field($field) !~ /auto_increment/i);
Branch: 1 branch
BranchTrueFalseTotal
unless $table->field($field) =~ /auto_increment/i 0 0 0
435 0 ▶ next if($table->isa_index($field));
Branch: 1 branch
BranchTrueFalseTotal
if $table->isa_index($field) 0 0 0
436 0 ▶ next if($primary && $table->isa_primary($field));
Branch: 1 branch
BranchTrueFalseTotal
if $primary and $table->isa_primary($field) 0 0 0
Condition0%0/3
$primary and $table->isa_primary($field)
!ll&&!rl&&r
000
MC/DC0%0/2
$primary and $table->isa_primary($field)
$primary $table->isa_primary($field)
Truth table0%0/3
A and B
A= $primaryB= $table->isa_primary($field)
ABresult
0X0
100
111
437  
438 0 return $field;
439 }
440  
441 0 return;
442 }
443  
444 0 ▶sub _fields_from_key {
Subroutine _fields_from_key: not called CC=1 SCAR=6.9
445 0 my $key = shift;
446 0 $key =~ s/^\s*\((.*)\)\s*$/$1/g; # strip brackets if any
447 0 split /\s*,\s*/, $key;
448 }
449  
450 0 ▶sub _key_covers_auto_col {
Subroutine _key_covers_auto_col: not called CC=3 SCAR=24.8
451 0 my ($table, $key) = @_;
452 0 my @fields = _fields_from_key($key);
453 0 for my $field (@fields) {
454 0 ▶ return 1 if $table->is_auto_inc($field);
Branch: 1 branch
BranchTrueFalseTotal
if $table->is_auto_inc($field) 0 0 0
455 }
456 0 return;
457 }
458  
459 0 ▶sub _index_auto_col {
Subroutine _index_auto_col: not called CC=1 SCAR=6.9
460 0 my ($table, $field) = @_;
461 0 my $name = $table->name;
462 0 return "ALTER TABLE $name ADD INDEX ($field); # auto columns must always be indexed\n";
463 }
464  
465 0 ▶sub _diff_options {
Subroutine _diff_options: not called CC=8 SCAR=42.8
466 0 my ($self, $table1, $table2) = @_;
467  
468 0 my $name = $table1->name();
469 0 my $options1 = $table1->options();
470 0 my $options2 = $table2->options();
471  
472 0 ▶ return () unless $options1 || $options2;
Branch: 1 branch
BranchTrueFalseTotal
unless $options1 or $options2 0 0 0
Condition0%0/3
$options1 or $options2
l!l&&r!l&&!r
000
MC/DC0%0/2
$options1 or $options2
$options1 $options2
Truth table0%0/3
A or B
A= $options1B= $options2
ABresult
1X1
011
000
473  
474 0 my @changes;
475  
476 0 ▶ if ($self->{opts}{tolerant}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opts"}{"tolerant"}) 0 0 0
477 0 for ($options1, $options2) {
478 0 s/ AUTO_INCREMENT=\d+//gi;
479 0 s/ COLLATE=[\w_]+//gi;
480 }
481 }
482  
483 0 ▶ if ($options1 ne $options2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options1 ne $options2) 0 0 0
484 0 my $change = "ALTER TABLE $name $options2;";
485 0 ▶ $change .= " # was " . ($options1 || 'blank') unless $self->{opts}{'no-old-defs'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"opts"}{"no-old-defs"} 0 0 0
Condition0%0/2
$options1 || 'blank'
l!l
00
MC/DC0%0/1
$options1 || 'blank'
$options1
Truth table0%0/2
A
A= $options1
Aresult
11
00
486 0 $change .= "\n";
487 0 push @changes, $change;
488 }
489  
490 0 return @changes;
491 }
492  
493 0 ▶sub _load_database {
Subroutine _load_database: not called CC=13 SCAR=52.0
494 0 my ($self, $arg, $authnum) = @_;
495  
496 0 debug(2, "parsing arg $authnum: '$arg'\n");
497  
498 0 my %auth;
499 0 for my $auth (qw/dbh host port user password socket/) {
500 0 ▶ $auth{$auth} = $self->{opts}{"$auth$authnum"} || $self->{opts}{$auth};
Condition0%0/3
$self->{'opts'}{"$auth$authnum"} || $self->{'opts'}{$auth}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'opts'}{"$auth$authnum"} || $self->{'opts'}{$auth}
$self->{'opts'}{"$auth$authnum"} $self->{'opts'}{$auth}
Truth table0%0/3
A || B
A= $self->{'opts'}{"$auth$authnum"}B= $self->{'opts'}{$auth}
ABresult
1X1
011
000
501 0 ▶ delete $auth{$auth} unless $auth{$auth};
Branch: 1 branch
BranchTrueFalseTotal
unless $auth{$auth} 0 0 0
502 }
503  
504 0 ▶ if ($arg =~ /^db:(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^db:(.*)/) 0 0 0
505 0 return MySQL::Diff::Database->new(db => $1, auth => \%auth, 'single-transaction' => $self->{opts}{'single-transaction'}, 'table-re' => $self->{opts}{'table-re'});
506 }
507  
508 0 ▶ if ($self->{opts}{"dbh"} ||
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opts"}{"dbh"} or $self->{"opts"}{"host$authnum"} or $self->{"opts"}{"port$authnum"} or $self->{"opts"}{"user$authnum"} or $self->{"opts"}{"password$authnum"} or $self->{"opts"}{"socket$authnum"}) 0 0 0
Condition0%0/15
$self->{"opts"}{"dbh"} or $self->{"opts"}{"host$authnum"} or $self->{"opts"}{"port$authnum"} or $self->{"opts"}{"user$authnum"} or $self->{"opts"}{"password$authnum"} or $self->{"opts"}{"socket$authnum"}
l!l&&r!l&&!r
000
$self->{"opts"}{"dbh"} or $self->{"opts"}{"host$authnum"} or $self->{"opts"}{"port$authnum"} or $self->{"opts"}{"user$authnum"} or $self->{"opts"}{"password$authnum"}
l!l&&r!l&&!r
000
$self->{"opts"}{"dbh"} or $self->{"opts"}{"host$authnum"} or $self->{"opts"}{"port$authnum"} or $self->{"opts"}{"user$authnum"}
l!l&&r!l&&!r
000
$self->{"opts"}{"dbh"} or $self->{"opts"}{"host$authnum"} or $self->{"opts"}{"port$authnum"}
l!l&&r!l&&!r
000
$self->{"opts"}{"dbh"} or $self->{"opts"}{"host$authnum"}
l!l&&r!l&&!r
000
MC/DC0%0/6
$self->{"opts"}{"dbh"} or $self->{"opts"}{"host$authnum"} or $self->{"opts"}{"port$authnum"} or $self->{"opts"}{"user$authnum"} or $self->{"opts"}{"password$authnum"} or $self->{"opts"}{"socket$authnum"}
$self->{"opts"}{"dbh"} $self->{"opts"}{"host$authnum"} $self->{"opts"}{"port$authnum"} $self->{"opts"}{"user$authnum"} $self->{"opts"}{"password$authnum"} $self->{"opts"}{"socket$authnum"}
Truth table0%0/7
A or B or C or D or E or F
A= $self->{"opts"}{"dbh"}B= $self->{"opts"}{"host$authnum"}C= $self->{"opts"}{"port$authnum"}D= $self->{"opts"}{"user$authnum"}E= $self->{"opts"}{"password$authnum"}F= $self->{"opts"}{"socket$authnum"}
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
509 $self->{opts}{"host$authnum"} ||
510 $self->{opts}{"port$authnum"} ||
511 $self->{opts}{"user$authnum"} ||
512 $self->{opts}{"password$authnum"} ||
513 $self->{opts}{"socket$authnum"}) {
514 0 return MySQL::Diff::Database->new(db => $arg, auth => \%auth, 'single-transaction' => $self->{opts}{'single-transaction'}, 'table-re' => $self->{opts}{'table-re'});
515 }
516  
517 0 ▶ if (-f $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $arg) 0 0 0
518 0 return MySQL::Diff::Database->new(file => $arg, auth => \%auth, 'single-transaction' => $self->{opts}{'single-transaction'}, 'table-re' => $self->{opts}{'table-re'});
519 }
520  
521 0 my %dbs = MySQL::Diff::Database::available_dbs(%auth);
522 0 debug(2, " available databases: ", (join ', ', keys %dbs), "\n");
523  
524 0 ▶ if ($dbs{$arg}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbs{$arg}) 0 0 0
525 0 return MySQL::Diff::Database->new(db => $arg, auth => \%auth, 'single-transaction' => $self->{opts}{'single-transaction'}, 'table-re' => $self->{opts}{'table-re'});
526 }
527  
528 0 warn "'$arg' is not a valid file or database.\n";
529 0 return;
530 }
531  
532 0 ▶sub _debug_level {
Subroutine _debug_level: not called CC=1 SCAR=6.9
533 0 my ($self,$level) = @_;
534 0 debug_level($level);
535 }
536  
537 2 1;
538  
539 __END__
↑ Summary Diff/Table.pm »