OnlineDDL/Helper/MySQL.pm

filter: Statementstmt 17.8% 33 / 185 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 24.3% 9 / 37 Podpod 100% 17 / 17 totaltotal 18.8% 59 / 313 CC 31 SCAR 37.7
CC
31
Cov
13%
post_fk_add_cleanup_stmts
156.0
foreign_key_info
90.0
_build_is_galera_cluster
12.0
CRAP 43.4
« OnlineDDL.pm ↑ Summary OnlineDDL/Helper/Base.pm »
1 package DBIx::OnlineDDL::Helper::MySQL;
2  
3 1 our $AUTHORITY = 'cpan:GSG';
4 # ABSTRACT: Private OnlineDDL helper for MySQL-specific code
5 1 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 our $VERSION = 'v1.1.1'; # VERSION
7  
8 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 extends 'DBIx::OnlineDDL::Helper::Base';
12  
13 1 ▶use Types::Standard qw( Bool );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use DBI::Const::GetInfoType;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use DBIx::ParseError::MySQL;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Sub::Util qw( set_subname );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use namespace::clean; # don't export the above
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 #pod =encoding utf8
23 #pod
24 #pod =head1 DESCRIPTION
25 #pod
26 #pod This is a private helper module for any MySQL-specific code. B<As a private module, any
27 #pod methods or attributes here are subject to change.>
28 #pod
29 #pod You should really be reading documentation for L<DBIx::OnlineDDL>. Or, if you want to
30 #pod create a helper module for a different RDBMS, read the docs for
31 #pod L<DBIx::OnlineDDL::Helper::Base>.
32 #pod
33 #pod =cut
34  
35 0 ▶sub dbms_uses_global_fk_namespace { 1 }
Subroutine dbms_uses_global_fk_namespace: not called CC=1 SCAR=6.9
Pod documented
36 0 ▶sub child_fks_need_adjusting { 1 }
Subroutine child_fks_need_adjusting: not called CC=1 SCAR=6.9
Pod documented
37 0 ▶sub null_safe_equals_op { '<=>' }
Subroutine null_safe_equals_op: not called CC=1 SCAR=6.9
Pod documented
38  
39 sub current_catalog_schema {
40 0 ▶ my $self = shift;
Subroutine current_catalog_schema: not called CC=1 SCAR=6.9
Pod documented
41  
42 0 my ($schema) = $self->dbh->selectrow_array('SELECT DATABASE()');
43 0 return (undef, $schema);
44 }
45  
46 1 has is_galera_cluster => (
47 is => 'lazy',
48 isa => Bool,
49 );
50  
51 sub _build_is_galera_cluster {
52 0 ▶ my $self = shift;
Subroutine _build_is_galera_cluster: not called CC=3 SCAR=24.8
53  
54 0 my ($name, $val) = $self->dbh->selectrow_array("SHOW GLOBAL STATUS LIKE 'wsrep_cluster_size'");
55 0 ▶ return $val && int($val) && $val > 1;
Condition0%0/6
$val && int $val && $val > 1
!ll&&!rl&&r
000
$val and int $val
!ll&&!rl&&r
000
MC/DC0%0/3
$val && int $val && $val > 1
$val int $val $val > 1
Truth table0%0/4
A and B && C
A= $valB= int $valC= $val > 1
ABCresult
0XX0
10X0
1100
1111
56 }
57  
58 sub insert_select_stmt {
59 0 ▶ my ($self, $column_list_str) = @_;
Subroutine insert_select_stmt: not called CC=1 SCAR=6.9
Pod documented
60  
61 0 my $dbh = $self->dbh;
62  
63 0 my $id_name_quote = $dbh->quote_identifier( $self->copy_opts->{id_name} );
64  
65 0 my $orig_table_name_quote = $dbh->quote_identifier($self->table_name);
66 0 my $new_table_name_quote = $dbh->quote_identifier($self->new_table_name);
67  
68 # Use INSERT IGNORE to ignore dupe key errors. The LOCK IN SHARE MODE write-locks
69 # the source rows until they are copied. If anything needs to make any changes after
70 # that, the triggers will cover those.
71 0 return join("\n",
72 "INSERT IGNORE INTO $new_table_name_quote",
73 " ($column_list_str)",
74 "SELECT",
75 " $column_list_str",
76 "FROM $orig_table_name_quote",
77 "WHERE $id_name_quote BETWEEN ? AND ?",
78 "LOCK IN SHARE MODE"
79 );
80 }
81  
82 sub post_connection_stmts {
83 0 ▶ my $self = shift;
Subroutine post_connection_stmts: not called CC=1 SCAR=6.9
Pod documented
84  
85 # Initialize these cached attributes
86 0 $self->mmver;
87 0 $self->is_galera_cluster;
88  
89 0 my $db_timeouts = $self->db_timeouts;
90 return (
91 # Use the right database, just in case it's not in the DSN.
92 "USE ".$self->dbh->quote_identifier($self->vars->{schema}),
93  
94 # Foreign key constraints should not interrupt the process. Nor should they be
95 # checked when trying to add or remove them. This would cause a simple FK DDL
96 # to turn into a long-running operation on pre-existing tables.
97 'SET SESSION foreign_key_checks=0',
98  
99 # DB timeouts
100 'SET SESSION wait_timeout='.$db_timeouts->{session},
101 'SET SESSION lock_wait_timeout='.$db_timeouts->{lock_db},
102 'SET SESSION innodb_lock_wait_timeout='.$db_timeouts->{lock_row},
103 0 );
104 }
105  
106 sub is_error_retryable {
107 0 ▶ my ($self, $error) = @_;
Subroutine is_error_retryable: not called CC=1 SCAR=6.9
Pod documented
108 0 return DBIx::ParseError::MySQL->new($error)->is_transient;
109 }
110  
111 sub create_table_sql {
112 0 ▶ my ($self, $table_name) = @_;
Subroutine create_table_sql: not called CC=1 SCAR=6.9
Pod documented
113  
114 0 my $table_name_quote = $self->dbh->quote_identifier($table_name);
115  
116 return $self->dbh_runner(run => set_subname '_create_table_sql', sub {
117 # SHOW CREATE TABLE reports two columns, so might as well pull it out by name
118 0 ▶ local $_->{FetchHashKeyName} = 'NAME_uc';
Subroutine __ANON__: not called CC=1 SCAR=6.9
119 0 $_->selectrow_hashref("SHOW CREATE TABLE $table_name_quote")->{'CREATE TABLE'};
120 0 });
121 }
122  
123 sub rename_fks_in_table_sql {
124 0 ▶ my ($self, $table_name, $table_sql) = @_;
Subroutine rename_fks_in_table_sql: not called CC=3 SCAR=24.8
Pod documented
125  
126 0 my $dbh = $self->dbh;
127  
128 # Since MySQL uses a global namespace for foreign keys, these will have to be renamed
129 0 ▶ my $iq_chars = $dbh->get_info( $GetInfoType{SQL_IDENTIFIER_QUOTE_CHAR} ) || '`';
Condition0%0/2
$dbh->get_info($DBIx::OnlineDDL::Helper::MySQL::GetInfoType{'SQL_IDENTIFIER_QUOTE_CHAR'}) || '`'
l!l
00
MC/DC0%0/1
$dbh->get_info($DBIx::OnlineDDL::Helper::MySQL::GetInfoType{'SQL_IDENTIFIER_QUOTE_CHAR'}) || '`'
$dbh->get_info($DBIx::OnlineDDL::Helper::MySQL::GetInfoType{'SQL_IDENTIFIER_QUOTE_CHAR'})
Truth table0%0/2
A
A= $dbh->get_info($DBIx::OnlineDDL::Helper::MySQL::GetInfoType{'SQL_IDENTIFIER_QUOTE_CHAR'})
Aresult
11
00
130 0 $iq_chars .= '"'; # include ANSI quotes
131  
132 0 my $iqre = '['.quotemeta($iq_chars).']';
133 0 my $noniq_re = '[^'.quotemeta($iq_chars).'\s]';
134  
135 0 my @fk_names = ($table_sql =~ /CONSTRAINT ${iqre}(${noniq_re}+)${iqre} FOREIGN KEY/ig);
136  
137 0 foreach my $fk_name (@fk_names) {
138 my $new_fk_name = $self->find_new_identifier(
139 "_${fk_name}" => set_subname '_fk_name_finder', sub {
140 0 ▶ $_[0]->selectrow_array(
Subroutine __ANON__: not called CC=1 SCAR=6.9
141 'SELECT table_name FROM information_schema.key_column_usage WHERE '.join(' AND ',
142 'constraint_schema = DATABASE()',
143 'constraint_name = ?',
144 # This is required for MySQL 5 server table cache optimization in the EXPLAIN plan
145 'table_schema = DATABASE()',
146 ), undef, $_[1]
147 );
148 },
149 0 );
150 0 $self->vars->{foreign_keys}{orig_names}{"$table_name.$new_fk_name"} = $fk_name;
151  
152 0 my $fk_name_re = quotemeta $fk_name;
153 0 $table_sql =~ s/(?<=CONSTRAINT ${iqre})$fk_name_re(?=${iqre} FOREIGN KEY)/$new_fk_name/;
154 }
155  
156 0 return $table_sql;
157 }
158  
159 sub has_conflicting_triggers_on_table {
160 0 ▶ my ($self, $table_name) = @_;
Subroutine has_conflicting_triggers_on_table: not called CC=2 SCAR=17.9
Pod documented
161 0 my $mmver = $self->mmver;
162  
163 # Multiple triggers aren't allowed in MySQL 5.6
164 0 ▶ if ($mmver < 5.007) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mmver < 5.007) { } 0 0 0
165 return $self->dbh_runner(run => set_subname '_has_triggers_on_table', sub {
166 0 ▶ $_->selectrow_array(
Subroutine __ANON__: not called CC=1 SCAR=6.9
167 'SELECT trigger_name FROM information_schema.triggers WHERE event_object_schema = DATABASE() AND event_object_table = ?',
168 undef, $table_name
169 );
170 0 });
171 }
172 # MySQL 5.7+ allows them, so look for anything that looks like a leftover OnlineDDL
173 # trigger name.
174 else {
175 return $self->dbh_runner(run => set_subname '_has_onlineddl_triggers_on_table', sub {
176 0 ▶ $_->selectrow_array(
Subroutine __ANON__: not called CC=1 SCAR=6.9
177 'SELECT trigger_name FROM information_schema.triggers WHERE '.join(' AND ',
178 'event_object_schema = DATABASE()',
179 'event_object_table = ?',
180 'trigger_name LIKE ?',
181 ), undef, $table_name, "\%${table_name}\\_onlineddl\\_\%"
182 );
183 0 });
184 }
185 }
186  
187 sub has_triggers_on_table_to_be_copied {
188 0 ▶ my ($self, $table_name) = @_;
Subroutine has_triggers_on_table_to_be_copied: not called CC=2 SCAR=17.9
Pod documented
189 0 my $mmver = $self->mmver;
190 # Multiple triggers aren't allowed on MySQL 5.6, so there's nothing to copy
191 0 ▶ return 0 if $mmver < 5.007;
Branch: 1 branch
BranchTrueFalseTotal
if $mmver < "5.007" 0 0 0
192  
193 # Look for anything that isn't a leftover OnlineDDL trigger name.
194 return $self->dbh_runner(run => set_subname '_has_non_onlineddl_triggers_on_table', sub {
195 0 ▶ $_->selectrow_array(
Subroutine __ANON__: not called CC=1 SCAR=6.9
196 'SELECT trigger_name FROM information_schema.triggers WHERE '.join(' AND ',
197 'event_object_schema = DATABASE()',
198 'event_object_table = ?',
199 'trigger_name NOT LIKE ?',
200 ), undef, $table_name, "\%${table_name}\\_onlineddl\\_\%"
201 );
202 0 });
203 }
204  
205 sub find_new_trigger_identifier {
206 0 ▶ my ($self, $trigger_name) = @_;
Subroutine find_new_trigger_identifier: not called CC=1 SCAR=6.9
Pod documented
207  
208 return $self->find_new_identifier(
209 $trigger_name => sub {
210 0 ▶ $_[0]->selectrow_array(
Subroutine __ANON__: not called CC=1 SCAR=6.9
211 'SELECT trigger_name FROM information_schema.triggers WHERE event_object_schema = DATABASE() AND trigger_name = ?',
212 undef, $_[1]
213 );
214 },
215 0 );
216 }
217  
218 sub modify_trigger_dml_stmts {
219 0 ▶ my ($self, $stmts) = @_;
Subroutine modify_trigger_dml_stmts: not called CC=1 SCAR=6.9
Pod documented
220  
221 # Ignore errors
222 0 $stmts->{delete_for_update} =~ s/^DELETE/DELETE IGNORE/;
223 0 $stmts->{delete_for_delete} =~ s/^DELETE/DELETE IGNORE/;
224 }
225  
226 # Keep Base->analyze_table
227  
228 sub swap_tables {
229 0 ▶ my ($self, $new_table_name, $orig_table_name, $old_table_name) = @_;
Subroutine swap_tables: not called CC=3 SCAR=24.8
Pod documented
230 0 my $dbh = $self->dbh;
231 0 my $mmver = $self->mmver;
232  
233 # Galera MySQL 8 has the potential to enter a bad MDL BF-BF conflict state, if the
234 # cluster is under enough load to make synchronous replication fall a bit behind and
235 # a RENAME TABLE statement is ran. This workaround makes a broad assumption that the
236 # bug is some sort of race condition between the statements that make updates to the
237 # table, and the RENAME TABLE statement. Thus, we give replication a chance to catch
238 # up first before we execute the RENAME TABLE statement.
239 #
240 # More info: https://perconadev.atlassian.net/browse/PXC-4512
241  
242 0 ▶ if ($mmver >= 8 && $self->is_galera_cluster) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mmver >= 8 and $self->is_galera_cluster) 0 0 0
Condition0%0/3
$mmver >= 8 and $self->is_galera_cluster
!ll&&!rl&&r
000
MC/DC0%0/2
$mmver >= 8 and $self->is_galera_cluster
$mmver >= 8 $self->is_galera_cluster
Truth table0%0/3
A and B
A= $mmver >= 8B= $self->is_galera_cluster
ABresult
0X0
100
111
243 0 $self->progress->message(" Sleeping for 5 seconds");
244 0 sleep 5;
245 }
246  
247 0 my $new_table_name_quote = $dbh->quote_identifier($new_table_name);
248 0 my $orig_table_name_quote = $dbh->quote_identifier($orig_table_name);
249 0 my $old_table_name_quote = $dbh->quote_identifier($old_table_name);
250  
251 0 $self->dbh_runner_do(
252 "RENAME TABLE $orig_table_name_quote TO $old_table_name_quote, $new_table_name_quote TO $orig_table_name_quote"
253 );
254 }
255  
256 sub get_trigger_data {
257 0 ▶ my ($self, $table_name) = @_;
Subroutine get_trigger_data: not called CC=2 SCAR=17.9
Pod documented
258 0 my $mmver = $self->mmver;
259 # Multiple triggers aren't allowed on MySQL 5.6, so there's nothing to copy
260 0 ▶ return 0 if $mmver < 5.007;
Branch: 1 branch
BranchTrueFalseTotal
if $mmver < "5.007" 0 0 0
261  
262 # Look for anything that isn't a leftover OnlineDDL trigger name.
263 $self->dbh_runner(run => set_subname '_non_onlineddl_triggers_on_table', sub {
264 0 ▶ local $_->{FetchHashKeyName} = 'NAME_lc';
Subroutine __ANON__: not called CC=1 SCAR=6.9
265 0 $self->vars->{existing_triggers} = $_->selectall_hashref(
266 'SELECT * FROM information_schema.triggers WHERE '.join(' AND ',
267 'event_object_schema = DATABASE()',
268 'event_object_table = ?',
269 'trigger_name NOT LIKE ?',
270 ), 'trigger_name', undef, $table_name, "\%${table_name}\\_onlineddl\\_\%"
271 );
272 0 });
273 }
274  
275 ### NOTE: The typical SQL in DBD::mysql is badly optimized for MySQL 5 and very large sets
276 ### of databases/table/column combos. Furthermore, the kludgy join to TABLE_CONSTRAINTS
277 ### is entirely unnecessary. See also: https://github.com/perl5-dbi/DBD-mysql/issues/326
278 sub foreign_key_info {
279 0 ▶ my $self = shift;
Subroutine foreign_key_info: not called CC=9 SCAR=45.0
Pod documented
280 0 my $dbh = $self->dbh;
281 0 my $mmver = $self->mmver;
282  
283 # MySQL 8's information_schema implementation isn't a complete dumpster fire of
284 # in-memory jank, and totes won't send your precious DB server into a horrible
285 # OOM death. So, skip this noise in that case.
286 #
287 # More info: https://mysqlserverteam.com/mysql-8-0-improvements-to-information_schema/
288 0 ▶ return $dbh->foreign_key_info(@_) if $mmver >= 8;
Branch: 1 branch
BranchTrueFalseTotal
if $mmver >= 8 0 0 0
289 0 ▶ return if $mmver < 5; # not supported by OnlineDDL, anyway
Branch: 1 branch
BranchTrueFalseTotal
if $mmver < 5 0 0 0
290  
291 my (
292 0 $pk_catalog, $pk_schema, $pk_table,
293 $fk_catalog, $fk_schema, $fk_table,
294 ) = @_;
295  
296 0 my $sql = <<'EOF';
297 SELECT
298 NULL AS PKTABLE_CAT,
299 REFERENCED_TABLE_SCHEMA AS PKTABLE_SCHEM,
300 REFERENCED_TABLE_NAME AS PKTABLE_NAME,
301 REFERENCED_COLUMN_NAME AS PKCOLUMN_NAME,
302 TABLE_CATALOG AS FKTABLE_CAT,
303 TABLE_SCHEMA AS FKTABLE_SCHEM,
304 TABLE_NAME AS FKTABLE_NAME,
305 COLUMN_NAME AS FKCOLUMN_NAME,
306 ORDINAL_POSITION AS KEY_SEQ,
307 NULL AS UPDATE_RULE,
308 NULL AS DELETE_RULE,
309 CONSTRAINT_NAME AS FK_NAME,
310 NULL AS PK_NAME,
311 NULL AS DEFERABILITY,
312 NULL AS UNIQUE_OR_PRIMARY
313 FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
314 EOF
315  
316 ### XXX: This breaks OnlineDDL's re-applying of cross-database FKs in foreign child
317 ### tables. But, without this fix, MySQL has to scan all databases to find them.
318 ### Again, this is no longer a problem with MySQL 8.
319 0 ▶ $fk_schema //= $pk_schema;
Condition0%0/3
$fk_schema //= $pk_schema
l!l&&r!l&&!r
000
MC/DC0%0/2
$fk_schema //= $pk_schema
$fk_schema $pk_schema
Truth table0%0/3
A //= B
A= $fk_schemaB= $pk_schema
ABresult
1X1
011
000
320  
321 0 my @where;
322 my @bind;
323  
324 0 ▶ if (defined $pk_schema) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pk_schema) 0 0 0
325 0 push @where, 'REFERENCED_TABLE_SCHEMA = ?';
326 0 push @bind, $pk_schema;
327 }
328  
329 0 ▶ if (defined $pk_table) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pk_table) 0 0 0
330 0 push @where, 'REFERENCED_TABLE_NAME = ?';
331 0 push @bind, $pk_table;
332 }
333  
334 0 ▶ if (defined $fk_schema) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fk_schema) 0 0 0
335 0 push @where, 'TABLE_SCHEMA = ?';
336 0 push @bind, $fk_schema;
337 }
338  
339 0 ▶ if (defined $fk_table) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fk_table) 0 0 0
340 0 push @where, 'TABLE_NAME = ?';
341 0 push @bind, $fk_table;
342 }
343  
344 0 ▶ if (@where) {
Branch: 1 branch
BranchTrueFalseTotal
if (@where) 0 0 0
345 0 $sql .= "\nWHERE ";
346 0 $sql .= join ' AND ', @where;
347 }
348 0 $sql .= "\nORDER BY TABLE_SCHEMA, TABLE_NAME, ORDINAL_POSITION";
349  
350 # NOTE: This FetchHashKeyName setting is consistent with how DBI's *_info methods column names work.
351 0 local $dbh->{FetchHashKeyName} = 'NAME_uc';
352 0 my $sth = $dbh->prepare($sql);
353 0 $sth->execute(@bind);
354  
355 0 return $sth;
356 }
357  
358 # MySQL uses 'FOREIGN KEY' on DROPs, for some reason
359 around 'remove_fks_from_child_tables_stmts' => sub {
360 0 ▶ my $orig = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
361 0 my @stmts = $orig->(@_);
362  
363 0 return map { s/ DROP CONSTRAINT / DROP FOREIGN KEY /; $_ } @stmts;
364 1 };
365  
366 around 'rename_fks_back_to_original_stmts' => sub {
367 0 ▶ my $orig = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
368 0 my @stmts = $orig->(@_);
369  
370 0 return map { s/ DROP CONSTRAINT / DROP FOREIGN KEY /; $_ } @stmts;
371 1 };
372  
373 # Keep Base->add_fks_back_to_child_tables_stmts (no DROPs on those)
374  
375 # Look for default FK-created indexes that get mysteriously renamed after the FKs are
376 # recreated. (SM-3039)
377 sub post_fk_add_cleanup_stmts {
378 0 ▶ my $self = shift;
Subroutine post_fk_add_cleanup_stmts: not called CC=12 SCAR=50.5
Pod documented
379 0 my $dbh = $self->dbh;
380 0 my $mmver = $self->mmver;
381 0 my $vars = $self->vars;
382 0 my $catalog = $vars->{catalog};
383 0 my $schema = $vars->{schema};
384 0 my $idx_hash = $vars->{indexes}{definitions};
385  
386 0 my @stmts;
387 0 foreach my $table_name (sort keys %$idx_hash) {
388 0 my %old_idx_data = %{ $idx_hash->{$table_name} };
389 0 my %new_idx_data = %{ $self->get_idx_hash($table_name) };
390  
391 0 foreach my $index_name (sort keys %old_idx_data) {
392 0 ▶ next if $index_name eq 'PRIMARY';
Branch: 1 branch
BranchTrueFalseTotal
if $index_name eq "PRIMARY" 0 0 0
393  
394 0 my $old_idx = $old_idx_data{$index_name};
395 0 my $new_idx = $new_idx_data{$index_name};
396 0 my $old_col_str = join ', ', @{$old_idx->{columns}};
397  
398 next if $new_idx &&
399 0 $old_col_str eq join(', ', @{$new_idx->{columns}}) &&
400 $old_idx->{unique} == $new_idx->{unique}
401 0 ▶ ;
Branch: 1 branch
BranchTrueFalseTotal
if $new_idx and $old_col_str eq join(", ", @{$new_idx->{"columns"};}) and $old_idx->{"unique"} == $new_idx->{"unique"} 0 0 0
Condition0%0/6
$new_idx and $old_col_str eq join(", ", @{$new_idx->{"columns"};}) and $old_idx->{"unique"} == $new_idx->{"unique"}
!ll&&!rl&&r
000
$new_idx and $old_col_str eq join(", ", @{$new_idx->{"columns"};})
!ll&&!rl&&r
000
MC/DC0%0/3
$new_idx and $old_col_str eq join(", ", @{$new_idx->{"columns"};}) and $old_idx->{"unique"} == $new_idx->{"unique"}
$new_idx $old_col_str eq join(", ", @{$new_idx->{"columns"};}) $old_idx->{"unique"} == $new_idx->{"unique"}
Truth table0%0/4
A and B and C
A= $new_idxB= $old_col_str eq join(", ", @{$new_idx->{"columns"};})C= $old_idx->{"unique"} == $new_idx->{"unique"}
ABCresult
0XX0
10X0
1100
1111
402  
403 # It failed one of the other checks?
404 0 ▶ if ($new_idx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_idx) 0 0 0
405 my $conditional =
406 $old_idx->{unique} != $new_idx->{unique} ?
407 ($new_idx->{unique} ? "it is now UNIQUE" : "it is no longer UNIQUE") :
408 0 ▶ "its columns have changed (".join(', ', @{$new_idx->{columns}}).")"
Branch: 2 branches
BranchTrueFalseTotal
$old_idx->{'unique'} != $new_idx->{'unique'} ? : 0 0 0
$new_idx->{'unique'} ? : 0 0 0
409 ;
410  
411 0 $self->progress->message( join "\n",
412 '',
413 "WARNING: Found index $table_name.$index_name ($old_col_str), but $conditional!",
414 "Please double-check that the indexes on the table are what you expect!",
415 '',
416 );
417 0 next;
418 }
419  
420 # It looks like we have a mismatch at this point. Try to find the renamed index.
421 $new_idx = first {
422 $_->{name} ne 'PRIMARY' &&
423 0 $old_col_str eq join(', ', @{$_->{columns}}) &&
424 $old_idx->{unique} == $_->{unique}
425 0 ▶ } values %new_idx_data;
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
$_->{"name"} ne "PRIMARY" and $old_col_str eq join(", ", @{$_->{"columns"};}) and $old_idx->{"unique"} == $_->{"unique"} 0 0 0
Condition0%0/3
$_->{"name"} ne "PRIMARY" and $old_col_str eq join(", ", @{$_->{"columns"};})
!ll&&!rl&&r
000
MC/DC0%0/2
$_->{"name"} ne "PRIMARY" and $old_col_str eq join(", ", @{$_->{"columns"};})
$_->{"name"} ne "PRIMARY" $old_col_str eq join(", ", @{$_->{"columns"};})
Truth table0%0/3
A and B
A= $_->{"name"} ne "PRIMARY"B= $old_col_str eq join(", ", @{$_->{"columns"};})
ABresult
0X0
100
111
426  
427 # It disappeared?
428 0 ▶ unless ($new_idx) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($new_idx) 0 0 0
429 0 $self->progress->message( join "\n",
430 '',
431 "WARNING: Found index $table_name.$index_name ($old_col_str), which may have been renamed, but a matching index cannot be found!",
432 "Please double-check that the indexes on the table are what you expect!",
433 '',
434 );
435 0 next;
436 }
437  
438 0 my $new_index_name = $new_idx->{name};
439  
440 # We can only rename in MySQL 5.7
441 0 ▶ if ($mmver < 5.007) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mmver < "5.007") 0 0 0
442 0 $self->progress->message( join "\n",
443 '',
444 "WARNING: Found index $table_name.$index_name ($old_col_str), which was renamed to $new_index_name!",
445 "The index cannot safely renamed in MySQL $mmver, so you may need to DROP/ADD this",
446 "index a different way to correct the problem.",
447 '',
448 );
449 0 next;
450 }
451  
452 # Finally, add a RENAME INDEX statement
453 0 push @stmts, join(' ',
454 "ALTER TABLE",
455 $dbh->quote_identifier( $table_name ),
456 "RENAME INDEX",
457 $dbh->quote_identifier( $new_index_name ),
458 "TO",
459 $dbh->quote_identifier( $index_name ),
460 );
461 }
462 }
463  
464 0 return @stmts;
465 }
466  
467 1 1;
468  
469 __END__
« OnlineDDL.pm ↑ Summary OnlineDDL/Helper/Base.pm »