Firebird/Diff/Index.pm

filter: Statementstmt 100% 55 / 55 Branchbran 91.6% 11 / 12 Conditioncond 70.0% 7 / 10 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 92.4% 86 / 93 CC 16 SCAR 21.2
CC
16
Cov
95%
diff
13.1
as_sql
4.0
BEGIN
1.0
CRAP 8.3
« Firebird/Storage/InterBase.pm ↑ Summary Firebird/Introspect/Tables.pm »
1 package DBIO::Firebird::Diff::Index;
2 # ABSTRACT: Diff operations for Firebird indexes
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use DBIO::SQL::Util qw(_quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use DBIO::Diff::Compare qw(changed_fields);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 # new() and the action accessor come from DBIO::Diff::Op.
14 3 __PACKAGE__->mk_diff_accessors(qw/table_name index_name index_info/);
15  
16  
17 10 ▶sub diff {
Subroutine diff: called CC=13 SCAR=25.7
Pod documented
18 10 my ($class, $source, $target, $source_tables, $target_tables) = @_;
19 10 my @ops;
20  
21 # Tables present in the source model but absent from the target are being
22 # dropped in this same pass. Their indexes go with the table via DROP TABLE,
23 # so a later standalone DROP INDEX for one of them must be suppressed
24 # (karr #14).
25 10 ▶ $source_tables //= {};
Condition100%2/2
$source_tables //= {}
l!l
64
MC/DC100%1/1
$source_tables //= {}
$source_tables
Truth table100%2/2
A
A= $source_tables
Aresult
11
00
26 10 ▶ $target_tables //= {};
Condition100%2/2
$target_tables //= {}
l!l
64
MC/DC100%1/1
$target_tables //= {}
$target_tables
Truth table100%2/2
A
A= $target_tables
Aresult
11
00
27 3 my %dropped_table = map { $_ => 1 }
28 10 grep { !exists $target_tables->{$_} } keys %$source_tables;
29  
30 10 for my $table_name (sort keys %$target) {
31 8 ▶ my $src_idxs = $source->{$table_name} // {};
Condition50%1/2
$source->{$table_name} // {}
l!l
80
MC/DC0%0/1
$source->{$table_name} // {}
$source->{$table_name}
Truth table50%1/2
A
A= $source->{$table_name}
Aresult
11
00
32 8 my $tgt_idxs = $target->{$table_name};
33  
34 8 for my $name (sort keys %$tgt_idxs) {
35 4 my $tgt = $tgt_idxs->{$name};
36  
37 4 ▶ if (!exists $src_idxs->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $src_idxs->{$name}) 3 1 4
38 3 push @ops, $class->new(
39 action => 'create',
40 table_name => $table_name,
41 index_name => $name,
42 index_info => $tgt,
43 );
44 3 next;
45 }
46  
47 1 my $src = $src_idxs->{$name};
48 # Firebird keeps ORDER-SENSITIVE column comparison: a reordered index is
49 # a different index. So `columns` is a `dim` field (order-preserving),
50 # NOT the order-independent `array`/is_same_index semantics core uses for
51 # engines where index column order is irrelevant.
52 1 my $changed = changed_fields($src, $tgt,
53 bool => ['is_unique'],
54 dim => ['columns'],
55 );
56  
57 1 ▶ if ($changed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed) 1 0 1
58 1 push @ops, $class->new(
59 action => 'drop', table_name => $table_name,
60 index_name => $name, index_info => $src,
61 );
62 1 push @ops, $class->new(
63 action => 'create', table_name => $table_name,
64 index_name => $name, index_info => $tgt,
65 );
66 }
67 }
68 }
69  
70 10 for my $table_name (sort keys %$source) {
71 # Skip the whole table when it is itself being dropped this pass; Firebird's
72 # DROP TABLE already removes its indexes, so a standalone DROP INDEX would
73 # fail against the (now absent) table (karr #14).
74 10 ▶ next if $dropped_table{$table_name};
Branch: 1 branch
BranchTrueFalseTotal
if $dropped_table{$table_name} 2 8 10
75 8 my $src_idxs = $source->{$table_name};
76 8 ▶ my $tgt_idxs = $target->{$table_name} // {};
Condition50%1/2
$target->{$table_name} // {}
l!l
80
MC/DC0%0/1
$target->{$table_name} // {}
$target->{$table_name}
Truth table50%1/2
A
A= $target->{$table_name}
Aresult
11
00
77 8 for my $name (sort keys %$src_idxs) {
78 4 ▶ next if exists $tgt_idxs->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $tgt_idxs->{$name} 1 3 4
79 push @ops, $class->new(
80 action => 'drop',
81 table_name => $table_name,
82 index_name => $name,
83 3 index_info => $src_idxs->{$name},
84 );
85 }
86 }
87  
88 10 return @ops;
89 }
90  
91  
92 8 ▶sub as_sql {
Subroutine as_sql: called CC=4 SCAR=13.9
Pod documented
93 8 my ($self) = @_;
94  
95 8 ▶ if ($self->action eq 'create') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "create") 5 3 8
96 5 ▶ my $unique = $self->index_info->{is_unique} ? 'UNIQUE ' : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->index_info->{'is_unique'} ? : 2 3 5
97 my $cols = join ', ',
98 5 ▶ map { _quote_ident($_) } @{ $self->index_info->{columns} // [] };
Condition50%1/2
$self->index_info->{"columns"} // []
l!l
50
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
99 5 return sprintf 'CREATE %sINDEX %s ON %s (%s);',
100 $unique,
101 _quote_ident($self->index_name),
102 _quote_ident($self->table_name),
103 $cols;
104 }
105 3 return sprintf 'DROP INDEX %s;', _quote_ident($self->index_name);
106 }
107  
108  
109 3 ▶sub summary {
Subroutine summary: called CC=1 SCAR=0.0
Pod documented
110 3 my ($self) = @_;
111 3 return sprintf ' %sindex: %s on %s',
112 $self->summary_prefix, $self->index_name, $self->table_name;
113 }
114  
115 3 1;
116  
117 __END__
118  
119 =pod
120  
121 =encoding UTF-8
122  
123 =head1 NAME
124  
125 DBIO::Firebird::Diff::Index - Diff operations for Firebird indexes
126  
127 =head1 VERSION
128  
129 version 0.900001
130  
131 =head1 DESCRIPTION
132  
133 Index-level diff operations for Firebird. Firebird supports C<CREATE INDEX>
134 and C<DROP INDEX>. Changed index definitions become a drop-then-create pair.
135  
136 =head1 METHODS
137  
138 =head2 diff
139  
140 my @ops = DBIO::Firebird::Diff::Index->diff(
141 $source, $target, $source_tables, $target_tables);
142  
143 Compares index sets across all tables. Index identity is by name; a changed
144 definition produces a drop-then-create pair.
145  
146 The optional C<$source_tables> / C<$target_tables> hashrefs are the C<tables>
147 sections of the two models (threaded in by L<DBIO::Firebird::Diff> via its
148 aux-section wiring). They are used to detect tables that are being dropped in
149 this same diff pass: Firebird's C<DROP TABLE> already removes that table's own
150 indexes, so a later standalone C<DROP INDEX> for one of them would fail once
151 the table is gone and abort the deploy (karr #14). When these arguments are
152 absent (e.g. direct unit-test calls) the dropped-table set is empty and no
153 drops are suppressed, preserving the original two-argument behaviour.
154  
155 =head2 as_sql
156  
157 =head2 summary
158  
159 =head1 AUTHOR
160  
161 DBIO & DBIx::Class Authors
162  
163 =head1 COPYRIGHT AND LICENSE
164  
165 Copyright (C) 2026 DBIO Authors
166 Portions Copyright (C) 2005-2025 DBIx::Class Authors
167 Based on DBIx::Class, heavily modified.
168  
169 This is free software; you can redistribute it and/or modify it under
170 the same terms as the Perl 5 programming language system itself.
171  
172 =cut
« Firebird/Storage/InterBase.pm ↑ Summary Firebird/Introspect/Tables.pm »