MySQL/Diff/Index.pm

filter: Statementstmt 88.1% 37 / 42 Branchbran 60.0% 6 / 10 Conditioncond 88.8% 8 / 9 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 81.8% 9 / 11 Podpod 100% 3 / 3 totaltotal 83.7% 67 / 80 CC 10 SCAR 10.5
CC
10
Cov
84%
summary
6.0
as_sql
4.0
_is_auto
3.0
CRAP 2.9
« MySQL/Result.pm ↑ Summary MySQL/Storage/MariaDB/Async.pm »
1 package DBIO::MySQL::Diff::Index;
2 # ABSTRACT: Diff operations for MySQL/MariaDB 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::Diff::Compare qw(changed_index_fields);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 __PACKAGE__->mk_diff_accessors(qw(
12 table_name index_name index_info
13 ));
14  
15  
16  
17 9 ▶sub diff {
Subroutine diff: called CC=3 SCAR=11.0
Pod documented
18 9 my ($class, $source, $target, $source_tables, $target_tables) = @_;
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
23 # #23). Backward compatible: absent table sections -> empty set -> no
24 # suppression.
25 9 ▶ $source_tables //= {};
Condition100%2/2
$source_tables //= {}
l!l
63
MC/DC100%1/1
$source_tables //= {}
$source_tables
Truth table100%2/2
A
A= $source_tables
Aresult
11
00
26 9 ▶ $target_tables //= {};
Condition100%2/2
$target_tables //= {}
l!l
63
MC/DC100%1/1
$target_tables //= {}
$target_tables
Truth table100%2/2
A
A= $target_tables
Aresult
11
00
27 2 my %dropped_table = map { $_ => 1 }
28 9 grep { !exists $target_tables->{$_} } keys %$source_tables;
29  
30 # Indexes need a "union" view of the per-table membership: an index on
31 # a brand-new table is just as much a create as one on a table that
32 # exists in both source and target, and an index on a table that is
33 # being dropped is just as much a drop. So we walk with `scope => 'all'`:
34 # * target tables -> on_new, on_changed
35 # * source-only tables -> on_gone
36 # * both-side tables -> member-level comparison + on_gone for
37 # members that vanished
38 # That's three kinds of "thing happened" with one walk.
39 return $class->diff_nested(
40 $source, $target,
41 scope => 'all',
42 skip => \&_is_auto,
43 changed_when => \&changed_index_fields,
44 1 ▶ on_new => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
45 1 my ($table_name, $index_name, $index_info) = @_;
46 1 $class->new(action => 'create', table_name => $table_name,
47 index_name => $index_name, index_info => $index_info);
48 },
49 0 ▶ on_changed => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 0 my ($table_name, $index_name, $old_info, $new_info) = @_;
51 (
52 0 $class->new(action => 'drop', table_name => $table_name,
53 index_name => $index_name, index_info => $old_info),
54 $class->new(action => 'create', table_name => $table_name,
55 index_name => $index_name, index_info => $new_info),
56 );
57 },
58 5 ▶ on_gone => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
59 5 my ($table_name, $index_name, $index_info) = @_;
60 # Owning table dropped this pass -> DROP TABLE already took the index;
61 # a standalone DROP INDEX would fail (karr #23).
62 5 ▶ return () if $dropped_table{$table_name};
Branch: 1 branch
BranchTrueFalseTotal
if $dropped_table{$table_name} 2 3 5
63 3 $class->new(action => 'drop', table_name => $table_name,
64 index_name => $index_name, index_info => $index_info);
65 },
66 9 );
67 }
68  
69 8 ▶sub _is_auto {
Subroutine _is_auto: called CC=3 SCAR=11.0
70 8 my ($info) = @_;
71 8 ▶ return 0 unless defined $info->{origin};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info->{"origin"} 0 8 8
72 8 ▶ return $info->{origin} eq 'pk' || $info->{origin} eq 'u';
Condition100%3/3
$info->{'origin'} eq 'pk' || $info->{'origin'} eq 'u'
l!l&&r!l&&!r
116
MC/DC100%2/2
$info->{'origin'} eq 'pk' || $info->{'origin'} eq 'u'
$info->{'origin'} eq 'pk' $info->{'origin'} eq 'u'
Truth table100%3/3
A || B
A= $info->{'origin'} eq 'pk'B= $info->{'origin'} eq 'u'
ABresult
1X1
011
000
73 }
74  
75  
76 4 ▶sub as_sql {
Subroutine as_sql: called CC=4 SCAR=14.0
Pod documented
77 4 my ($self) = @_;
78  
79 4 ▶ if ($self->action eq 'create') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "create") 1 3 4
80 1 ▶ my $unique = $self->index_info->{is_unique} ? 'UNIQUE ' : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->index_info->{'is_unique'} ? : 1 0 1
81 my $cols = join ', ',
82 1 ▶ map { "`$_`" } @{ $self->index_info->{columns} // [] };
Condition50%1/2
$self->index_info->{"columns"} // []
l!l
10
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
83 1 return sprintf 'CREATE %sINDEX `%s` ON `%s` (%s);',
84 $unique, $self->index_name, $self->table_name, $cols;
85 }
86 3 return sprintf 'DROP INDEX `%s` ON `%s`;',
87 $self->index_name, $self->table_name;
88 }
89  
90  
91 0 ▶sub summary {
Subroutine summary: not called CC=2 SCAR=17.9
Pod documented
92 0 my ($self) = @_;
93 0 ▶ my $prefix = $self->action eq 'create' ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$self->action eq 'create' ? : 0 0 0
94 0 return sprintf ' %sindex: %s on %s',
95 $prefix, $self->index_name, $self->table_name;
96 }
97  
98 4 1;
99  
100 __END__
101  
102 =pod
103  
104 =encoding UTF-8
105  
106 =head1 NAME
107  
108 DBIO::MySQL::Diff::Index - Diff operations for MySQL/MariaDB indexes
109  
110 =head1 VERSION
111  
112 version 0.900001
113  
114 =head1 DESCRIPTION
115  
116 Represents an index-level diff operation: C<CREATE INDEX> or
117 C<DROP INDEX>. Auto-generated indexes (PRIMARY KEY and UNIQUE constraint
118 indexes from inline column definitions) are skipped -- they belong to
119 the table itself.
120  
121 =head1 METHODS
122  
123 =head2 diff
124  
125 my @ops = DBIO::MySQL::Diff::Index->diff(
126 $source, $target, $source_tables, $target_tables);
127  
128 The optional C<$source_tables> / C<$target_tables> hashrefs are the C<tables>
129 sections of the two models (threaded in by L<DBIO::MySQL::Diff>, the same way it
130 already threads them into C<Diff::Column> / C<Diff::ForeignKey>). They are used
131 to detect tables that are being dropped in this same diff pass: C<DROP TABLE>
132 already removes that table's own indexes, so emitting a later standalone
133 C<DROP INDEX ... ON <table>> for one of them would fail against the server (the
134 C<ON <table>> target no longer exists), aborting the deploy (karr #23). When
135 these arguments are absent (e.g. direct unit-test calls) the set is empty and no
136 drops are suppressed, preserving the original two-argument behaviour.
137  
138 =head2 as_sql
139  
140 =head2 summary
141  
142 =head1 AUTHOR
143  
144 DBIO & DBIx::Class Authors
145  
146 =head1 COPYRIGHT AND LICENSE
147  
148 Copyright (C) 2026 DBIO Authors
149 Portions Copyright (C) 2005-2025 DBIx::Class Authors
150 Based on DBIx::Class, heavily modified.
151  
152 This is free software; you can redistribute it and/or modify it under
153 the same terms as the Perl 5 programming language system itself.
154  
155 =cut
« MySQL/Result.pm ↑ Summary MySQL/Storage/MariaDB/Async.pm »