OnlineDDL.pm

filter: Statementstmt 77.1% 449 / 582 Branchbran 35.6% 57 / 160 Conditioncond 23.3% 42 / 180 MC/DCmcdc 7.0% 7 / 100 Subroutinesub 81.9% 59 / 72 Podpod 92.8% 13 / 14 totaltotal 56.5% 627 / 1108 CC 160 SCAR 48.2
CC
160
Cov
59%
_fk_info_to_hash
600.0
BUILD
429.4
_fk_to_sql
42.0
CRAP 124.3
↑ Summary OnlineDDL/Helper/MySQL.pm »
1 package DBIx::OnlineDDL;
2  
3 4 our $AUTHORITY = 'cpan:GSG';
4 # ABSTRACT: Run DDL on online databases safely
5 4 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 our $VERSION = 'v1.1.2'; # VERSION
7  
8 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use open qw(:utf8 :std);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use MooX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 ▶use Types::Standard qw( Str Bool HashRef CodeRef InstanceOf Dict Optional );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use Types::Common::Numeric qw( PositiveNum PositiveInt );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use DBI::Const::GetInfoType;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use DBIx::BatchChunker 0.92; # with stmt attrs
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use Eval::Reversible;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use List::Util 1.44 (qw( uniq any all first )); # 1.44 has uniq
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use Sub::Util qw( subname set_subname );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use Term::ProgressBar 2.14; # with silent option
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # Don't export the above, but don't conflict with StrictConstructor, either
27 4 ▶use namespace::clean -except => [qw< new meta >];
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 4 my $DEFAULT_MAX_ATTEMPTS = 20;
30  
31 #pod =encoding utf8
32 #pod
33 #pod =head1 SYNOPSIS
34 #pod
35 #pod use DBIx::OnlineDDL;
36 #pod use DBIx::BatchChunker;
37 #pod
38 #pod DBIx::OnlineDDL->construct_and_execute(
39 #pod rsrc => $dbic_schema->source('Account'),
40 #pod ### OR ###
41 #pod dbi_connector => $dbix_connector_retry_object,
42 #pod table_name => 'accounts',
43 #pod
44 #pod coderef_hooks => {
45 #pod # This is the phase where the DDL is actually run
46 #pod before_triggers => \&drop_foobar,
47 #pod
48 #pod # Run other operations right before the swap
49 #pod # WARNING: DML only! No DDL here!
50 #pod before_swap => \&delete_deprecated_accounts,
51 #pod },
52 #pod
53 #pod process_name => 'Dropping foobar from accounts',
54 #pod
55 #pod copy_opts => {
56 #pod chunk_size => 5000,
57 #pod debug => 1,
58 #pod },
59 #pod );
60 #pod
61 #pod sub drop_foobar {
62 #pod my $oddl = shift;
63 #pod my $name = $oddl->new_table_name;
64 #pod my $qname = $oddl->dbh->quote_identifier($name);
65 #pod
66 #pod # Drop the 'foobar' column, since it is no longer used
67 #pod $oddl->dbh_runner_do("ALTER TABLE $qname DROP COLUMN foobar");
68 #pod }
69 #pod
70 #pod sub delete_deprecated_accounts {
71 #pod my $oddl = shift;
72 #pod my $name = $oddl->new_table_name;
73 #pod my $dbh = $oddl->dbh; # only use for quoting!
74 #pod
75 #pod my $qname = $dbh->quote_identifier($name);
76 #pod
77 #pod DBIx::BatchChunker->construct_and_execute(
78 #pod chunk_size => 5000,
79 #pod
80 #pod debug => 1,
81 #pod
82 #pod process_name => 'Deleting deprecated accounts',
83 #pod process_past_max => 1,
84 #pod
85 #pod dbic_storage => $oddl->rsrc->storage,
86 #pod min_stmt => "SELECT MIN(account_id) FROM $qname",
87 #pod max_stmt => "SELECT MAX(account_id) FROM $qname",
88 #pod stmt => join("\n",
89 #pod "DELETE FROM $qname",
90 #pod "WHERE",
91 #pod " account_type = ".$dbh->quote('deprecated')." AND",
92 #pod " account_id BETWEEN ? AND ?",
93 #pod ),
94 #pod );
95 #pod }
96 #pod
97 #pod =head1 DESCRIPTION
98 #pod
99 #pod This is a database utility class for running DDL operations (like C<ALTER TABLE>) safely
100 #pod on large tables. It has a similar scope as L<DBIx::BatchChunker>, but is designed for
101 #pod DDL, rather than DML. It also has a similar function to other utilities like
102 #pod L<pt-online-schema-change|https://www.percona.com/doc/percona-toolkit/LATEST/pt-online-schema-change.html> or
103 #pod L<gh-ost|https://github.com/github/gh-ost>, but actually works properly with foreign
104 #pod keys, and is written as a Perl module to hook directly into a DBI handle.
105 #pod
106 #pod Like most online schema change tools, this works by creating a new shell table that looks
107 #pod just like the old table, running the DDL changes (through the L</before_triggers> hook),
108 #pod copying data to the new table, and swapping the tables. Triggers are created to keep the
109 #pod data in sync. See L</STEP METHODS> for more information.
110 #pod
111 #pod The full operation is protected with an L<undo stack|/reversible> via L<Eval::Reversible>.
112 #pod If any step in the process fails, the undo stack is run to return the DB back to normal.
113 #pod
114 #pod This module uses as many of the DBI info methods as possible, along with ANSI SQL in most
115 #pod places, to be compatible with multiple RDBMS. So far, it will work with MySQL or SQLite,
116 #pod but can be expanded to include more systems with a relatively small amount of code
117 #pod changes. (See L<DBIx::OnlineDDL::Helper::Base> for details.)
118 #pod
119 #pod B<DISCLAIMER:> You should not rely on this class to magically fix any and all locking
120 #pod problems the DB might experience just because it's being used. Thorough testing and
121 #pod best practices are still required.
122 #pod
123 #pod =head2 When you shouldn't use this module
124 #pod
125 #pod =head3 Online DDL is already available in the RDBMS
126 #pod
127 #pod If you're running MySQL 5.6+ without clustering, just use C<LOCK=NONE> for every DDL
128 #pod statement. It is seriously simple and guarantees that the table changes you make are not
129 #pod going to lock the table, or it will fail right away to tell you it's an incompatible
130 #pod change.
131 #pod
132 #pod If you're running something like Galera clusters, this typically wouldn't be an option,
133 #pod as it would lock up the clusters while the C<ALTER TABLE> statement is running, despite
134 #pod the C<LOCK=NONE> statement. (Galera clusters were the prime motivation for writing this
135 #pod module.)
136 #pod
137 #pod Other RDBMSs may have support for online DDL as well. Check the documentation first. If
138 #pod they don't, patches for this tool are welcome!
139 #pod
140 #pod =head3 The operation is small
141 #pod
142 #pod Does your DDL only take 2 seconds? Just do it! Don't bother with trying to swap tables
143 #pod around, wasting time with full table copies, etc. It's not worth the time spent or risk.
144 #pod
145 #pod =head3 When you actually want to run DML, not DDL
146 #pod
147 #pod L<DBIx::BatchChunker> is more appropriate for running DML operations (like C<INSERT>,
148 #pod C<UPDATE>, C<DELETE>). If you need to do both, you can use the L</before_triggers> hook
149 #pod for DDL, and the L</before_swap> hook for DML. Or just run DBIx::BatchChunker after the
150 #pod OnlineDDL process is complete.
151 #pod
152 #pod =head3 Other online schema change tools fit your needs
153 #pod
154 #pod Don't have foreign key constraints and C<gh-ost> is already working for you? Great!
155 #pod Keep using it.
156 #pod
157 #pod =head1 ATTRIBUTES
158 #pod
159 #pod =head2 DBIC Attributes
160 #pod
161 #pod =head3 rsrc
162 #pod
163 #pod A L<DBIx::Class::ResultSource>. This will be the source used for all operations, DDL or
164 #pod otherwise. Optional, but recommended for DBIC users.
165 #pod
166 #pod The DBIC storage handler's C<connect_info> will be tweaked to ensure sane defaults and
167 #pod proper post-connection details.
168 #pod
169 #pod =cut
170  
171 4 has rsrc => (
172 is => 'ro',
173 isa => InstanceOf['DBIx::Class::ResultSource'],
174 required => 0,
175 );
176  
177 #pod =head3 dbic_retry_opts
178 #pod
179 #pod A hashref of DBIC retry options. These options control how retry protection works within
180 #pod DBIC. Right now, this is just limited to C<max_attempts>, which controls the number of
181 #pod times to retry. The default C<max_attempts> is 20.
182 #pod
183 #pod =cut
184  
185 has dbic_retry_opts => (
186 is => 'ro',
187 isa => HashRef,
188 required => 0,
189 29 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
190 4 );
191  
192 #pod =head2 DBI Attributes
193 #pod
194 #pod =head3 dbi_connector
195 #pod
196 #pod A L<DBIx::Connector::Retry> object. Instead of L<DBI> statement handles, this is the
197 #pod recommended non-DBIC way for OnlineDDL (and BatchChunker) to interface with the DBI, as
198 #pod it handles retries on failures. The connection mode used is whatever default is set
199 #pod within the object.
200 #pod
201 #pod Required, except for DBIC users, who should be setting L</rsrc> above. It is also
202 #pod assumed that the correct database is already active.
203 #pod
204 #pod The object will be tweaked to ensure sane defaults, proper post-connection details, a
205 #pod custom C<retry_handler>, and set a default C<max_attempts> of 20, if not already set.
206 #pod
207 #pod =cut
208  
209 4 has dbi_connector => (
210 is => 'ro',
211 isa => InstanceOf['DBIx::Connector::Retry'],
212 required => 0,
213 );
214  
215 #pod =head3 table_name
216 #pod
217 #pod The table name to be copied and eventually replaced. Required unless L</rsrc> is
218 #pod specified.
219 #pod
220 #pod =cut
221  
222 has table_name => (
223 is => 'ro',
224 isa => Str,
225 required => 1,
226 lazy => 1,
227 29 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
228 29 ▶ my $rsrc = shift->rsrc // return;
Condition50%1/2
(shift())->rsrc // (return)
l!l
290
MC/DC0%0/1
(shift())->rsrc // (return)
(shift())->rsrc
Truth table50%1/2
A
A= (shift())->rsrc
Aresult
11
00
229 29 $rsrc->from;
230 },
231 4 );
232  
233 #pod =head3 new_table_name
234 #pod
235 #pod The new table name to be created, copied to, and eventually used as the final table.
236 #pod Optional.
237 #pod
238 #pod If not defined, a name will be created automatically. This might be the better route,
239 #pod since the default builder will search for an unused name in the DB right before OnlineDDL
240 #pod needs it.
241 #pod
242 #pod =cut
243  
244 4 has new_table_name => (
245 is => 'ro',
246 isa => Str,
247 required => 0,
248 lazy => 1,
249 builder => 1,
250 );
251  
252 29 ▶sub _build_new_table_name {
Subroutine _build_new_table_name: called CC=2 SCAR=6.9
253 29 my $self = shift;
254 29 my $dbh = $self->dbh;
255 29 my $vars = $self->_vars;
256  
257 29 my $catalog = $vars->{catalog};
258 29 my $schema = $vars->{schema};
259 29 my $orig_table_name = $self->table_name;
260  
261 29 ▶ my $escape = $dbh->get_info( $GetInfoType{SQL_SEARCH_PATTERN_ESCAPE} ) // '\\';
Condition50%1/2
$dbh->get_info($DBIx::OnlineDDL::GetInfoType{"SQL_SEARCH_PATTERN_ESCAPE"}) // "\\"
l!l
290
MC/DC0%0/1
$dbh->get_info($DBIx::OnlineDDL::GetInfoType{"SQL_SEARCH_PATTERN_ESCAPE"}) // "\\"
$dbh->get_info($DBIx::OnlineDDL::GetInfoType{"SQL_SEARCH_PATTERN_ESCAPE"})
Truth table50%1/2
A
A= $dbh->get_info($DBIx::OnlineDDL::GetInfoType{"SQL_SEARCH_PATTERN_ESCAPE"})
Aresult
11
00
262  
263 return $self->_find_new_identifier(
264 29 ▶ "_${orig_table_name}_new" => set_subname('_new_table_name_finder', sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
265 29 $dbh = shift;
266 29 my $like_expr = shift;
267 29 $like_expr =~ s/([_%])/$escape$1/g;
268  
269 29 $dbh->table_info($catalog, $schema, $like_expr)->fetchrow_array;
270 29 }),
271 'SQL_MAXIMUM_TABLE_NAME_LENGTH',
272 );
273 }
274  
275 #pod =head2 Progress Bar Attributes
276 #pod
277 #pod =head3 progress_bar
278 #pod
279 #pod The progress bar used for most of the process. A different one is used for the actual
280 #pod table copy with L<DBIx::BatchChunker>, since that step takes longer.
281 #pod
282 #pod Optional. If the progress bar isn't specified, a default one will be created. If the
283 #pod terminal isn't interactive, the default L<Term::ProgressBar> will be set to C<silent> to
284 #pod naturally skip the output.
285 #pod
286 #pod =cut
287  
288 4 has progress_bar => (
289 is => 'rw',
290 isa => InstanceOf['Term::ProgressBar'],
291 );
292  
293 29 ▶sub _progress_bar_setup {
Subroutine _progress_bar_setup: called CC=3 SCAR=12.0
294 29 my $self = shift;
295 29 my $vars = $self->_vars;
296  
297 29 my $steps = 7 + scalar keys %{ $self->coderef_hooks };
298  
299 29 ▶ my $progress = $self->progress_bar || Term::ProgressBar->new({
Condition33%2/6
$self->progress_bar || 'Term::ProgressBar'->new({'name', $self->progress_name, 'count', $steps, 'ETA', 'linear', 'silent', !(-t *STDERR && -t *STDIN)})
l!l&&r!l&&!r
0290
-t *STDERR && -t *STDIN
!ll&&!rl&&r
2900
MC/DC0%0/2
$self->progress_bar || 'Term::ProgressBar'->new({'name', $self->progress_name, 'count', $steps, 'ETA', 'linear', 'silent', !(-t *STDERR && -t *STDIN)})
$self->progress_bar 'Term::ProgressBar'->new({'name', $self->progress_name, 'count', $steps, 'ETA', 'linear', 'silent', !(-t *STDERR && -t *STDIN)})
MC/DC0%0/2
-t *STDERR && -t *STDIN
-t *STDERR -t *STDIN
Truth table33%1/3
A || B
A= $self->progress_barB= 'Term::ProgressBar'->new({'name', $self->progress_name, 'count', $steps, 'ETA', 'linear', 'silent', !(-t *STDERR && -t *STDIN)})
ABresult
1X1
011
000
Truth table33%1/3
A && B
A= -t *STDERRB= -t *STDIN
ABresult
0X0
100
111
300 name => $self->progress_name,
301 count => $steps,
302 ETA => 'linear',
303 silent => !(-t *STDERR && -t *STDIN), # STDERR is what {fh} is set to use
304 });
305  
306 29 $vars->{progress_bar} = $progress;
307 }
308  
309 #pod =head3 progress_name
310 #pod
311 #pod A string used to assist in creating a progress bar. Ignored if L</progress_bar> is
312 #pod already specified.
313 #pod
314 #pod This is the preferred way of customizing the progress bar without having to create one
315 #pod from scratch.
316 #pod
317 #pod =cut
318  
319 has progress_name => (
320 is => 'rw',
321 isa => Str,
322 required => 0,
323 lazy => 1,
324 29 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
325 29 my $table_name = shift->table_name;
326 29 ▶ 'Altering'.($table_name ? " $table_name" : '');
Branch: 1 branch
BranchTrueFalseTotal
$table_name ? : 29 0 29
327 },
328 4 );
329  
330 #pod =head2 Other Attributes
331 #pod
332 #pod =head3 coderef_hooks
333 #pod
334 #pod A hashref of coderefs. Each of these are used in different steps in the process. All
335 #pod of these are optional, but it is B<highly recommended> that C<before_triggers> is
336 #pod specified. Otherwise, you're not actually running any DDL and the table copy is
337 #pod essentially a no-op.
338 #pod
339 #pod All of these triggers pass the C<DBIx::OnlineDDL> object as the only argument. The
340 #pod L</new_table_name> can be acquired from that and used in SQL statements. The L</dbh_runner>
341 #pod and L</dbh_runner_do> methods should be used to protect against disconnections or locks.
342 #pod
343 #pod There is room to add more hooks here, but only if there's a good reason to do so.
344 #pod (Running the wrong kind of SQL at the wrong time could be dangerous.) Create a GitHub
345 #pod issue if you can think of one.
346 #pod
347 #pod =head4 before_triggers
348 #pod
349 #pod This is called before the table triggers are applied. Your DDL should take place here,
350 #pod for a few reasons:
351 #pod
352 #pod 1. The table is empty, so DDL should take no time at all now.
353 #pod
354 #pod 2. After this hook, the table is reanalyzed to make sure it has an accurate picture
355 #pod of the new columns. This is critical for the creation of the triggers.
356 #pod
357 #pod =head4 before_swap
358 #pod
359 #pod This is called after the new table has been analyzed, but before the big table swap. This
360 #pod hook might be used if a large DML operation needs to be done while the new table is still
361 #pod available. If you use this hook, it's highly recommended that you use something like
362 #pod L<DBIx::BatchChunker> to make sure the changes are made in a safe and batched manner.
363 #pod
364 #pod =cut
365  
366 has coderef_hooks => (
367 is => 'ro',
368 isa => Dict[
369 before_triggers => Optional[CodeRef],
370 before_swap => Optional[CodeRef],
371 ],
372 required => 0,
373 0 ▶ default => sub { +{} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
374 4 );
375  
376 #pod =head3 copy_opts
377 #pod
378 #pod A hashref of different options to pass to L<DBIx::BatchChunker>, which is used in the
379 #pod L</copy_rows> step. Some of these are defined automatically. It's recommended that you
380 #pod specify at least these options:
381 #pod
382 #pod chunk_size => 5000, # or whatever is a reasonable size for that table
383 #pod id_name => 'pk_id', # especially if there isn't an obvious integer PK
384 #pod
385 #pod Specifying L<DBIx::BatchChunker/coderef> is not recommended, since Active DBI Processing
386 #pod mode will be used.
387 #pod
388 #pod These options will be included into the hashref, unless specifically overridden by key
389 #pod name:
390 #pod
391 #pod id_name => $first_pk_column, # will warn if the PK is multi-column
392 #pod target_time => 1,
393 #pod sleep => 0.5,
394 #pod
395 #pod # If using DBIC
396 #pod dbic_storage => $rsrc->storage,
397 #pod rsc => $id_rsc,
398 #pod dbic_retry_opts => {
399 #pod max_attempts => 20,
400 #pod # best not to change this, unless you know what you're doing
401 #pod retry_handler => $onlineddl_retry_handler,
402 #pod },
403 #pod
404 #pod # If using DBI
405 #pod dbi_connector => $oddl->dbi_connector,
406 #pod min_stmt => $min_sql,
407 #pod max_stmt => $max_sql,
408 #pod
409 #pod # For both
410 #pod count_stmt => $count_sql,
411 #pod stmt => $insert_select_sql,
412 #pod progress_name => $copying_msg,
413 #pod
414 #pod =cut
415  
416 has copy_opts => (
417 is => 'ro',
418 isa => HashRef,
419 required => 0,
420 lazy => 1,
421 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
422 4 );
423  
424 # This is filled in during copy_rows, since the _column_list call needs to happen after
425 # the DDL has run.
426 28 ▶sub _fill_copy_opts {
Subroutine _fill_copy_opts: called CC=16 SCAR=29.7
427 28 my $self = shift;
428 28 my $rsrc = $self->rsrc;
429 28 my $dbh = $self->dbh;
430 28 my $vars = $self->_vars;
431  
432 28 my $copy_opts = $self->copy_opts;
433 28 my $helper = $self->_helper;
434  
435 28 my $catalog = $vars->{catalog};
436 28 my $schema = $vars->{schema};
437 28 my $orig_table_name = $self->table_name;
438 28 my $new_table_name = $self->new_table_name;
439  
440 28 my $orig_table_name_quote = $dbh->quote_identifier($orig_table_name);
441 28 my $new_table_name_quote = $dbh->quote_identifier($new_table_name);
442  
443 # Sane defaults for timing
444 28 ▶ $copy_opts->{target_time} //= 1;
Condition100%2/2
$copy_opts->{"target_time"} //= 1
l!l
1216
MC/DC100%1/1
$copy_opts->{"target_time"} //= 1
$copy_opts->{"target_time"}
Truth table100%2/2
A
A= $copy_opts->{"target_time"}
Aresult
11
00
445 # Copies create lots of rapid I/O, binlog generation, etc. on the primary.
446 # Some sleep time gives other servers a chance to catch up:
447 28 ▶ $copy_opts->{sleep} //= 0.5;
Condition100%2/2
$copy_opts->{"sleep"} //= "0.5"
l!l
1216
MC/DC100%1/1
$copy_opts->{"sleep"} //= "0.5"
$copy_opts->{"sleep"}
Truth table100%2/2
A
A= $copy_opts->{"sleep"}
Aresult
11
00
448  
449 # Figure out what the id_name is going to be
450 14 ▶ my $id_name = $copy_opts->{id_name} //= $self->dbh_runner(run => set_subname '_pk_finder', sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
451 14 $dbh = $_;
452 14 my @ids = $dbh->primary_key($catalog, $schema, $orig_table_name);
453  
454 14 ▶ die "No primary key found for $orig_table_name" unless @ids;
Branch: 1 branch
BranchTrueFalseTotal
unless @ids 0 14 14
455 14 ▶ warn "Using the first column of a multi-column primary key for $orig_table_name" if @ids > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @ids > 1 0 14 14
456  
457 14 $ids[0];
458 28 ▶ });
Condition66%2/3
$copy_opts->{"id_name"} //= $self->dbh_runner("run", set_subname("_pk_finder", sub { $dbh = $_; my(@ids) = $dbh->primary_key($catalog, $schema, $orig_table_name); die "No primary key found for $orig_table_name" unless @ids; warn "Using the first column of a multi-column primary key for $orig_table_name" if @ids > 1; $ids[0]; } ))
l!l&&r!l&&!r
14140
MC/DC0%0/2
$copy_opts->{"id_name"} //= $self->dbh_runner("run", set_subname("_pk_finder", sub { $dbh = $_; my(@ids) = $dbh->primary_key($catalog, $schema, $orig_table_name); die "No primary key found for $orig_table_name" unless @ids; warn "Using the first column of a multi-column primary key for $orig_table_name" if @ids > 1; $ids[0]; } ))
$copy_opts->{"id_name"} $self->dbh_runner("run", set_subname("_pk_finder", sub { $dbh = $_; my(@ids) = $dbh->primary_key($catalog, $schema, $orig_table_name); die "No primary key found for $orig_table_name" unless @ids; warn "Using the first column of a multi-column primary key for $orig_table_name" if @ids > 1; $ids[0]; } ))
Truth table66%2/3
A //= B
A= $copy_opts->{"id_name"}B= $self->dbh_runner("run", set_subname("_pk_finder", sub { $dbh = $_; my(@ids) = $dbh->primary_key($catalog, $schema, $orig_table_name); die "No primary key found for $orig_table_name" unless @ids; warn "Using the first column of a multi-column primary key for $orig_table_name" if @ids > 1; $ids[0]; } ))
ABresult
1X1
011
000
459  
460 28 my $id_name_quote = $dbh->quote_identifier($id_name);
461  
462 28 ▶ if ($rsrc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rsrc) { } 28 0 28
463 28 ▶ $copy_opts->{dbic_storage} //= $rsrc->storage;
Condition66%2/3
$copy_opts->{"dbic_storage"} //= $rsrc->storage
l!l&&r!l&&!r
12160
MC/DC0%0/2
$copy_opts->{"dbic_storage"} //= $rsrc->storage
$copy_opts->{"dbic_storage"} $rsrc->storage
Truth table66%2/3
A //= B
A= $copy_opts->{"dbic_storage"}B= $rsrc->storage
ABresult
1X1
011
000
464 28 ▶ $copy_opts->{rsc} //= $rsrc->resultset->get_column($id_name);
Condition66%2/3
$copy_opts->{"rsc"} //= $rsrc->resultset->get_column($id_name)
l!l&&r!l&&!r
12160
MC/DC0%0/2
$copy_opts->{"rsc"} //= $rsrc->resultset->get_column($id_name)
$copy_opts->{"rsc"} $rsrc->resultset->get_column($id_name)
Truth table66%2/3
A //= B
A= $copy_opts->{"rsc"}B= $rsrc->resultset->get_column($id_name)
ABresult
1X1
011
000
465  
466 28 ▶ $copy_opts->{dbic_retry_opts} //= {};
Condition100%2/2
$copy_opts->{"dbic_retry_opts"} //= {}
l!l
1216
MC/DC100%1/1
$copy_opts->{"dbic_retry_opts"} //= {}
$copy_opts->{"dbic_retry_opts"}
Truth table100%2/2
A
A= $copy_opts->{"dbic_retry_opts"}
Aresult
11
00
467 28 ▶ $copy_opts->{dbic_retry_opts}{max_attempts} //= $DEFAULT_MAX_ATTEMPTS;
Condition66%2/3
$copy_opts->{"dbic_retry_opts"}{"max_attempts"} //= $DEFAULT_MAX_ATTEMPTS
l!l&&r!l&&!r
12160
MC/DC0%0/2
$copy_opts->{"dbic_retry_opts"}{"max_attempts"} //= $DEFAULT_MAX_ATTEMPTS
$copy_opts->{"dbic_retry_opts"}{"max_attempts"} $DEFAULT_MAX_ATTEMPTS
Truth table66%2/3
A //= B
A= $copy_opts->{"dbic_retry_opts"}{"max_attempts"}B= $DEFAULT_MAX_ATTEMPTS
ABresult
1X1
011
000
468 28 ▶ $copy_opts->{dbic_retry_opts}{retry_handler} = sub { $self->_retry_handler(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
469 }
470 else {
471 0 ▶ $copy_opts->{dbi_connector} //= $self->dbi_connector;
Condition0%0/3
$copy_opts->{"dbi_connector"} //= $self->dbi_connector
l!l&&r!l&&!r
000
MC/DC0%0/2
$copy_opts->{"dbi_connector"} //= $self->dbi_connector
$copy_opts->{"dbi_connector"} $self->dbi_connector
Truth table0%0/3
A //= B
A= $copy_opts->{"dbi_connector"}B= $self->dbi_connector
ABresult
1X1
011
000
472 0 ▶ $copy_opts->{min_stmt} //= "SELECT MIN($id_name_quote) FROM $orig_table_name_quote";
Condition0%0/2
$copy_opts->{"min_stmt"} //= "SELECT MIN($id_name_quote) FROM $orig_table_name_quote"
l!l
00
MC/DC0%0/1
$copy_opts->{"min_stmt"} //= "SELECT MIN($id_name_quote) FROM $orig_table_name_quote"
$copy_opts->{"min_stmt"}
Truth table0%0/2
A
A= $copy_opts->{"min_stmt"}
Aresult
11
00
473 0 ▶ $copy_opts->{max_stmt} //= "SELECT MAX($id_name_quote) FROM $orig_table_name_quote";
Condition0%0/2
$copy_opts->{"max_stmt"} //= "SELECT MAX($id_name_quote) FROM $orig_table_name_quote"
l!l
00
MC/DC0%0/1
$copy_opts->{"max_stmt"} //= "SELECT MAX($id_name_quote) FROM $orig_table_name_quote"
$copy_opts->{"max_stmt"}
Truth table0%0/2
A
A= $copy_opts->{"max_stmt"}
Aresult
11
00
474 }
475  
476 28 my @column_list = $self->_column_list;
477 28 my $column_list_str = join(', ', map { $dbh->quote_identifier($_) } @column_list );
478  
479 # The INSERT..SELECT is a bit different depending on the RDBMS used, mostly because
480 # of the IGNORE part
481 28 my $insert_select_stmt = $helper->insert_select_stmt($column_list_str);
482  
483 28 ▶ $copy_opts->{count_stmt} //= "SELECT COUNT(*) FROM $orig_table_name_quote WHERE $id_name_quote BETWEEN ? AND ?";
Condition100%2/2
$copy_opts->{"count_stmt"} //= "SELECT COUNT(*) FROM $orig_table_name_quote WHERE $id_name_quote BETWEEN ? AND ?"
l!l
1216
MC/DC100%1/1
$copy_opts->{"count_stmt"} //= "SELECT COUNT(*) FROM $orig_table_name_quote WHERE $id_name_quote BETWEEN ? AND ?"
$copy_opts->{"count_stmt"}
Truth table100%2/2
A
A= $copy_opts->{"count_stmt"}
Aresult
11
00
484 28 ▶ $copy_opts->{stmt} //= $insert_select_stmt;
Condition66%2/3
$copy_opts->{"stmt"} //= $insert_select_stmt
l!l&&r!l&&!r
12160
MC/DC0%0/2
$copy_opts->{"stmt"} //= $insert_select_stmt
$copy_opts->{"stmt"} $insert_select_stmt
Truth table66%2/3
A //= B
A= $copy_opts->{"stmt"}B= $insert_select_stmt
ABresult
1X1
011
000
485  
486 28 ▶ $copy_opts->{progress_name} //= "Copying $orig_table_name" unless $copy_opts->{progress_bar};
Branch: 1 branch
BranchTrueFalseTotal
unless $copy_opts->{"progress_bar"} 28 0 28
Condition100%2/2
$copy_opts->{"progress_name"} //= "Copying $orig_table_name"
l!l
1216
MC/DC100%1/1
$copy_opts->{"progress_name"} //= "Copying $orig_table_name"
$copy_opts->{"progress_name"}
Truth table100%2/2
A
A= $copy_opts->{"progress_name"}
Aresult
11
00
487  
488 28 return $copy_opts;
489 }
490  
491 #pod =head3 db_timeouts
492 #pod
493 #pod A hashref of timeouts used for various DB operations, and usually set at the beginning of
494 #pod each connection. Some of these settings may be RDBMS-specific.
495 #pod
496 #pod =head4 lock_file
497 #pod
498 #pod Amount of time (in seconds) to wait when attempting to acquire filesystem locks (on
499 #pod filesystems which support locking). Float or fractional values are allowed. This
500 #pod currently only applies to SQLite.
501 #pod
502 #pod Default value is 1 second. The downside is that the SQLite default is actually 0, so
503 #pod other (non-OnlineDDL) connections should have a setting that is more than that to prevent
504 #pod lock contention.
505 #pod
506 #pod =head4 lock_db
507 #pod
508 #pod Amount of time (in whole seconds) to wait when attempting to acquire table and/or database
509 #pod level locks before falling back to retry.
510 #pod
511 #pod Default value is 60 seconds.
512 #pod
513 #pod =head4 lock_row
514 #pod
515 #pod Amount of time (in whole seconds) to wait when attempting to acquire row-level locks,
516 #pod which apply to much lower-level operations than L</lock_db>. At this scope, the lesser
517 #pod of either of these two settings will take precedence.
518 #pod
519 #pod Default value is 2 seconds. Lower values are preferred for row lock wait timeouts, so
520 #pod that OnlineDDL is more likely to be the victim of lock contention. OnlineDDL can simply
521 #pod retry the connection at that point.
522 #pod
523 #pod =head4 session
524 #pod
525 #pod Amount of time (in whole seconds) for inactive session timeouts on the database side.
526 #pod
527 #pod Default value is 28,800 seconds (8 hours), which is MySQL's default.
528 #pod
529 #pod =cut
530  
531 4 has db_timeouts => (
532 is => 'ro',
533 isa => Dict[
534 lock_file => Optional[PositiveNum],
535 lock_db => Optional[PositiveInt],
536 lock_row => Optional[PositiveInt],
537 session => Optional[PositiveInt],
538 ],
539 required => 0,
540 );
541  
542 #pod =head3 reversible
543 #pod
544 #pod A L<Eval::Reversible> object, used for rollbacks. A default will be created, if not
545 #pod specified.
546 #pod
547 #pod =cut
548  
549 has reversible => (
550 is => 'rw',
551 isa => InstanceOf['Eval::Reversible'],
552 required => 1,
553 lazy => 1,
554 29 ▶ default => sub { Eval::Reversible->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
555 4 );
556  
557 ### Private attributes
558  
559 has _vars => (
560 is => 'rw',
561 isa => HashRef,
562 required => 0,
563 init_arg => undef,
564 lazy => 1,
565 29 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
566 4 );
567  
568 4 has _helper => (
569 is => 'ro',
570 isa => InstanceOf['DBIx::OnlineDDL::Helper::Base'],
571 required => 0,
572 init_arg => undef,
573 lazy => 1,
574 builder => '_build_helper',
575 );
576  
577 29 ▶sub _build_helper {
Subroutine _build_helper: called CC=3 SCAR=11.6
578 29 my $self = shift;
579  
580 29 my $dbh = $self->dbh;
581  
582 # Get and store the DBMS_NAME. This is not the lowercase driver name (ie: mysql),
583 # unless the {Driver}{Name} alternative wins out.
584 29 ▶ my $dbms_name = $self->_vars->{dbms_name} = $dbh->get_info( $GetInfoType{SQL_DBMS_NAME} ) // $dbh->{Driver}->{Name};
Condition33%1/3
$dbh->get_info($DBIx::OnlineDDL::GetInfoType{"SQL_DBMS_NAME"}) // $dbh->{"Driver"}{"Name"}
l!l&&r!l&&!r
2900
MC/DC0%0/2
$dbh->get_info($DBIx::OnlineDDL::GetInfoType{"SQL_DBMS_NAME"}) // $dbh->{"Driver"}{"Name"}
$dbh->get_info($DBIx::OnlineDDL::GetInfoType{"SQL_DBMS_NAME"}) $dbh->{"Driver"}{"Name"}
Truth table33%1/3
A // B
A= $dbh->get_info($DBIx::OnlineDDL::GetInfoType{"SQL_DBMS_NAME"})B= $dbh->{"Driver"}{"Name"}
ABresult
1X1
011
000
585  
586 29 my $helper_class = "DBIx::OnlineDDL::Helper::$dbms_name";
587  
588 # Die if we can't load the RDBMS-specific class, since there's a lot of gaps in Base
589 29 ▶ die "OnlineDDL is not designed for $dbms_name systems yet!" unless Class::Load::load_optional_class($helper_class);
Branch: 1 branch
BranchTrueFalseTotal
unless Class::Load::load_optional_class($helper_class) 0 29 29
590  
591 29 return $helper_class->new( online_ddl => $self );
592 }
593  
594 ### BUILD methods
595  
596 #pod =for Pod::Coverage BUILDARGS BUILD
597 #pod
598 #pod =cut
599  
600 29 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=13 SCAR=32.3
601 29 my $next = shift;
602 29 my $class = shift;
603  
604 0 ▶ my %args = @_ == 1 ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 29 29
605  
606 # Quick sanity checks
607 die 'A DBIC ResultSource (rsrc) or DBIx::Connector::Retry object (dbi_connector) is required' unless (
608 $args{rsrc} || $args{dbi_connector}
609 29 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"rsrc"} or $args{"dbi_connector"} 0 29 29
Condition33%1/3
$args{"rsrc"} or $args{"dbi_connector"}
l!l&&r!l&&!r
2900
MC/DC0%0/2
$args{"rsrc"} or $args{"dbi_connector"}
$args{"rsrc"} $args{"dbi_connector"}
Truth table33%1/3
A or B
A= $args{"rsrc"}B= $args{"dbi_connector"}
ABresult
1X1
011
000
610  
611 29 ▶ $args{db_timeouts} //= {};
Condition50%1/2
$args{"db_timeouts"} //= {}
l!l
029
MC/DC0%0/1
$args{"db_timeouts"} //= {}
$args{"db_timeouts"}
Truth table50%1/2
A
A= $args{"db_timeouts"}
Aresult
11
00
612 29 ▶ $args{coderef_hooks} //= {};
Condition50%1/2
$args{"coderef_hooks"} //= {}
l!l
290
MC/DC0%0/1
$args{"coderef_hooks"} //= {}
$args{"coderef_hooks"}
Truth table50%1/2
A
A= $args{"coderef_hooks"}
Aresult
11
00
613 29 ▶ if (ref $args{db_timeouts} ne 'HASH' || ref $args{coderef_hooks} ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"db_timeouts"} ne "HASH" or ref $args{"coderef_hooks"} ne "HASH") 0 29 29
Condition33%1/3
ref $args{"db_timeouts"} ne "HASH" or ref $args{"coderef_hooks"} ne "HASH"
l!l&&r!l&&!r
0029
MC/DC0%0/2
ref $args{"db_timeouts"} ne "HASH" or ref $args{"coderef_hooks"} ne "HASH"
ref $args{"db_timeouts"} ne "HASH" ref $args{"coderef_hooks"} ne "HASH"
Truth table33%1/3
A or B
A= ref $args{"db_timeouts"} ne "HASH"B= ref $args{"coderef_hooks"} ne "HASH"
ABresult
1X1
011
000
614 # Let Moo complain about the isa check
615 0 $class->$next( %args );
616 0 die "Should have failed an isa check...";
617 }
618  
619 29 ▶ unless ($args{coderef_hooks}{before_triggers}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"coderef_hooks"}{"before_triggers"}) 0 29 29
620 0 warn
621 "No before_triggers hook appears to be defined. There may be a few reasons for this:\n\n".
622  
623 "1. Are you running DDL? If not, this may be the wrong tool for you.\n".
624 "2. Did you add DDL into the wrong coderef hook? This is widely regarded as a Very Bad Ideaâ„¢.\n".
625 "3. Are you intending to use this as an OPTIMIZE TABLE or ALTER TABLE FORCE operation? If so,\n".
626 " add in an empty coderef for before_triggers to silence this warning.\n"
627 ;
628 }
629  
630 # Defaults for db_timeouts (see POD above). We set these here, because each
631 # individual timeout should be checked to see if it's defined.
632 29 ▶ $args{db_timeouts}{lock_file} //= 1;
Condition50%1/2
$args{"db_timeouts"}{"lock_file"} //= 1
l!l
029
MC/DC0%0/1
$args{"db_timeouts"}{"lock_file"} //= 1
$args{"db_timeouts"}{"lock_file"}
Truth table50%1/2
A
A= $args{"db_timeouts"}{"lock_file"}
Aresult
11
00
633 29 ▶ $args{db_timeouts}{lock_db} //= 60;
Condition50%1/2
$args{"db_timeouts"}{"lock_db"} //= 60
l!l
029
MC/DC0%0/1
$args{"db_timeouts"}{"lock_db"} //= 60
$args{"db_timeouts"}{"lock_db"}
Truth table50%1/2
A
A= $args{"db_timeouts"}{"lock_db"}
Aresult
11
00
634 29 ▶ $args{db_timeouts}{lock_row} //= 2;
Condition50%1/2
$args{"db_timeouts"}{"lock_row"} //= 2
l!l
029
MC/DC0%0/1
$args{"db_timeouts"}{"lock_row"} //= 2
$args{"db_timeouts"}{"lock_row"}
Truth table50%1/2
A
A= $args{"db_timeouts"}{"lock_row"}
Aresult
11
00
635 29 ▶ $args{db_timeouts}{session} //= 28_800;
Condition50%1/2
$args{"db_timeouts"}{"session"} //= 28800
l!l
029
MC/DC0%0/1
$args{"db_timeouts"}{"session"} //= 28800
$args{"db_timeouts"}{"session"}
Truth table50%1/2
A
A= $args{"db_timeouts"}{"session"}
Aresult
11
00
636  
637 29 $class->$next( %args );
638 4 };
639  
640 29 ▶sub BUILD {
Subroutine BUILD: called CC=28 SCAR=60.6
Pod undocumented
641 29 my $self = shift;
642 29 my $rsrc = $self->rsrc;
643  
644 29 my $dbh = $self->dbh;
645 29 my $helper = $self->_helper;
646  
647 # Get the current catalog/schema
648 29 my ($catalog, $schema) = $helper->current_catalog_schema;
649  
650 29 $self->_vars->{catalog} = $catalog;
651 29 $self->_vars->{schema} = $schema;
652  
653 # Add in the post-connection details
654 29 my @stmts = $helper->post_connection_stmts;
655  
656 29 ▶ if ($rsrc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rsrc) { } 29 0 29
657 ### DBIC Storage
658  
659 29 my @post_connection_details = map { [ do_sql => $_ ] } @stmts;
660  
661 # XXX: Tapping into a private attribute here, but it's a lot better than parsing
662 # $storage->connect_info. We are also not attaching these details to
663 # connect_info, so public introspection won't pick up our changes. Undecided
664 # whether this is good or bad...
665  
666 29 my $storage = $rsrc->storage;
667 29 my $on_connect_call = $storage->_dbic_connect_attributes->{on_connect_call};
668  
669 # Parse on_connect_call to make sure we can add to it
670 29 ▶ my $ref = defined $on_connect_call && ref $on_connect_call;
Condition33%1/3
defined $on_connect_call && ref $on_connect_call
!ll&&!rl&&r
2900
MC/DC0%0/2
defined $on_connect_call && ref $on_connect_call
defined $on_connect_call ref $on_connect_call
Truth table33%1/3
A && B
A= defined $on_connect_callB= ref $on_connect_call
ABresult
0X0
100
111
671 29 ▶ unless ($on_connect_call) {
Branch: 4 branches
BranchTrueFalseTotal
if ($on_connect_call) { } 0 29 29
if (do { not $ref }) { } 0 0 0
elsif ($ref eq 'ARRAY') { } 0 0 0
elsif ($ref eq 'CODE') { } 0 0 0
672 29 $on_connect_call = \@post_connection_details;
673 }
674 elsif (!$ref) {
675 0 $on_connect_call = [ [ do_sql => $on_connect_call ], @post_connection_details ];
676 }
677 elsif ($ref eq 'ARRAY') {
678 # Double-check that we're not repeating ourselves by inspecting the array for
679 # our own statements.
680 @$on_connect_call = grep {
681 0 my $e = $_;
682 !( # exclude any of ours
683 $e && ref $e && ref $e eq 'ARRAY' && @$e == 2 &&
684 $e->[0] && !ref $e->[0] && $e->[0] eq 'do_sql' &&
685 0 ▶ $e->[1] && !ref $e->[1] && (any { $e->[1] eq $_ } @stmts)
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/27
$e && ref $e && ref $e eq 'ARRAY' && @$e == 2 && $e->[0] && !ref($e->[0]) && $e->[0] eq 'do_sql' && $e->[1] && !ref($e->[1]) && &any(sub { $e->[1] eq $_; } , @stmts)
!ll&&!rl&&r
000
$e and ref $e and ref $e eq "ARRAY" and @$e == 2 and $e->[0] and not ref $e->[0] and $e->[0] eq "do_sql" and $e->[1] and not ref $e->[1]
!ll&&!rl&&r
000
$e and ref $e and ref $e eq "ARRAY" and @$e == 2 and $e->[0] and not ref $e->[0] and $e->[0] eq "do_sql" and $e->[1]
!ll&&!rl&&r
000
$e and ref $e and ref $e eq "ARRAY" and @$e == 2 and $e->[0] and not ref $e->[0] and $e->[0] eq "do_sql"
!ll&&!rl&&r
000
$e and ref $e and ref $e eq "ARRAY" and @$e == 2 and $e->[0] and not ref $e->[0]
!ll&&!rl&&r
000
$e and ref $e and ref $e eq "ARRAY" and @$e == 2 and $e->[0]
!ll&&!rl&&r
000
$e and ref $e and ref $e eq "ARRAY" and @$e == 2
!ll&&!rl&&r
000
$e and ref $e and ref $e eq "ARRAY"
!ll&&!rl&&r
000
$e and ref $e
!ll&&!rl&&r
000
MC/DC0%0/10
$e && ref $e && ref $e eq 'ARRAY' && @$e == 2 && $e->[0] && !ref($e->[0]) && $e->[0] eq 'do_sql' && $e->[1] && !ref($e->[1]) && &any(sub { $e->[1] eq $_; } , @stmts)
$e ref $e ref $e eq "ARRAY" @$e == 2 $e->[0] not ref $e->[0] $e->[0] eq "do_sql" $e->[1] not ref $e->[1] &any(sub { $e->[1] eq $_; } , @stmts)
Truth table0%0/11
A and B and C and D and E and not(F) and G and H and not(I) && J
A= $eB= ref $eC= ref $e eq "ARRAY"D= @$e == 2E= $e->[0]F= not ref $e->[0]G= $e->[0] eq "do_sql"H= $e->[1]I= not ref $e->[1]J= &any(sub { $e->[1] eq $_; } , @stmts)
ABCDEFGHIJresult
0XXXXXXXXX0
10XXXXXXXX0
110XXXXXXX0
1110XXXXXX0
11110XXXXX0
111110XXXX0
1111110XXX0
11111110XX0
111111110X0
11111111100
11111111111
686 );
687 } @$on_connect_call;
688  
689 0 my $first_occ = $on_connect_call->[0];
690 0 ▶ if ($first_occ && ref $first_occ && ref $first_occ eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_occ and ref $first_occ and ref $first_occ eq 'ARRAY') { } 0 0 0
Condition0%0/6
$first_occ and ref $first_occ and ref $first_occ eq "ARRAY"
!ll&&!rl&&r
000
$first_occ and ref $first_occ
!ll&&!rl&&r
000
MC/DC0%0/3
$first_occ and ref $first_occ and ref $first_occ eq "ARRAY"
$first_occ ref $first_occ ref $first_occ eq "ARRAY"
Truth table0%0/4
A and B and C
A= $first_occB= ref $first_occC= ref $first_occ eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
691 0 $on_connect_call = [ @$on_connect_call, @post_connection_details ];
692 }
693 else {
694 0 $on_connect_call = [ $on_connect_call, @post_connection_details ];
695 }
696 }
697 elsif ($ref eq 'CODE') {
698 0 $on_connect_call = [ $on_connect_call, @post_connection_details ];
699 }
700 else {
701 0 die "Illegal reftype $ref for on_connect_call connection attribute!";
702 }
703  
704 # Set the new options on the relevant attributes that Storage::DBI->connect_info touches.
705 29 $storage->_dbic_connect_attributes->{on_connect_call} = $on_connect_call;
706 29 $storage->on_connect_call($on_connect_call);
707 }
708 else {
709 ### DBIx::Connector::Retry (via DBI Callbacks)
710  
711 0 my $conn = $self->dbi_connector;
712 0 my $dbi_attrs = $conn->connect_info->[3];
713  
714 # Playing with refs, so no need to re-set connect_info
715 0 ▶ $conn->connect_info->[3] = $dbi_attrs = {} unless $dbi_attrs;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbi_attrs 0 0 0
716  
717 # Make sure the basic settings are sane
718 0 $dbi_attrs->{AutoCommit} = 1;
719 0 $dbi_attrs->{RaiseError} = 1;
720  
721 # Add the DBI callback
722 0 ▶ my $callbacks = $dbi_attrs->{Callbacks} //= {};
Condition0%0/2
$dbi_attrs->{"Callbacks"} //= {}
l!l
00
MC/DC0%0/1
$dbi_attrs->{"Callbacks"} //= {}
$dbi_attrs->{"Callbacks"}
Truth table0%0/2
A
A= $dbi_attrs->{"Callbacks"}
Aresult
11
00
723 0 my $package_re = quotemeta(__PACKAGE__.'::_dbi_connected_callback');
724  
725 0 ▶ my $ref = defined $callbacks->{connected} && ref $callbacks->{connected};
Condition0%0/3
defined $callbacks->{'connected'} && ref $callbacks->{'connected'}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $callbacks->{'connected'} && ref $callbacks->{'connected'}
defined $callbacks->{'connected'} ref $callbacks->{'connected'}
Truth table0%0/3
A && B
A= defined $callbacks->{'connected'}B= ref $callbacks->{'connected'}
ABresult
0X0
100
111
726 0 ▶ unless ($callbacks->{connected}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($callbacks->{'connected'}) { } 0 0 0
if (do { not $ref or $ref ne 'CODE' }) { } 0 0 0
elsif (subname($callbacks->{'connected'}) =~ /^$package_re/) { } 0 0 0
727 0 ▶ $callbacks->{connected} = set_subname '_dbi_connected_callback' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
728 0 shift->do($_) for @stmts;
729 0 return;
730 0 };
731 }
732 ▶ elsif (!$ref || $ref ne 'CODE') {
Condition0%0/3
not $ref or $ref ne "CODE"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $ref or $ref ne "CODE"
not $ref $ref ne "CODE"
Truth table0%0/3
not(A) or B
A= not $refB= $ref ne "CODE"
ABresult
1X1
011
000
733 0 die "Illegal reftype $ref for connected DBI Callback!";
734 }
735 elsif (subname($callbacks->{connected}) =~ /^$package_re/) { # allow for *_wrapped below
736 # This is one of our callbacks; leave it alone!
737 }
738 else {
739 # This is somebody else's callback; wrap around it
740 0 my $old_coderef = $callbacks->{connected};
741 0 ▶ $callbacks->{connected} = set_subname '_dbi_connected_callback_wrapped' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
742 0 my $h = shift;
743 0 $old_coderef->($h);
744 0 $h->do($_) for @stmts;
745 0 return;
746 0 };
747 }
748  
749 # Add a proper retry_handler
750 0 ▶ $conn->retry_handler(sub { $self->_retry_handler(@_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
751  
752 # And max_attempts. XXX: Maybe they actually wanted 10 and not just the default?
753 0 ▶ $conn->max_attempts($DEFAULT_MAX_ATTEMPTS) if $conn->max_attempts == 10;
Branch: 1 branch
BranchTrueFalseTotal
if $conn->max_attempts == 10 0 0 0
754 }
755  
756 # Go ahead and run the post-connection statements for this session
757 29 $dbh->{AutoCommit} = 1;
758 29 $dbh->{RaiseError} = 1;
759 29 $dbh->do($_) for @stmts;
760 }
761  
762 #pod =head1 CONSTRUCTORS
763 #pod
764 #pod See L</ATTRIBUTES> for information on what can be passed into these constructors.
765 #pod
766 #pod =head2 new
767 #pod
768 #pod my $online_ddl = DBIx::OnlineDDL->new(...);
769 #pod
770 #pod A standard object constructor. If you use this constructor, you will need to manually
771 #pod call L</execute> to execute the DB changes.
772 #pod
773 #pod You'll probably just want to use L</construct_and_execute>.
774 #pod
775 #pod =head2 construct_and_execute
776 #pod
777 #pod my $online_ddl = DBIx::OnlineDDL->construct_and_execute(...);
778 #pod
779 #pod Constructs a DBIx::OnlineDDL object and automatically calls each method step, including
780 #pod hooks. Anything passed to this method will be passed through to the constructor.
781 #pod
782 #pod Returns the constructed object, post-execution. This is typically only useful if you want
783 #pod to inspect the attributes after the process has finished. Otherwise, it's safe to just
784 #pod ignore the return and throw away the object immediately.
785 #pod
786 #pod =cut
787  
788 0 ▶sub construct_and_execute {
Subroutine construct_and_execute: not called CC=1 SCAR=6.9
Pod documented
789 0 my $class = shift;
790 0 my $online_ddl = $class->new(@_);
791  
792 0 $online_ddl->execute;
793  
794 0 return $online_ddl;
795 }
796  
797 #pod =head1 METHODS
798 #pod
799 #pod =head2 Step Runners
800 #pod
801 #pod =head3 execute
802 #pod
803 #pod Runs all of the steps as documented in L</STEP METHODS>. This also includes undo
804 #pod protection, in case of exceptions.
805 #pod
806 #pod =cut
807  
808 29 ▶sub execute {
Subroutine execute: called CC=1 SCAR=0.0
Pod documented
809 29 my $self = shift;
810 29 my $reversible = $self->reversible;
811  
812 29 $self->_progress_bar_setup;
813  
814 29 ▶ $reversible->run_reversibly(set_subname '_execute_part_one', sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
815 29 $self->create_new_table;
816 29 $self->create_triggers;
817 28 $self->copy_rows;
818 28 $self->swap_tables;
819 29 });
820 28 ▶ $reversible->run_reversibly(set_subname '_execute_part_two', sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
821 28 $self->readd_triggers;
822 28 $self->drop_old_table;
823 28 $self->cleanup_foreign_keys;
824 28 });
825 }
826  
827 #pod =head3 fire_hook
828 #pod
829 #pod $online_ddl->fire_hook('before_triggers');
830 #pod
831 #pod Fires one of the coderef hooks, if it exists. This also updates the progress bar.
832 #pod
833 #pod See L</coderef_hooks> for more details.
834 #pod
835 #pod =cut
836  
837 57 ▶sub fire_hook {
Subroutine fire_hook: called CC=3 SCAR=11.0
Pod documented
838 57 my ($self, $hook_name) = @_;
839  
840 57 my $hooks = $self->coderef_hooks;
841 57 my $vars = $self->_vars;
842  
843 57 my $progress = $vars->{progress_bar};
844  
845 57 ▶ return unless $hooks && $hooks->{$hook_name};
Branch: 1 branch
BranchTrueFalseTotal
unless $hooks and $hooks->{$hook_name} 27 30 57
Condition66%2/3
$hooks and $hooks->{$hook_name}
!ll&&!rl&&r
02730
MC/DC50%1/2
$hooks and $hooks->{$hook_name}
$hooks $hooks->{$hook_name}
Truth table66%2/3
A and B
A= $hooksB= $hooks->{$hook_name}
ABresult
0X0
100
111
846  
847 30 $progress->message("Firing hook for $hook_name");
848  
849 # Fire the hook
850 30 $hooks->{$hook_name}->($self);
851  
852 30 $progress->update;
853 }
854  
855 #pod =head2 DBI Helpers
856 #pod
857 #pod =head3 dbh
858 #pod
859 #pod $online_ddl->dbh;
860 #pod
861 #pod Acquires a database handle, either from L</rsrc> or L</dbi_connector>. Not recommended
862 #pod for active work, as it doesn't offer retry protection. Instead, use L</dbh_runner> or
863 #pod L</dbh_runner_do>.
864 #pod
865 #pod =cut
866  
867 700 ▶sub dbh {
Subroutine dbh: called CC=1 SCAR=0.0
Pod documented
868 700 my $self = shift;
869  
870 # Even acquiring a $dbh could die (eg: 'USE $db' or other pre-connect commands), so
871 # also try to wrap this in our retry handler.
872 700 ▶ my $dbh = $self->dbh_runner( run => sub { $_[0] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
873 700 return $dbh;
874 }
875  
876 #pod =head3 dbh_runner
877 #pod
878 #pod my @items = $online_ddl->dbh_runner(run => sub {
879 #pod my $dbh = $_; # or $_[0]
880 #pod $dbh->selectall_array(...);
881 #pod });
882 #pod
883 #pod Runs the C<$coderef>, locally setting C<$_> to and passing in the database handle. This
884 #pod is essentially a shortcut interface into either L<dbi_connector> or DBIC's L<BlockRunner|DBIx::Class::Storage::BlockRunner>.
885 #pod
886 #pod The first argument can either be C<run> or C<txn>, which controls whether to wrap the
887 #pod code in a DB transaction or not. The return is passed directly back, and return context
888 #pod is honored.
889 #pod
890 #pod =cut
891  
892 2 ▶sub _retry_handler {
Subroutine _retry_handler: called CC=6 SCAR=21.3
893 2 my ($self, $runner) = @_;
894 2 my $vars = $self->_vars;
895  
896 # NOTE: There's a lot of abusing the fact that BlockRunner and DBIx::Connector::Retry
897 # (a la $runner) share similar accessor interfaces.
898  
899 2 my $error = $runner->last_exception;
900 2 my $is_retryable = $self->_helper->is_error_retryable($error);
901  
902 2 ▶ if ($is_retryable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_retryable) 2 0 2
903 2 my ($failed, $max) = ($runner->failed_attempt_count, $runner->max_attempts);
904 2 my $progress = $vars->{progress_bar};
905  
906 # Warn about the last error
907 2 ▶ $progress->message("Encountered a recoverable error: $error") if $progress;
Branch: 1 branch
BranchTrueFalseTotal
if $progress 2 0 2
908  
909 # Pause for an incremental amount of seconds first, to discourage any future locks
910 2 sleep $failed;
911  
912 # If retries are escalating, try forcing a disconnect
913 2 ▶ if ($failed >= $max / 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failed >= $max / 2) 0 2 2
914 # Finally have some differences between the two classes...
915 0 ▶ if ($runner->isa('DBIx::Class::Storage::BlockRunner')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($runner->isa('DBIx::Class::Storage::BlockRunner')) { } 0 0 0
916 0 eval { $runner->storage->disconnect };
917 }
918 else {
919 0 eval { $runner->disconnect };
920 }
921 }
922  
923 2 ▶ $progress->message( sprintf(
Branch: 1 branch
BranchTrueFalseTotal
if $progress 2 0 2
924 "Attempt %u of %u", $failed, $max
925 ) ) if $progress;
926 }
927  
928 2 return $is_retryable;
929 }
930  
931 1277 ▶sub dbh_runner {
Subroutine dbh_runner: called CC=14 SCAR=33.7
Pod documented
932 1277 my ($self, $method, $coderef) = @_;
933 1277 my $wantarray = wantarray;
934  
935 1277 ▶ die "Only 'txn' or 'run' are acceptable run methods" unless $method =~ /^(?:txn|run)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $method =~ /^(?:txn|run)$/ 0 1277 1277
936  
937 1277 my @res;
938 1277 ▶ if (my $rsrc = $self->rsrc) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rsrc = $self->rsrc) { } 1277 0 1277
939 # No need to load BlockRunner, since DBIC loads it in before us if we're using
940 # this method.
941 my $block_runner = DBIx::Class::Storage::BlockRunner->new(
942 # defaults
943 max_attempts => $DEFAULT_MAX_ATTEMPTS,
944  
945 # never overrides the important ones below
946 1277 ▶ %{ $self->dbic_retry_opts },
Branch: 1 branch
BranchTrueFalseTotal
$method eq 'txn' ? : 28 1249 1277
947  
948 0 ▶ retry_handler => sub { $self->_retry_handler(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
949 1277 storage => $rsrc->storage,
950 wrap_txn => ($method eq 'txn' ? 1 : 0),
951 );
952  
953 # This wrapping nonsense is necessary because Try::Tiny within BlockRunner has its own
954 # localization of $_. Fortunately, we can pass arguments to avoid closures.
955 1277 ▶ my $wrapper = set_subname '_dbh_run_blockrunner_wrapper' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
956 1277 my ($s, $c) = @_;
957 1277 my $dbh = $s->rsrc->storage->dbh;
958  
959 1277 local $_ = $dbh;
960 1277 $c->($dbh); # also pass it in, because that's what DBIx::Connector does
961 1277 };
962  
963 # BlockRunner can still die post-failure, if $storage->ensure_connected (which calls ping
964 # and tries to reconnect) dies. If that's the case, use our retry handler to check the new
965 # error message, and throw it back into BlockRunner.
966 1277 my $br_method = 'run';
967 1277 ▶ while ($block_runner->failed_attempt_count < $block_runner->max_attempts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_runner->failed_attempt_count < $block_runner->max_attempts) 1277 0 1277
968 1277 eval {
969 1277 ▶ unless (defined $wantarray) { $block_runner->$br_method($wrapper, $self, $coderef) }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $wantarray) { } 945 332 1277
if (do { $wantarray }) { } 173 772 945
970 173 elsif ($wantarray) { @res = $block_runner->$br_method($wrapper, $self, $coderef) }
971 772 else { $res[0] = $block_runner->$br_method($wrapper, $self, $coderef) }
972 };
973  
974 # 'run' resets failed_attempt_count, so subsequent attempts must use
975 # '_run', which does not
976 1277 $br_method = '_run';
977  
978 1277 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) { } 0 1277 1277
979 # Time to really die
980 0 ▶ die $err if $err =~ /Reached max_attempts amount of / || $block_runner->failed_attempt_count >= $block_runner->max_attempts;
Branch: 1 branch
BranchTrueFalseTotal
if $err =~ /Reached max_attempts amount of / or $block_runner->failed_attempt_count >= $block_runner->max_attempts 0 0 0
Condition0%0/3
$err =~ /Reached max_attempts amount of / or $block_runner->failed_attempt_count >= $block_runner->max_attempts
l!l&&r!l&&!r
000
MC/DC0%0/2
$err =~ /Reached max_attempts amount of / or $block_runner->failed_attempt_count >= $block_runner->max_attempts
$err =~ /Reached max_attempts amount of / $block_runner->failed_attempt_count >= $block_runner->max_attempts
Truth table0%0/3
A or B
A= $err =~ /Reached max_attempts amount of /B= $block_runner->failed_attempt_count >= $block_runner->max_attempts
ABresult
1X1
011
000
981  
982 # See if the retry handler likes it
983 0 push @{ $block_runner->exception_stack }, $err;
984 0 $block_runner->_set_failed_attempt_count( $block_runner->failed_attempt_count + 1 );
985 0 ▶ die $err unless $self->_retry_handler($block_runner);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_retry_handler($block_runner) 0 0 0
986 }
987 else {
988 1277 last;
989 }
990 }
991 }
992 else {
993 0 my $conn = $self->dbi_connector;
994 0 ▶ unless (defined $wantarray) { $conn->$method($coderef) }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $wantarray) { } 0 0 0
if (do { $wantarray }) { } 0 0 0
995 0 elsif ($wantarray) { @res = $conn->$method($coderef) }
996 0 else { $res[0] = $conn->$method($coderef) }
997 }
998  
999 1277 ▶ return $wantarray ? @res : $res[0];
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 173 1104 1277
1000 }
1001  
1002 #pod =head3 dbh_runner_do
1003 #pod
1004 #pod $online_ddl->dbh_runner_do(
1005 #pod "ALTER TABLE $table_name ADD COLUMN foobar",
1006 #pod ["ALTER TABLE ? DROP COLUMN ?", undef, $table_name, 'baz'],
1007 #pod );
1008 #pod
1009 #pod Runs a list of commands, encapsulating each of them in a L</dbh_runner> coderef with calls
1010 #pod to L<DBI/do>. This is handy when you want to run a list of DDL commands, which you don't
1011 #pod care about the output of, but don't want to bundle them into a single non-idempotant
1012 #pod repeatable coderef. Or if you want to save typing on a single do-able SQL command.
1013 #pod
1014 #pod The items can either be a SQL string or an arrayref of options to pass to L<DBI/do>.
1015 #pod
1016 #pod The statement is assumed to be non-transactional. If you want to run a DB transaction,
1017 #pod you should use L</dbh_runner> instead.
1018 #pod
1019 #pod =cut
1020  
1021 186 ▶sub dbh_runner_do {
Subroutine dbh_runner_do: called CC=4 SCAR=15.0
Pod documented
1022 186 my ($self, @commands) = @_;
1023  
1024 186 foreach my $command (@commands) {
1025 190 my $ref = ref $command;
1026 190 ▶ die "$ref references not valid in dbh_runner_do" if $ref && $ref ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $ref and $ref ne "ARRAY" 0 190 190
Condition33%1/3
$ref and $ref ne "ARRAY"
!ll&&!rl&&r
19000
MC/DC0%0/2
$ref and $ref ne "ARRAY"
$ref $ref ne "ARRAY"
Truth table33%1/3
A and B
A= $refB= $ref ne "ARRAY"
ABresult
0X0
100
111
1027  
1028 190 ▶ $self->dbh_runner(run => set_subname '_dbh_runner_do', sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
1029 190 ▶ $_->do( $ref ? @$command : $command );
Branch: 1 branch
BranchTrueFalseTotal
$ref ? : 0 190 190
1030 190 });
1031 }
1032 }
1033  
1034 #pod =head1 STEP METHODS
1035 #pod
1036 #pod You can call these methods individually, but using L</construct_and_execute> instead is
1037 #pod highly recommended. If you do run these yourself, the exception will need to be caught
1038 #pod and the L</reversible> undo stack should be run to get the DB back to normal.
1039 #pod
1040 #pod =head2 create_new_table
1041 #pod
1042 #pod Creates the new table, making sure to preserve as much of the original table properties
1043 #pod as possible.
1044 #pod
1045 #pod =cut
1046  
1047 29 ▶sub create_new_table {
Subroutine create_new_table: called CC=3 SCAR=11.0
Pod documented
1048 29 my $self = shift;
1049 29 my $dbh = $self->dbh;
1050 29 my $vars = $self->_vars;
1051  
1052 29 my $progress = $vars->{progress_bar};
1053 29 my $reversible = $self->reversible;
1054 29 my $helper = $self->_helper;
1055  
1056 29 my $orig_table_name = $self->table_name;
1057 29 my $new_table_name = $self->new_table_name;
1058  
1059 29 my $orig_table_name_quote = $dbh->quote_identifier($orig_table_name);
1060 29 my $new_table_name_quote = $dbh->quote_identifier($new_table_name);
1061  
1062 # ANSI quotes could also appear in the statement
1063 29 my $orig_table_name_ansi_quote = '"'.$orig_table_name.'"';
1064  
1065 29 $progress->message("Creating new table $new_table_name");
1066  
1067 29 my $table_sql = $helper->create_table_sql($orig_table_name);
1068 29 ▶ die "Table $orig_table_name does not exist in the database!" unless $table_sql;
Branch: 1 branch
BranchTrueFalseTotal
unless $table_sql 0 29 29
1069  
1070 29 ▶ $table_sql = $helper->rename_fks_in_table_sql($orig_table_name, $table_sql) if $helper->dbms_uses_global_fk_namespace;
Branch: 1 branch
BranchTrueFalseTotal
if $helper->dbms_uses_global_fk_namespace 0 29 29
1071  
1072 # Change the old->new table name
1073 29 my $orig_table_name_quote_re = '('.join('|',
1074 quotemeta($orig_table_name_quote), quotemeta($orig_table_name_ansi_quote), quotemeta($orig_table_name)
1075 ).')';
1076 29 $table_sql =~ s/(?<=^CREATE TABLE )$orig_table_name_quote_re/$new_table_name_quote/;
1077  
1078 # NOTE: This SQL will still have the old table name in self-referenced FKs. This is
1079 # okay, since no supported RDBMS currently auto-renames the referenced table name
1080 # during table moves, and the old table is still the definitive point-of-record until
1081 # the table swap. Furthermore, pointing the FK to the new table may cause bad FK
1082 # constraint failures within the triggers, if the referenced ID hasn't been copied to
1083 # the new table yet.
1084 #
1085 # If we ever have a RDBMS that does some sort of auto-renaming of FKs, we'll need to
1086 # accommodate it. It's also worth noting that turning FKs on during the session can
1087 # actually affect this kind of behavior. For example, both MySQL & SQLite will rename
1088 # them during table swaps, but only if the FK checks are on.
1089  
1090 # Actually create the table
1091 29 $self->dbh_runner_do($table_sql);
1092  
1093 # Undo commands, including a failure warning update
1094 29 $reversible->failure_warning("\nDropping the new table and rolling back to start!\n\n");
1095 29 ▶ $reversible->add_undo(sub { $self->dbh_runner_do("DROP TABLE $new_table_name_quote") });
Subroutine __ANON__: called CC=1 SCAR=0.0
1096  
1097 29 $progress->update;
1098 }
1099  
1100 #pod =head2 create_triggers
1101 #pod
1102 #pod Creates triggers on the original table to make sure any new changes are captured into the
1103 #pod new table.
1104 #pod
1105 #pod =cut
1106  
1107 29 ▶sub create_triggers {
Subroutine create_triggers: called CC=13 SCAR=25.7
Pod documented
1108 29 my $self = shift;
1109 29 my $rsrc = $self->rsrc;
1110 29 my $dbh = $self->dbh;
1111 29 my $vars = $self->_vars;
1112  
1113 29 my $progress = $vars->{progress_bar};
1114 29 my $reversible = $self->reversible;
1115 29 my $helper = $self->_helper;
1116  
1117 29 my $catalog = $vars->{catalog};
1118 29 my $schema = $vars->{schema};
1119 29 my $orig_table_name = $self->table_name;
1120 29 my $new_table_name = $self->new_table_name;
1121  
1122 # Fire the before_triggers hook, which would typically include the DDL
1123 29 $self->fire_hook('before_triggers');
1124  
1125 29 $progress->message("Creating triggers");
1126  
1127 # This shouldn't be cached until now, since the actual DDL may change the column list
1128 29 my @column_list = $self->_column_list;
1129  
1130 ### Look for a unique ID set
1131  
1132 # We need to find a proper PK or unique constraint for UPDATE/DELETE triggers.
1133 # Unlike BatchChunker, we can't just rely on part of a PK.
1134 29 my @unique_ids;
1135 29 my $indexes = $self->_get_idx_hash($orig_table_name);
1136  
1137 29 my %potential_unique_ids;
1138 29 $potential_unique_ids{ $_->{name} } = $_ for grep { $_->{unique} } values %$indexes;
1139  
1140 29 my %column_set = map { $_ => 1 } @column_list;
1141 29 foreach my $index_name ('PRIMARY',
1142 # sort by the number of columns (asc), though PRIMARY still has top priority
1143 16 sort { scalar(@{ $potential_unique_ids{$a}{columns} }) <=> scalar(@{ $potential_unique_ids{$b}{columns} }) }
1144 65 grep { $_ ne 'PRIMARY' }
1145 keys %potential_unique_ids
1146 ) {
1147 65 my @unique_cols = @{ $potential_unique_ids{$index_name}{columns} };
1148 65 ▶ next unless @unique_cols;
Branch: 1 branch
BranchTrueFalseTotal
unless @unique_cols 0 65 65
1149  
1150 # Only use this set if all of the columns exist in both tables
1151 65 ▶ next unless all { $column_set{$_} } @unique_cols;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless &all(sub { $column_set{$_}; } , @unique_cols) 0 65 65
1152  
1153 65 @unique_ids = @unique_cols;
1154 }
1155  
1156 29 ▶ die "Cannot find an appropriate unique index for $orig_table_name!" unless @unique_ids;
Branch: 1 branch
BranchTrueFalseTotal
unless @unique_ids 0 29 29
1157  
1158 ### Check to make sure existing (conflicting) triggers aren't on the table
1159  
1160 29 ▶ die "Found conflicting triggers on $orig_table_name! Please remove them first, so that our INSERT/UPDATE/DELETE triggers can be applied."
Branch: 1 branch
BranchTrueFalseTotal
if $helper->has_conflicting_triggers_on_table($orig_table_name) 1 28 29
1161 if $helper->has_conflicting_triggers_on_table($orig_table_name);
1162  
1163 ### Get any info on existing triggers (to copy later)
1164  
1165 28 ▶ $helper->get_trigger_data($orig_table_name) if $helper->has_triggers_on_table_to_be_copied($orig_table_name);
Branch: 1 branch
BranchTrueFalseTotal
if $helper->has_triggers_on_table_to_be_copied($orig_table_name) 0 28 28
1166  
1167 ### Find a good set of trigger names
1168  
1169 28 foreach my $trigger_type (qw< INSERT UPDATE DELETE >) {
1170 84 my $trigger_name = $helper->find_new_trigger_identifier(
1171 "${orig_table_name}_onlineddl_".lc($trigger_type)
1172 );
1173 84 $vars->{trigger_names} {$trigger_type} = $trigger_name;
1174 84 $vars->{trigger_names_quoted}{$trigger_type} = $dbh->quote_identifier($trigger_name);
1175 }
1176  
1177 ### Now create the triggers, with (mostly) ANSI SQL
1178  
1179 28 my $orig_table_name_quote = $dbh->quote_identifier($orig_table_name);
1180 28 my $new_table_name_quote = $dbh->quote_identifier($new_table_name);
1181  
1182 28 my $column_list_str = join(', ', map { $dbh->quote_identifier($_) } @column_list );
1183 28 my $new_column_list_str = join(', ', map { "NEW.".$dbh->quote_identifier($_) } @column_list );
1184  
1185 28 my $nseo = $helper->null_safe_equals_op;
1186 28 my %trigger_dml_stmts;
1187  
1188 # Using REPLACE just in case the row already exists from the copy
1189 28 $trigger_dml_stmts{replace} = join("\n",
1190 "REPLACE INTO $new_table_name_quote",
1191 " ($column_list_str)",
1192 "VALUES",
1193 " ($new_column_list_str)",
1194 );
1195  
1196 my $update_unique_where_str = join(' AND ',
1197 (map {
1198 28 join(
1199 # Use NULL-safe equals, since unique indexes could be nullable
1200 " $nseo ",
1201 "OLD.".$dbh->quote_identifier($_),
1202 "NEW.".$dbh->quote_identifier($_),
1203 );
1204 } @unique_ids)
1205 );
1206  
1207 my $delete_unique_where_str = join(' AND ',
1208 (map {
1209 28 join(
1210 # Use NULL-safe equals, since unique indexes could be nullable
1211 " $nseo ",
1212 "$new_table_name_quote.".$dbh->quote_identifier($_),
1213 "OLD.".$dbh->quote_identifier($_),
1214 );
1215 } @unique_ids)
1216 );
1217  
1218 # For the UPDATE trigger, DELETE the row, but only if the unique IDs have been
1219 # changed. The "NOT ($update_unique_where_str)" part keeps from deleting rows where
1220 # the unique ID is untouched.
1221 28 $trigger_dml_stmts{delete_for_update} = join("\n",
1222 "DELETE FROM $new_table_name_quote WHERE",
1223 " NOT ($update_unique_where_str) AND",
1224 " $delete_unique_where_str"
1225 );
1226  
1227 28 $trigger_dml_stmts{delete_for_delete} = join("\n",
1228 "DELETE FROM $new_table_name_quote WHERE",
1229 " $delete_unique_where_str"
1230 );
1231  
1232 28 $helper->modify_trigger_dml_stmts( \%trigger_dml_stmts );
1233  
1234 28 foreach my $trigger_type (qw< INSERT UPDATE DELETE >) {
1235 my $trigger_header = join(' ',
1236 84 "CREATE TRIGGER ".$vars->{trigger_names_quoted}{$trigger_type},
1237 "AFTER $trigger_type ON $orig_table_name_quote FOR EACH ROW"
1238 );
1239  
1240 # Even though some of these are just a single SQL statement, not every RDBMS
1241 # (like SQLite) supports leaving out the BEGIN/END keywords.
1242 84 my $trigger_sql = join("\n",
1243 $trigger_header,
1244 "BEGIN",
1245 '',
1246 );
1247  
1248 84 ▶ if ($trigger_type eq 'INSERT') {
Branch: 3 branches
BranchTrueFalseTotal
if ($trigger_type eq 'INSERT') { } 28 56 84
elsif ($trigger_type eq 'UPDATE') { } 28 28 56
elsif ($trigger_type eq 'DELETE') { } 28 0 28
1249 # INSERT trigger: Just a REPLACE command
1250 28 $trigger_sql .= $trigger_dml_stmts{replace}.';';
1251 }
1252 elsif ($trigger_type eq 'UPDATE') {
1253 # UPDATE trigger: DELETE special unique ID changes, then another REPLACE command.
1254 $trigger_sql .= join("\n",
1255 $trigger_dml_stmts{delete_for_update}.';',
1256 28 $trigger_dml_stmts{replace}.';',
1257 );
1258 }
1259 elsif ($trigger_type eq 'DELETE') {
1260 # DELETE trigger: Just a DELETE command
1261 28 $trigger_sql .= $trigger_dml_stmts{delete_for_delete}.';';
1262 }
1263 84 $trigger_sql .= "\nEND";
1264  
1265 # DOIT!
1266 84 $self->dbh_runner_do($trigger_sql);
1267  
1268 0 ▶ $reversible->add_undo(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1269 0 $self->dbh_runner_do( "DROP TRIGGER IF EXISTS ".$self->_vars->{trigger_names_quoted}{$trigger_type} );
1270 84 });
1271 }
1272  
1273 28 $progress->update;
1274 }
1275  
1276 #pod =head2 copy_rows
1277 #pod
1278 #pod Fires up a L<DBIx::BatchChunker> process to copy all of the rows from the old table to
1279 #pod the new.
1280 #pod
1281 #pod =cut
1282  
1283 28 ▶sub copy_rows {
Subroutine copy_rows: called CC=1 SCAR=0.0
Pod documented
1284 28 my $self = shift;
1285 28 my $dbh = $self->dbh;
1286 28 my $vars = $self->_vars;
1287  
1288 28 my $progress = $vars->{progress_bar};
1289 28 my $copy_opts = $self->_fill_copy_opts;
1290  
1291 28 $progress->message("Copying all rows to the new table");
1292  
1293 28 DBIx::BatchChunker->construct_and_execute( %$copy_opts );
1294 28 $vars->{new_table_copied} = 1;
1295  
1296 # Analyze the table, since we have a ton of new rows now
1297 28 $progress->message("Analyzing table");
1298 28 $self->_helper->analyze_table( $self->new_table_name );
1299  
1300 28 $progress->update;
1301 }
1302  
1303 #pod =head2 swap_tables
1304 #pod
1305 #pod With the new table completely modified and set up, this swaps the old/new tables.
1306 #pod
1307 #pod =cut
1308  
1309 28 ▶sub swap_tables {
Subroutine swap_tables: called CC=11 SCAR=34.4
Pod documented
1310 28 my $self = shift;
1311 28 my $dbh = $self->dbh;
1312 28 my $vars = $self->_vars;
1313  
1314 28 my $progress = $vars->{progress_bar};
1315 28 my $reversible = $self->reversible;
1316 28 my $helper = $self->_helper;
1317  
1318 28 my $catalog = $vars->{catalog};
1319 28 my $schema = $vars->{schema};
1320 28 my $orig_table_name = $self->table_name;
1321 28 my $new_table_name = $self->new_table_name;
1322  
1323 28 ▶ my $escape = $dbh->get_info( $GetInfoType{SQL_SEARCH_PATTERN_ESCAPE} ) // '\\';
Condition50%1/2
$dbh->get_info($DBIx::OnlineDDL::GetInfoType{"SQL_SEARCH_PATTERN_ESCAPE"}) // "\\"
l!l
280
MC/DC0%0/1
$dbh->get_info($DBIx::OnlineDDL::GetInfoType{"SQL_SEARCH_PATTERN_ESCAPE"}) // "\\"
$dbh->get_info($DBIx::OnlineDDL::GetInfoType{"SQL_SEARCH_PATTERN_ESCAPE"})
Truth table50%1/2
A
A= $dbh->get_info($DBIx::OnlineDDL::GetInfoType{"SQL_SEARCH_PATTERN_ESCAPE"})
Aresult
11
00
1324  
1325 # Fire the before_swap hook
1326 28 $self->fire_hook('before_swap');
1327  
1328 28 ▶ if ($helper->dbms_uses_global_fk_namespace || $helper->child_fks_need_adjusting) {
Branch: 1 branch
BranchTrueFalseTotal
if ($helper->dbms_uses_global_fk_namespace or $helper->child_fks_need_adjusting) 0 28 28
Condition33%1/3
$helper->dbms_uses_global_fk_namespace or $helper->child_fks_need_adjusting
l!l&&r!l&&!r
0028
MC/DC0%0/2
$helper->dbms_uses_global_fk_namespace or $helper->child_fks_need_adjusting
$helper->dbms_uses_global_fk_namespace $helper->child_fks_need_adjusting
Truth table33%1/3
A or B
A= $helper->dbms_uses_global_fk_namespaceB= $helper->child_fks_need_adjusting
ABresult
1X1
011
000
1329 # The existing parent/child FK list needs to be captured prior to the swap. The FKs
1330 # have already been created, and possibly changed/deleted, from the new table, so we
1331 # use that as reference. They have *not* been re-created on the child tables, so
1332 # the original table is used as reference.
1333 0 ▶ my $fk_hash = $vars->{foreign_keys}{definitions} //= {};
Condition0%0/2
$vars->{"foreign_keys"}{"definitions"} //= {}
l!l
00
MC/DC0%0/1
$vars->{"foreign_keys"}{"definitions"} //= {}
$vars->{"foreign_keys"}{"definitions"}
Truth table0%0/2
A
A= $vars->{"foreign_keys"}{"definitions"}
Aresult
11
00
1334 0 ▶ $self->dbh_runner(run => set_subname '_fk_parent_info_query', sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1335 0 $fk_hash->{parent} = $self->_fk_info_to_hash( $helper->foreign_key_info(undef, undef, undef, $catalog, $schema, $new_table_name) );
1336 0 });
1337 0 ▶ $self->dbh_runner(run => set_subname '_fk_child_info_query', sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1338 0 $fk_hash->{child} = $self->_fk_info_to_hash( $helper->foreign_key_info($catalog, $schema, $orig_table_name, undef, undef, undef) );
1339 0 });
1340  
1341 # Furthermore, we should capture the indexes from parent/child tables in case the data
1342 # is needed for FK cleanup
1343 0 ▶ my $idx_hash = $vars->{indexes}{definitions} //= {};
Condition0%0/2
$vars->{"indexes"}{"definitions"} //= {}
l!l
00
MC/DC0%0/1
$vars->{"indexes"}{"definitions"} //= {}
$vars->{"indexes"}{"definitions"}
Truth table0%0/2
A
A= $vars->{"indexes"}{"definitions"}
Aresult
11
00
1344 0 ▶ if ($dbh->can('statistics_info') && %$fk_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh->can("statistics_info") and %$fk_hash) 0 0 0
Condition0%0/3
$dbh->can("statistics_info") and %$fk_hash
!ll&&!rl&&r
000
MC/DC0%0/2
$dbh->can("statistics_info") and %$fk_hash
$dbh->can("statistics_info") %$fk_hash
Truth table0%0/3
A and B
A= $dbh->can("statistics_info")B= %$fk_hash
ABresult
0X0
100
111
1345 0 foreach my $fk_table_name (
1346 uniq sort
1347 0 ▶ grep { defined && $_ ne $orig_table_name && $_ ne $new_table_name }
Condition0%0/6
defined $_ && $_ ne $orig_table_name && $_ ne $new_table_name
!ll&&!rl&&r
000
defined $_ and $_ ne $orig_table_name
!ll&&!rl&&r
000
MC/DC0%0/3
defined $_ && $_ ne $orig_table_name && $_ ne $new_table_name
defined $_ $_ ne $orig_table_name $_ ne $new_table_name
Truth table0%0/4
A and B && C
A= defined $_B= $_ ne $orig_table_nameC= $_ ne $new_table_name
ABCresult
0XX0
10X0
1100
1111
1348 0 map { ($_->{pk_table_name}, $_->{fk_table_name}) }
1349 0 (values %{$fk_hash->{parent}}, values %{$fk_hash->{child}})
1350 ) {
1351 0 $idx_hash->{$fk_table_name} = $self->_get_idx_hash($fk_table_name);
1352 }
1353 }
1354 }
1355  
1356 # Find an "_old" table name first
1357 my $old_table_name = $vars->{old_table_name} = $self->_find_new_identifier(
1358 28 ▶ "_${orig_table_name}_old" => set_subname('_old_table_name_finder', sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1359 28 my ($d, $like_expr) = @_;
1360 28 $like_expr =~ s/([_%])/$escape$1/g;
1361  
1362 28 $d->table_info($catalog, $schema, $like_expr)->fetchrow_array;
1363 28 }),
1364 'SQL_MAXIMUM_TABLE_NAME_LENGTH',
1365 );
1366 28 my $old_table_name_quote = $dbh->quote_identifier($old_table_name);
1367 28 $progress->message("Swapping tables ($new_table_name --> $orig_table_name --> $old_table_name)");
1368  
1369 # Let's swap tables!
1370 28 $helper->swap_tables($new_table_name, $orig_table_name, $old_table_name);
1371  
1372 # Kill the undo stack now, just in case something weird happens between now and the
1373 # end of the reversibly block. We've reached a "mostly successful" state, so rolling
1374 # back here would be undesirable.
1375 28 $reversible->clear_undo;
1376 28 $vars->{new_table_swapped} = 1;
1377  
1378 28 $progress->update;
1379 }
1380  
1381 #pod =head2 readd_triggers
1382 #pod
1383 #pod Re-adds the existing triggers onto the new table. Since the old table was swapped, and since the
1384 #pod C<CREATE TABLE> statement wouldn't have included them, any triggers it had will need to be re-added
1385 #pod to the new table.
1386 #pod
1387 #pod Because this operation isn't atomic, there's a small window (between the swap and this operation)
1388 #pod where triggers won't fire. You should prepare for potential clean up of rows impacted during this
1389 #pod window.
1390 #pod
1391 #pod This step only applies to RDBMS where multiple triggers are allowed to be applied per table/action
1392 #pod (so far, MySQL 5.7+), and only if there were existing triggers in the first place.
1393 #pod
1394 #pod =cut
1395  
1396 28 ▶sub readd_triggers {
Subroutine readd_triggers: called CC=2 SCAR=7.1
Pod documented
1397 28 my $self = shift;
1398 28 my $dbh = $self->dbh;
1399 28 my $vars = $self->_vars;
1400  
1401 28 my $progress = $vars->{progress_bar};
1402 28 my $reversible = $self->reversible;
1403 28 my $helper = $self->_helper;
1404  
1405 28 my $orig_table_name = $self->table_name;
1406  
1407 28 $reversible->failure_warning( join "\n",
1408 '',
1409 "The new table has been swapped, but since the process was interrupted, foreign keys and",
1410 "triggers will need to be cleaned up, and the old table dropped.",
1411 '',
1412 );
1413  
1414 28 ▶ if ($vars->{existing_triggers}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vars->{"existing_triggers"}) 0 28 28
1415 0 $progress->message("Re-adding triggers to $orig_table_name");
1416  
1417 0 $self->dbh_runner_do(
1418 $helper->add_triggers_back_to_table($orig_table_name)
1419 );
1420 }
1421  
1422 28 $progress->update;
1423 }
1424  
1425 #pod =head2 drop_old_table
1426 #pod
1427 #pod Drops the old table. This will also remove old foreign keys on child tables. (Those FKs
1428 #pod are re-applied to the new table in the next step.)
1429 #pod
1430 #pod =cut
1431  
1432 28 ▶sub drop_old_table {
Subroutine drop_old_table: called CC=2 SCAR=7.0
Pod documented
1433 28 my $self = shift;
1434 28 my $dbh = $self->dbh;
1435 28 my $vars = $self->_vars;
1436  
1437 28 my $progress = $vars->{progress_bar};
1438 28 my $reversible = $self->reversible;
1439 28 my $helper = $self->_helper;
1440  
1441 28 my $old_table_name = $vars->{old_table_name};
1442 28 my $old_table_name_quote = $dbh->quote_identifier($old_table_name);
1443  
1444 28 $reversible->failure_warning( join "\n",
1445 '',
1446 "The new table has been swapped, but since the process was interrupted, foreign keys will",
1447 "need to be cleaned up, and the old table dropped.",
1448 '',
1449 );
1450  
1451 # The RDBMS may complain about dangling non-functional FKs if the DROP happens first,
1452 # so let's remove those child FKs first, and reapply them later. We turn off FK
1453 # checks, so these constraint drops are quick and low risk.
1454 #
1455 # SQLite doesn't actually support DROP CONSTRAINT, but it doesn't do any messy business with
1456 # FK renames, either. So, SQLite can just skip this step.
1457 28 ▶ if ($helper->child_fks_need_adjusting) {
Branch: 1 branch
BranchTrueFalseTotal
if ($helper->child_fks_need_adjusting) 0 28 28
1458 0 $progress->message("Removing FKs from child tables");
1459  
1460 0 $self->dbh_runner_do(
1461 $helper->remove_fks_from_child_tables_stmts
1462 );
1463 }
1464  
1465 # Now, the actual DROP
1466 28 $progress->message("Dropping old table $old_table_name");
1467  
1468 28 $self->dbh_runner_do("DROP TABLE $old_table_name_quote");
1469  
1470 28 $progress->update;
1471 }
1472  
1473 #pod =head2 cleanup_foreign_keys
1474 #pod
1475 #pod Clean up foreign keys on both the new and child tables.
1476 #pod
1477 #pod =cut
1478  
1479 28 ▶sub cleanup_foreign_keys {
Subroutine cleanup_foreign_keys: called CC=3 SCAR=12.7
Pod documented
1480 28 my $self = shift;
1481 28 my $dbh = $self->dbh;
1482 28 my $vars = $self->_vars;
1483  
1484 28 my $progress = $vars->{progress_bar};
1485 28 my $reversible = $self->reversible;
1486 28 my $helper = $self->_helper;
1487  
1488 28 $reversible->failure_warning( join "\n",
1489 '',
1490 "The new table is live, but since the process was interrupted, foreign keys will need to be",
1491 "cleaned up.",
1492 '',
1493 );
1494  
1495 28 ▶ if ($helper->dbms_uses_global_fk_namespace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($helper->dbms_uses_global_fk_namespace) 0 28 28
1496 # The DB has global namespaces for foreign keys, so we are renaming them back to
1497 # their original names. The original table has already been dropped, so there's
1498 # no more risk of bumping into that namespace.
1499 0 $progress->message("Renaming parent FKs back to the original constraint names");
1500  
1501 0 $self->dbh_runner_do(
1502 $helper->rename_fks_back_to_original_stmts
1503 );
1504 }
1505  
1506 28 ▶ if ($helper->child_fks_need_adjusting) {
Branch: 1 branch
BranchTrueFalseTotal
if ($helper->child_fks_need_adjusting) 0 28 28
1507 # Since we captured the child FK names prior to the swap, they should have the
1508 # original FK names, even before MySQL's "helpful" changes on "${tbl_name}_ibfk_" FK
1509 # names.
1510 0 $progress->message("Adding FKs back on child tables");
1511  
1512 0 $self->dbh_runner_do(
1513 $helper->add_fks_back_to_child_tables_stmts
1514 );
1515  
1516 # The RDBMS may need some post-FK cleanup
1517 0 $progress->message("Post-FK cleanup");
1518  
1519 0 $self->dbh_runner_do(
1520 $helper->post_fk_add_cleanup_stmts
1521 );
1522 }
1523  
1524 28 $progress->update;
1525 }
1526  
1527 ### Private methods
1528  
1529 144 ▶sub _find_new_identifier {
Subroutine _find_new_identifier: called CC=11 SCAR=29.6
1530 144 my ($self, $desired_identifier, $finder_sub, $length_info_str) = @_;
1531 144 ▶ $length_info_str ||= 'SQL_MAXIMUM_IDENTIFIER_LENGTH';
Condition100%2/2
$length_info_str ||= "SQL_MAXIMUM_IDENTIFIER_LENGTH"
l!l
5787
MC/DC100%1/1
$length_info_str ||= "SQL_MAXIMUM_IDENTIFIER_LENGTH"
$length_info_str
Truth table100%2/2
A
A= $length_info_str
Aresult
11
00
1532  
1533 144 state $hash_digits = ['a' .. 'z', '0' .. '9'];
1534  
1535 144 my $hash = join '', map { $hash_digits->[rand @$hash_digits] } 1 .. 10;
1536 144 ▶ my $max_len = $self->dbh->get_info( $GetInfoType{$length_info_str} ) || 256;
Condition50%1/2
$self->dbh->get_info($DBIx::OnlineDDL::GetInfoType{$length_info_str}) || 256
l!l
1440
MC/DC0%0/1
$self->dbh->get_info($DBIx::OnlineDDL::GetInfoType{$length_info_str}) || 256
$self->dbh->get_info($DBIx::OnlineDDL::GetInfoType{$length_info_str})
Truth table50%1/2
A
A= $self->dbh->get_info($DBIx::OnlineDDL::GetInfoType{$length_info_str})
Aresult
11
00
1537  
1538 # Pre-shrink: abbreviate the longest [a-z]+ sections to their first letter until the identifier
1539 # fits within the max length
1540 144 ▶ while (length($desired_identifier) > $max_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $desired_identifier > $max_len) 0 144 144
1541 0 my ($best_pos, $best_len) = (-1, 1);
1542 0 ▶ while ($desired_identifier =~ /([a-z]{2,})/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desired_identifier =~ /([a-z]{2,})/g) 0 0 0
1543 0 ▶ if (length($1) > $best_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $1 > $best_len) 0 0 0
1544 0 $best_len = length($1);
1545 0 $best_pos = $-[1];
1546 }
1547 }
1548 0 ▶ last if $best_pos == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $best_pos == -1 0 0 0
1549 0 substr($desired_identifier, $best_pos + 1, $best_len - 1, '');
1550 }
1551  
1552 # Test out some potential names
1553 144 my @potential_names = map { $_, "_$_" } (
1554 $desired_identifier, "${desired_identifier}_${hash}", $hash,
1555 );
1556  
1557 144 my $new_name;
1558 144 foreach my $potential_name (@potential_names) {
1559 # avoid the ID name character limit
1560 144 ▶ $potential_name = substr($potential_name, -$max_len) if length($potential_name) > $max_len;
Branch: 1 branch
BranchTrueFalseTotal
if length $potential_name > $max_len 0 144 144
1561  
1562 144 ▶ my @results = $self->dbh_runner(run => set_subname '_find_new_identifier_dbh_runner', sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1563 144 $finder_sub->($_, $potential_name);
1564 144 });
1565  
1566 # Skip if we found it
1567 144 ▶ next if @results;
Branch: 1 branch
BranchTrueFalseTotal
if @results 0 144 144
1568  
1569 144 $new_name = $potential_name;
1570 144 last;
1571 }
1572  
1573 # This really shouldn't happen...
1574 144 ▶ die "Cannot find a proper identifier name for $desired_identifier! All of them are taken!" unless defined $new_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new_name 0 144 144
1575  
1576 144 return $new_name;
1577 }
1578  
1579 57 ▶sub _column_list {
Subroutine _column_list: called CC=1 SCAR=0.0
1580 57 my $self = shift;
1581 57 my $dbh = $self->dbh;
1582 57 my $vars = $self->_vars;
1583  
1584 57 my $catalog = $vars->{catalog};
1585 57 my $schema = $vars->{schema};
1586 57 my $orig_table_name = $self->table_name;
1587 57 my $new_table_name = $self->new_table_name;
1588  
1589 57 my (@old_column_list, @new_column_list);
1590 57 ▶ $self->dbh_runner(run => set_subname '_column_list_runner', sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1591 57 $dbh = $_;
1592 @old_column_list =
1593 198 map { $_->{COLUMN_NAME} }
1594 57 @{ $dbh->column_info( $catalog, $schema, $orig_table_name, '%' )->fetchall_arrayref({ COLUMN_NAME => 1 }) }
1595 ;
1596 @new_column_list =
1597 226 map { $_->{COLUMN_NAME} }
1598 57 @{ $dbh->column_info( $catalog, $schema, $new_table_name, '%' )->fetchall_arrayref({ COLUMN_NAME => 1 }) }
1599 ;
1600 57 });
1601  
1602 # We only care about columns that exist in both tables. If a column was added on the
1603 # new table, there's no data to copy. If a column was deleted from the new table, we
1604 # don't care about keeping it.
1605 57 my %new_column_set = map { $_ => 1 } @new_column_list;
1606 57 return grep { $new_column_set{$_} } @old_column_list;
1607 }
1608  
1609 29 ▶sub _get_idx_hash {
Subroutine _get_idx_hash: called CC=5 SCAR=16.1
1610 29 my ($self, $table_name) = @_;
1611  
1612 29 my $vars = $self->_vars;
1613 29 my $catalog = $vars->{catalog};
1614 29 my $schema = $vars->{schema};
1615  
1616 my %idxs = (
1617 PRIMARY => {
1618 name => 'PRIMARY',
1619 29 ▶ columns => [ $self->dbh_runner(run => set_subname '_pk_info_query', sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1620 29 $_->primary_key($catalog, $schema, $table_name)
1621 29 }) ],
1622 unique => 1,
1623 },
1624 );
1625 29 ▶ delete $idxs{PRIMARY} unless @{ $idxs{PRIMARY}{columns} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$idxs{"PRIMARY"}{"columns"};} 0 29 29
1626  
1627 29 ▶ return \%idxs unless $self->dbh->can('statistics_info');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dbh->can("statistics_info") 0 29 29
1628  
1629 # Sometimes, this still dies, even with the 'can' check (eg: older DBD::mysql drivers)
1630 29 my $index_stats = [];
1631 29 eval {
1632 29 ▶ $index_stats = $self->dbh_runner(run => set_subname '_idx_info_query', sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1633 29 $_->statistics_info($catalog, $schema, $table_name, 0, 1)->fetchall_arrayref({});
1634 29 });
1635 };
1636 29 ▶ $index_stats = [] if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 29 29
1637  
1638 29 foreach my $index_name (uniq map { $_->{INDEX_NAME} } @$index_stats) {
1639 62 ▶ my $index_stat = first { $_->{INDEX_NAME} eq $index_name } @$index_stats;
Subroutine __ANON__: called CC=1 SCAR=0.0
1640 my @cols =
1641 96 map { $_->{COLUMN_NAME} }
1642 38 sort { $a->{ORDINAL_POSITION} <=> $b->{ORDINAL_POSITION} }
1643 62 grep { $_->{INDEX_NAME} eq $index_name }
1644 @$index_stats
1645 ;
1646 $idxs{$index_name} = {
1647 name => $index_name,
1648 columns => \@cols,
1649 unique => !$index_stat->{NON_UNIQUE},
1650 62 };
1651 }
1652  
1653 29 return \%idxs;
1654 }
1655  
1656 0 ▶sub _fk_info_to_hash {
Subroutine _fk_info_to_hash: not called CC=24 SCAR=64.0
1657 0 my ($self, $fk_sth) = @_;
1658 0 my $vars = $self->_vars;
1659 0 my $dbh = $self->dbh;
1660  
1661 # NOTE: Need to account for alternate ODBC names
1662  
1663 0 my @fk_rows = @{ $fk_sth->fetchall_arrayref({}) };
1664 @fk_rows = sort {
1665 # Sort by FK name, then by the column sequence number
1666 0 $a->{FK_NAME} cmp $b->{FK_NAME} ||
1667 ($a->{KEY_SEQ} // $a->{ORDINAL_POSITION}) <=> ($a->{KEY_SEQ} // $a->{ORDINAL_POSITION})
1668 0 ▶ } @fk_rows;
Condition0%0/9
$DBIx::OnlineDDL::a->{'FK_NAME'} cmp $DBIx::OnlineDDL::b->{'FK_NAME'} || ($DBIx::OnlineDDL::a->{'KEY_SEQ'} // $DBIx::OnlineDDL::a->{'ORDINAL_POSITION'}) <=> ($DBIx::OnlineDDL::a->{'KEY_SEQ'} // $DBIx::OnlineDDL::a->{'ORDINAL_POSITION'})
l!l&&r!l&&!r
000
$DBIx::OnlineDDL::a->{"KEY_SEQ"} // $DBIx::OnlineDDL::a->{"ORDINAL_POSITION"}
l!l&&r!l&&!r
000
$DBIx::OnlineDDL::a->{"KEY_SEQ"} // $DBIx::OnlineDDL::a->{"ORDINAL_POSITION"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$DBIx::OnlineDDL::a->{'FK_NAME'} cmp $DBIx::OnlineDDL::b->{'FK_NAME'} || ($DBIx::OnlineDDL::a->{'KEY_SEQ'} // $DBIx::OnlineDDL::a->{'ORDINAL_POSITION'}) <=> ($DBIx::OnlineDDL::a->{'KEY_SEQ'} // $DBIx::OnlineDDL::a->{'ORDINAL_POSITION'})
$DBIx::OnlineDDL::a->{'FK_NAME'} cmp $DBIx::OnlineDDL::b->{'FK_NAME'} $DBIx::OnlineDDL::a->{"KEY_SEQ"} $DBIx::OnlineDDL::a->{"ORDINAL_POSITION"}
Truth table0%0/4
A || B // C
A= $DBIx::OnlineDDL::a->{'FK_NAME'} cmp $DBIx::OnlineDDL::b->{'FK_NAME'}B= $DBIx::OnlineDDL::a->{"KEY_SEQ"}C= $DBIx::OnlineDDL::a->{"ORDINAL_POSITION"}
ABCresult
1XX1
01X1
0011
0000
1669  
1670 0 my (%fks, %create_table_sql);
1671 0 foreach my $row (@fk_rows) {
1672 # Some of these rows aren't even FKs
1673 0 ▶ next unless $row->{PKTABLE_NAME} || $row->{UK_TABLE_CAT};
Branch: 1 branch
BranchTrueFalseTotal
unless $row->{"PKTABLE_NAME"} or $row->{"UK_TABLE_CAT"} 0 0 0
Condition0%0/3
$row->{"PKTABLE_NAME"} or $row->{"UK_TABLE_CAT"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$row->{"PKTABLE_NAME"} or $row->{"UK_TABLE_CAT"}
$row->{"PKTABLE_NAME"} $row->{"UK_TABLE_CAT"}
Truth table0%0/3
A or B
A= $row->{"PKTABLE_NAME"}B= $row->{"UK_TABLE_CAT"}
ABresult
1X1
011
000
1674 0 ▶ next unless $row->{FKTABLE_NAME} || $row->{FK_TABLE_NAME};
Branch: 1 branch
BranchTrueFalseTotal
unless $row->{"FKTABLE_NAME"} or $row->{"FK_TABLE_NAME"} 0 0 0
Condition0%0/3
$row->{"FKTABLE_NAME"} or $row->{"FK_TABLE_NAME"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$row->{"FKTABLE_NAME"} or $row->{"FK_TABLE_NAME"}
$row->{"FKTABLE_NAME"} $row->{"FK_TABLE_NAME"}
Truth table0%0/3
A or B
A= $row->{"FKTABLE_NAME"}B= $row->{"FK_TABLE_NAME"}
ABresult
1X1
011
000
1675  
1676 0 ▶ my $fk_name = $row->{FK_NAME} // $row->{FKCOLUMN_NAME};
Condition0%0/3
$row->{"FK_NAME"} // $row->{"FKCOLUMN_NAME"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$row->{"FK_NAME"} // $row->{"FKCOLUMN_NAME"}
$row->{"FK_NAME"} $row->{"FKCOLUMN_NAME"}
Truth table0%0/3
A // B
A= $row->{"FK_NAME"}B= $row->{"FKCOLUMN_NAME"}
ABresult
1X1
011
000
1677 0 ▶ my $fk_table_name = $row->{FKTABLE_NAME} // $row->{FK_TABLE_NAME};
Condition0%0/3
$row->{"FKTABLE_NAME"} // $row->{"FK_TABLE_NAME"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$row->{"FKTABLE_NAME"} // $row->{"FK_TABLE_NAME"}
$row->{"FKTABLE_NAME"} $row->{"FK_TABLE_NAME"}
Truth table0%0/3
A // B
A= $row->{"FKTABLE_NAME"}B= $row->{"FK_TABLE_NAME"}
ABresult
1X1
011
000
1678  
1679 my $key = join( '.',
1680 $row->{PKTABLE_NAME} // $row->{UK_TABLE_CAT},
1681 0 ▶ $fk_name,
Condition0%0/3
$row->{"PKTABLE_NAME"} // $row->{"UK_TABLE_CAT"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$row->{"PKTABLE_NAME"} // $row->{"UK_TABLE_CAT"}
$row->{"PKTABLE_NAME"} $row->{"UK_TABLE_CAT"}
Truth table0%0/3
A // B
A= $row->{"PKTABLE_NAME"}B= $row->{"UK_TABLE_CAT"}
ABresult
1X1
011
000
1682 );
1683  
1684 # Since there may be multiple columns per FK, those associated columns are
1685 # arrayrefs.
1686 0 ▶ unless ($fks{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fks{$key}) { } 0 0 0
1687  
1688 $fks{$key} = {
1689 fk_name => $fk_name,
1690  
1691 # The table where the original PK exists
1692 pk_table_name => $row->{PKTABLE_NAME} // $row->{UK_TABLE_CAT},
1693 pk_columns => [ $row->{PKCOLUMN_NAME} // $row->{UK_COLUMN_NAME} ],
1694  
1695 # The table where the FK constraint has been declared
1696 fk_table_name => $fk_table_name,
1697 0 ▶ fk_columns => [ $row->{FKCOLUMN_NAME} // $row->{FK_COLUMN_NAME} ],
Condition0%0/9
$row->{"PKTABLE_NAME"} // $row->{"UK_TABLE_CAT"}
l!l&&r!l&&!r
000
$row->{"PKCOLUMN_NAME"} // $row->{"UK_COLUMN_NAME"}
l!l&&r!l&&!r
000
$row->{"FKCOLUMN_NAME"} // $row->{"FK_COLUMN_NAME"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$row->{"PKTABLE_NAME"} // $row->{"UK_TABLE_CAT"}
$row->{"PKTABLE_NAME"} $row->{"UK_TABLE_CAT"}
MC/DC0%0/2
$row->{"PKCOLUMN_NAME"} // $row->{"UK_COLUMN_NAME"}
$row->{"PKCOLUMN_NAME"} $row->{"UK_COLUMN_NAME"}
MC/DC0%0/2
$row->{"FKCOLUMN_NAME"} // $row->{"FK_COLUMN_NAME"}
$row->{"FKCOLUMN_NAME"} $row->{"FK_COLUMN_NAME"}
Truth table0%0/3
A // B
A= $row->{"PKTABLE_NAME"}B= $row->{"UK_TABLE_CAT"}
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $row->{"PKCOLUMN_NAME"}B= $row->{"UK_COLUMN_NAME"}
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $row->{"FKCOLUMN_NAME"}B= $row->{"FK_COLUMN_NAME"}
ABresult
1X1
011
000
1698 };
1699  
1700 # Sadly, foreign_key_info doesn't always fill in all of the details for the FK, so the
1701 # CREATE TABLE SQL is actually the better record. Fortunately, this is all ANSI SQL.
1702 0 ▶ my $create_table_sql = $create_table_sql{$fk_table_name} //= $self->_helper->create_table_sql($fk_table_name);
Condition0%0/3
$create_table_sql{$fk_table_name} //= $self->_helper->create_table_sql($fk_table_name)
l!l&&r!l&&!r
000
MC/DC0%0/2
$create_table_sql{$fk_table_name} //= $self->_helper->create_table_sql($fk_table_name)
$create_table_sql{$fk_table_name} $self->_helper->create_table_sql($fk_table_name)
Truth table0%0/3
A //= B
A= $create_table_sql{$fk_table_name}B= $self->_helper->create_table_sql($fk_table_name)
ABresult
1X1
011
000
1703 0 my $fk_name_quote_re = '(?:'.join('|',
1704 quotemeta( $dbh->quote_identifier($fk_name) ), quotemeta('"'.$fk_name.'"'), quotemeta($fk_name)
1705 ).')';
1706  
1707 0 ▶ if ($create_table_sql =~ m<
Branch: 1 branch
BranchTrueFalseTotal
if ($create_table_sql =~ m[ CONSTRAINT \s $fk_name_quote_re \s ( # start capture of full SQL FOREIGN \s KEY \s \( [^\)]+ \) \s # "FOREIGN KEY" plus column list (which we already have above) REFERENCES \s [^\(]+ \s \( [^\)]+ \) # "REFERENCES" plus table+column list (again, already captured above) \s? ( [^\)\,]* ) # ON DELETE/UPDATE, DEFER, MATCH, etc. ) # end capture of full SQL ]six) 0 0 0
1708 CONSTRAINT \s $fk_name_quote_re \s ( # start capture of full SQL
1709 FOREIGN \s KEY \s \( [^\)]+ \) \s # "FOREIGN KEY" plus column list (which we already have above)
1710 REFERENCES \s [^\(]+ \s \( [^\)]+ \) # "REFERENCES" plus table+column list (again, already captured above)
1711 \s? ( [^\)\,]* ) # ON DELETE/UPDATE, DEFER, MATCH, etc.
1712 ) # end capture of full SQL
1713 >isx) {
1714 0 my ($fk_sql, $extra_sql) = ($1, $2);
1715 0 $fk_sql =~ s/^\s+|\s+$//g;
1716  
1717 0 $fks{$key}{fk_sql} = $fk_sql;
1718 0 ▶ $fks{$key}{delete_rule} = $1 if $extra_sql =~ /ON DELETE ((?:SET |NO )?\w+)/i;
Branch: 1 branch
BranchTrueFalseTotal
if $extra_sql =~ /ON DELETE ((?:SET |NO )?\w+)/i 0 0 0
1719 0 ▶ $fks{$key}{update_rule} = $1 if $extra_sql =~ /ON UPDATE ((?:SET |NO )?\w+)/i;
Branch: 1 branch
BranchTrueFalseTotal
if $extra_sql =~ /ON UPDATE ((?:SET |NO )?\w+)/i 0 0 0
1720 0 ▶ $fks{$key}{defer} = $1 if $extra_sql =~ /((?:NOT )?DEFERRABLE(?: INITIALLY \w+)?)/i;
Branch: 1 branch
BranchTrueFalseTotal
if $extra_sql =~ /((?:NOT )?DEFERRABLE(?: INITIALLY \w+)?)/i 0 0 0
1721 0 ▶ $fks{$key}{match} = $1 if $extra_sql =~ /(MATCH \w+)/i;
Branch: 1 branch
BranchTrueFalseTotal
if $extra_sql =~ /(MATCH \w+)/i 0 0 0
1722 }
1723 }
1724 else {
1725 0 ▶ push @{ $fks{$key}{pk_columns} }, $row->{PKCOLUMN_NAME} // $row->{UK_COLUMN_NAME};
Condition0%0/3
$row->{"PKCOLUMN_NAME"} // $row->{"UK_COLUMN_NAME"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$row->{"PKCOLUMN_NAME"} // $row->{"UK_COLUMN_NAME"}
$row->{"PKCOLUMN_NAME"} $row->{"UK_COLUMN_NAME"}
Truth table0%0/3
A // B
A= $row->{"PKCOLUMN_NAME"}B= $row->{"UK_COLUMN_NAME"}
ABresult
1X1
011
000
1726 0 ▶ push @{ $fks{$key}{fk_columns} }, $row->{FKCOLUMN_NAME} // $row->{FK_COLUMN_NAME};
Condition0%0/3
$row->{"FKCOLUMN_NAME"} // $row->{"FK_COLUMN_NAME"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$row->{"FKCOLUMN_NAME"} // $row->{"FK_COLUMN_NAME"}
$row->{"FKCOLUMN_NAME"} $row->{"FK_COLUMN_NAME"}
Truth table0%0/3
A // B
A= $row->{"FKCOLUMN_NAME"}B= $row->{"FK_COLUMN_NAME"}
ABresult
1X1
011
000
1727 }
1728 }
1729  
1730 0 return \%fks;
1731 }
1732  
1733 0 ▶sub _fk_to_sql {
Subroutine _fk_to_sql: not called CC=6 SCAR=37.4
1734 0 my ($self, $fk) = @_;
1735 0 my $dbh = $self->dbh;
1736  
1737 # Everything after the CONSTRAINT keyword (ANSI SQL)
1738  
1739 0 ▶ if ($fk->{fk_sql}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fk->{"fk_sql"}) 0 0 0
1740 # Already have most of the SQL
1741 return join(' ',
1742 $dbh->quote_identifier($fk->{fk_name}),
1743 $fk->{fk_sql},
1744 0 );
1745 }
1746  
1747 return join(' ',
1748 $dbh->quote_identifier($fk->{fk_name}),
1749 'FOREIGN KEY',
1750 0 '('.join(', ', map { $dbh->quote_identifier($_) } @{ $fk->{fk_columns} }).')',
1751 'REFERENCES',
1752 $dbh->quote_identifier($fk->{pk_table_name}),
1753 0 ▶ '('.join(', ', map { $dbh->quote_identifier($_) } @{ $fk->{pk_columns} }).')',
Branch: 4 branches
BranchTrueFalseTotal
$fk->{'match'} ? : 0 0 0
$fk->{'delete_rule'} ? : 0 0 0
$fk->{'update_rule'} ? : 0 0 0
$fk->{'defer'} ? : 0 0 0
1754 ( $fk->{match} ? $fk->{match} : () ),
1755 ( $fk->{delete_rule} ? 'ON DELETE '.$fk->{delete_rule} : () ),
1756 ( $fk->{update_rule} ? 'ON UPDATE '.$fk->{update_rule} : () ),
1757 0 ( $fk->{defer} ? $fk->{defer} : () ),
1758 );
1759 }
1760  
1761 #pod =head1 SEE ALSO
1762 #pod
1763 #pod =over
1764 #pod
1765 #pod =item *
1766 #pod
1767 #pod L<Percona's pt-online-schema-change|https://www.percona.com/doc/percona-toolkit/LATEST/pt-online-schema-change.html>
1768 #pod
1769 #pod =item *
1770 #pod
1771 #pod L<GitHub's gh-ost|https://github.com/github/gh-ost>
1772 #pod
1773 #pod =item *
1774 #pod
1775 #pod L<Facebook's OSC|https://www.facebook.com/notes/mysql-at-facebook/online-schema-change-for-mysql/430801045932/>
1776 #pod
1777 #pod =item *
1778 #pod
1779 #pod L<MySQL's Online DDL|https://dev.mysql.com/doc/refman/5.6/en/innodb-online-ddl.html>
1780 #pod
1781 #pod =back
1782 #pod
1783 #pod =head1 WHY YET ANOTHER OSC?
1784 #pod
1785 #pod The biggest reason is that none of the above fully support foreign key constraints.
1786 #pod Percona's C<pt-osc> comes close, but also includes this paragraph:
1787 #pod
1788 #pod Due to a limitation in MySQL, foreign keys will not have the same names after the ALTER
1789 #pod that they did prior to it. The tool has to rename the foreign key when it redefines it,
1790 #pod which adds a leading underscore to the name. In some cases, MySQL also automatically
1791 #pod renames indexes required for the foreign key.
1792 #pod
1793 #pod So, tables swapped with C<pt-osc> are not exactly what they used to be before the swap.
1794 #pod It also had a number of other quirks that just didn't work out for us, related to FKs and
1795 #pod the amount of switches required to make it (semi-)work.
1796 #pod
1797 #pod Additionally, by making DBIx::OnlineDDL its own Perl module, it's a lot easier to run
1798 #pod Perl-based schema changes along side L<DBIx::BatchChunker> without having to switch
1799 #pod between Perl and CLI. If other people want to subclass this module for their own
1800 #pod environment-specific quirks, they have the power to do so, too.
1801 #pod
1802 #pod =cut
1803  
1804 4 1;
1805  
1806 __END__
↑ Summary OnlineDDL/Helper/MySQL.pm »