Firebird/Diff/Index.pm

filter: Statementstmt 100% 56 / 56 Branchbran 100% 12 / 12 Conditioncond 80.0% 8 / 10 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 95.7% 90 / 94 CC 16 SCAR 21.2
CC
16
Cov
97%
diff
13.0
as_sql
4.0
BEGIN
1.0
CRAP 8.3
« Firebird/Util.pm ↑ Summary Firebird/Diff/View.pm »
1 package DBIO::Firebird::Diff::Index;
2 # ABSTRACT: Diff operations for Firebird indexes
3  
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 our $VERSION = '0.900007';
8  
9 11 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 ▶use DBIO::Firebird::DDL qw(_firebird_quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use DBIO::Diff::Compare qw(changed_fields);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 # new() and the action accessor come from DBIO::Diff::Op.
16 11 __PACKAGE__->mk_diff_accessors(qw/table_name index_name index_info replaced/);
17  
18  
19 153 ▶sub diff {
Subroutine diff: called CC=13 SCAR=25.7
Pod documented
20 153 my ($class, $source, $target, $source_tables, $target_tables) = @_;
21 153 my @ops;
22  
23 # Tables present in the source model but absent from the target are being
24 # dropped in this same pass. Their indexes go with the table via DROP TABLE,
25 # so a later standalone DROP INDEX for one of them must be suppressed
26 # (karr k14).
27 153 ▶ $source_tables //= {};
Condition100%2/2
$source_tables //= {}
l!l
1485
MC/DC100%1/1
$source_tables //= {}
$source_tables
Truth table100%2/2
A
A= $source_tables
Aresult
11
00
28 153 ▶ $target_tables //= {};
Condition100%2/2
$target_tables //= {}
l!l
1485
MC/DC100%1/1
$target_tables //= {}
$target_tables
Truth table100%2/2
A
A= $target_tables
Aresult
11
00
29 28 my %dropped_table = map { $_ => 1 }
30 153 grep { !exists $target_tables->{$_} } keys %$source_tables;
31  
32 153 for my $table_name (sort keys %$target) {
33 20 ▶ my $src_idxs = $source->{$table_name} // {};
Condition50%1/2
$source->{$table_name} // {}
l!l
200
MC/DC0%0/1
$source->{$table_name} // {}
$source->{$table_name}
Truth table50%1/2
A
A= $source->{$table_name}
Aresult
11
00
34 20 my $tgt_idxs = $target->{$table_name};
35  
36 20 for my $name (sort keys %$tgt_idxs) {
37 18 my $tgt = $tgt_idxs->{$name};
38  
39 18 ▶ if (!exists $src_idxs->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $src_idxs->{$name}) 13 5 18
40 13 push @ops, $class->new(
41 action => 'create',
42 table_name => $table_name,
43 index_name => $name,
44 index_info => $tgt,
45 );
46 13 next;
47 }
48  
49 5 my $src = $src_idxs->{$name};
50 # Firebird keeps ORDER-SENSITIVE column comparison: a reordered index is
51 # a different index. So `columns` is a `dim` field (order-preserving),
52 # NOT the order-independent `array`/is_same_index semantics core uses for
53 # engines where index column order is irrelevant.
54 5 my $changed = changed_fields($src, $tgt,
55 bool => ['is_unique'],
56 dim => ['columns'],
57 );
58  
59 5 ▶ if ($changed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed) 2 3 5
60 2 push @ops, $class->new(
61 action => 'drop', table_name => $table_name,
62 index_name => $name, index_info => $src, replaced => 1,
63 );
64 2 push @ops, $class->new(
65 action => 'create', table_name => $table_name,
66 index_name => $name, index_info => $tgt,
67 );
68 }
69 }
70 }
71  
72 153 for my $table_name (sort keys %$source) {
73 # Skip the whole table when it is itself being dropped this pass; Firebird's
74 # DROP TABLE already removes its indexes, so a standalone DROP INDEX would
75 # fail against the (now absent) table (karr k14).
76 23 ▶ next if $dropped_table{$table_name};
Branch: 1 branch
BranchTrueFalseTotal
if $dropped_table{$table_name} 2 21 23
77 21 my $src_idxs = $source->{$table_name};
78 21 ▶ my $tgt_idxs = $target->{$table_name} // {};
Condition100%2/2
$target->{$table_name} // {}
l!l
201
MC/DC100%1/1
$target->{$table_name} // {}
$target->{$table_name}
Truth table100%2/2
A
A= $target->{$table_name}
Aresult
11
00
79 21 for my $name (sort keys %$src_idxs) {
80 18 ▶ next if exists $tgt_idxs->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $tgt_idxs->{$name} 5 13 18
81 push @ops, $class->new(
82 action => 'drop',
83 table_name => $table_name,
84 index_name => $name,
85 13 index_info => $src_idxs->{$name},
86 );
87 }
88 }
89  
90 153 return @ops;
91 }
92  
93  
94 18 ▶sub as_sql {
Subroutine as_sql: called CC=4 SCAR=13.9
Pod documented
95 18 my ($self) = @_;
96  
97 18 ▶ if ($self->action eq 'create') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "create") 10 8 18
98 10 ▶ my $unique = $self->index_info->{is_unique} ? 'UNIQUE ' : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->index_info->{'is_unique'} ? : 2 8 10
99 my $cols = join ', ',
100 10 ▶ map { _firebird_quote_ident($_) } @{ $self->index_info->{columns} // [] };
Condition50%1/2
$self->index_info->{"columns"} // []
l!l
100
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
101 10 return sprintf 'CREATE %sINDEX %s ON %s (%s);',
102 $unique,
103 _firebird_quote_ident($self->index_name),
104 _firebird_quote_ident($self->table_name),
105 $cols;
106 }
107 8 return sprintf 'DROP INDEX %s;', _firebird_quote_ident($self->index_name);
108 }
109  
110  
111 3 ▶sub summary {
Subroutine summary: called CC=1 SCAR=0.0
Pod documented
112 3 my ($self) = @_;
113 3 return sprintf ' %sindex: %s on %s',
114 $self->summary_prefix, $self->index_name, $self->table_name;
115 }
116  
117 11 1;
118  
119 __END__
« Firebird/Util.pm ↑ Summary Firebird/Diff/View.pm »