blib/lib/DBIO/MySQL/Diff.pm

filter: Statementstmt 99.3% 492 / 495 Branchbran 88.0% 125 / 142 Conditioncond 64.7% 169 / 261 MC/DCmcdc 37.8% 50 / 132 Subroutinesub 100% 33 / 33 Podpod 100% 2 / 2 totaltotal 81.7% 871 / 1065 CC 205 SCAR 35.6
CC
205
Cov
88%
_protect_foreign_key_indexes
70.9
_build_operations_for_target
45.0
_pk_foreign_keys
36.7
CRAP 35.2
« blib/lib/DBIO/MySQL/Introspect/Indexes.pm ↑ Summary blib/lib/DBIO/MySQL/Storage.pm »
1 package DBIO::MySQL::Diff;
2 # ABSTRACT: Compare two introspected MySQL/MariaDB models
3  
4 55 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 55 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 55 our $VERSION = '0.900007';
8  
9 55 ▶use base 'DBIO::Diff::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 55 ▶use DBIO::MySQL::Diff::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 55 ▶use DBIO::MySQL::Diff::Column;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 55 ▶use DBIO::MySQL::Diff::Index;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 55 ▶use DBIO::MySQL::Diff::ForeignKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 55 ▶use DBIO::MySQL::Diff::View;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 55 ▶use DBIO::MySQL::Diff::PrimaryKey qw(primary_key_columns);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 55 ▶use DBIO::MySQL::Adapter;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 55 ▶use DBIO::MySQL::DDL ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 55 ▶use DBIO::MySQL::IndexType ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 55 ▶use DBIO::MySQL::Identifier ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 55 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 55 ▶use DBIO::Diff::Compare qw(changed_fk_fields);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 55 ▶use Scalar::Util 'refaddr';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 55 my $ADAPTER = DBIO::MySQL::Adapter->new;
26  
27  
28  
29 # Parse adapter native_type (e.g. "BIGINT", "CHAR(32)", "DECIMAL(10,2)") into
30 # the lowercase data_type + column_type pair that information_schema reports.
31 8 ▶sub _native_to_mysql_types {
Subroutine _native_to_mysql_types: called CC=11 SCAR=24.4
32 8 my ($native) = @_;
33 8 my $lc = lc $native;
34  
35 # BIGINT → bigint / bigint(20)
36 8 ▶ return ('bigint', 'bigint(20)') if $lc eq 'bigint';
Branch: 1 branch
BranchTrueFalseTotal
if $lc eq "bigint" 1 7 8
37  
38 # TINYINT(1) → tinyint / tinyint(1)
39 7 ▶ return ('tinyint', 'tinyint(1)') if $lc eq 'tinyint(1)';
Branch: 1 branch
BranchTrueFalseTotal
if $lc eq "tinyint(1)" 0 7 7
40  
41 # DOUBLE → double / double
42 7 ▶ return ('double', 'double') if $lc eq 'double';
Branch: 1 branch
BranchTrueFalseTotal
if $lc eq "double" 0 7 7
43  
44 # LONGTEXT → longtext / longtext
45 7 ▶ return ('longtext', 'longtext') if $lc eq 'longtext';
Branch: 1 branch
BranchTrueFalseTotal
if $lc eq "longtext" 0 7 7
46  
47 # LONGBLOB → longblob / longblob
48 7 ▶ return ('longblob', 'longblob') if $lc eq 'longblob';
Branch: 1 branch
BranchTrueFalseTotal
if $lc eq "longblob" 0 7 7
49  
50 # DATETIME → datetime / datetime
51 7 ▶ return ('datetime', 'datetime') if $lc eq 'datetime';
Branch: 1 branch
BranchTrueFalseTotal
if $lc eq "datetime" 1 6 7
52  
53 # CHAR(n) → char / char(n)
54 6 ▶ if ($lc =~ /\Achar\((\d+)\)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lc =~ /\Achar\((\d+)\)\z/) 0 6 6
55 0 return ('char', "char($1)");
56 }
57  
58 # DECIMAL(p,s) → decimal / decimal(p,s)
59 6 ▶ if ($lc =~ /\Adecimal\((\d+),(\d+)\)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lc =~ /\Adecimal\((\d+),(\d+)\)\z/) 1 5 6
60 1 return ('decimal', "decimal($1,$2)");
61 }
62  
63 # DECIMAL(p) → decimal / decimal(p,0). MySQL and MariaDB both store DECIMAL(M)
64 # as DECIMAL(M,0), and information_schema always reports both members back, so
65 # the target has to spell the scale out: the adapter renders a precision-only
66 # numeric as DECIMAL(p) since karr #41, and 'decimal(8)' never equals the
67 # 'decimal(8,0)' the server answers with (karr #43).
68 5 ▶ if ($lc =~ /\Adecimal\((\d+)\)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lc =~ /\Adecimal\((\d+)\)\z/) 3 2 5
69 3 return ('decimal', "decimal($1,0)");
70 }
71  
72 # Bare DECIMAL → decimal / decimal(10,0), MySQL's and MariaDB's documented
73 # default precision/scale -- and what the server reports back for it (karr #43).
74 2 ▶ return ('decimal', 'decimal(10,0)') if $lc eq 'decimal';
Branch: 1 branch
BranchTrueFalseTotal
if $lc eq "decimal" 1 1 2
75  
76 # Fallback: lowercase as-is for both
77 1 (my $bare = $lc) =~ s/\(.*//;
78 1 return ($bare, $lc);
79 }
80  
81 # Types whose charset/collation is NULL in information_schema (binary/numeric/datetime).
82 # Lives in DBIO::MySQL::Adapter as %NO_CHARSET, exposed via $ADAPTER->no_charset_for.
83  
84 9 ▶sub target_from_compiled {
Subroutine target_from_compiled: called CC=11 SCAR=24.0
Pod documented
85 9 my ($class, $compiled) = @_;
86 9 my (%tables, %columns);
87  
88 9 ▶ for my $tname (sort keys %{ $compiled->{tables} // {} }) {
Condition50%1/2
$compiled->{"tables"} // {}
l!l
90
MC/DC0%0/1
$compiled->{"tables"} // {}
$compiled->{"tables"}
Truth table50%1/2
A
A= $compiled->{"tables"}
Aresult
11
00
89 # the name comes from the schema: one that names nothing is refused as
90 # install_ddl refuses it, and database.table is a table of another
91 # database, which the diff does not read (karr k120)
92 8 DBIO::MySQL::Identifier::check_table_name($tname);
93 8 ▶ next if $tname =~ /\./;
Branch: 1 branch
BranchTrueFalseTotal
if $tname =~ /\./ 2 6 8
94 6 my $t = $compiled->{tables}{$tname};
95 6 $tables{$tname} = { table_name => $tname };
96  
97 6 my @cols;
98 # the position in the key is the order of the primary key of the schema
99 6 my $pk_at = 0;
100 6 ▶ my %pk_pos = map { $_ => ++$pk_at } @{ $t->{primary_key} // [] };
Condition100%2/2
$t->{"primary_key"} // []
l!l
33
MC/DC100%1/1
$t->{"primary_key"} // []
$t->{"primary_key"}
Truth table100%2/2
A
A= $t->{"primary_key"}
Aresult
11
00
101 6 ▶ for my $c (@{ $t->{columns} // [] }) {
Condition50%1/2
$t->{"columns"} // []
l!l
60
MC/DC0%0/1
$t->{"columns"} // []
$t->{"columns"}
Truth table50%1/2
A
A= $t->{"columns"}
Aresult
11
00
102 8 my ($data_type, $column_type) = _native_to_mysql_types($c->{native_type});
103  
104 # charset/collation: leave undef for binary/numeric types so that the
105 # comparison (_norm returns '') equals the undef in the live introspect.
106 # For text/char types the live DB will have a server-assigned charset;
107 # we also leave undef here — see ESCALATION NOTE below.
108 8 my ($charset, $collation) = (undef, undef);
109 8 ▶ if ($ADAPTER->no_charset_for($c->{native_type})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ADAPTER->no_charset_for($c->{"native_type"})) 7 1 8
110 7 $charset = $collation = undef; # already undef; explicit for clarity
111 }
112  
113 # A portable schema prescribes a default only when it declares one; when it
114 # does not, leave default_value undef so the always-on desired-state
115 # contract in DBIO::Diff::Compare skips it and leaves whatever the live
116 # column reports alone. The introspection reports undef as default_value
117 # for a nullable column with no explicit DEFAULT clause (on MariaDB too,
118 # see DBIO::MySQL::Introspect::Columns), so synthesising a literal string
119 # 'NULL' here produced a phantom MODIFY of every such column against that
120 # undef.
121 8 ▶ my $not_null = ($c->{not_null} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$c->{'not_null'} ? : 7 1 8
122 8 my $default = $c->{default};
123  
124 push @cols, {
125 column_name => $c->{column_name},
126 data_type => $data_type,
127 column_type => $column_type,
128 not_null => $not_null,
129 default_value => $default,
130 is_pk => ($c->{is_pk} ? 1 : 0),
131 pk_position => $pk_pos{ $c->{column_name} },
132 8 ▶ is_auto_increment => ($c->{auto_increment} ? 1 : 0),
Branch: 2 branches
BranchTrueFalseTotal
$c->{'is_pk'} ? : 1 7 8
$c->{'auto_increment'} ? : 0 8 8
133 character_set => $charset,
134 collation => $collation,
135 };
136 }
137 6 $columns{$tname} = \@cols;
138 }
139  
140 # ESCALATION NOTE: MySQL/MariaDB assigns character_set and collation from the
141 # database/server defaults to every text-family column (CHAR, VARCHAR, TEXT,
142 # LONGTEXT, …). A portable schema does not prescribe charset/collation, so
143 # target_from_compiled cannot derive these values from the schema alone.
144 # changed_column_fields() compares character_set and collation; leaving them undef in
145 # the target produces phantom diffs for text columns whenever the live DB
146 # reports a non-null charset (e.g. utf8mb4 / utf8mb4_uca1400_ai_ci on MariaDB
147 # 11.8). This is a design question for the controller: should the desired-state
148 # diff ignore attributes the target leaves unspecified?
149  
150 return {
151 9 tables => \%tables,
152 columns => \%columns,
153 indexes => {},
154 foreign_keys => {},
155 };
156 }
157  
158 # A foreign key of the target that carries no name (a model made by hand; the
159 # introspection always names) gets the fixed name install_ddl would give it,
160 # and the foreign keys of the whole model are named together, so two that would
161 # meet in one name are told apart as in install_ddl
162 # (DBIO::MySQL::DDL::foreign_key_names, karr k106). Returns the target itself
163 # when every foreign key has a name, else a copy; the model of the caller is
164 # not changed.
165 169 ▶sub _named_target {
Subroutine _named_target: called CC=4 SCAR=13.9
166 169 my ($target) = @_;
167 169 ▶ my $by_table = $target->{foreign_keys} // {};
Condition50%1/2
$target->{"foreign_keys"} // {}
l!l
1690
MC/DC0%0/1
$target->{"foreign_keys"} // {}
$target->{"foreign_keys"}
Truth table50%1/2
A
A= $target->{"foreign_keys"}
Aresult
11
00
168 169 my @unnamed = map { my $t = $_; map { [ $t, $_ ] } grep { !defined $_->{constraint_name} } @{ $by_table->{$t} } }
169 sort keys %$by_table;
170 169 ▶ return $target unless @unnamed;
Branch: 1 branch
BranchTrueFalseTotal
unless @unnamed 167 2 169
171  
172 2 my @names = DBIO::MySQL::DDL::foreign_key_names(map { +{ %{ $_->[1] }, table => $_->[0] } } @unnamed);
173 2 my %name_of; @name_of{ map { "$_->[1]" } @unnamed } = @names;
174 return { %$target, foreign_keys => { map {
175 2 my $t = $_;
176 4 ▶ ($t => [ map { defined $_->{constraint_name} ? $_ : { %$_, constraint_name => $name_of{"$_"} } } @{ $by_table->{$t} } ])
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{'constraint_name'} ? : 0 4 4
177 } keys %$by_table } };
178 }
179  
180 171 ▶sub _build_operations {
Subroutine _build_operations: called CC=1 SCAR=0.0
181 171 my ($self) = @_;
182 171 my @other = $self->_other_database_operations;
183 171 my @views = DBIO::MySQL::Diff::View->diff($self->source->{tables}, $self->target->{tables});
184  
185 # A view is no table (karr k79): everything below works on models without
186 # their views, the views have operations of their own. The drops stand at
187 # the head of the diff, before the constraints, the tables and the columns
188 # the view may read; the creates at its end, once all of them are what the
189 # target says.
190 169 local $self->{source} = DBIO::MySQL::Diff::View::model_without_views($self->{source});
191 169 local $self->{target} = _named_target(DBIO::MySQL::Diff::View::model_without_views($self->{target}));
192 169 my $ops = $self->_build_operations_for_target;
193 return [
194 @other,
195 65 (grep { $_->action eq 'drop' } @views),
196 @$ops,
197 160 (grep { $_->action ne 'drop' } @views),
198 ];
199 }
200  
201 # The operations on the tables of other databases (karr k122): one diff per
202 # database, over the models in other_databases, every operation marked with its
203 # database. They come before the operations on the current database -- nothing
204 # in another database reads from this one (a key across databases is refused),
205 # but a view here may read from there.
206 171 ▶sub _other_database_operations {
Subroutine _other_database_operations: called CC=4 SCAR=13.9
207 171 my ($self) = @_;
208 171 ▶ my ($source, $target) = map { $_->{other_databases} // {} } $self->source, $self->target;
Condition100%2/2
$_->{"other_databases"} // {}
l!l
4338
MC/DC100%1/1
$_->{"other_databases"} // {}
$_->{"other_databases"}
Truth table100%2/2
A
A= $_->{"other_databases"}
Aresult
11
00
209 0 my %option = map { $_ => $self->{$_} } grep { !/\A(?:source|target|operations)\z/ } keys %$self;
210 171 ▶ my $empty = sub { +{ tables => {}, columns => {}, indexes => {}, foreign_keys => {} } };
Subroutine __ANON__: called CC=1 SCAR=0.0
211 171 my %seen;
212 return map {
213 3 my $database = $_;
214 my $diff = ref($self)->new(%option,
215 3 ▶ source => $source->{$database} // $empty->(), target => $target->{$database} // $empty->());
Condition66%4/6
$source->{$database} // &$empty()
l!l&&r!l&&!r
210
$target->{$database} // &$empty()
l!l&&r!l&&!r
210
MC/DC0%0/2
$source->{$database} // &$empty()
$source->{$database} &$empty()
MC/DC0%0/2
$target->{$database} // &$empty()
$target->{$database} &$empty()
Truth table66%2/3
A // B
A= $source->{$database}B= &$empty()
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $target->{$database}B= &$empty()
ABresult
1X1
011
000
216 3 map { $_->database($database); $_ } @{ $diff->operations };
217 171 } grep { !$seen{$_}++ } sort(keys %$source), sort(keys %$target);
218 }
219  
220  
221 6 ▶sub summary {
Subroutine summary: called CC=3 SCAR=11.0
Pod documented
222 6 my ($self) = @_;
223 return join "\n", map {
224 10 my $line = $_->summary;
225 10 ▶ $_->can('database') && defined $_->database ? "$line (database " . $_->database . ')' : $line;
Branch: 1 branch
BranchTrueFalseTotal
$_->can('database') && defined $_->database ? : 4 6 10
Condition66%2/3
$_->can("database") and defined $_->database
!ll&&!rl&&r
064
MC/DC50%1/2
$_->can("database") and defined $_->database
$_->can("database") defined $_->database
Truth table66%2/3
A and B
A= $_->can("database")B= defined $_->database
ABresult
0X0
100
111
226 6 } @{ $self->operations };
227 }
228  
229 169 ▶sub _build_operations_for_target {
Subroutine _build_operations_for_target: called CC=42 SCAR=38.1
230 169 my ($self) = @_;
231 169 my @ops;
232  
233 # The index operations are split around the column changes (karr k80). An
234 # index is dropped while every column it is over is still there: the server
235 # takes an index along with the last column it was over, so a DROP INDEX
236 # after that DROP COLUMN names an index that is gone (1091), and MariaDB
237 # refuses to drop one column of a unique key over several (1072). An index is
238 # created once its columns are what the target says: added, and of the type
239 # they are to have. A changed index is a drop and a create, one on each side.
240 my @index_ops = DBIO::MySQL::Diff::Index->diff(
241 $self->source->{indexes}, $self->target->{indexes},
242 $self->source->{tables}, $self->target->{tables},
243 169 );
244 my @fk_ops = DBIO::MySQL::Diff::ForeignKey->diff(
245 $self->source->{foreign_keys}, $self->target->{foreign_keys},
246 $self->source->{tables}, $self->target->{tables},
247 169 );
248  
249 # The foreign keys come around all of it (karr k94): a constraint that
250 # leaves is dropped before the tables and the indexes, because the server
251 # refuses to drop the table a constraint references and the index it needs
252 # while it is there; one that arrives is added after the indexes.
253 169 push @ops, grep { $_->action eq 'drop' } @fk_ops;
254  
255 # The foreign keys that need the primary key of a table that changes or goes
256 # as their index are dropped here as well and added again after the key
257 # (karr k107); one the target declares over a key it no longer has is refused.
258 my @pk_ops = DBIO::MySQL::Diff::PrimaryKey->diff(
259 $self->source->{columns}, $self->target->{columns},
260 $self->source->{tables}, $self->target->{tables},
261 169 );
262 169 my ($pk_fk_drops, $pk_fk_adds) = $self->_pk_foreign_keys(\@pk_ops, \@index_ops);
263 168 push @ops, @$pk_fk_drops;
264  
265 # A foreign key of a new table that needs an index the same diff creates, or
266 # whose columns one covers, is not written into the CREATE TABLE but added
267 # after the index (karr k81, karr k92).
268 168 my ($inline_fks, @deferred_fks) = $self->_split_inline_foreign_keys(\@index_ops);
269  
270 # The key of an AUTO_INCREMENT column outside the primary key of a new table
271 # stands in its CREATE TABLE, not after it (karr k107).
272 168 my $inline_indexes;
273 168 ($inline_indexes, @index_ops) = $self->_split_inline_indexes(@index_ops);
274  
275 # The new tables are created parents first, as install_ddl does, and the
276 # foreign keys that close a cycle are added after everything else (karr k106).
277 168 my ($table_order, @cycle_fks) = $self->_order_new_tables($inline_fks);
278 168 push @deferred_fks, @cycle_fks;
279  
280 my @table_ops = DBIO::MySQL::Diff::Table->diff(
281 $self->source->{tables}, $self->target->{tables},
282 $self->target->{columns}, $inline_fks, $inline_indexes,
283 $self->source->{columns}, $self->source->{foreign_keys},
284 168 );
285 push @ops,
286 13 (sort { $table_order->{ $a->table_name } <=> $table_order->{ $b->table_name } }
287 55 grep { $_->action eq 'create' } @table_ops),
288 162 grep { $_->action ne 'create' } @table_ops;
289 my @column_ops = DBIO::MySQL::Diff::Column->diff(
290 $self->source->{columns}, $self->target->{columns},
291 $self->source->{tables}, $self->target->{tables},
292 162 );
293 162 my @drop_ops = grep { $_->action eq 'drop' } @index_ops;
294 162 my @create_ops = grep { $_->action ne 'drop' } @index_ops;
295  
296 # An AUTO_INCREMENT column that is not the primary key has to have a key, and
297 # the server refuses the DROP INDEX of its only one (1075), whether the
298 # column goes or only loses the attribute. Such a column is written without
299 # AUTO_INCREMENT first -- the MODIFY the diff has for it anyway, or one made
300 # for a column that leaves -- and the DROP INDEX follows (karr k80).
301 162 my %under_drop;
302 162 for my $op (@drop_ops) {
303 32 ▶ $under_drop{ $op->table_name }{$_} = 1 for @{ $op->index_info->{columns} // [] };
Condition50%1/2
$op->index_info->{"columns"} // []
l!l
320
MC/DC0%0/1
$op->index_info->{"columns"} // []
$op->index_info->{"columns"}
Truth table50%1/2
A
A= $op->index_info->{"columns"}
Aresult
11
00
304 }
305 162 my (@release, @rest);
306 162 for my $op (@column_ops) {
307 57 my $old = $op->old_info;
308 57 my $col = $op->column_name;
309 57 ▶ if ($op->action ne 'add'
Branch: 1 branch
BranchTrueFalseTotal
if ($op->action ne "add" and $old and $old->{"is_auto_increment"} and not $old->{"is_pk"} and $under_drop{$op->table_name}{$col}) 3 54 57
Condition83%10/12
$op->action ne "add" and $old and $old->{"is_auto_increment"} and not $old->{"is_pk"} and $under_drop{$op->table_name}{$col}
!ll&&!rl&&r
5403
$op->action ne "add" and $old and $old->{"is_auto_increment"} and not $old->{"is_pk"}
!ll&&!rl&&r
5133
$op->action ne "add" and $old and $old->{"is_auto_increment"}
!ll&&!rl&&r
17346
$op->action ne "add" and $old
!ll&&!rl&&r
17040
MC/DC60%3/5
$op->action ne "add" and $old and $old->{"is_auto_increment"} and not $old->{"is_pk"} and $under_drop{$op->table_name}{$col}
$op->action ne "add" $old $old->{"is_auto_increment"} not $old->{"is_pk"} $under_drop{$op->table_name}{$col}
Truth table66%4/6
A and B and C and not(D) and E
A= $op->action ne "add"B= $oldC= $old->{"is_auto_increment"}D= not $old->{"is_pk"}E= $under_drop{$op->table_name}{$col}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
310 && $old && $old->{is_auto_increment} && !$old->{is_pk}
311 && $under_drop{ $op->table_name }{$col}) {
312 3 ▶ if ($op->action eq 'modify' && !$op->new_info->{is_auto_increment}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->action eq "modify" and not $op->new_info->{"is_auto_increment"}) 2 1 3
Condition66%2/3
$op->action eq "modify" and not $op->new_info->{"is_auto_increment"}
!ll&&!rl&&r
102
MC/DC50%1/2
$op->action eq "modify" and not $op->new_info->{"is_auto_increment"}
$op->action eq "modify" not $op->new_info->{"is_auto_increment"}
Truth table66%2/3
A and not(B)
A= $op->action eq "modify"B= not $op->new_info->{"is_auto_increment"}
ABresult
0X0
100
111
313 2 push @release, $op;
314 2 next;
315 }
316 1 ▶ if ($op->action eq 'drop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->action eq "drop") 1 0 1
317 1 push @release, DBIO::MySQL::Diff::Column->new(
318 action => 'modify', table_name => $op->table_name, column_name => $col,
319 old_info => $old, new_info => { %$old, is_auto_increment => 0 },
320 );
321 }
322 }
323 55 push @rest, $op;
324 }
325  
326 # An index a foreign key that stays needs is not dropped before the index
327 # that takes its place exists (karr k81).
328 162 my ($drops, $creates, $late_drops, $extra_fk_drops, $extra_fk_adds) = $self->_protect_foreign_key_indexes(
329 \@drop_ops, \@create_ops, $inline_fks, $pk_fk_drops);
330  
331 # The primary keys of the tables in both models (karr k107): after the column
332 # changes -- the columns of the key exist by then -- and before the indexes
333 # that are created and the foreign keys that are added. Where a column of the
334 # key gains AUTO_INCREMENT the MODIFY needs the key (the server refuses
335 # AUTO_INCREMENT on a column that is no key), so the key comes first.
336 160 my (@pk_first, @pk_early, @pk_after, @reacquire);
337 160 for my $pk (@pk_ops) {
338 27 my %in_key = map { $_ => 1 } @{ $pk->columns };
339 my $gains = grep {
340 27 $_->action eq 'modify' && $_->table_name eq $pk->table_name
341 && $in_key{ $_->column_name }
342 && $_->new_info->{is_auto_increment} && !$_->old_info->{is_auto_increment}
343 6 ▶ } @rest;
Condition75%9/12
$_->action eq 'modify' && $_->table_name eq $pk->table_name && $in_key{$_->column_name} && $_->new_info->{'is_auto_increment'} && !$_->old_info->{'is_auto_increment'}
!ll&&!rl&&r
501
$_->action eq "modify" and $_->table_name eq $pk->table_name and $in_key{$_->column_name} and $_->new_info->{"is_auto_increment"}
!ll&&!rl&&r
501
$_->action eq "modify" and $_->table_name eq $pk->table_name and $in_key{$_->column_name}
!ll&&!rl&&r
411
$_->action eq "modify" and $_->table_name eq $pk->table_name
!ll&&!rl&&r
402
MC/DC40%2/5
$_->action eq 'modify' && $_->table_name eq $pk->table_name && $in_key{$_->column_name} && $_->new_info->{'is_auto_increment'} && !$_->old_info->{'is_auto_increment'}
$_->action eq "modify" $_->table_name eq $pk->table_name $in_key{$_->column_name} $_->new_info->{"is_auto_increment"} !$_->old_info->{'is_auto_increment'}
Truth table50%3/6
A and B and C and D && not(E)
A= $_->action eq "modify"B= $_->table_name eq $pk->table_nameC= $in_key{$_->column_name}D= $_->new_info->{"is_auto_increment"}E= !$_->old_info->{'is_auto_increment'}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
344 # A new AUTO_INCREMENT column of the key is added with it, in one statement:
345 # the server refuses the column without its key.
346 my @taken = grep {
347 27 $_->action eq 'add' && $_->table_name eq $pk->table_name
348 && $in_key{ $_->column_name } && $_->new_info->{is_auto_increment}
349 6 ▶ } @rest;
Condition77%7/9
$_->action eq 'add' && $_->table_name eq $pk->table_name && $in_key{$_->column_name} && $_->new_info->{'is_auto_increment'}
!ll&&!rl&&r
312
$_->action eq "add" and $_->table_name eq $pk->table_name and $in_key{$_->column_name}
!ll&&!rl&&r
303
$_->action eq "add" and $_->table_name eq $pk->table_name
!ll&&!rl&&r
303
MC/DC50%2/4
$_->action eq 'add' && $_->table_name eq $pk->table_name && $in_key{$_->column_name} && $_->new_info->{'is_auto_increment'}
$_->action eq "add" $_->table_name eq $pk->table_name $in_key{$_->column_name} $_->new_info->{'is_auto_increment'}
Truth table60%3/5
A and B and C && D
A= $_->action eq "add"B= $_->table_name eq $pk->table_nameC= $in_key{$_->column_name}D= $_->new_info->{'is_auto_increment'}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
350 27 ▶ if (@taken) {
Branch: 1 branch
BranchTrueFalseTotal
if (@taken) 2 25 27
351 2 my %taken = map { refaddr($_) => 1 } @taken;
352 2 @rest = grep { !$taken{ refaddr $_ } } @rest;
353 2 $pk->with_columns(\@taken);
354 }
355 27 ▶ if ($pk->action eq 'drop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pk->action eq "drop") 6 21 27
356 # A column of the key that stays AUTO_INCREMENT (it has a key of another
357 # kind in the target) is released for the drop and gets the attribute
358 # back once that key exists. One that loses the attribute has its MODIFY
359 # among the column changes, before the key goes.
360 6 my $table = $pk->table_name;
361 6 ▶ my %now = map { $_->{column_name} => $_ } @{ $self->source->{columns}{$table} // [] };
Condition50%1/2
$self->source->{"columns"}{$table} // []
l!l
60
MC/DC0%0/1
$self->source->{"columns"}{$table} // []
$self->source->{"columns"}{$table}
Truth table50%1/2
A
A= $self->source->{"columns"}{$table}
Aresult
11
00
362 6 ▶ my %then = map { $_->{column_name} => $_ } @{ $self->target->{columns}{$table} // [] };
Condition50%1/2
$self->target->{"columns"}{$table} // []
l!l
60
MC/DC0%0/1
$self->target->{"columns"}{$table} // []
$self->target->{"columns"}{$table}
Truth table50%1/2
A
A= $self->target->{"columns"}{$table}
Aresult
11
00
363 6 for my $name (@{ $pk->old_columns }) {
364 7 my ($old, $new) = ($now{$name}, $then{$name});
365 7 ▶ next unless $old && $new && $old->{is_auto_increment} && $new->{is_auto_increment};
Branch: 1 branch
BranchTrueFalseTotal
unless $old and $new and $old->{"is_auto_increment"} and $new->{"is_auto_increment"} 6 1 7
Condition66%6/9
$old and $new and $old->{"is_auto_increment"} and $new->{"is_auto_increment"}
!ll&&!rl&&r
511
$old and $new and $old->{"is_auto_increment"}
!ll&&!rl&&r
052
$old and $new
!ll&&!rl&&r
007
MC/DC50%2/4
$old and $new and $old->{"is_auto_increment"} and $new->{"is_auto_increment"}
$old $new $old->{"is_auto_increment"} $new->{"is_auto_increment"}
Truth table60%3/5
A and B and C and D
A= $oldB= $newC= $old->{"is_auto_increment"}D= $new->{"is_auto_increment"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
366 1 my %released = (%$new, is_auto_increment => 0);
367 1 push @pk_after, DBIO::MySQL::Diff::Column->new(
368 action => 'modify', table_name => $table, column_name => $name,
369 old_info => $old, new_info => \%released,
370 );
371 1 push @reacquire, DBIO::MySQL::Diff::Column->new(
372 action => 'modify', table_name => $table, column_name => $name,
373 old_info => \%released, new_info => $new,
374 );
375 }
376 }
377 # An AUTO_INCREMENT column that leaves the key, or does not stand first in
378 # the new one, has to be a key of the table by another index when the
379 # statement runs (the server refuses it with 1075): the UNIQUE index the
380 # target has over it is created before the key (karr k107).
381 27 ▶ if ($pk->action eq 'replace') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pk->action eq "replace") 15 12 27
382 15 my $table = $pk->table_name;
383 15 my @new = @{ $pk->columns };
384 15 ▶ for my $col (@{ $self->target->{columns}{$table} // [] }) {
Condition50%1/2
$self->target->{"columns"}{$table} // []
l!l
150
MC/DC0%0/1
$self->target->{"columns"}{$table} // []
$self->target->{"columns"}{$table}
Truth table50%1/2
A
A= $self->target->{"columns"}{$table}
Aresult
11
00
385 32 my $name = $col->{column_name};
386 32 ▶ next unless $col->{is_auto_increment};
Branch: 1 branch
BranchTrueFalseTotal
unless $col->{"is_auto_increment"} 28 4 32
387 4 ▶ next unless grep { $_ eq $name } @{ $pk->old_columns };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $name;} @{$pk->old_columns;} 1 3 4
388 3 ▶ next if @new && $new[0] eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if @new and $new[0] eq $name 1 2 3
Condition66%2/3
@new and $new[0] eq $name
!ll&&!rl&&r
021
MC/DC50%1/2
@new and $new[0] eq $name
@new $new[0] eq $name
Truth table66%2/3
A and B
A= @newB= $new[0] eq $name
ABresult
0X0
100
111
389 my ($index) = grep {
390 2 ▶ $_->table_name eq $table && (($_->index_info->{columns} // [])->[0] // '') eq $name
Condition42%3/7
$_->table_name eq $table && (+($_->index_info->{'columns'} // [])->[0] // '') eq $name
!ll&&!rl&&r
002
+($_->index_info->{"columns"} // [])->[0] // ""
l!l
20
$_->index_info->{"columns"} // []
l!l
20
MC/DC0%0/2
$_->table_name eq $table && (+($_->index_info->{'columns'} // [])->[0] // '') eq $name
$_->table_name eq $table $_->index_info->{"columns"}
Truth table33%1/3
A && B
A= $_->table_name eq $tableB= $_->index_info->{"columns"}
ABresult
0X0
100
111
391 } @$creates;
392 2 ▶ next unless $index;
Branch: 1 branch
BranchTrueFalseTotal
unless $index 0 2 2
393 2 push @pk_early, $index;
394 2 $creates = [ grep { refaddr($_) != refaddr($index) } @$creates ];
395 }
396 }
397 27 ▶ push @{ $gains ? \@pk_first : \@pk_after }, $pk;
Branch: 1 branch
BranchTrueFalseTotal
$gains ? : 1 26 27
398 }
399  
400 160 push @ops, @release, @$extra_fk_drops, @$drops, @pk_first, @rest, @pk_early, @pk_after, @$late_drops;
401 160 push @ops, @$creates, @reacquire;
402 160 push @ops, grep { $_->action ne 'drop' } @fk_ops;
403 160 push @ops, @$pk_fk_adds, @$extra_fk_adds;
404 160 push @ops, @deferred_fks;
405  
406 160 return \@ops;
407 }
408  
409 # The foreign keys a primary key operation (karr k107) has to take along, and
410 # the refusal. Returns two array refs of ForeignKey ops: the extra DROPs, which
411 # stand with the other foreign key drops, and the ADDs, which stand with the
412 # other foreign key adds. A foreign key is taken along when it stays as it is
413 # (a changed one or one that goes is dropped and added by the foreign key diff
414 # already, once), when it needs an index over columns the old key starts with
415 # -- the columns it references, in a table that points at this one, or its own
416 # columns, in this table -- and when neither the new key nor an index that stays
417 # starts with them: the server refuses the drop (MySQL 1553) or the key that
418 # does not start with them (MariaDB takes some of those, and the extra drop is
419 # harmless there). A foreign key the TARGET declares into the table over such
420 # columns, with no key and no index of the target that starts with them, has
421 # nothing to point at, and dies.
422 169 ▶sub _pk_foreign_keys {
Subroutine _pk_foreign_keys: called CC=33 SCAR=36.0
423 169 my ($self, $pk_ops, $index_ops) = @_;
424 169 my ($source, $target) = ($self->source, $self->target);
425 169 my @changing = grep { $_->action ne 'add' } @$pk_ops;
426 169 ▶ return ([], []) unless @changing;
Branch: 1 branch
BranchTrueFalseTotal
unless @changing 147 22 169
427  
428 22 ▶ my $target_tables = $target->{tables} // {};
Condition50%1/2
$target->{"tables"} // {}
l!l
220
MC/DC0%0/1
$target->{"tables"} // {}
$target->{"tables"}
Truth table50%1/2
A
A= $target->{"tables"}
Aresult
11
00
429 22 my (@drops, @adds, %seen);
430  
431 22 for my $pk (@changing) {
432 22 my $table = $pk->table_name;
433 22 my $old = { columns => $pk->old_columns };
434 22 my $new = { columns => $pk->columns };
435 22 my %dropped = map { $_->index_name => 1 } grep { $_->table_name eq $table } @$index_ops;
436 4 ▶ my @source_others = grep { ($_->{origin} // '') ne 'pk' && !$dropped{ $_->{index_name} // '' } }
Condition57%4/7
($_->{'origin'} // '') ne 'pk' && !$dropped{$_->{'index_name'} // ''}
!ll&&!rl&&r
031
$_->{"origin"} // ""
l!l
40
$_->{"index_name"} // ""
l!l
40
MC/DC50%1/2
($_->{'origin'} // '') ne 'pk' && !$dropped{$_->{'index_name'} // ''}
$_->{"origin"} !$dropped{$_->{'index_name'} // ''}
MC/DC0%0/1
$_->{"index_name"} // ""
$_->{"index_name"}
Truth table66%2/3
A && not(B)
A= $_->{"origin"}B= !$dropped{$_->{'index_name'} // ''}
ABresult
0X0
100
111
Truth table50%1/2
A
A= $_->{"index_name"}
Aresult
11
00
437 22 ▶ values %{ $source->{indexes}{$table} // {} };
Condition50%1/2
$source->{"indexes"}{$table} // {}
l!l
220
MC/DC0%0/1
$source->{"indexes"}{$table} // {}
$source->{"indexes"}{$table}
Truth table50%1/2
A
A= $source->{"indexes"}{$table}
Aresult
11
00
438 22 ▶ my @target_all = values %{ $target->{indexes}{$table} // {} };
Condition50%1/2
$target->{"indexes"}{$table} // {}
l!l
220
MC/DC0%0/1
$target->{"indexes"}{$table} // {}
$target->{"indexes"}{$table}
Truth table50%1/2
A
A= $target->{"indexes"}{$table}
Aresult
11
00
439  
440 # the foreign keys of the target into this table
441 22 ▶ for my $from (sort keys %{ $target->{foreign_keys} // {} }) {
Condition50%1/2
$target->{"foreign_keys"} // {}
l!l
220
MC/DC0%0/1
$target->{"foreign_keys"} // {}
$target->{"foreign_keys"}
Truth table50%1/2
A
A= $target->{"foreign_keys"}
Aresult
11
00
442 11 for my $fk (@{ $target->{foreign_keys}{$from} }) {
443 10 ▶ next unless $fk->{to_table} eq $table;
Branch: 1 branch
BranchTrueFalseTotal
unless $fk->{"to_table"} eq $table 3 7 10
444 7 ▶ my $cols = $fk->{to_columns} // [];
Condition50%1/2
$fk->{"to_columns"} // []
l!l
70
MC/DC0%0/1
$fk->{"to_columns"} // []
$fk->{"to_columns"}
Truth table50%1/2
A
A= $fk->{"to_columns"}
Aresult
11
00
445 7 ▶ next unless _index_covers($old, $cols);
Branch: 1 branch
BranchTrueFalseTotal
unless _index_covers($old, $cols) 2 5 7
446 5 ▶ next if _index_covers($new, $cols) || grep { _index_covers($_, $cols) } @target_all;
Branch: 1 branch
BranchTrueFalseTotal
if _index_covers($new, $cols) or grep {_index_covers($_, $cols);} @target_all 4 1 5
Condition100%3/3
_index_covers($new, $cols) or grep {_index_covers($_, $cols);} @target_all
l!l&&r!l&&!r
131
MC/DC100%2/2
_index_covers($new, $cols) or grep {_index_covers($_, $cols);} @target_all
_index_covers($new, $cols) grep {_index_covers($_, $cols);} @target_all
Truth table100%3/3
A or B
A= _index_covers($new, $cols)B= grep {_index_covers($_, $cols);} @target_all
ABresult
1X1
011
000
447 DBIO::Exception->throw(sprintf
448 "the primary key of %s is %s in the target, and the foreign key %s of %s references (%s) of it: the target has no key over those columns any more, and the server would refuse the constraint",
449 0 ▶ $table, ($pk->action eq 'drop' ? 'gone' : 'composed differently (' . join(', ', @{ $pk->columns }) . ')'),
Branch: 1 branch
BranchTrueFalseTotal
defined $fk->{'constraint_name'} ? : 1 0 1
450 1 ▶ defined $fk->{constraint_name} ? "'$fk->{constraint_name}'" : '(unnamed)', $from, join(', ', @$cols));
Branch: 1 branch
BranchTrueFalseTotal
$pk->action eq 'drop' ? : 1 0 1
451 }
452 }
453  
454 6 ▶ my $served = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
455 6 my ($cols) = @_;
456 6 ▶ return _index_covers($new, $cols) || scalar grep { _index_covers($_, $cols) } @source_others;
Condition100%2/2
_index_covers($new, $cols) || scalar grep({_index_covers($_, $cols);} @source_others)
l!l
15
MC/DC100%1/1
_index_covers($new, $cols) || scalar grep({_index_covers($_, $cols);} @source_others)
_index_covers($new, $cols)
Truth table100%2/2
A
A= _index_covers($new, $cols)
Aresult
11
00
457 21 };
458  
459 21 ▶ for my $from (sort keys %{ $source->{foreign_keys} // {} }) {
Condition50%1/2
$source->{"foreign_keys"} // {}
l!l
210
MC/DC0%0/1
$source->{"foreign_keys"} // {}
$source->{"foreign_keys"}
Truth table50%1/2
A
A= $source->{"foreign_keys"}
Aresult
11
00
460 10 ▶ next unless exists $target_tables->{$from};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $target_tables->{$from} 0 10 10
461 10 ▶ my %target_fk = map { ($_->{constraint_name} // '') => $_ } @{ $target->{foreign_keys}{$from} // [] };
Condition50%2/4
$_->{"constraint_name"} // ""
l!l
90
$target->{"foreign_keys"}{$from} // []
l!l
100
MC/DC0%0/1
$_->{"constraint_name"} // ""
$_->{"constraint_name"}
MC/DC0%0/1
$target->{"foreign_keys"}{$from} // []
$target->{"foreign_keys"}{$from}
Truth table50%1/2
A
A= $_->{"constraint_name"}
Aresult
11
00
Truth table50%1/2
A
A= $target->{"foreign_keys"}{$from}
Aresult
11
00
462 10 for my $fk (@{ $source->{foreign_keys}{$from} }) {
463 10 my @needs;
464 10 ▶ push @needs, $fk->{to_columns} if $fk->{to_table} eq $table;
Branch: 1 branch
BranchTrueFalseTotal
if $fk->{"to_table"} eq $table 7 3 10
465 10 ▶ push @needs, $fk->{from_columns} if $from eq $table;
Branch: 1 branch
BranchTrueFalseTotal
if $from eq $table 3 7 10
466 10 ▶ next unless grep { _index_covers($old, $_) && !$served->($_) } @needs;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {_index_covers($old, $_) and not &$served($_);} @needs 6 4 10
Condition100%3/3
_index_covers($old, $_) && !&$served($_)
!ll&&!rl&&r
424
MC/DC100%2/2
_index_covers($old, $_) && !&$served($_)
_index_covers($old, $_) !&$served($_)
Truth table100%3/3
A && not(B)
A= _index_covers($old, $_)B= !&$served($_)
ABresult
0X0
100
111
467 4 ▶ my $stays = $target_fk{ $fk->{constraint_name} // '' };
Condition50%1/2
$fk->{"constraint_name"} // ""
l!l
40
MC/DC0%0/1
$fk->{"constraint_name"} // ""
$fk->{"constraint_name"}
Truth table50%1/2
A
A= $fk->{"constraint_name"}
Aresult
11
00
468 4 ▶ next if !$stays || changed_fk_fields($fk, $stays);
Branch: 1 branch
BranchTrueFalseTotal
if not $stays or changed_fk_fields($fk, $stays) 2 2 4
Condition100%3/3
not $stays or changed_fk_fields($fk, $stays)
l!l&&r!l&&!r
112
MC/DC100%2/2
not $stays or changed_fk_fields($fk, $stays)
not $stays changed_fk_fields($fk, $stays)
Truth table100%3/3
not(A) or B
A= not $staysB= changed_fk_fields($fk, $stays)
ABresult
1X1
011
000
469 2 ▶ next if $seen{ refaddr $fk }++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{builtin::refaddr($fk)}++ 0 2 2
470 push @drops, DBIO::MySQL::Diff::ForeignKey->new(
471 action => 'drop', table_name => $from, replaced => 1,
472 2 constraint_name => $fk->{constraint_name}, fk_info => $fk);
473 push @adds, DBIO::MySQL::Diff::ForeignKey->new(
474 action => 'add', table_name => $from,
475 2 constraint_name => $fk->{constraint_name}, fk_info => $fk);
476 }
477 }
478 }
479 21 return (\@drops, \@adds);
480 }
481  
482 # Do the first columns of an index ($index, an introspected index hash) have
483 # the columns of a foreign key, in their order? FULLTEXT and SPATIAL indexes
484 # are no key for a foreign key.
485 85 ▶sub _index_covers {
Subroutine _index_covers: called CC=9 SCAR=22.4
486 85 my ($index, $columns) = @_;
487 85 ▶ return 0 if ($index->{index_type} // '') =~ /FULLTEXT|SPATIAL/i;
Branch: 1 branch
BranchTrueFalseTotal
if ($index->{"index_type"} // "") =~ /FULLTEXT|SPATIAL/i 0 85 85
Condition100%2/2
$index->{"index_type"} // ""
l!l
2164
MC/DC100%1/1
$index->{"index_type"} // ""
$index->{"index_type"}
Truth table100%2/2
A
A= $index->{"index_type"}
Aresult
11
00
488 85 ▶ my @have = @{ $index->{columns} // [] };
Condition50%1/2
$index->{"columns"} // []
l!l
850
MC/DC0%0/1
$index->{"columns"} // []
$index->{"columns"}
Truth table50%1/2
A
A= $index->{"columns"}
Aresult
11
00
489 85 ▶ return 0 if !@$columns || @have < @$columns;
Branch: 1 branch
BranchTrueFalseTotal
if not @$columns or @have < @$columns 6 79 85
Condition66%2/3
not @$columns or @have < @$columns
l!l&&r!l&&!r
0679
MC/DC50%1/2
not @$columns or @have < @$columns
not @$columns @have < @$columns
Truth table66%2/3
not(A) or B
A= not @$columnsB= @have < @$columns
ABresult
1X1
011
000
490 79 for my $i (0 .. $#$columns) {
491 79 ▶ return 0 unless defined $have[$i] && $have[$i] eq $columns->[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $have[$i] and $have[$i] eq $columns->[$i] 29 50 79
Condition66%2/3
defined $have[$i] and $have[$i] eq $columns->[$i]
!ll&&!rl&&r
02950
MC/DC50%1/2
defined $have[$i] and $have[$i] eq $columns->[$i]
defined $have[$i] $have[$i] eq $columns->[$i]
Truth table66%2/3
A and B
A= defined $have[$i]B= $have[$i] eq $columns->[$i]
ABresult
0X0
100
111
492 }
493 50 return 1;
494 }
495  
496 # Of the index creates, those that belong in the CREATE TABLE of a new table:
497 # for each AUTO_INCREMENT column that is not in the primary key, the first index
498 # the target has over it as its first column. Returns a hashref table =>
499 # [ { index_name, index_info } ] and the ops that are left.
500 168 ▶sub _split_inline_indexes {
Subroutine _split_inline_indexes: called CC=13 SCAR=26.5
501 168 my ($self, @index_ops) = @_;
502 168 ▶ my $source_tables = $self->source->{tables} // {};
Condition50%1/2
$self->source->{"tables"} // {}
l!l
1680
MC/DC0%0/1
$self->source->{"tables"} // {}
$self->source->{"tables"}
Truth table50%1/2
A
A= $self->source->{"tables"}
Aresult
11
00
503 168 my (%inline, %taken, @rest);
504 168 for my $op (@index_ops) {
505 65 my $table = $op->table_name;
506 65 ▶ if ($op->action eq 'create' && !exists $source_tables->{$table}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->action eq "create" and not exists $source_tables->{$table}) 9 56 65
Condition100%3/3
$op->action eq "create" and not exists $source_tables->{$table}
!ll&&!rl&&r
32249
MC/DC100%2/2
$op->action eq "create" and not exists $source_tables->{$table}
$op->action eq "create" not exists $source_tables->{$table}
Truth table100%3/3
A and not(B)
A= $op->action eq "create"B= not exists $source_tables->{$table}
ABresult
0X0
100
111
507 9 my $info = $op->index_info;
508 9 ▶ my ($first) = @{ $info->{columns} // [] };
Condition50%1/2
$info->{"columns"} // []
l!l
90
MC/DC0%0/1
$info->{"columns"} // []
$info->{"columns"}
Truth table50%1/2
A
A= $info->{"columns"}
Aresult
11
00
509 9 my ($class_kw) = DBIO::MySQL::IndexType::index_type_split($info->{index_type});
510 9 ▶ if (defined $first && !$class_kw && !$taken{$table}{$first}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $first and not $class_kw and not $taken{$table}{$first} and grep {$_->{"column_name"} eq $first and $_->{"is_auto_increment"} and not $_->{"is_pk"};} @{$self->target->{"columns"}{$table} // [];}) 3 6 9
Condition44%4/9
defined $first and not $class_kw and not $taken{$table}{$first} and grep {$_->{"column_name"} eq $first and $_->{"is_auto_increment"} and not $_->{"is_pk"};} @{$self->target->{"columns"}{$table} // [];}
!ll&&!rl&&r
063
defined $first and not $class_kw and not $taken{$table}{$first}
!ll&&!rl&&r
009
defined $first and not $class_kw
!ll&&!rl&&r
009
MC/DC25%1/4
defined $first and not $class_kw and not $taken{$table}{$first} and grep {$_->{"column_name"} eq $first and $_->{"is_auto_increment"} and not $_->{"is_pk"};} @{$self->target->{"columns"}{$table} // [];}
defined $first not $class_kw not $taken{$table}{$first} grep {$_->{"column_name"} eq $first and $_->{"is_auto_increment"} and not $_->{"is_pk"};} @{$self->target->{"columns"}{$table} // [];}
Truth table40%2/5
A and not(B) and not(C) and D
A= defined $firstB= not $class_kwC= not $taken{$table}{$first}D= grep {$_->{"column_name"} eq $first and $_->{"is_auto_increment"} and not $_->{"is_pk"};} @{$self->target->{"columns"}{$table} // [];}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
511 21 ▶ && grep { $_->{column_name} eq $first && $_->{is_auto_increment} && !$_->{is_pk} }
Condition83%5/6
$_->{'column_name'} eq $first && $_->{'is_auto_increment'} && !$_->{'is_pk'}
!ll&&!rl&&r
1803
$_->{"column_name"} eq $first and $_->{"is_auto_increment"}
!ll&&!rl&&r
1263
MC/DC66%2/3
$_->{'column_name'} eq $first && $_->{'is_auto_increment'} && !$_->{'is_pk'}
$_->{"column_name"} eq $first $_->{"is_auto_increment"} !$_->{'is_pk'}
Truth table75%3/4
A and B && not(C)
A= $_->{"column_name"} eq $firstB= $_->{"is_auto_increment"}C= !$_->{'is_pk'}
ABCresult
0XX0
10X0
1100
1111
512 9 ▶ @{ $self->target->{columns}{$table} // [] }) {
Condition50%1/2
$self->target->{"columns"}{$table} // []
l!l
90
MC/DC0%0/1
$self->target->{"columns"}{$table} // []
$self->target->{"columns"}{$table}
Truth table50%1/2
A
A= $self->target->{"columns"}{$table}
Aresult
11
00
513 3 $taken{$table}{$first} = 1;
514 3 push @{ $inline{$table} }, { index_name => $op->index_name, index_info => $info };
515 3 next;
516 }
517 }
518 62 push @rest, $op;
519 }
520 168 return (\%inline, @rest);
521 }
522  
523 # The target foreign keys of the tables the diff creates, split into those the
524 # CREATE TABLE of the table carries (a hashref table => [ fks ]) and those
525 # that are added after the indexes (the list of ADD ops). A foreign key is
526 # deferred when it references columns that the primary key of the referenced
527 # table does not start with and an index the diff creates does: at CREATE TABLE
528 # time that index is not there, and the server refuses the constraint (1822 on
529 # MySQL, errno 150 on MariaDB) (karr k81). It is deferred as well when an index
530 # the diff creates on the new table covers its own columns (karr k92).
531 168 ▶sub _split_inline_foreign_keys {
Subroutine _split_inline_foreign_keys: called CC=19 SCAR=29.8
532 168 my ($self, $index_ops) = @_;
533 168 ▶ my $source_tables = $self->source->{tables} // {};
Condition50%1/2
$self->source->{"tables"} // {}
l!l
1680
MC/DC0%0/1
$self->source->{"tables"} // {}
$self->source->{"tables"}
Truth table50%1/2
A
A= $self->source->{"tables"}
Aresult
11
00
534 168 my $target = $self->target;
535  
536 168 my %created_index;
537 33 push @{ $created_index{ $_->table_name } }, $_->index_info
538 168 for grep { $_->action eq 'create' } @$index_ops;
539  
540 168 my (%inline, @deferred);
541 168 ▶ for my $table (sort keys %{ $target->{foreign_keys} // {} }) {
Condition50%1/2
$target->{"foreign_keys"} // {}
l!l
1680
MC/DC0%0/1
$target->{"foreign_keys"} // {}
$target->{"foreign_keys"}
Truth table50%1/2
A
A= $target->{"foreign_keys"}
Aresult
11
00
542 39 my $fks = $target->{foreign_keys}{$table};
543 39 ▶ if (exists $source_tables->{$table}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $source_tables->{$table}) 23 16 39
544 23 $inline{$table} = $fks; # not a table the diff creates: not looked at
545 23 next;
546 }
547 16 for my $fk (@$fks) {
548 17 my $to = $fk->{to_table};
549 17 ▶ my $columns = $fk->{to_columns} // [];
Condition50%1/2
$fk->{"to_columns"} // []
l!l
170
MC/DC0%0/1
$fk->{"to_columns"} // []
$fk->{"to_columns"}
Truth table50%1/2
A
A= $fk->{"to_columns"}
Aresult
11
00
550 2 ▶ my $by_pk = grep { ($_->{origin} // '') eq 'pk' && _index_covers($_, $columns) }
Condition40%2/5
($_->{'origin'} // '') eq 'pk' && _index_covers($_, $columns)
!ll&&!rl&&r
200
$_->{"origin"} // ""
l!l
20
MC/DC0%0/2
($_->{'origin'} // '') eq 'pk' && _index_covers($_, $columns)
$_->{"origin"} _index_covers($_, $columns)
Truth table0%0/3
A && B
A= $_->{"origin"}B= _index_covers($_, $columns)
ABresult
0X0
100
111
551 17 ▶ values %{ $target->{indexes}{$to} // {} };
Condition100%2/2
$target->{"indexes"}{$to} // {}
l!l
143
MC/DC100%1/1
$target->{"indexes"}{$to} // {}
$target->{"indexes"}{$to}
Truth table100%2/2
A
A= $target->{"indexes"}{$to}
Aresult
11
00
552 17 ▶ unless ($by_pk) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($by_pk) 17 0 17
553 17 ▶ my @pk = map { $_->{column_name} } grep { $_->{is_pk} } @{ $target->{columns}{$to} // [] };
Condition50%1/2
$target->{"columns"}{$to} // []
l!l
170
MC/DC0%0/1
$target->{"columns"}{$to} // []
$target->{"columns"}{$to}
Truth table50%1/2
A
A= $target->{"columns"}{$to}
Aresult
11
00
554 17 $by_pk = _index_covers({ columns => \@pk }, $columns);
555 }
556 17 ▶ my $waits = !$by_pk && grep { _index_covers($_, $columns) } @{ $created_index{$to} // [] };
Condition100%5/5
!$by_pk && grep({_index_covers($_, $columns);} @{$created_index{$to} // [];})
!ll&&!rl&&r
1331
$created_index{$to} // []
l!l
13
MC/DC100%2/2
!$by_pk && grep({_index_covers($_, $columns);} @{$created_index{$to} // [];})
!$by_pk grep({_index_covers($_, $columns);} @{$created_index{$to} // [];})
MC/DC100%1/1
$created_index{$to} // []
$created_index{$to}
Truth table100%3/3
not(A) && B
A= !$by_pkB= grep({_index_covers($_, $columns);} @{$created_index{$to} // [];})
ABresult
0X0
100
111
Truth table100%2/2
A
A= $created_index{$to}
Aresult
11
00
557 # An index the diff creates on the table itself over the columns of the
558 # foreign key: in the CREATE TABLE the server makes an index of its own
559 # for the constraint, and the CREATE INDEX after it is a second one over
560 # the same columns. Added after the index, the constraint finds it there
561 # (karr k92).
562 17 ▶ $waits ||= grep { _index_covers($_, $fk->{from_columns} // []) } @{ $created_index{$table} // [] };
Condition85%6/7
$waits ||= grep({_index_covers($_, $fk->{"from_columns"} // []);} @{$created_index{$table} // [];})
l!l&&r!l&&!r
1115
$fk->{"from_columns"} // []
l!l
10
$created_index{$table} // []
l!l
115
MC/DC100%2/2
$waits ||= grep({_index_covers($_, $fk->{"from_columns"} // []);} @{$created_index{$table} // [];})
$waits grep({_index_covers($_, $fk->{"from_columns"} // []);} @{$created_index{$table} // [];})
MC/DC0%0/1
$fk->{"from_columns"} // []
$fk->{"from_columns"}
MC/DC100%1/1
$created_index{$table} // []
$created_index{$table}
Truth table100%3/3
A ||= B
A= $waitsB= grep({_index_covers($_, $fk->{"from_columns"} // []);} @{$created_index{$table} // [];})
ABresult
1X1
011
000
Truth table50%1/2
A
A= $fk->{"from_columns"}
Aresult
11
00
Truth table100%2/2
A
A= $created_index{$table}
Aresult
11
00
563 17 ▶ if ($waits) {
Branch: 1 branch
BranchTrueFalseTotal
if ($waits) { } 2 15 17
564 push @deferred, DBIO::MySQL::Diff::ForeignKey->new(
565 action => 'add', table_name => $table,
566 2 constraint_name => $fk->{constraint_name}, fk_info => $fk,
567 );
568 }
569 else {
570 15 push @{ $inline{$table} }, $fk;
571 }
572 }
573 }
574 168 return (\%inline, @deferred);
575 }
576  
577 # The order of the tables the diff creates (core hands them over in name
578 # order, which puts a child before its parent): a hashref table => position,
579 # worked out by the rule install_ddl uses (DBIO::MySQL::DDL::order_by_foreign_keys).
580 # $inline_fks is changed in place: the foreign keys of a cycle that cannot stand
581 # in the CREATE TABLE are taken out and returned as ADD ops, to go after all
582 # tables and indexes.
583 168 ▶sub _order_new_tables {
Subroutine _order_new_tables: called CC=6 SCAR=17.9
584 168 my ($self, $inline_fks) = @_;
585 168 ▶ my $source_tables = $self->source->{tables} // {};
Condition50%1/2
$self->source->{"tables"} // {}
l!l
1680
MC/DC0%0/1
$self->source->{"tables"} // {}
$self->source->{"tables"}
Truth table50%1/2
A
A= $self->source->{"tables"}
Aresult
11
00
586 168 ▶ my @new = grep { !exists $source_tables->{$_} } sort keys %{ $self->target->{tables} // {} };
Condition50%1/2
$self->target->{"tables"} // {}
l!l
1680
MC/DC0%0/1
$self->target->{"tables"} // {}
$self->target->{"tables"}
Truth table50%1/2
A
A= $self->target->{"tables"}
Aresult
11
00
587  
588 168 my (%position, @held_ops);
589 168 my $at = 0;
590 168 for my $step (DBIO::MySQL::DDL::order_by_foreign_keys(
591 37 ▶ [ map { { name => $_, fks => ($inline_fks->{$_} // []) } } @new ],
Condition100%2/2
$inline_fks->{$_} // []
l!l
1423
MC/DC100%1/1
$inline_fks->{$_} // []
$inline_fks->{$_}
Truth table100%2/2
A
A= $inline_fks->{$_}
Aresult
11
00
592 50 ▶ sub { $_[0]{to_table} },
Subroutine __ANON__: called CC=1 SCAR=0.0
593 )) {
594 37 my $name = $step->{table}{name};
595 37 $position{$name} = $at++;
596 37 ▶ next unless @{ $step->{held} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$step->{"held"};} 35 2 37
597 2 $inline_fks->{$name} = $step->{inline};
598 push @held_ops, map {
599 DBIO::MySQL::Diff::ForeignKey->new(
600 action => 'add', table_name => $name,
601 2 constraint_name => $_->{constraint_name}, fk_info => $_,
602 )
603 2 } @{ $step->{held} };
604 }
605 168 return (\%position, @held_ops);
606 }
607  
608 # Which index drops would leave a foreign key that stays without an index
609 # (1553), and what to do about it. Returns the index drops and the index
610 # creates, in the order they are written:
611 # - the drop of an index a foreign key still needs, with no other index of
612 # the table left over those columns, is preceded by the create of the
613 # index that takes its place (one of the creates of this diff);
614 # - when that index has the name of the dropped one (a changed index), the
615 # two are one ALTER TABLE ... DROP INDEX, ADD INDEX;
616 # - when the replacement is over columns the table does not have yet, the
617 # whole of it is written where the creates are, and the column changes
618 # come first -- unless a column of the dropped index leaves, which cannot
619 # be put right and dies;
620 # - when the primary key of the TARGET starts with the columns the foreign key
621 # needs (the key grows by them, or a UNIQUE index becomes the key), that key
622 # is the index that takes its place: the drop goes into a third list, which
623 # the caller writes after the key operations (karr k107); but the index the
624 # server made for the constraint itself is dropped by the server with the
625 # key, so there the foreign key is dropped before the index and added again
626 # after the key;
627 # - no replacement dies, naming the index and the foreign key.
628 # Returns the drops, the creates, those late drops and the drops and adds of the
629 # foreign keys that go around an index the server drops itself.
630 # The foreign keys that count are those that are still there when the index is
631 # dropped: the ones of the source that the target keeps as they are, and those
632 # of the CREATE TABLEs (which come first). Constraints that are dropped or
633 # changed are dropped first; those that are added come after the creates.
634 162 ▶sub _protect_foreign_key_indexes {
Subroutine _protect_foreign_key_indexes: called CC=60 SCAR=42.6
635 162 my ($self, $drops, $creates, $inline_fks, $taken_along) = @_;
636 162 my ($source, $target) = ($self->source, $self->target);
637  
638 # A foreign key the primary key operation drops and adds again (karr k107) is
639 # not there when the indexes are dropped: nothing to protect.
640 162 ▶ my %taken = map { refaddr($_->fk_info) => 1 } @{ $taken_along // [] };
Condition50%1/2
$taken_along // []
l!l
1620
MC/DC0%0/1
$taken_along // []
$taken_along
Truth table50%1/2
A
A= $taken_along
Aresult
11
00
641  
642 # The constraints there are when the first index is dropped, as
643 # [ table, columns ]: each one needs an index on the referencing table over
644 # its columns, and one on the referenced table.
645 162 my @alive;
646 162 ▶ my $target_tables = $target->{tables} // {};
Condition50%1/2
$target->{"tables"} // {}
l!l
1620
MC/DC0%0/1
$target->{"tables"} // {}
$target->{"tables"}
Truth table50%1/2
A
A= $target->{"tables"}
Aresult
11
00
647 162 ▶ for my $table (sort keys %{ $source->{foreign_keys} // {} }) {
Condition50%1/2
$source->{"foreign_keys"} // {}
l!l
1620
MC/DC0%0/1
$source->{"foreign_keys"} // {}
$source->{"foreign_keys"}
Truth table50%1/2
A
A= $source->{"foreign_keys"}
Aresult
11
00
648 28 ▶ next unless exists $target_tables->{$table};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $target_tables->{$table} 2 26 28
649 26 ▶ my %new = map { ($_->{constraint_name} // '') => $_ } @{ $target->{foreign_keys}{$table} // [] };
Condition75%3/4
$_->{"constraint_name"} // ""
l!l
200
$target->{"foreign_keys"}{$table} // []
l!l
215
MC/DC0%0/1
$_->{"constraint_name"} // ""
$_->{"constraint_name"}
MC/DC100%1/1
$target->{"foreign_keys"}{$table} // []
$target->{"foreign_keys"}{$table}
Truth table50%1/2
A
A= $_->{"constraint_name"}
Aresult
11
00
Truth table100%2/2
A
A= $target->{"foreign_keys"}{$table}
Aresult
11
00
650 26 for my $fk (@{ $source->{foreign_keys}{$table} }) {
651 26 ▶ my $new = $new{ $fk->{constraint_name} // '' } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $new = $new{$fk->{"constraint_name"} // ""} or next 20 6 26
Condition50%1/2
$fk->{"constraint_name"} // ""
l!l
260
MC/DC0%0/1
$fk->{"constraint_name"} // ""
$fk->{"constraint_name"}
Truth table50%1/2
A
A= $fk->{"constraint_name"}
Aresult
11
00
652 20 ▶ next if changed_fk_fields($fk, $new) || $taken{ refaddr $fk };
Branch: 1 branch
BranchTrueFalseTotal
if changed_fk_fields($fk, $new) or $taken{builtin::refaddr($fk)} 4 16 20
Condition100%3/3
changed_fk_fields($fk, $new) or $taken{builtin::refaddr($fk)}
l!l&&r!l&&!r
2216
MC/DC100%2/2
changed_fk_fields($fk, $new) or $taken{builtin::refaddr($fk)}
changed_fk_fields($fk, $new) $taken{builtin::refaddr($fk)}
Truth table100%3/3
A or B
A= changed_fk_fields($fk, $new)B= $taken{builtin::refaddr($fk)}
ABresult
1X1
011
000
653 16 push @alive, [ $table, $fk->{from_columns} ], [ $fk->{to_table}, $fk->{to_columns} ];
654 }
655 }
656 162 for my $table (sort keys %$inline_fks) {
657 37 ▶ next if exists( ($source->{tables} // {})->{$table} );
Branch: 1 branch
BranchTrueFalseTotal
if exists +($source->{"tables"} // {})->{$table} 23 14 37
Condition50%1/2
$source->{"tables"} // {}
l!l
370
MC/DC0%0/1
$source->{"tables"} // {}
$source->{"tables"}
Truth table50%1/2
A
A= $source->{"tables"}
Aresult
11
00
658 push @alive, [ $table, $_->{from_columns} ], [ $_->{to_table}, $_->{to_columns} ]
659 14 for @{ $inline_fks->{$table} };
660 }
661  
662 162 my (%dropped, %before, %replace_drop, %remove_drop, %after, %replace_create, %remove_create, %late);
663 162 my (@extra_drops, @extra_adds);
664 162 $dropped{ $_->table_name }{ $_->index_name } = 1 for @$drops;
665  
666 162 for my $drop (@$drops) {
667 32 my ($table, $info) = ($drop->table_name, $drop->index_info);
668 32 ▶ my @need = grep { $_->[0] eq $table && _index_covers($info, $_->[1]) } @alive;
Condition66%2/3
$_->[0] eq $table && _index_covers($info, $_->[1])
!ll&&!rl&&r
10010
MC/DC50%1/2
$_->[0] eq $table && _index_covers($info, $_->[1])
$_->[0] eq $table _index_covers($info, $_->[1])
Truth table66%2/3
A && B
A= $_->[0] eq $tableB= _index_covers($info, $_->[1])
ABresult
0X0
100
111
669 32 ▶ next unless @need;
Branch: 1 branch
BranchTrueFalseTotal
unless @need 22 10 32
670  
671 24 ▶ my $wants = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
672 24 my ($index, $need) = @_;
673 24 return _index_covers($index, $need->[1]);
674 10 };
675 # The primary key is one of the surviving indexes.
676 11 ▶ my @survivors = grep { !$dropped{$table}{ $_->{index_name} // '' } && $_->{index_name} ne $drop->index_name }
Condition60%3/5
!$dropped{$table}{$_->{'index_name'} // ''} && $_->{'index_name'} ne $drop->index_name
!ll&&!rl&&r
1001
$_->{"index_name"} // ""
l!l
110
MC/DC50%1/2
!$dropped{$table}{$_->{'index_name'} // ''} && $_->{'index_name'} ne $drop->index_name
!$dropped{$table}{$_->{'index_name'} // ''} $_->{'index_name'} ne $drop->index_name
MC/DC0%0/1
$_->{"index_name"} // ""
$_->{"index_name"}
Truth table66%2/3
not(A) && B
A= !$dropped{$table}{$_->{'index_name'} // ''}B= $_->{'index_name'} ne $drop->index_name
ABresult
0X0
100
111
Truth table50%1/2
A
A= $_->{"index_name"}
Aresult
11
00
677 10 ▶ values %{ $source->{indexes}{$table} // {} };
Condition50%1/2
$source->{"indexes"}{$table} // {}
l!l
100
MC/DC0%0/1
$source->{"indexes"}{$table} // {}
$source->{"indexes"}{$table}
Truth table50%1/2
A
A= $source->{"indexes"}{$table}
Aresult
11
00
678 10 push @survivors, { columns => [ primary_key_columns($source->{columns}{$table}) ] };
679  
680 my @open = grep {
681 10 my $need = $_;
682 10 !grep { $wants->($_, $need) } @survivors;
683 } @need;
684 10 ▶ next unless @open;
Branch: 1 branch
BranchTrueFalseTotal
unless @open 1 9 10
685  
686 # The key the target has is one of the indexes that are left, once the key
687 # operation has been done: the drop waits for it. The foreign key needs
688 # its index at every moment, so the index stays until the key is there --
689 # unless a column of the index leaves with this diff, which cannot wait.
690 9 ▶ my %stays = map { $_->{column_name} => 1 } @{ $target->{columns}{$table} // [] };
Condition50%1/2
$target->{"columns"}{$table} // []
l!l
90
MC/DC0%0/1
$target->{"columns"}{$table} // []
$target->{"columns"}{$table}
Truth table50%1/2
A
A= $target->{"columns"}{$table}
Aresult
11
00
691 9 my $target_key = { columns => [ primary_key_columns($target->{columns}{$table}) ] };
692 9 ▶ if (!grep { !$wants->($target_key, $_) } @open
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {not &$wants($target_key, $_);} @open or grep {not $stays{$_};} @{$info->{"columns"} // [];}) 3 6 9
Condition66%2/3
grep {not &$wants($target_key, $_);} @open or grep {not $stays{$_};} @{$info->{"columns"} // [];}
l!l&&r!l&&!r
603
MC/DC50%1/2
grep {not &$wants($target_key, $_);} @open or grep {not $stays{$_};} @{$info->{"columns"} // [];}
grep {not &$wants($target_key, $_);} @open grep {not $stays{$_};} @{$info->{"columns"} // [];}
Truth table66%2/3
A or B
A= grep {not &$wants($target_key, $_);} @openB= grep {not $stays{$_};} @{$info->{"columns"} // [];}
ABresult
1X1
011
000
693 3 ▶ and !grep { !$stays{$_} } @{ $info->{columns} // [] }) {
Condition50%1/2
$info->{"columns"} // []
l!l
30
MC/DC0%0/1
$info->{"columns"} // []
$info->{"columns"}
Truth table50%1/2
A
A= $info->{"columns"}
Aresult
11
00
694 # The index the server made for the constraint itself (the name of the
695 # constraint, the columns of the foreign key) is dropped by the server
696 # when the key that replaces it is created, and a DROP INDEX after the
697 # key finds nothing: the foreign key is dropped before and added again
698 # after, with the key (as for a key change that it needs), and the index
699 # goes before it as ever.
700 3 ▶ my %target_fk = map { ($_->{constraint_name} // '') => $_ } @{ $target->{foreign_keys}{$table} // [] };
Condition75%3/4
$_->{"constraint_name"} // ""
l!l
20
$target->{"foreign_keys"}{$table} // []
l!l
21
MC/DC0%0/1
$_->{"constraint_name"} // ""
$_->{"constraint_name"}
MC/DC100%1/1
$target->{"foreign_keys"}{$table} // []
$target->{"foreign_keys"}{$table}
Truth table50%1/2
A
A= $_->{"constraint_name"}
Aresult
11
00
Truth table100%2/2
A
A= $target->{"foreign_keys"}{$table}
Aresult
11
00
701 my @server_made = grep {
702 2 my $fk = $_;
703 !$taken{ refaddr $fk }
704 && ($fk->{constraint_name} // '') eq $drop->index_name && !$info->{is_unique}
705 2 ▶ && join("\0", @{ $fk->{from_columns} }) eq join("\0", @{ $info->{columns} // [] })
Condition50%1/2
$info->{"columns"} // []
l!l
20
MC/DC0%0/1
$info->{"columns"} // []
$info->{"columns"}
Truth table50%1/2
A
A= $info->{"columns"}
Aresult
11
00
706 && $target_fk{ $fk->{constraint_name} } && !changed_fk_fields($fk, $target_fk{ $fk->{constraint_name} })
707 3 ▶ } @{ $source->{foreign_keys}{$table} // [] };
Condition42%8/19
!$taken{builtin::refaddr($fk)} && ($fk->{'constraint_name'} // '') eq $drop->index_name && !$info->{'is_unique'} && join("\000", @{$fk->{'from_columns'};}) eq join("\000", @{$info->{'columns'} // [];}) && $target_fk{$fk->{'constraint_name'}} && !changed_fk_fields($fk, $target_fk{$fk->{'constraint_name'}})
!ll&&!rl&&r
002
not $taken{builtin::refaddr($fk)} and ($fk->{"constraint_name"} // "") eq $drop->index_name and not $info->{"is_unique"} and join("\0", @{$fk->{"from_columns"};}) eq join("\0", @{$info->{"columns"} // [];}) and $target_fk{$fk->{"constraint_name"}}
!ll&&!rl&&r
002
not $taken{builtin::refaddr($fk)} and ($fk->{"constraint_name"} // "") eq $drop->index_name and not $info->{"is_unique"} and join("\0", @{$fk->{"from_columns"};}) eq join("\0", @{$info->{"columns"} // [];})
!ll&&!rl&&r
002
not $taken{builtin::refaddr($fk)} and ($fk->{"constraint_name"} // "") eq $drop->index_name and not $info->{"is_unique"}
!ll&&!rl&&r
002
not $taken{builtin::refaddr($fk)} and ($fk->{"constraint_name"} // "") eq $drop->index_name
!ll&&!rl&&r
002
$fk->{"constraint_name"} // ""
l!l
20
$source->{"foreign_keys"}{$table} // []
l!l
21
MC/DC0%0/6
!$taken{builtin::refaddr($fk)} && ($fk->{'constraint_name'} // '') eq $drop->index_name && !$info->{'is_unique'} && join("\000", @{$fk->{'from_columns'};}) eq join("\000", @{$info->{'columns'} // [];}) && $target_fk{$fk->{'constraint_name'}} && !changed_fk_fields($fk, $target_fk{$fk->{'constraint_name'}})
not $taken{builtin::refaddr($fk)} $fk->{"constraint_name"} not $info->{"is_unique"} join("\0", @{$fk->{"from_columns"};}) eq join("\0", @{$info->{"columns"} // [];}) $target_fk{$fk->{"constraint_name"}} !changed_fk_fields($fk, $target_fk{$fk->{'constraint_name'}})
MC/DC100%1/1
$source->{"foreign_keys"}{$table} // []
$source->{"foreign_keys"}{$table}
Truth table14%1/7
not(A) and B and not(C) and D and E && not(F)
A= not $taken{builtin::refaddr($fk)}B= $fk->{"constraint_name"}C= not $info->{"is_unique"}D= join("\0", @{$fk->{"from_columns"};}) eq join("\0", @{$info->{"columns"} // [];})E= $target_fk{$fk->{"constraint_name"}}F= !changed_fk_fields($fk, $target_fk{$fk->{'constraint_name'}})
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
Truth table100%2/2
A
A= $source->{"foreign_keys"}{$table}
Aresult
11
00
708 3 ▶ if (@server_made && !grep {
Branch: 1 branch
BranchTrueFalseTotal
if (@server_made and not grep({my $need = $_; not grep({join("\0", @{$_->{"from_columns"};}) eq join("\0", @{$need->[1];});} @server_made);} @open)) 2 1 3
Condition66%2/3
@server_made and not grep({my $need = $_; not grep({join("\0", @{$_->{"from_columns"};}) eq join("\0", @{$need->[1];});} @server_made);} @open)
!ll&&!rl&&r
102
MC/DC50%1/2
@server_made and not grep({my $need = $_; not grep({join("\0", @{$_->{"from_columns"};}) eq join("\0", @{$need->[1];});} @server_made);} @open)
@server_made not grep({my $need = $_; not grep({join("\0", @{$_->{"from_columns"};}) eq join("\0", @{$need->[1];});} @server_made);} @open)
Truth table66%2/3
A and not(B)
A= @server_madeB= not grep({my $need = $_; not grep({join("\0", @{$_->{"from_columns"};}) eq join("\0", @{$need->[1];});} @server_made);} @open)
ABresult
0X0
100
111
709 2 my $need = $_;
710 2 !grep { join("\0", @{ $_->{from_columns} }) eq join("\0", @{ $need->[1] }) } @server_made
711 } @open) {
712 2 for my $fk (@server_made) {
713 2 $taken{ refaddr $fk } = 1;
714 push @extra_drops, DBIO::MySQL::Diff::ForeignKey->new(
715 action => 'drop', table_name => $table, replaced => 1,
716 2 constraint_name => $fk->{constraint_name}, fk_info => $fk);
717 push @extra_adds, DBIO::MySQL::Diff::ForeignKey->new(
718 action => 'add', table_name => $table,
719 2 constraint_name => $fk->{constraint_name}, fk_info => $fk);
720 }
721 2 next;
722 }
723 1 $late{ refaddr $drop } = 1;
724 1 next;
725 }
726  
727 my ($replacement) = grep {
728 6 my $create = $_;
729 $create->table_name eq $table
730 4 ▶ && !grep { !$wants->($create->index_info, $_) } @open;
Condition33%1/3
$create->table_name eq $table && !grep({not &$wants($create->index_info, $_);} @open)
!ll&&!rl&&r
004
MC/DC0%0/2
$create->table_name eq $table && !grep({not &$wants($create->index_info, $_);} @open)
$create->table_name eq $table !grep({not &$wants($create->index_info, $_);} @open)
Truth table33%1/3
A && not(B)
A= $create->table_name eq $tableB= !grep({not &$wants($create->index_info, $_);} @open)
ABresult
0X0
100
111
731 } @$creates;
732 6 my $needed_by = join ', ', map { '(' . join(', ', @{ $_->[1] }) . ')' } @open;
733 6 ▶ DBIO::Exception->throw(sprintf "DBIO::MySQL::Diff: the index %s on %s is dropped, and a foreign key over the columns %s of that table still needs one: no index of this diff takes its place, and the server refuses (1553)",
Branch: 1 branch
BranchTrueFalseTotal
unless $replacement 2 4 6
734 $drop->index_name, $table, $needed_by)
735 unless $replacement;
736  
737 4 ▶ next if $remove_create{ refaddr $replacement }; # moved for another drop already
Branch: 1 branch
BranchTrueFalseTotal
if $remove_create{builtin::refaddr($replacement)} 0 4 4
738 4 ▶ my %have = map { $_->{column_name} => 1 } @{ $source->{columns}{$table} // [] };
Condition50%1/2
$source->{"columns"}{$table} // []
l!l
40
MC/DC0%0/1
$source->{"columns"}{$table} // []
$source->{"columns"}{$table}
Truth table50%1/2
A
A= $source->{"columns"}{$table}
Aresult
11
00
739 4 ▶ my $early = !%have || !grep { !$have{$_} } @{ $replacement->index_info->{columns} };
Condition66%2/3
%have && grep({not $have{$_};} @{$replacement->index_info->{'columns'};})
!ll&&!rl&&r
022
MC/DC50%1/2
%have && grep({not $have{$_};} @{$replacement->index_info->{'columns'};})
%have grep({not $have{$_};} @{$replacement->index_info->{'columns'};})
Truth table66%2/3
A && B
A= %haveB= grep({not $have{$_};} @{$replacement->index_info->{'columns'};})
ABresult
0X0
100
111
740 4 my $merge = $replacement->index_name eq $drop->index_name;
741  
742 4 ▶ if (!$early) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($early) 2 2 4
743 2 ▶ my %stay = map { $_->{column_name} => 1 } @{ $target->{columns}{$table} // [] };
Condition50%1/2
$target->{"columns"}{$table} // []
l!l
20
MC/DC0%0/1
$target->{"columns"}{$table} // []
$target->{"columns"}{$table}
Truth table50%1/2
A
A= $target->{"columns"}{$table}
Aresult
11
00
744 DBIO::Exception->throw(sprintf "DBIO::MySQL::Diff: the index %s on %s is dropped, a foreign key still needs one, its replacement is over columns the table does not have yet, and a column of the dropped index leaves: no order of the statements is valid",
745 $drop->index_name, $table)
746 2 ▶ if grep { !$stay{$_} } @{ $info->{columns} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $stay{$_};} @{$$info{"columns"};} 0 2 2
747 }
748  
749 4 ▶ if ($merge) {
Branch: 2 branches
BranchTrueFalseTotal
if ($merge) { } 2 2 4
elsif ($early) { } 1 1 2
750 2 my $op = DBIO::MySQL::Diff::Index->new(
751 action => 'replace', table_name => $table, index_name => $drop->index_name,
752 index_info => $replacement->index_info, old_index_info => $info,
753 );
754 2 ▶ if ($early) { $replace_drop{ refaddr $drop } = $op; $remove_create{ refaddr $replacement } = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($early) { } 1 1 2
755 1 else { $remove_drop{ refaddr $drop } = 1; $replace_create{ refaddr $replacement } = $op }
756 }
757 elsif ($early) {
758 1 push @{ $before{ refaddr $drop } }, $replacement;
759 1 $remove_create{ refaddr $replacement } = 1;
760 }
761 else {
762 1 $remove_drop{ refaddr $drop } = 1;
763 1 push @{ $after{ refaddr $replacement } }, $drop;
764 }
765 }
766  
767 160 my @late = grep { $late{ refaddr $_ } } @$drops;
768 my @drops = map {
769 160 my $id = refaddr $_;
770 $late{$id} ? ()
771 : $replace_drop{$id} ? $replace_drop{$id}
772 : $remove_drop{$id} ? ()
773 30 ▶ : (@{ $before{$id} // [] }, $_)
Branch: 3 branches
BranchTrueFalseTotal
$late{$id} ? : 1 29 30
$replace_drop{$id} ? : 1 28 29
$remove_drop{$id} ? : 2 26 28
Condition100%2/2
$before{$id} // []
l!l
125
MC/DC100%1/1
$before{$id} // []
$before{$id}
Truth table100%2/2
A
A= $before{$id}
Aresult
11
00
774 } @$drops;
775 my @creates = map {
776 160 my $id = refaddr $_;
777 $remove_create{$id} ? ()
778 : $replace_create{$id} ? $replace_create{$id}
779 30 ▶ : ($_, @{ $after{$id} // [] })
Branch: 2 branches
BranchTrueFalseTotal
$remove_create{$id} ? : 2 28 30
$replace_create{$id} ? : 1 27 28
Condition100%2/2
$after{$id} // []
l!l
126
MC/DC100%1/1
$after{$id} // []
$after{$id}
Truth table100%2/2
A
A= $after{$id}
Aresult
11
00
780 } @$creates;
781 160 return (\@drops, \@creates, \@late, \@extra_drops, \@extra_adds);
782 }
783  
784 55 1;
785  
786 __END__
« blib/lib/DBIO/MySQL/Introspect/Indexes.pm ↑ Summary blib/lib/DBIO/MySQL/Storage.pm »