| 1 | package DBIO::MySQL::Diff::Index; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for MySQL/MariaDB indexes | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 55 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 55 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 55 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 55 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 55 | ▶ | use DBIO::Diff::Compare qw(changed_index_fields); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 55 | ▶ | use DBIO::MySQL::Identifier qw(quote_ident quote_table_in); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 55 | ▶ | use DBIO::MySQL::IndexType (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 55 | ▶ | use DBIO::MySQL::Diff::View (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 55 | __PACKAGE__->mk_diff_accessors(qw( | |||||||||||||||||||||||||||
| 17 | table_name index_name index_info old_index_info database replaced | ||||||||||||||||||||||||||||
| 18 | )); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 192 | ▶ | sub diff { | ||||||||||||||||||||||||||
|
Subroutine
diff: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 192 | my ($class, $source, $target, $source_tables, $target_tables) = @_; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # Tables present in the source model but absent from the target are being | ||||||||||||||||||||||||||||
| 26 | # dropped in this same pass. Their indexes go with the table via DROP TABLE, | ||||||||||||||||||||||||||||
| 27 | # so a later standalone DROP INDEX for one of them must be suppressed (karr | ||||||||||||||||||||||||||||
| 28 | # #23). Backward compatible: absent table sections -> empty set -> no | ||||||||||||||||||||||||||||
| 29 | # suppression. | ||||||||||||||||||||||||||||
| 30 | 192 | ▶ | $source_tables //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$source_tables //= {}
MC/DC100%1/1
$source_tables //= {}
$source_tables
Truth table100%2/2
A
A= $source_tables
| |||||||||||||||||||||||||||||
| 31 | 192 | ▶ | $target_tables //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$target_tables //= {}
MC/DC100%1/1
$target_tables //= {}
$target_tables
Truth table100%2/2
A
A= $target_tables
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # a view is no table, and has no index to create or drop (karr k79) | ||||||||||||||||||||||||||||
| 34 | 192 | ($source_tables, $source) = DBIO::MySQL::Diff::View::without_views($source_tables, $source); | |||||||||||||||||||||||||||
| 35 | 192 | ($target_tables, $target) = DBIO::MySQL::Diff::View::without_views($target_tables, $target); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 12 | my %dropped_table = map { $_ => 1 } | |||||||||||||||||||||||||||
| 38 | 192 | grep { !exists $target_tables->{$_} } keys %$source_tables; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # Indexes need a "union" view of the per-table membership: an index on | ||||||||||||||||||||||||||||
| 41 | # a brand-new table is just as much a create as one on a table that | ||||||||||||||||||||||||||||
| 42 | # exists in both source and target, and an index on a table that is | ||||||||||||||||||||||||||||
| 43 | # being dropped is just as much a drop. So we walk with `scope => 'all'`: | ||||||||||||||||||||||||||||
| 44 | # * target tables -> on_new, on_changed | ||||||||||||||||||||||||||||
| 45 | # * source-only tables -> on_gone | ||||||||||||||||||||||||||||
| 46 | # * both-side tables -> member-level comparison + on_gone for | ||||||||||||||||||||||||||||
| 47 | # members that vanished | ||||||||||||||||||||||||||||
| 48 | # That's three kinds of "thing happened" with one walk. | ||||||||||||||||||||||||||||
| 49 | return $class->diff_nested( | ||||||||||||||||||||||||||||
| 50 | $source, $target, | ||||||||||||||||||||||||||||
| 51 | scope => 'all', | ||||||||||||||||||||||||||||
| 52 | skip => \&_is_auto, | ||||||||||||||||||||||||||||
| 53 | changed_when => \&changed_index_fields, | ||||||||||||||||||||||||||||
| 54 | 31 | ▶ | on_new => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 31 | my ($table_name, $index_name, $index_info) = @_; | |||||||||||||||||||||||||||
| 56 | 31 | $class->new(action => 'create', table_name => $table_name, | |||||||||||||||||||||||||||
| 57 | index_name => $index_name, index_info => $index_info); | ||||||||||||||||||||||||||||
| 58 | }, | ||||||||||||||||||||||||||||
| 59 | 12 | ▶ | on_changed => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | 12 | my ($table_name, $index_name, $old_info, $new_info) = @_; | |||||||||||||||||||||||||||
| 61 | ( | ||||||||||||||||||||||||||||
| 62 | 12 | $class->new(action => 'drop', table_name => $table_name, replaced => 1, | |||||||||||||||||||||||||||
| 63 | index_name => $index_name, index_info => $old_info), | ||||||||||||||||||||||||||||
| 64 | $class->new(action => 'create', table_name => $table_name, | ||||||||||||||||||||||||||||
| 65 | index_name => $index_name, index_info => $new_info), | ||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||
| 67 | }, | ||||||||||||||||||||||||||||
| 68 | 32 | ▶ | on_gone => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 69 | 32 | my ($table_name, $index_name, $index_info) = @_; | |||||||||||||||||||||||||||
| 70 | # Owning table dropped this pass -> DROP TABLE already took the index; | ||||||||||||||||||||||||||||
| 71 | # a standalone DROP INDEX would fail (karr #23). | ||||||||||||||||||||||||||||
| 72 | 32 | ▶ | return () if $dropped_table{$table_name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 27 | $class->new(action => 'drop', table_name => $table_name, | |||||||||||||||||||||||||||
| 74 | index_name => $index_name, index_info => $index_info); | ||||||||||||||||||||||||||||
| 75 | }, | ||||||||||||||||||||||||||||
| 76 | 192 | ); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # Only the PRIMARY KEY is left out: Diff::Table writes it into CREATE TABLE. | ||||||||||||||||||||||||||||
| 80 | # A unique index (origin 'u') is NOT an auto index here -- the server keeps a | ||||||||||||||||||||||||||||
| 81 | # UNIQUE constraint as nothing but a unique index, and no other diff operation | ||||||||||||||||||||||||||||
| 82 | # renders one, so skipping it meant it never reached the database (karr k70). | ||||||||||||||||||||||||||||
| 83 | 141 | ▶ | sub _is_auto { | ||||||||||||||||||||||||||
|
Subroutine
_is_auto: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 84 | 141 | my ($info) = @_; | |||||||||||||||||||||||||||
| 85 | 141 | ▶ | return 0 unless defined $info->{origin}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 138 | return $info->{origin} eq 'pk'; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 71 | ▶ | sub as_sql { | ||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | 71 | my ($self) = @_; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 71 | ▶ | if ($self->action eq 'create' || $self->action eq 'replace') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->action eq "create" or $self->action eq "replace"
MC/DC100%2/2
$self->action eq "create" or $self->action eq "replace"
$self->action eq "create"
$self->action eq "replace"
Truth table100%3/3
A or B
A= $self->action eq "create"B= $self->action eq "replace"
| |||||||||||||||||||||||||||||
| 94 | 39 | my $info = $self->index_info; | |||||||||||||||||||||||||||
| 95 | 39 | ▶ | my $unique = $info->{is_unique} ? 'UNIQUE ' : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # index_type is the engine's index class/type from introspect (BTREE, | ||||||||||||||||||||||||||||
| 98 | # FULLTEXT, SPATIAL, HASH -- ADR 0052). Render it through the same split | ||||||||||||||||||||||||||||
| 99 | # DDL.pm uses (DBIO::MySQL::IndexType), so a FULLTEXT index a diff creates | ||||||||||||||||||||||||||||
| 100 | # is a FULLTEXT index and not the silent BTREE default (karr k52). A | ||||||||||||||||||||||||||||
| 101 | # typeless index (index_type undef, the common case) or an unrecognised | ||||||||||||||||||||||||||||
| 102 | # token yields no fragments and renders exactly as before -- the empty-list | ||||||||||||||||||||||||||||
| 103 | # return leaves both fragments undef, defaulted to '' here. | ||||||||||||||||||||||||||||
| 104 | 39 | my ($class_kw, $type) = DBIO::MySQL::IndexType::index_type_split($info->{index_type}); | |||||||||||||||||||||||||||
| 105 | 39 | ▶ | $class_kw //= ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$class_kw //= ""
MC/DC100%1/1
$class_kw //= ""
$class_kw
Truth table100%2/2
A
A= $class_kw
| |||||||||||||||||||||||||||||
| 106 | 39 | ▶ | $type //= ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$type //= ""
MC/DC100%1/1
$type //= ""
$type
Truth table100%2/2
A
A= $type
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | my $cols = join ', ', | ||||||||||||||||||||||||||||
| 109 | 39 | ▶ | map { quote_ident($_) } @{ $info->{columns} // [] }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$info->{"columns"} // []
MC/DC0%0/1
$info->{"columns"} // []
$info->{"columns"}
Truth table50%1/2
A
A= $info->{"columns"}
| |||||||||||||||||||||||||||||
| 110 | 39 | ▶ | return sprintf 'ALTER TABLE %s DROP INDEX %s, ADD %s%sINDEX %s (%s)%s;', | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | quote_table_in($self->database, $self->table_name), quote_ident($self->index_name), | ||||||||||||||||||||||||||||
| 112 | $unique, $class_kw, quote_ident($self->index_name), $cols, $type | ||||||||||||||||||||||||||||
| 113 | if $self->action eq 'replace'; | ||||||||||||||||||||||||||||
| 114 | 37 | return sprintf 'CREATE %s%sINDEX %s ON %s (%s)%s;', | |||||||||||||||||||||||||||
| 115 | $unique, $class_kw, quote_ident($self->index_name), | ||||||||||||||||||||||||||||
| 116 | quote_table_in($self->database, $self->table_name), $cols, $type; | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | 32 | return sprintf 'DROP INDEX %s ON %s;', | |||||||||||||||||||||||||||
| 119 | quote_ident($self->index_name), quote_table_in($self->database, $self->table_name); | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 3 | ▶ | sub summary { | ||||||||||||||||||||||||||
|
Subroutine
summary: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 124 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 125 | 3 | ▶ | my $prefix = $self->action eq 'create' ? '+' : $self->action eq 'replace' ? '~' : '-'; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 126 | 3 | return sprintf ' %sindex: %s on %s', | |||||||||||||||||||||||||||
| 127 | $prefix, $self->index_name, $self->table_name; | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 55 | 1; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | __END__ | ||||||||||||||||||||||||||||