SQLite/Diff/Index.pm

filter: Statementstmt 89.2% 50 / 56 Branchbran 56.2% 9 / 16 Conditioncond 44.4% 8 / 18 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 75.0% 87 / 116 CC 16 SCAR 16.4
CC
16
Cov
74%
as_sql
11.2
__ANON__
9.4
diff
3.1
CRAP 5.1
« SQLite/Introspect/Columns.pm ↑ Summary SQLite/Test.pm »
1 package DBIO::SQLite::Diff::Index;
2 # ABSTRACT: Diff operations for SQLite indexes
3  
4 197 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 197 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 197 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 197 ▶use DBIO::Diff::Compare qw(changed_fields);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 197 ▶use DBIO::SQL::Util qw(_quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 197 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 197 __PACKAGE__->mk_diff_accessors(qw/table_name index_name index_info/);
15  
16  
17 23 ▶sub diff {
Subroutine diff: called CC=3 SCAR=11.4
Pod documented
18 23 my ($class, $source, $target, $source_tables, $target_tables) = @_;
19  
20 # Tables present in the source model but absent from the target are being
21 # dropped in this same pass. SQLite's DROP TABLE removes the table's own
22 # indexes automatically, so a standalone DROP INDEX for one of them must be
23 # suppressed -- it would otherwise fail with "no such index" (karr #14).
24 23 ▶ $source_tables //= {};
Condition100%2/2
$source_tables //= {}
l!l
194
MC/DC100%1/1
$source_tables //= {}
$source_tables
Truth table100%2/2
A
A= $source_tables
Aresult
11
00
25 23 ▶ $target_tables //= {};
Condition100%2/2
$target_tables //= {}
l!l
194
MC/DC100%1/1
$target_tables //= {}
$target_tables
Truth table100%2/2
A
A= $target_tables
Aresult
11
00
26 2 my %dropped_table = map { $_ => 1 }
27 23 grep { !exists $target_tables->{$_} } keys %$source_tables;
28  
29 # Index members are already keyed by name, so no index_by. scope 'all'
30 # because an index drop must be emitted even for source-only tables (the
31 # generic walk handles that trailing pass). _is_auto skips PK / UNIQUE
32 # auto-indexes on both sides. SQLite has no ALTER INDEX, so a changed index
33 # is rendered as a drop-then-create pair.
34 return $class->diff_nested($source, $target,
35 scope => 'all',
36 skip => \&_is_auto,
37 1 ▶ changed_when => sub {
Subroutine __ANON__: called CC=5 SCAR=22.5
38 1 my ($src, $tgt) = @_;
39 1 my @changed = changed_fields($src, $tgt,
40 bool => ['is_unique'],
41 dim => ['columns'],
42 );
43 # SQLite-specific: also diff the original CREATE INDEX sql (partial
44 # / expression indexes) when both sides carry it.
45 1 ▶ if (!@changed && (defined $src->{sql} && defined $tgt->{sql})
Branch: 1 branch
BranchTrueFalseTotal
if (not @changed and (defined $src->{"sql"} and defined $tgt->{"sql"}) and $src->{"sql"} ne $tgt->{"sql"}) 0 1 1
Condition22%2/9
not @changed and (defined $src->{"sql"} and defined $tgt->{"sql"}) and $src->{"sql"} ne $tgt->{"sql"}
!ll&&!rl&&r
100
not @changed and (defined $src->{"sql"} and defined $tgt->{"sql"})
!ll&&!rl&&r
100
defined $src->{"sql"} and defined $tgt->{"sql"}
!ll&&!rl&&r
000
MC/DC0%0/4
not @changed and (defined $src->{"sql"} and defined $tgt->{"sql"}) and $src->{"sql"} ne $tgt->{"sql"}
not @changed defined $src->{"sql"} defined $tgt->{"sql"} $src->{"sql"} ne $tgt->{"sql"}
Truth table20%1/5
not(A) and B and C and D
A= not @changedB= defined $src->{"sql"}C= defined $tgt->{"sql"}D= $src->{"sql"} ne $tgt->{"sql"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
46 && $src->{sql} ne $tgt->{sql}) {
47 0 push @changed, 'sql';
48 }
49 1 scalar @changed;
50 },
51 1 ▶ on_new => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
52 1 my ($table, $name, $new) = @_;
53 1 $class->new(
54 action => 'create',
55 table_name => $table,
56 index_name => $name,
57 index_info => $new,
58 );
59 },
60 1 ▶ on_changed => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
61 1 my ($table, $name, $old, $new) = @_;
62 (
63 1 $class->new(
64 action => 'drop', table_name => $table,
65 index_name => $name, index_info => $old,
66 ),
67 $class->new(
68 action => 'create', table_name => $table,
69 index_name => $name, index_info => $new,
70 ),
71 );
72 },
73 5 ▶ on_gone => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
74 5 my ($table, $name, $old) = @_;
75 # Suppress the standalone DROP INDEX when the owning table is itself
76 # being dropped this pass: SQLite's DROP TABLE already removed it, so a
77 # later DROP INDEX would fail with "no such index" (karr #14).
78 5 ▶ return if $dropped_table{$table};
Branch: 1 branch
BranchTrueFalseTotal
if $dropped_table{$table} 2 3 5
79 3 $class->new(
80 action => 'drop',
81 table_name => $table,
82 index_name => $name,
83 index_info => $old,
84 );
85 },
86 23 );
87 }
88  
89 10 ▶sub _is_auto {
Subroutine _is_auto: called CC=3 SCAR=11.4
90 10 my ($info) = @_;
91 10 ▶ return 0 unless defined $info->{origin};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info->{"origin"} 0 10 10
92 10 ▶ return $info->{origin} eq 'u' || $info->{origin} eq 'pk';
Condition66%2/3
$info->{'origin'} eq 'u' || $info->{'origin'} eq 'pk'
l!l&&r!l&&!r
019
MC/DC50%1/2
$info->{'origin'} eq 'u' || $info->{'origin'} eq 'pk'
$info->{'origin'} eq 'u' $info->{'origin'} eq 'pk'
Truth table66%2/3
A || B
A= $info->{'origin'} eq 'u'B= $info->{'origin'} eq 'pk'
ABresult
1X1
011
000
93 }
94  
95  
96 5 ▶sub as_sql {
Subroutine as_sql: called CC=6 SCAR=24.1
Pod documented
97 5 my ($self) = @_;
98  
99 5 ▶ if ($self->action eq 'create') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "create") 2 3 5
100 2 ▶ if (my $sql = $self->index_info->{sql}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sql = $self->index_info->{"sql"}) 2 0 2
101 2 ▶ $sql .= ';' unless $sql =~ /;\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $sql =~ /;\s*$/ 2 0 2
102 2 return $sql;
103 }
104 0 ▶ my $unique = $self->index_info->{is_unique} ? 'UNIQUE ' : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->index_info->{'is_unique'} ? : 0 0 0
105 my $cols = join ', ',
106 0 ▶ map { _quote_ident($_) } @{ $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
107 0 return sprintf 'CREATE %sINDEX %s ON %s (%s);',
108 $unique,
109 _quote_ident($self->index_name),
110 _quote_ident($self->table_name),
111 $cols;
112 }
113 3 return sprintf 'DROP INDEX %s;', _quote_ident($self->index_name);
114 }
115  
116  
117 1 ▶sub summary {
Subroutine summary: called CC=2 SCAR=7.1
Pod documented
118 1 my ($self) = @_;
119 1 ▶ my $prefix = $self->action eq 'create' ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$self->action eq 'create' ? : 1 0 1
120 1 return sprintf ' %sindex: %s on %s', $prefix, $self->index_name, $self->table_name;
121 }
122  
123 197 1;
124  
125 __END__
126  
127 =pod
128  
129 =encoding UTF-8
130  
131 =head1 NAME
132  
133 DBIO::SQLite::Diff::Index - Diff operations for SQLite indexes
134  
135 =head1 VERSION
136  
137 version 0.900001
138  
139 =head1 DESCRIPTION
140  
141 Represents an index-level diff operation: C<CREATE INDEX> or
142 C<DROP INDEX>. SQLite has no C<ALTER INDEX>, so changed definitions
143 become a drop-then-create pair.
144  
145 Auto-generated indexes (origin C<u> for UNIQUE constraints, C<pk> for
146 primary keys) are skipped -- they belong to the table itself, not to
147 explicit C<CREATE INDEX> statements.
148  
149 =head1 METHODS
150  
151 =head2 diff
152  
153 my @ops = DBIO::SQLite::Diff::Index->diff(
154 $source, $target, $source_tables, $target_tables);
155  
156 C<$source> and C<$target> are the C<indexes> sub-models from
157 L<DBIO::SQLite::Introspect>: C<< { $table_name => { $idx_name => $info } } >>.
158  
159 The optional C<$source_tables> / C<$target_tables> hashrefs are the C<tables>
160 sections of the two models (threaded in by L<DBIO::SQLite::Diff>). They are
161 used to detect tables that are being dropped in this same diff pass: SQLite's
162 C<DROP TABLE> automatically removes that table's own indexes, so emitting a
163 later standalone C<DROP INDEX> for one of them would fail with C<no such index>
164 and abort the deploy (karr #14). When these arguments are absent (e.g. direct
165 unit-test calls) the dropped set is empty and no drops are suppressed,
166 preserving the original two-argument behaviour.
167  
168 =head2 as_sql
169  
170 Returns C<CREATE INDEX> (preferring the original C<sql> from
171 C<sqlite_master> if available) or C<DROP INDEX>.
172  
173 =head2 summary
174  
175 =head1 AUTHOR
176  
177 DBIO & DBIx::Class Authors
178  
179 =head1 COPYRIGHT AND LICENSE
180  
181 Copyright (C) 2026 DBIO Authors
182 Portions Copyright (C) 2005-2025 DBIx::Class Authors
183 Based on DBIx::Class, heavily modified.
184  
185 This is free software; you can redistribute it and/or modify it under
186 the same terms as the Perl 5 programming language system itself.
187  
188 =cut
« SQLite/Introspect/Columns.pm ↑ Summary SQLite/Test.pm »