DB2/Diff/Index.pm

filter: Statementstmt 98.9% 93 / 94 Branchbran 96.8% 31 / 32 Conditioncond 71.4% 30 / 42 MC/DCmcdc 44.4% 8 / 18 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 87.9% 175 / 199 CC 41 SCAR 32.6
CC
41
Cov
92%
diff
36.1
as_sql
4.0
_is_pk
3.1
CRAP 26.0
« DB2/Introspect/ForeignKeys.pm ↑ Summary DB2/DDL.pm »
1 package DBIO::DB2::Diff::Index;
2 # ABSTRACT: Diff operations for DB2 indexes
3  
4 46 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 46 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 46 our $VERSION = '0.900007';
8  
9 46 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 46 ▶use DBIO::DB2::DDL qw(_db2_quote_ident _db2_quote_table _split_table_name _index_is_duplicate);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 46 ▶use DBIO::Diff::Compare qw(changed_index_fields);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 46 __PACKAGE__->mk_diff_accessors(qw/table_name index_name index_info/);
16  
17  
18 # The index behind a primary key is the primary key: made by the PRIMARY KEY
19 # clause of CREATE TABLE, gone with the constraint. Its name is the server's and
20 # differs in every schema (karr k45, ADR 0011).
21 59 ▶sub _is_pk { return (($_[0] // {})->{origin} // '') eq 'pk' }
Subroutine _is_pk: called CC=3 SCAR=11.2
Condition75%3/4
+($_[0] // {})->{"origin"} // ""
l!l
1049
$_[0] // {}
l!l
590
MC/DC0%0/1
+($_[0] // {})->{"origin"} // ""
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
22  
23 150 ▶sub diff {
Subroutine diff: called CC=35 SCAR=35.9
Pod documented
24 150 my ($class, $source, $target, $source_tables, $target_tables,
25 $target_columns, $target_ucs, $source_columns) = @_;
26 150 my (@ops, @early);
27  
28 # Tables present in the source model but absent from the target are being
29 # dropped in this same pass. Their indexes go with the table via DROP TABLE,
30 # so a later standalone DROP INDEX for one of them must be suppressed (karr k19).
31 150 ▶ $source_tables //= {};
Condition100%2/2
$source_tables //= {}
l!l
1428
MC/DC100%1/1
$source_tables //= {}
$source_tables
Truth table100%2/2
A
A= $source_tables
Aresult
11
00
32 150 ▶ $target_tables //= {};
Condition100%2/2
$target_tables //= {}
l!l
1428
MC/DC100%1/1
$target_tables //= {}
$target_tables
Truth table100%2/2
A
A= $target_tables
Aresult
11
00
33 10 my %dropped_table = map { $_ => 1 }
34 150 grep { !exists $target_tables->{$_} } keys %$source_tables;
35  
36 150 for my $table_name (sort keys %$target) {
37 31 ▶ my $src_idxs = $source->{$table_name} // {};
Condition100%2/2
$source->{$table_name} // {}
l!l
1615
MC/DC100%1/1
$source->{$table_name} // {}
$source->{$table_name}
Truth table100%2/2
A
A= $source->{$table_name}
Aresult
11
00
38 31 my $tgt_idxs = $target->{$table_name};
39  
40 # what covers a column sequence on this table, in the order the server
41 # meets it: primary key, UNIQUE constraints, the indexes in name order
42 31 my @existing;
43 11 my @pk = map { $_->{column_name} }
44 0 ▶ sort { ($a->{pk_position} // 0) <=> ($b->{pk_position} // 0) }
Condition0%0/4
$DBIO::DB2::Diff::Index::a->{"pk_position"} // 0
l!l
00
$DBIO::DB2::Diff::Index::b->{"pk_position"} // 0
l!l
00
MC/DC0%0/1
$DBIO::DB2::Diff::Index::a->{"pk_position"} // 0
$DBIO::DB2::Diff::Index::a->{"pk_position"}
MC/DC0%0/1
$DBIO::DB2::Diff::Index::b->{"pk_position"} // 0
$DBIO::DB2::Diff::Index::b->{"pk_position"}
Truth table0%0/2
A
A= $DBIO::DB2::Diff::Index::a->{"pk_position"}
Aresult
11
00
Truth table0%0/2
A
A= $DBIO::DB2::Diff::Index::b->{"pk_position"}
Aresult
11
00
45 31 ▶ grep { $_->{is_pk} } @{ ($target_columns // {})->{$table_name} // [] };
Condition100%4/4
+($target_columns // {})->{$table_name} // []
l!l
229
$target_columns // {}
l!l
229
MC/DC100%1/1
+($target_columns // {})->{$table_name} // []
$target_columns
Truth table100%2/2
A
A= $target_columns
Aresult
11
00
46 31 ▶ push @existing, [ \@pk, 1 ] if @pk;
Branch: 1 branch
BranchTrueFalseTotal
if @pk 11 20 31
47 2 push @existing, map { [ $_->[1], 1 ] }
48 31 ▶ @{ ($target_ucs // {})->{$table_name} // [] };
Condition100%4/4
+($target_ucs // {})->{$table_name} // []
l!l
625
$target_ucs // {}
l!l
1120
MC/DC100%1/1
+($target_ucs // {})->{$table_name} // []
$target_ucs
Truth table100%2/2
A
A= $target_ucs
Aresult
11
00
49  
50 31 for my $name (sort keys %$tgt_idxs) {
51 40 my $tgt = $tgt_idxs->{$name};
52 40 ▶ next if _is_pk($tgt);
Branch: 1 branch
BranchTrueFalseTotal
if _is_pk($tgt) 5 35 40
53 35 ▶ my $cols = $tgt->{columns} // [];
Condition50%1/2
$tgt->{"columns"} // []
l!l
350
MC/DC0%0/1
$tgt->{"columns"} // []
$tgt->{"columns"}
Truth table50%1/2
A
A= $tgt->{"columns"}
Aresult
11
00
54  
55 35 ▶ if (_index_is_duplicate($cols, $tgt->{is_unique}, \@existing)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_index_is_duplicate($cols, $tgt->{"is_unique"}, \@existing)) 8 27 35
56 8 next;
57 }
58 27 ▶ push @existing, [ $cols, $tgt->{is_unique} ? 1 : 0 ];
Branch: 1 branch
BranchTrueFalseTotal
$tgt->{'is_unique'} ? : 4 23 27
59  
60 27 ▶ if (!exists $src_idxs->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $src_idxs->{$name}) 20 7 27
61 20 push @ops, $class->new(
62 action => 'create',
63 table_name => $table_name,
64 index_name => $name,
65 index_info => $tgt,
66 );
67 20 next;
68 }
69  
70 7 my $src = $src_idxs->{$name};
71  
72 7 ▶ if (scalar changed_index_fields($src, $tgt)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar changed_index_fields($src, $tgt)) 1 6 7
73 1 push @ops, $class->new(
74 action => 'drop', table_name => $table_name,
75 index_name => $name, index_info => $src,
76 );
77 1 push @ops, $class->new(
78 action => 'create', table_name => $table_name,
79 index_name => $name, index_info => $tgt,
80 );
81 }
82 }
83 }
84  
85 150 for my $table_name (sort keys %$source) {
86 # Db2 drops an index together with a column it covers (DROP COLUMN), so a
87 # DROP INDEX behind it finds nothing (SQL0204N, karr k57): the columns this
88 # pass drops, from the source and target column sections.
89 23 my %gone;
90 23 ▶ if ($source_columns && $source_columns->{$table_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source_columns and $source_columns->{$table_name}) 15 8 23
Condition66%2/3
$source_columns and $source_columns->{$table_name}
!ll&&!rl&&r
8015
MC/DC50%1/2
$source_columns and $source_columns->{$table_name}
$source_columns $source_columns->{$table_name}
Truth table66%2/3
A and B
A= $source_columnsB= $source_columns->{$table_name}
ABresult
0X0
100
111
91 15 ▶ my %kept = map { $_->{column_name} => 1 } @{ ($target_columns // {})->{$table_name} // [] };
Condition75%3/4
+($target_columns // {})->{$table_name} // []
l!l
141
$target_columns // {}
l!l
150
MC/DC0%0/1
+($target_columns // {})->{$table_name} // []
$target_columns
Truth table50%1/2
A
A= $target_columns
Aresult
11
00
92 5 %gone = map { $_->{column_name} => 1 }
93 15 grep { !$kept{ $_->{column_name} } } @{ $source_columns->{$table_name} };
94 }
95 23 my $src_idxs = $source->{$table_name};
96 23 ▶ my $tgt_idxs = $target->{$table_name} // {};
Condition100%2/2
$target->{$table_name} // {}
l!l
167
MC/DC100%1/1
$target->{$table_name} // {}
$target->{$table_name}
Truth table100%2/2
A
A= $target->{$table_name}
Aresult
11
00
97 # Skip the whole table when it is itself being dropped this pass; DROP TABLE
98 # already removes its indexes, so a standalone DROP INDEX would fail (karr k19).
99 23 ▶ next if $dropped_table{$table_name};
Branch: 1 branch
BranchTrueFalseTotal
if $dropped_table{$table_name} 2 21 23
100 21 ▶ my @creates = grep { $_->action eq 'create' && $_->table_name eq $table_name } @ops;
Condition66%2/3
$_->action eq 'create' && $_->table_name eq $table_name
!ll&&!rl&&r
107
MC/DC50%1/2
$_->action eq 'create' && $_->table_name eq $table_name
$_->action eq 'create' $_->table_name eq $table_name
Truth table66%2/3
A && B
A= $_->action eq 'create'B= $_->table_name eq $table_name
ABresult
0X0
100
111
101 21 for my $name (sort keys %$src_idxs) {
102 26 ▶ next if exists $tgt_idxs->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $tgt_idxs->{$name} 7 19 26
103 19 ▶ next if _is_pk($src_idxs->{$name});
Branch: 1 branch
BranchTrueFalseTotal
if _is_pk($src_idxs->{$name}) 5 14 19
104 14 ▶ next if grep { $gone{$_} } @{ $src_idxs->{$name}{columns} // [] };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$gone{$_};} @{$src_idxs->{$name}{"columns"} // [];} 3 11 14
Condition50%1/2
$src_idxs->{$name}{"columns"} // []
l!l
140
MC/DC0%0/1
$src_idxs->{$name}{"columns"} // []
$src_idxs->{$name}{"columns"}
Truth table50%1/2
A
A= $src_idxs->{$name}{"columns"}
Aresult
11
00
105 my $drop = $class->new(
106 action => 'drop',
107 table_name => $table_name,
108 index_name => $name,
109 11 index_info => $src_idxs->{$name},
110 );
111 # a create over the columns of this index is refused (SQL0605W) while the
112 # index is still there: the drop goes first (karr k47)
113 11 my $src = $src_idxs->{$name};
114 11 ▶ if (grep { _index_is_duplicate($_->index_info->{columns}, $_->index_info->{is_unique},
Branch: 1 branch
BranchTrueFalseTotal
if (grep {_index_is_duplicate($_->index_info->{'columns'}, $_->index_info->{'is_unique'}, [[$src->{'columns'} // [], $src->{'is_unique'} ? 1 : 0]]);} @creates) { } 1 10 11
115 2 ▶ [ [ $src->{columns} // [], $src->{is_unique} ? 1 : 0 ] ]) } @creates) {
Branch: 1 branch
BranchTrueFalseTotal
$src->{'is_unique'} ? : 0 2 2
Condition50%1/2
$src->{"columns"} // []
l!l
20
MC/DC0%0/1
$src->{"columns"} // []
$src->{"columns"}
Truth table50%1/2
A
A= $src->{"columns"}
Aresult
11
00
116 1 push @early, $drop;
117 }
118 else {
119 10 push @ops, $drop;
120 }
121 }
122 }
123  
124 150 return (@early, @ops);
125 }
126  
127 # An index lives in the schema of its table (karr k57): for a table named
128 # schema.table the index name is qualified with that schema.
129 34 ▶sub _quoted_index_name {
Subroutine _quoted_index_name: called CC=2 SCAR=6.9
130 34 my ($self) = @_;
131 34 my ($schema) = _split_table_name($self->table_name);
132 34 ▶ return (defined $schema ? _db2_quote_ident($schema) . '.' : '') . _db2_quote_ident($self->index_name);
Branch: 1 branch
BranchTrueFalseTotal
defined $schema ? : 2 32 34
133 }
134  
135  
136 34 ▶sub as_sql {
Subroutine as_sql: called CC=4 SCAR=13.9
Pod documented
137 34 my ($self) = @_;
138  
139 34 ▶ if ($self->action eq 'create') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "create") 19 15 34
140 19 ▶ my $unique = $self->index_info->{is_unique} ? 'UNIQUE ' : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->index_info->{'is_unique'} ? : 3 16 19
141 my $cols = join ', ',
142 19 ▶ map { _db2_quote_ident($_) } @{ $self->index_info->{columns} // [] };
Condition50%1/2
$self->index_info->{"columns"} // []
l!l
190
MC/DC0%0/1
$self->index_info->{"columns"} // []
$self->index_info->{"columns"}
Truth table50%1/2
A
A= $self->index_info->{"columns"}
Aresult
11
00
143 19 return sprintf 'CREATE %sINDEX %s ON %s (%s);',
144 $unique,
145 $self->_quoted_index_name,
146 _db2_quote_table($self->table_name),
147 $cols;
148 }
149 15 return sprintf 'DROP INDEX %s;', $self->_quoted_index_name;
150 }
151  
152  
153 6 ▶sub summary {
Subroutine summary: called CC=1 SCAR=0.0
Pod documented
154 6 my ($self) = @_;
155 6 return sprintf ' %sindex: %s on %s',
156 $self->summary_prefix, $self->index_name, $self->table_name;
157 }
158  
159 46 1;
160  
161 __END__
« DB2/Introspect/ForeignKeys.pm ↑ Summary DB2/DDL.pm »