MSSQL/Diff/Index.pm

filter: Statementstmt 96.0% 98 / 102 Branchbran 92.1% 35 / 38 Conditioncond 70.9% 22 / 31 MC/DCmcdc 44.4% 8 / 18 Subroutinesub 100% 16 / 16 Podpod 100% 4 / 4 totaltotal 87.5% 183 / 209 CC 36 SCAR 23.2
CC
36
Cov
91%
normalize_filter
15.7
as_sql
15.1
diff
6.0
CRAP 10.2
« MSSQL/Diff/Column.pm ↑ Summary MSSQL/Introspect/Indexes.pm »
1 package DBIO::MSSQL::Diff::Index;
2 # ABSTRACT: Diff operations for MSSQL indexes
3  
4 38 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 38 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 38 our $VERSION = '0.900007';
8  
9 38 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 38 ▶use DBIO::MSSQL::DDL qw(_quote_ident _quote_table);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 38 ▶use DBIO::Diff::Compare qw(changed_fields);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 38 ▶use DBIO::MSSQL::Diff::Table ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 38 __PACKAGE__->mk_diff_accessors(qw/table_name index_name index_info/);
17  
18  
19 101 ▶sub diff {
Subroutine diff: called CC=6 SCAR=18.0
Pod documented
20 101 my ($class, $source, $target, $source_tables, $target_tables) = @_;
21  
22 # Tables present in the source model but absent from the target are being
23 # dropped in this same pass. MSSQL's DROP TABLE also removes the table's own
24 # indexes, so a later standalone DROP INDEX ... ON <table> would fail (the
25 # table no longer exists). Suppress those drops (karr k15). Absent table
26 # sections yield an empty set, preserving the original two-argument
27 # behaviour used by direct unit-test calls.
28 101 ▶ $source_tables //= {};
Condition100%2/2
$source_tables //= {}
l!l
947
MC/DC100%1/1
$source_tables //= {}
$source_tables
Truth table100%2/2
A
A= $source_tables
Aresult
11
00
29 101 ▶ $target_tables //= {};
Condition100%2/2
$target_tables //= {}
l!l
947
MC/DC100%1/1
$target_tables //= {}
$target_tables
Truth table100%2/2
A
A= $target_tables
Aresult
11
00
30  
31 # indexes of a view are not diffed: a view is Diff::View's (karr k82)
32 101 my %is_view = map { $_ => 1 }
33 DBIO::MSSQL::Diff::Table->view_names($source_tables, $target_tables);
34 101 ▶ if (%is_view) {
Branch: 1 branch
BranchTrueFalseTotal
if (%is_view) 13 88 101
35 0 ▶ $source = { map { $_ => $source->{$_} } grep { !$is_view{$_} } keys %{ $source // {} } };
Condition50%1/2
$source // {}
l!l
130
MC/DC0%0/1
$source // {}
$source
Truth table50%1/2
A
A= $source
Aresult
11
00
36 0 ▶ $target = { map { $_ => $target->{$_} } grep { !$is_view{$_} } keys %{ $target // {} } };
Condition50%1/2
$target // {}
l!l
130
MC/DC0%0/1
$target // {}
$target
Truth table50%1/2
A
A= $target
Aresult
11
00
37 }
38 20 my %dropped_table = map { $_ => 1 }
39 101 grep { !exists $target_tables->{$_} } keys %$source_tables;
40  
41 27 ▶ my $create = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
42 27 my ($table, $name, $info) = @_;
43 27 $class->new(action => 'create', table_name => $table,
44 index_name => $name, index_info => $info);
45 101 };
46 22 ▶ my $drop = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
47 22 my ($table, $name, $info) = @_;
48 22 $class->new(action => 'drop', table_name => $table,
49 index_name => $name, index_info => $info);
50 101 };
51  
52 # scope 'all': indexes are diffed for every target table (including
53 # brand-new ones, whose standalone indexes are not created inline by the
54 # table op) plus a trailing drop pass for source-only tables.
55 return $class->diff_nested($source, $target,
56 scope => 'all',
57 35 ▶ changed_when => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
58 scalar changed_fields($_[0], $_[1],
59 bool => ['is_unique', 'is_unique_constraint'],
60 dim => ['columns'],
61 scalar => ['kind'])
62 or $class->normalize_filter($_[0]{filter})
63 35 ▶ ne $class->normalize_filter($_[1]{filter});
Branch: 1 branch
BranchTrueFalseTotal
scalar changed_fields($_[0], $_[1], "bool", ["is_unique", "is_unique_constraint"], "dim", ["columns"], "scalar", ["kind"]) or $class->normalize_filter($_[0]{"filter"}) ne $class->normalize_filter($_[1]{"filter"}) 8 27 35
64 },
65 16 ▶ on_new => sub { $create->(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
66 11 ▶ on_changed => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
67 11 my ($table, $name, $old, $new) = @_;
68 11 ($drop->($table, $name, $old), $create->($table, $name, $new));
69 },
70 15 ▶ on_gone => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
71 15 my ($table, $name, $info) = @_;
72 # Skip when the owning table is itself being dropped this pass; the
73 # DROP TABLE already removes this index (karr k15).
74 15 ▶ return () if $dropped_table{$table};
Branch: 1 branch
BranchTrueFalseTotal
if $dropped_table{$table} 4 11 15
75 11 return $drop->($table, $name, $info);
76 },
77 101 );
78 }
79  
80  
81 75 ▶sub normalize_filter {
Subroutine normalize_filter: called CC=15 SCAR=27.6
Pod documented
82 75 my ($class, $f) = @_;
83 75 ▶ return '' unless defined $f && $f =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $f and $f =~ /\S/ 49 26 75
Condition66%2/3
defined $f and $f =~ /\S/
!ll&&!rl&&r
49026
MC/DC50%1/2
defined $f and $f =~ /\S/
defined $f $f =~ /\S/
Truth table66%2/3
A and B
A= defined $fB= $f =~ /\S/
ABresult
0X0
100
111
84 # literal and non-literal parts alternate; '' inside a literal is an escape
85 26 my @parts = split /('(?:[^']|'')*')/, $f, -1;
86 26 my $out = '';
87 26 for my $i (0 .. $#parts) {
88 38 ▶ if ($i % 2) { $out .= $parts[$i]; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($i % 2) 6 32 38
89 32 my $p = lc $parts[$i];
90 32 $p =~ s/\[(\w+)\]/$1/g;
91 32 $p =~ s/\s+/ /g;
92 32 $p =~ s/ ?([=<>!(,]) ?/$1/g;
93 32 $p =~ s/ \)/)/g;
94 # parentheses around a number: the server writes (1) where a declaration has 1
95 32 ▶ 1 while $p =~ s/(?<=[=<>(,])\((-?[\d.]+)\)/$1/;
Branch: 1 branch
BranchTrueFalseTotal
$p =~ s/(?<=[=<>(,])\((-?[\d.]+)\)/$1/ and do { '???' } 9 32 41
96 32 $out .= $p;
97 }
98 26 $out =~ s/^ +| +$//g;
99 # parentheses around the whole predicate
100 26 ▶ while ($out =~ /^\((.*)\)$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out =~ /^\((.*)\)$/s) 9 26 35
101 9 my ($inner, $depth, $wraps) = ($1, 0, 1);
102 9 my @in = split /('(?:[^']|'')*')/, $inner, -1;
103 9 OUTER: for my $i (0 .. $#in) {
104 13 ▶ next if $i % 2;
Branch: 1 branch
BranchTrueFalseTotal
if $i % 2 2 11 13
105 11 for my $c (split //, $in[$i]) {
106 58 ▶ $depth++ if $c eq '(';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "(" 2 56 58
107 0 ▶ if ($c eq ')' && --$depth < 0) { $wraps = 0; last OUTER }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq ")" and --$depth < 0) 0 58 58
Condition66%2/3
$c eq ")" and --$depth < 0
!ll&&!rl&&r
5620
MC/DC0%0/2
$c eq ")" and --$depth < 0
$c eq ")" --$depth < 0
Truth table66%2/3
A and B
A= $c eq ")"B= --$depth < 0
ABresult
0X0
100
111
108 }
109 }
110 9 ▶ last unless $wraps && $depth == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $wraps and $depth == 0 0 9 9
Condition33%1/3
$wraps and $depth == 0
!ll&&!rl&&r
009
MC/DC0%0/2
$wraps and $depth == 0
$wraps $depth == 0
Truth table33%1/3
A and B
A= $wrapsB= $depth == 0
ABresult
0X0
100
111
111 9 $out = $inner;
112 }
113 26 return $out;
114 }
115  
116  
117 54 ▶sub as_sql {
Subroutine as_sql: called CC=15 SCAR=27.2
Pod documented
118 54 my ($self) = @_;
119  
120 54 my $constraint = $self->index_info->{is_unique_constraint};
121  
122 54 ▶ if ($self->action eq 'create' && $constraint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "create" and $constraint) 11 43 54
Condition100%3/3
$self->action eq "create" and $constraint
!ll&&!rl&&r
241911
MC/DC100%2/2
$self->action eq "create" and $constraint
$self->action eq "create" $constraint
Truth table100%3/3
A and B
A= $self->action eq "create"B= $constraint
ABresult
0X0
100
111
123 return sprintf 'ALTER TABLE %s ADD CONSTRAINT %s UNIQUE %s(%s);',
124 _quote_table($self->table_name), _quote_ident($self->index_name),
125 (($self->index_info->{kind} || '') eq 'clustered' ? 'CLUSTERED ' : ''),
126 11 ▶ join ', ', map { _quote_ident($_) } @{ $self->index_info->{columns} // [] };
Branch: 1 branch
BranchTrueFalseTotal
($self->index_info->{'kind'} || '') eq 'clustered' ? : 0 11 11
Condition75%3/4
$self->index_info->{'kind'} || ''
l!l
74
$self->index_info->{"columns"} // []
l!l
110
MC/DC100%1/1
$self->index_info->{'kind'} || ''
$self->index_info->{'kind'}
MC/DC0%0/1
$self->index_info->{"columns"} // []
$self->index_info->{"columns"}
Truth table100%2/2
A
A= $self->index_info->{'kind'}
Aresult
11
00
Truth table50%1/2
A
A= $self->index_info->{"columns"}
Aresult
11
00
127 }
128 43 ▶ if ($self->action eq 'create') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "create") 19 24 43
129 19 ▶ my $unique = $self->index_info->{is_unique} ? 'UNIQUE ' : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->index_info->{'is_unique'} ? : 3 16 19
130 19 ▶ my $kind = $self->index_info->{kind} || '';
Condition100%2/2
$self->index_info->{'kind'} || ''
l!l
109
MC/DC100%1/1
$self->index_info->{'kind'} || ''
$self->index_info->{'kind'}
Truth table100%2/2
A
A= $self->index_info->{'kind'}
Aresult
11
00
131 19 ▶ my $kind_sql = $kind eq 'clustered' ? 'CLUSTERED ' : $kind eq 'nonclustered' ? 'NONCLUSTERED ' : '';
Branch: 2 branches
BranchTrueFalseTotal
$kind eq 'clustered' ? : 1 18 19
$kind eq 'nonclustered' ? : 9 9 18
132 my $cols = join ', ',
133 19 ▶ map { _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
134 19 my $sql = sprintf 'CREATE %s%sINDEX %s ON %s (%s)',
135 $unique,
136 $kind_sql,
137 _quote_ident($self->index_name),
138 _quote_table($self->table_name),
139 $cols;
140 19 my $filter = $self->index_info->{filter};
141 19 ▶ $sql .= " WHERE $filter" if defined $filter && length $filter;
Branch: 1 branch
BranchTrueFalseTotal
if defined $filter and length $filter 3 16 19
Condition66%2/3
defined $filter and length $filter
!ll&&!rl&&r
1603
MC/DC50%1/2
defined $filter and length $filter
defined $filter length $filter
Truth table66%2/3
A and B
A= defined $filterB= length $filter
ABresult
0X0
100
111
142 19 return "$sql;";
143 }
144 24 ▶ return sprintf 'ALTER TABLE %s DROP CONSTRAINT %s;',
Branch: 1 branch
BranchTrueFalseTotal
if $constraint 8 16 24
145 _quote_table($self->table_name), _quote_ident($self->index_name)
146 if $constraint;
147 16 return sprintf 'DROP INDEX %s ON %s;', _quote_ident($self->index_name), _quote_table($self->table_name);
148 }
149  
150  
151 2 ▶sub summary {
Subroutine summary: called CC=1 SCAR=0.0
Pod documented
152 2 my ($self) = @_;
153 2 return sprintf ' %sindex: %s on %s',
154 $self->summary_prefix, $self->index_name, $self->table_name;
155 }
156  
157 38 1;
158  
159 __END__
« MSSQL/Diff/Column.pm ↑ Summary MSSQL/Introspect/Indexes.pm »