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