| 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 //= {}
MC/DC100%1/1
$source_tables //= {}
$source_tables
Truth table100%2/2
A
A= $source_tables
| |||||||||||||
| 28 | 153 | ▶ | $target_tables //= {}; | ||||||||||
|
Condition100%2/2
$target_tables //= {}
MC/DC100%1/1
$target_tables //= {}
$target_tables
Truth table100%2/2
A
A= $target_tables
| |||||||||||||
| 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} // {}
MC/DC0%0/1
$source->{$table_name} // {}
$source->{$table_name}
Truth table50%1/2
A
A= $source->{$table_name}
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 77 | 21 | my $src_idxs = $source->{$table_name}; | |||||||||||
| 78 | 21 | ▶ | my $tgt_idxs = $target->{$table_name} // {}; | ||||||||||
|
Condition100%2/2
$target->{$table_name} // {}
MC/DC100%1/1
$target->{$table_name} // {}
$target->{$table_name}
Truth table100%2/2
A
A= $target->{$table_name}
| |||||||||||||
| 79 | 21 | for my $name (sort keys %$src_idxs) { | |||||||||||
| 80 | 18 | ▶ | next if exists $tgt_idxs->{$name}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 98 | 10 | ▶ | my $unique = $self->index_info->{is_unique} ? 'UNIQUE ' : ''; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 99 | my $cols = join ', ', | ||||||||||||
| 100 | 10 | ▶ | map { _firebird_quote_ident($_) } @{ $self->index_info->{columns} // [] }; | ||||||||||
|
Condition50%1/2
$self->index_info->{"columns"} // []
MC/DC0%0/1
$self->index_info->{"columns"} // []
$self->index_info->{"columns"}
Truth table50%1/2
A
A= $self->index_info->{"columns"}
| |||||||||||||
| 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__ | ||||||||||||