blib/lib/DBIO/SQLite/Diff/Index.pm

filter: Statementstmt 95.1% 98 / 103 Branchbran 79.5% 35 / 44 Conditioncond 67.3% 35 / 52 MC/DCmcdc 50.0% 13 / 26 Subroutinesub 100% 17 / 17 Podpod 100% 3 / 3 totaltotal 82.0% 201 / 245 CC 42 SCAR 23.9
CC
42
Cov
84%
_comparable_sql
22.4
as_sql
11.2
__ANON__
5.2
CRAP 10.9
« blib/lib/DBIO/SQLite/Diff/Table.pm ↑ Summary blib/lib/DBIO/SQLite/Diff/View.pm »
1 package DBIO::SQLite::Diff::Index;
2 # ABSTRACT: Diff operations for SQLite indexes
3  
4 190 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 190 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 190 our $VERSION = '0.900007';
8  
9 190 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 190 ▶use DBIO::Diff::Compare qw(changed_fields);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 190 ▶use DBIO::SQLite::Util qw(quote_identifier sql_tokens token_identifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 190 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 190 __PACKAGE__->mk_diff_accessors(qw/table_name index_name index_info replaced/);
17  
18  
19 # The CREATE INDEX statement of an index in the form two of them are compared
20 # in -- see "What counts as a changed index". The tokens of the statement,
21 # without whitespace and comments, joined by one space; a name at a place where
22 # the grammar has nothing but a name is written in the driver's own quoting,
23 # whichever quoting it came in.
24 #
25 # CREATE [UNIQUE] INDEX name ON table ( entry, ... ) [WHERE ...]
26 204 ▶sub _comparable_sql {
Subroutine _comparable_sql: called CC=21 SCAR=31.1
27 204 my ($sql) = @_;
28 204 my @tokens = grep { $_->[0] ne 'skip' } sql_tokens($sql);
29 204 my $plain = join ' ', map { $_->[1] } @tokens;
30  
31 # A token that can be a name: a word, or quoted in anything but apostrophes.
32 706 ▶ my $name = sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
33 706 my ($token) = @_;
34 706 ▶ return unless $token
Branch: 1 branch
BranchTrueFalseTotal
unless $token and $token->[0] eq "word" || $token->[0] eq "quoted" && !($token->[1] =~ /\A'/) 2 704 706
Condition77%7/9
$token and $token->[0] eq "word" || $token->[0] eq "quoted" && !($token->[1] =~ /\A'/)
!ll&&!rl&&r
02704
$token->[0] eq "word" or $token->[0] eq "quoted" and not $token->[1] =~ /\A'/
l!l&&r!l&&!r
916132
$token->[0] eq "quoted" and not $token->[1] =~ /\A'/
!ll&&!rl&&r
02613
MC/DC50%2/4
$token and $token->[0] eq "word" || $token->[0] eq "quoted" && !($token->[1] =~ /\A'/)
$token $token->[0] eq "word" $token->[0] eq "quoted" not $token->[1] =~ /\A'/
Truth table60%3/5
A and B or C and not(D)
A= $tokenB= $token->[0] eq "word"C= $token->[0] eq "quoted"D= not $token->[1] =~ /\A'/
ABCDresult
0XXX0
100X0
10100
11XX1
10111
35 && ($token->[0] eq 'word' || ($token->[0] eq 'quoted' && $token->[1] !~ /\A'/));
36 704 return quote_identifier(token_identifier(@$token));
37 204 };
38 804 ▶ my $keyword = sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
39 804 my ($token, $word) = @_;
40 804 ▶ return $token && $token->[0] eq 'word' && uc $token->[1] eq $word;
Condition50%3/6
$token && $token->[0] eq 'word' && uc $token->[1] eq $word
!ll&&!rl&&r
0176628
$token and $token->[0] eq "word"
!ll&&!rl&&r
00804
MC/DC33%1/3
$token && $token->[0] eq 'word' && uc $token->[1] eq $word
$token $token->[0] eq "word" uc $token->[1] eq $word
Truth table50%2/4
A and B && C
A= $tokenB= $token->[0] eq "word"C= uc $token->[1] eq $word
ABCresult
0XX0
10X0
1100
1111
41 204 };
42  
43 204 my @out;
44 204 my $at = 0;
45 204 ▶ return $plain unless $keyword->($tokens[$at], 'CREATE');
Branch: 1 branch
BranchTrueFalseTotal
unless &$keyword($tokens[$at], "CREATE") 4 200 204
46 200 push @out, 'CREATE';
47 200 $at++;
48 200 ▶ if ($keyword->($tokens[$at], 'UNIQUE')) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$keyword($tokens[$at], "UNIQUE")) 28 172 200
49 28 push @out, 'UNIQUE';
50 28 $at++;
51 }
52 200 ▶ return $plain unless $keyword->($tokens[$at], 'INDEX');
Branch: 1 branch
BranchTrueFalseTotal
unless &$keyword($tokens[$at], "INDEX") 0 200 200
53 200 ▶ my $index = $name->($tokens[ $at + 1 ]) // return $plain;
Condition50%1/2
&$name($tokens[$at + 1]) // (return $plain)
l!l
2000
MC/DC0%0/1
&$name($tokens[$at + 1]) // (return $plain)
&$name($tokens[$at + 1])
Truth table50%1/2
A
A= &$name($tokens[$at + 1])
Aresult
11
00
54 200 ▶ return $plain unless $keyword->($tokens[ $at + 2 ], 'ON');
Branch: 1 branch
BranchTrueFalseTotal
unless &$keyword($tokens[$at + 2], "ON") 0 200 200
55 200 ▶ my $table = $name->($tokens[ $at + 3 ]) // return $plain;
Condition50%1/2
&$name($tokens[$at + 3]) // (return $plain)
l!l
2000
MC/DC0%0/1
&$name($tokens[$at + 3]) // (return $plain)
&$name($tokens[$at + 3])
Truth table50%1/2
A
A= &$name($tokens[$at + 3])
Aresult
11
00
56 200 my $open = $tokens[ $at + 4 ];
57 200 ▶ return $plain unless $open && $open->[0] eq 'punct' && $open->[1] eq '(';
Branch: 1 branch
BranchTrueFalseTotal
unless $open and $open->[0] eq "punct" and $open->[1] eq "(" 0 200 200
Condition33%2/6
$open and $open->[0] eq "punct" and $open->[1] eq "("
!ll&&!rl&&r
00200
$open and $open->[0] eq "punct"
!ll&&!rl&&r
00200
MC/DC0%0/3
$open and $open->[0] eq "punct" and $open->[1] eq "("
$open $open->[0] eq "punct" $open->[1] eq "("
Truth table25%1/4
A and B and C
A= $openB= $open->[0] eq "punct"C= $open->[1] eq "("
ABCresult
0XX0
10X0
1100
1111
58 200 push @out, 'INDEX', $index, 'ON', $table, '(';
59 200 $at += 5;
60  
61 # The entries of the column list, split at its own commas.
62 200 my @entry;
63 200 my $depth = 1;
64 200 ▶ while ($depth && $at <= $#tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth and $at <= $#tokens) 706 200 906
Condition66%2/3
$depth and $at <= $#tokens
!ll&&!rl&&r
2000706
MC/DC50%1/2
$depth and $at <= $#tokens
$depth $at <= $#tokens
Truth table66%2/3
A and B
A= $depthB= $at <= $#tokens
ABresult
0X0
100
111
65 706 my $token = $tokens[ $at++ ];
66 706 my ($kind, $text) = @$token;
67 706 ▶ if ($kind eq 'punct') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "punct") 358 348 706
68 358 ▶ $depth++ if $text eq '(';
Branch: 1 branch
BranchTrueFalseTotal
if $text eq "(" 16 342 358
69 358 ▶ $depth-- if $text eq ')';
Branch: 1 branch
BranchTrueFalseTotal
if $text eq ")" 216 142 358
70 358 ▶ if ($depth == 0 || ($depth == 1 && $text eq ',')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == 0 or $depth == 1 and $text eq ",") 326 32 358
Condition100%6/6
$depth == 0 or $depth == 1 and $text eq ","
l!l&&r!l&&!r
20012632
$depth == 1 and $text eq ","
!ll&&!rl&&r
1616126
MC/DC100%3/3
$depth == 0 or $depth == 1 and $text eq ","
$depth == 0 $depth == 1 $text eq ","
Truth table100%4/4
A or B and C
A= $depth == 0B= $depth == 1C= $text eq ","
ABCresult
1XX1
0111
00X0
0100
71 326 ▶ my $single = @entry == 1 ? $name->($entry[0]) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@entry == 1 ? : 306 20 326
72 326 ▶ push @out, (defined $single ? $single : map { $_->[1] } @entry), $text;
Branch: 1 branch
BranchTrueFalseTotal
defined $single ? : 304 22 326
73 326 @entry = ();
74 326 next;
75 }
76 }
77 380 push @entry, $token;
78 }
79 200 ▶ return $plain if $depth;
Branch: 1 branch
BranchTrueFalseTotal
if $depth 0 200 200
80  
81 200 return join ' ', @out, map { $_->[1] } @tokens[ $at .. $#tokens ];
82 }
83  
84  
85 420 ▶sub diff {
Subroutine diff: called CC=3 SCAR=11.0
Pod documented
86 420 my ($class, $source, $target, $source_tables, $target_tables) = @_;
87  
88 # Tables present in the source model but absent from the target are being
89 # dropped in this same pass. SQLite's DROP TABLE removes the table's own
90 # indexes automatically, so a standalone DROP INDEX for one of them must be
91 # suppressed -- it would otherwise fail with "no such index" (karr k14).
92 420 ▶ $source_tables //= {};
Condition100%2/2
$source_tables //= {}
l!l
36654
MC/DC100%1/1
$source_tables //= {}
$source_tables
Truth table100%2/2
A
A= $source_tables
Aresult
11
00
93 420 ▶ $target_tables //= {};
Condition100%2/2
$target_tables //= {}
l!l
36654
MC/DC100%1/1
$target_tables //= {}
$target_tables
Truth table100%2/2
A
A= $target_tables
Aresult
11
00
94 37 my %dropped_table = map { $_ => 1 }
95 420 grep { !exists $target_tables->{$_} } keys %$source_tables;
96  
97 # Index members are already keyed by name, so no index_by. scope 'all'
98 # because an index drop must be emitted even for source-only tables (the
99 # generic walk handles that trailing pass). _is_auto skips PK / UNIQUE
100 # auto-indexes on both sides. SQLite has no ALTER INDEX, so a changed index
101 # is rendered as a drop-then-create pair.
102 return $class->diff_nested($source, $target,
103 scope => 'all',
104 skip => \&_is_auto,
105 104 ▶ changed_when => sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
106 104 my ($src, $tgt) = @_;
107 104 my @changed = changed_fields($src, $tgt,
108 bool => ['is_unique'],
109 dim => ['columns'],
110 );
111 # SQLite-specific: also diff the original CREATE INDEX sql (partial
112 # / expression indexes) when both sides carry it -- as what it says,
113 # with a name read the same however it is quoted.
114 104 ▶ if (!@changed && (defined $src->{sql} && defined $tgt->{sql})
Branch: 1 branch
BranchTrueFalseTotal
if (not @changed and (defined $src->{"sql"} and defined $tgt->{"sql"}) and _comparable_sql($src->{"sql"}) ne _comparable_sql($tgt->{"sql"})) 33 71 104
Condition66%6/9
not @changed and (defined $src->{"sql"} and defined $tgt->{"sql"}) and _comparable_sql($src->{"sql"}) ne _comparable_sql($tgt->{"sql"})
!ll&&!rl&&r
26933
not @changed and (defined $src->{"sql"} and defined $tgt->{"sql"})
!ll&&!rl&&r
20102
defined $src->{"sql"} and defined $tgt->{"sql"}
!ll&&!rl&&r
00102
MC/DC50%2/4
not @changed and (defined $src->{"sql"} and defined $tgt->{"sql"}) and _comparable_sql($src->{"sql"}) ne _comparable_sql($tgt->{"sql"})
not @changed defined $src->{"sql"} defined $tgt->{"sql"} _comparable_sql($src->{"sql"}) ne _comparable_sql($tgt->{"sql"})
Truth table60%3/5
not(A) and B and C and D
A= not @changedB= defined $src->{"sql"}C= defined $tgt->{"sql"}D= _comparable_sql($src->{"sql"}) ne _comparable_sql($tgt->{"sql"})
ABCDresult
0XXX0
10XX0
110X0
11100
11111
115 && _comparable_sql($src->{sql}) ne _comparable_sql($tgt->{sql})) {
116 33 push @changed, 'sql';
117 }
118 104 scalar @changed;
119 },
120 9 ▶ on_new => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
121 9 my ($table, $name, $new) = @_;
122 9 $class->new(
123 action => 'create',
124 table_name => $table,
125 index_name => $name,
126 index_info => $new,
127 );
128 },
129 35 ▶ on_changed => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
130 35 my ($table, $name, $old, $new) = @_;
131 (
132 35 $class->new(
133 action => 'drop', table_name => $table,
134 index_name => $name, index_info => $old, replaced => 1,
135 ),
136 $class->new(
137 action => 'create', table_name => $table,
138 index_name => $name, index_info => $new,
139 ),
140 );
141 },
142 9 ▶ on_gone => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
143 9 my ($table, $name, $old) = @_;
144 # Suppress the standalone DROP INDEX when the owning table is itself
145 # being dropped this pass: SQLite's DROP TABLE already removed it, so a
146 # later DROP INDEX would fail with "no such index" (karr k14).
147 9 ▶ return if $dropped_table{$table};
Branch: 1 branch
BranchTrueFalseTotal
if $dropped_table{$table} 2 7 9
148 7 $class->new(
149 action => 'drop',
150 table_name => $table,
151 index_name => $name,
152 index_info => $old,
153 );
154 },
155 420 );
156 }
157  
158 358 ▶sub _is_auto {
Subroutine _is_auto: called CC=3 SCAR=11.0
159 358 my ($info) = @_;
160 358 ▶ return 0 unless defined $info->{origin};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info->{"origin"} 0 358 358
161 358 ▶ return $info->{origin} eq 'u' || $info->{origin} eq 'pk';
Condition100%3/3
$info->{'origin'} eq 'u' || $info->{'origin'} eq 'pk'
l!l&&r!l&&!r
8338237
MC/DC100%2/2
$info->{'origin'} eq 'u' || $info->{'origin'} eq 'pk'
$info->{'origin'} eq 'u' $info->{'origin'} eq 'pk'
Truth table100%3/3
A || B
A= $info->{'origin'} eq 'u'B= $info->{'origin'} eq 'pk'
ABresult
1X1
011
000
162 }
163  
164  
165 71 ▶sub as_sql {
Subroutine as_sql: called CC=6 SCAR=24.1
Pod documented
166 71 my ($self) = @_;
167  
168 71 ▶ if ($self->action eq 'create') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "create") 55 16 71
169 55 ▶ if (my $sql = $self->index_info->{sql}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sql = $self->index_info->{"sql"}) 55 0 55
170 55 ▶ $sql .= ';' unless $sql =~ /;\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $sql =~ /;\s*$/ 55 0 55
171 55 return $sql;
172 }
173 0 ▶ my $unique = $self->index_info->{is_unique} ? 'UNIQUE ' : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->index_info->{'is_unique'} ? : 0 0 0
174 my $cols = join ', ',
175 0 ▶ map { quote_identifier($_) } @{ $self->index_info->{columns} // [] };
Condition0%0/2
$self->index_info->{"columns"} // []
l!l
00
MC/DC0%0/1
$self->index_info->{"columns"} // []
$self->index_info->{"columns"}
Truth table0%0/2
A
A= $self->index_info->{"columns"}
Aresult
11
00
176 0 return sprintf 'CREATE %sINDEX %s ON %s (%s);',
177 $unique,
178 quote_identifier($self->index_name),
179 quote_identifier($self->table_name),
180 $cols;
181 }
182 16 return sprintf 'DROP INDEX %s;', quote_identifier($self->index_name);
183 }
184  
185  
186 2 ▶sub summary {
Subroutine summary: called CC=2 SCAR=6.9
Pod documented
187 2 my ($self) = @_;
188 2 ▶ my $prefix = $self->action eq 'create' ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$self->action eq 'create' ? : 1 1 2
189 2 return sprintf ' %sindex: %s on %s', $prefix, $self->index_name, $self->table_name;
190 }
191  
192 190 1;
193  
194 __END__
« blib/lib/DBIO/SQLite/Diff/Table.pm ↑ Summary blib/lib/DBIO/SQLite/Diff/View.pm »