OnlineDDL.pm

filter: Statementstmt 78.0% 447 / 573 Branchbran 35.7% 55 / 154 Conditioncond 24.1% 42 / 174 MC/DCmcdc 7.0% 7 / 100 Subroutinesub 81.9% 59 / 72 Podpod 92.8% 13 / 14 totaltotal 57.3% 623 / 1087 CC 155 SCAR 48.4
CC
155
Cov
60%
_fk_info_to_hash
600.0
BUILD
429.4
_fk_to_sql
42.0
CRAP 126.2
↑ Summary OnlineDDL/Helper/MySQL.pm »
1 package DBIx::OnlineDDL;
2  
3 3 our $AUTHORITY = 'cpan:GSG';
4 # ABSTRACT: Run DDL on online databases safely
5 3 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 our $VERSION = 'v1.1.1'; # VERSION
7  
8 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use open qw(:utf8 :std);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use MooX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use Types::Standard qw( Str Bool HashRef CodeRef InstanceOf Dict Optional );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Types::Common::Numeric qw( PositiveNum PositiveInt );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use DBI::Const::GetInfoType;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use DBIx::BatchChunker 0.92; # with stmt attrs
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use Eval::Reversible;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use List::Util 1.44 (qw( uniq any all first )); # 1.44 has uniq
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use Sub::Util qw( subname set_subname );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶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 3 ▶use namespace::clean -except => [qw< new meta >];
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 3 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 3 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 28 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
190 3 );
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 3 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 default => sub {
228 28 ▶ my $rsrc = shift->rsrc // return;
Subroutine __ANON__: called CC=2 SCAR=7.2
Condition50%1/2
(shift())->rsrc // (return)
l!l
280
MC/DC0%0/1
(shift())->rsrc // (return)
(shift())->rsrc
Truth table50%1/2
A
A= (shift())->rsrc
Aresult
11
00
229 28 $rsrc->from;
230 },
231 3 );
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 3 has new_table_name => (
245 is => 'ro',
246 isa => Str,
247 required => 0,
248 lazy => 1,
249 builder => 1,
250 );
251  
252 sub _build_new_table_name {
253 28 ▶ my $self = shift;
Subroutine _build_new_table_name: called CC=2 SCAR=6.9
254 28 my $dbh = $self->dbh;
255 28 my $vars = $self->_vars;
256  
257 28 my $catalog = $vars->{catalog};
258 28 my $schema = $vars->{schema};
259 28 my $orig_table_name = $self->table_name;
260  
261 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
262  
263 return $self->_find_new_identifier(
264 "_${orig_table_name}_new" => set_subname('_new_table_name_finder', sub {
265 28 ▶ $dbh = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
266 28 my $like_expr = shift;
267 28 $like_expr =~ s/([_%])/$escape$1/g;
268  
269 28 $dbh->table_info($catalog, $schema, $like_expr)->fetchrow_array;
270 28 }),
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 3 has progress_bar => (
289 is => 'rw',
290 isa => InstanceOf['Term::ProgressBar'],
291 );
292  
293 sub _progress_bar_setup {
294 28 ▶ my $self = shift;
Subroutine _progress_bar_setup: called CC=3 SCAR=12.0
295 28 my $vars = $self->_vars;
296  
297 28 my $steps = 7 + scalar keys %{ $self->coderef_hooks };
298  
299 28 ▶ 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
0280
-t *STDERR && -t *STDIN
!ll&&!rl&&r
2800
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 28 $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 default => sub {
325 28 ▶ my $table_name = shift->table_name;
Subroutine __ANON__: called CC=2 SCAR=7.2
326 28 ▶ 'Altering'.($table_name ? " $table_name" : '');
Branch: 1 branch
BranchTrueFalseTotal
$table_name ? : 28 0 28
327 },
328 3 );
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 3 );
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 3 );
423  
424 # This is filled in during copy_rows, since the _column_list call needs to happen after
425 # the DDL has run.
426 sub _fill_copy_opts {
427 27 ▶ my $self = shift;
Subroutine _fill_copy_opts: called CC=16 SCAR=29.7
428 27 my $rsrc = $self->rsrc;
429 27 my $dbh = $self->dbh;
430 27 my $vars = $self->_vars;
431  
432 27 my $copy_opts = $self->copy_opts;
433 27 my $helper = $self->_helper;
434  
435 27 my $catalog = $vars->{catalog};
436 27 my $schema = $vars->{schema};
437 27 my $orig_table_name = $self->table_name;
438 27 my $new_table_name = $self->new_table_name;
439  
440 27 my $orig_table_name_quote = $dbh->quote_identifier($orig_table_name);
441 27 my $new_table_name_quote = $dbh->quote_identifier($new_table_name);
442  
443 # Sane defaults for timing
444 27 ▶ $copy_opts->{target_time} //= 1;
Condition100%2/2
$copy_opts->{"target_time"} //= 1
l!l
1215
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 27 ▶ $copy_opts->{sleep} //= 0.5;
Condition100%2/2
$copy_opts->{"sleep"} //= "0.5"
l!l
1215
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 my $id_name = $copy_opts->{id_name} //= $self->dbh_runner(run => set_subname '_pk_finder', sub {
451 13 ▶ $dbh = $_;
Subroutine __ANON__: called CC=3 SCAR=11.3
452 13 my @ids = $dbh->primary_key($catalog, $schema, $orig_table_name);
453  
454 13 ▶ die "No primary key found for $orig_table_name" unless @ids;
Branch: 1 branch
BranchTrueFalseTotal
unless @ids 0 13 13
455 13 ▶ 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 13 13
456  
457 13 $ids[0];
458 27 ▶ });
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
14130
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 27 my $id_name_quote = $dbh->quote_identifier($id_name);
461  
462 27 ▶ if ($rsrc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rsrc) { } 27 0 27
463 27 ▶ $copy_opts->{dbic_storage} //= $rsrc->storage;
Condition66%2/3
$copy_opts->{"dbic_storage"} //= $rsrc->storage
l!l&&r!l&&!r
12150
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 27 ▶ $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
12150
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 27 ▶ $copy_opts->{dbic_retry_opts} //= {};
Condition100%2/2
$copy_opts->{"dbic_retry_opts"} //= {}
l!l
1215
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 27 ▶ $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
12150
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 27 ▶ $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 27 my @column_list = $self->_column_list;
477 27 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 27 my $insert_select_stmt = $helper->insert_select_stmt($column_list_str);
482  
483 27 ▶ $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
1215
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 27 ▶ $copy_opts->{stmt} //= $insert_select_stmt;
Condition66%2/3
$copy_opts->{"stmt"} //= $insert_select_stmt
l!l&&r!l&&!r
12150
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 27 ▶ $copy_opts->{progress_name} //= "Copying $orig_table_name" unless $copy_opts->{progress_bar};
Branch: 1 branch
BranchTrueFalseTotal
unless $copy_opts->{"progress_bar"} 27 0 27
Condition100%2/2
$copy_opts->{"progress_name"} //= "Copying $orig_table_name"
l!l
1215
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 27 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 3 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 28 ▶ default => sub { Eval::Reversible->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
555 3 );
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 28 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
566 3 );
567  
568 3 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 sub _build_helper {
578 28 ▶ my $self = shift;
Subroutine _build_helper: called CC=3 SCAR=11.6
579  
580 28 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 28 ▶ 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
2800
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 28 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 28 ▶ 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 28 28
590  
591 28 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 around BUILDARGS => sub {
601 28 ▶ my $next = shift;
Subroutine __ANON__: called CC=13 SCAR=32.3
602 28 my $class = shift;
603  
604 28 ▶ my %args = @_ == 1 ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 28 28
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 28 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"rsrc"} or $args{"dbi_connector"} 0 28 28
Condition33%1/3
$args{"rsrc"} or $args{"dbi_connector"}
l!l&&r!l&&!r
2800
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 28 ▶ $args{db_timeouts} //= {};
Condition50%1/2
$args{"db_timeouts"} //= {}
l!l
028
MC/DC0%0/1
$args{"db_timeouts"} //= {}
$args{"db_timeouts"}
Truth table50%1/2
A
A= $args{"db_timeouts"}
Aresult
11
00
612 28 ▶ $args{coderef_hooks} //= {};
Condition50%1/2
$args{"coderef_hooks"} //= {}
l!l
280
MC/DC0%0/1
$args{"coderef_hooks"} //= {}
$args{"coderef_hooks"}
Truth table50%1/2
A
A= $args{"coderef_hooks"}
Aresult
11
00
613 28 ▶ 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 28 28
Condition33%1/3
ref $args{"db_timeouts"} ne "HASH" or ref $args{"coderef_hooks"} ne "HASH"
l!l&&r!l&&!r
0028
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 28 ▶ unless ($args{coderef_hooks}{before_triggers}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"coderef_hooks"}{"before_triggers"}) 0 28 28
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 28 ▶ $args{db_timeouts}{lock_file} //= 1;
Condition50%1/2
$args{"db_timeouts"}{"lock_file"} //= 1
l!l
028
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 28 ▶ $args{db_timeouts}{lock_db} //= 60;
Condition50%1/2
$args{"db_timeouts"}{"lock_db"} //= 60
l!l
028
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 28 ▶ $args{db_timeouts}{lock_row} //= 2;
Condition50%1/2
$args{"db_timeouts"}{"lock_row"} //= 2
l!l
028
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 28 ▶ $args{db_timeouts}{session} //= 28_800;
Condition50%1/2
$args{"db_timeouts"}{"session"} //= 28800
l!l
028
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 28 $class->$next( %args );
638 3 };
639  
640 sub BUILD {
641 28 ▶ my $self = shift;
Subroutine BUILD: called CC=28 SCAR=60.6
Pod undocumented
642 28 my $rsrc = $self->rsrc;
643  
644 28 my $dbh = $self->dbh;
645 28 my $helper = $self->_helper;
646  
647 # Get the current catalog/schema
648 28 my ($catalog, $schema) = $helper->current_catalog_schema;
649  
650 28 $self->_vars->{catalog} = $catalog;
651 28 $self->_vars->{schema} = $schema;
652  
653 # Add in the post-connection details
654 28 my @stmts = $helper->post_connection_stmts;
655  
656 28 ▶ if ($rsrc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rsrc) { } 28 0 28
657 ### DBIC Storage
658  
659 28 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 28 my $storage = $rsrc->storage;
667 28 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 28 ▶ my $ref = defined $on_connect_call && ref $on_connect_call;
Condition33%1/3
defined $on_connect_call && ref $on_connect_call
!ll&&!rl&&r
2800
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 28 ▶ unless ($on_connect_call) {
Branch: 4 branches
BranchTrueFalseTotal
if ($on_connect_call) { } 0 28 28
if (do { not $ref }) { } 0 0 0
elsif ($ref eq 'ARRAY') { } 0 0 0
elsif ($ref eq 'CODE') { } 0 0 0
672 28 $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 28 $storage->_dbic_connect_attributes->{on_connect_call} = $on_connect_call;
706 28 $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 $callbacks->{connected} = set_subname '_dbi_connected_callback' => sub {
728 0 ▶ shift->do($_) for @stmts;
Subroutine __ANON__: not called CC=2 SCAR=17.9
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 $callbacks->{connected} = set_subname '_dbi_connected_callback_wrapped' => sub {
742 0 ▶ my $h = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
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 28 $dbh->{AutoCommit} = 1;
758 28 $dbh->{RaiseError} = 1;
759 28 $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 sub construct_and_execute {
789 0 ▶ my $class = shift;
Subroutine construct_and_execute: not called CC=1 SCAR=6.9
Pod documented
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 sub execute {
809 28 ▶ my $self = shift;
Subroutine execute: called CC=1 SCAR=0.0
Pod documented
810 28 my $reversible = $self->reversible;
811  
812 28 $self->_progress_bar_setup;
813  
814 $reversible->run_reversibly(set_subname '_execute_part_one', sub {
815 28 ▶ $self->create_new_table;
Subroutine __ANON__: called CC=1 SCAR=0.0
816 28 $self->create_triggers;
817 27 $self->copy_rows;
818 27 $self->swap_tables;
819 28 });
820 $reversible->run_reversibly(set_subname '_execute_part_two', sub {
821 27 ▶ $self->readd_triggers;
Subroutine __ANON__: called CC=1 SCAR=0.0
822 27 $self->drop_old_table;
823 27 $self->cleanup_foreign_keys;
824 27 });
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 sub fire_hook {
838 55 ▶ my ($self, $hook_name) = @_;
Subroutine fire_hook: called CC=3 SCAR=11.0
Pod documented
839  
840 55 my $hooks = $self->coderef_hooks;
841 55 my $vars = $self->_vars;
842  
843 55 my $progress = $vars->{progress_bar};
844  
845 55 ▶ return unless $hooks && $hooks->{$hook_name};
Branch: 1 branch
BranchTrueFalseTotal
unless $hooks and $hooks->{$hook_name} 26 29 55
Condition66%2/3
$hooks and $hooks->{$hook_name}
!ll&&!rl&&r
02629
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 29 $progress->message("Firing hook for $hook_name");
848  
849 # Fire the hook
850 29 $hooks->{$hook_name}->($self);
851  
852 29 $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 sub dbh {
868 676 ▶ my $self = shift;
Subroutine dbh: called CC=1 SCAR=0.0
Pod documented
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 676 ▶ my $dbh = $self->dbh_runner( run => sub { $_[0] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
873 676 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 sub _retry_handler {
893 2 ▶ my ($self, $runner) = @_;
Subroutine _retry_handler: called CC=6 SCAR=21.3
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 sub dbh_runner {
932 1234 ▶ my ($self, $method, $coderef) = @_;
Subroutine dbh_runner: called CC=14 SCAR=33.7
Pod documented
933 1234 my $wantarray = wantarray;
934  
935 1234 ▶ die "Only 'txn' or 'run' are acceptable run methods" unless $method =~ /^(?:txn|run)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $method =~ /^(?:txn|run)$/ 0 1234 1234
936  
937 1234 my @res;
938 1234 ▶ if (my $rsrc = $self->rsrc) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rsrc = $self->rsrc) { } 1234 0 1234
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 1234 ▶ %{ $self->dbic_retry_opts },
Branch: 1 branch
BranchTrueFalseTotal
$method eq 'txn' ? : 27 1207 1234
947  
948 0 ▶ retry_handler => sub { $self->_retry_handler(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
949 1234 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 my $wrapper = set_subname '_dbh_run_blockrunner_wrapper' => sub {
956 1234 ▶ my ($s, $c) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
957 1234 my $dbh = $s->rsrc->storage->dbh;
958  
959 1234 local $_ = $dbh;
960 1234 $c->($dbh); # also pass it in, because that's what DBIx::Connector does
961 1234 };
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 1234 my $br_method = 'run';
967 1234 ▶ while ($block_runner->failed_attempt_count < $block_runner->max_attempts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_runner->failed_attempt_count < $block_runner->max_attempts) 1234 0 1234
968 1234 eval {
969 1234 ▶ unless (defined $wantarray) { $block_runner->$br_method($wrapper, $self, $coderef) }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $wantarray) { } 912 322 1234
if (do { $wantarray }) { } 167 745 912
970 167 elsif ($wantarray) { @res = $block_runner->$br_method($wrapper, $self, $coderef) }
971 745 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 1234 $br_method = '_run';
977  
978 1234 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) { } 0 1234 1234
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 1234 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 1234 ▶ return $wantarray ? @res : $res[0];
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 167 1067 1234
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 sub dbh_runner_do {
1022 180 ▶ my ($self, @commands) = @_;
Subroutine dbh_runner_do: called CC=4 SCAR=15.0
Pod documented
1023  
1024 180 foreach my $command (@commands) {
1025 184 my $ref = ref $command;
1026 184 ▶ 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 184 184
Condition33%1/3
$ref and $ref ne "ARRAY"
!ll&&!rl&&r
18400
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 $self->dbh_runner(run => set_subname '_dbh_runner_do', sub {
1029 184 ▶ $_->do( $ref ? @$command : $command );
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$ref ? : 0 184 184
1030 184 });
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 sub create_new_table {
1048 28 ▶ my $self = shift;
Subroutine create_new_table: called CC=3 SCAR=11.0
Pod documented
1049 28 my $dbh = $self->dbh;
1050 28 my $vars = $self->_vars;
1051  
1052 28 my $progress = $vars->{progress_bar};
1053 28 my $reversible = $self->reversible;
1054 28 my $helper = $self->_helper;
1055  
1056 28 my $orig_table_name = $self->table_name;
1057 28 my $new_table_name = $self->new_table_name;
1058  
1059 28 my $orig_table_name_quote = $dbh->quote_identifier($orig_table_name);
1060 28 my $new_table_name_quote = $dbh->quote_identifier($new_table_name);
1061  
1062 # ANSI quotes could also appear in the statement
1063 28 my $orig_table_name_ansi_quote = '"'.$orig_table_name.'"';
1064  
1065 28 $progress->message("Creating new table $new_table_name");
1066  
1067 28 my $table_sql = $helper->create_table_sql($orig_table_name);
1068 28 ▶ die "Table $orig_table_name does not exist in the database!" unless $table_sql;
Branch: 1 branch
BranchTrueFalseTotal
unless $table_sql 0 28 28
1069  
1070 28 ▶ $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 28 28
1071  
1072 # Change the old->new table name
1073 28 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 28 $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 28 $self->dbh_runner_do($table_sql);
1092  
1093 # Undo commands, including a failure warning update
1094 28 $reversible->failure_warning("\nDropping the new table and rolling back to start!\n\n");
1095 28 ▶ $reversible->add_undo(sub { $self->dbh_runner_do("DROP TABLE $new_table_name_quote") });
Subroutine __ANON__: called CC=1 SCAR=0.0
1096  
1097 28 $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 sub create_triggers {
1108 28 ▶ my $self = shift;
Subroutine create_triggers: called CC=13 SCAR=25.7
Pod documented
1109 28 my $rsrc = $self->rsrc;
1110 28 my $dbh = $self->dbh;
1111 28 my $vars = $self->_vars;
1112  
1113 28 my $progress = $vars->{progress_bar};
1114 28 my $reversible = $self->reversible;
1115 28 my $helper = $self->_helper;
1116  
1117 28 my $catalog = $vars->{catalog};
1118 28 my $schema = $vars->{schema};
1119 28 my $orig_table_name = $self->table_name;
1120 28 my $new_table_name = $self->new_table_name;
1121  
1122 # Fire the before_triggers hook, which would typically include the DDL
1123 28 $self->fire_hook('before_triggers');
1124  
1125 28 $progress->message("Creating triggers");
1126  
1127 # This shouldn't be cached until now, since the actual DDL may change the column list
1128 28 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 28 my @unique_ids;
1135 28 my $indexes = $self->_get_idx_hash($orig_table_name);
1136  
1137 28 my %potential_unique_ids;
1138 28 $potential_unique_ids{ $_->{name} } = $_ for grep { $_->{unique} } values %$indexes;
1139  
1140 28 my %column_set = map { $_ => 1 } @column_list;
1141 28 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 64 grep { $_ ne 'PRIMARY' }
1145 keys %potential_unique_ids
1146 ) {
1147 64 my @unique_cols = @{ $potential_unique_ids{$index_name}{columns} };
1148 64 ▶ next unless @unique_cols;
Branch: 1 branch
BranchTrueFalseTotal
unless @unique_cols 0 64 64
1149  
1150 # Only use this set if all of the columns exist in both tables
1151 64 ▶ 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 64 64
1152  
1153 64 @unique_ids = @unique_cols;
1154 }
1155  
1156 28 ▶ die "Cannot find an appropriate unique index for $orig_table_name!" unless @unique_ids;
Branch: 1 branch
BranchTrueFalseTotal
unless @unique_ids 0 28 28
1157  
1158 ### Check to make sure existing (conflicting) triggers aren't on the table
1159  
1160 28 ▶ 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 27 28
1161 if $helper->has_conflicting_triggers_on_table($orig_table_name);
1162  
1163 ### Get any info on existing triggers (to copy later)
1164  
1165 27 ▶ $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 27 27
1166  
1167 ### Find a good set of trigger names
1168  
1169 27 foreach my $trigger_type (qw< INSERT UPDATE DELETE >) {
1170 81 my $trigger_name = $helper->find_new_trigger_identifier(
1171 "${orig_table_name}_onlineddl_".lc($trigger_type)
1172 );
1173 81 $vars->{trigger_names} {$trigger_type} = $trigger_name;
1174 81 $vars->{trigger_names_quoted}{$trigger_type} = $dbh->quote_identifier($trigger_name);
1175 }
1176  
1177 ### Now create the triggers, with (mostly) ANSI SQL
1178  
1179 27 my $orig_table_name_quote = $dbh->quote_identifier($orig_table_name);
1180 27 my $new_table_name_quote = $dbh->quote_identifier($new_table_name);
1181  
1182 27 my $column_list_str = join(', ', map { $dbh->quote_identifier($_) } @column_list );
1183 27 my $new_column_list_str = join(', ', map { "NEW.".$dbh->quote_identifier($_) } @column_list );
1184  
1185 27 my $nseo = $helper->null_safe_equals_op;
1186 27 my %trigger_dml_stmts;
1187  
1188 # Using REPLACE just in case the row already exists from the copy
1189 27 $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 27 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 27 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 27 $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 27 $trigger_dml_stmts{delete_for_delete} = join("\n",
1228 "DELETE FROM $new_table_name_quote WHERE",
1229 " $delete_unique_where_str"
1230 );
1231  
1232 27 $helper->modify_trigger_dml_stmts( \%trigger_dml_stmts );
1233  
1234 27 foreach my $trigger_type (qw< INSERT UPDATE DELETE >) {
1235 my $trigger_header = join(' ',
1236 81 "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 81 my $trigger_sql = join("\n",
1243 $trigger_header,
1244 "BEGIN",
1245 '',
1246 );
1247  
1248 81 ▶ if ($trigger_type eq 'INSERT') {
Branch: 3 branches
BranchTrueFalseTotal
if ($trigger_type eq 'INSERT') { } 27 54 81
elsif ($trigger_type eq 'UPDATE') { } 27 27 54
elsif ($trigger_type eq 'DELETE') { } 27 0 27
1249 # INSERT trigger: Just a REPLACE command
1250 27 $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 27 $trigger_dml_stmts{replace}.';',
1257 );
1258 }
1259 elsif ($trigger_type eq 'DELETE') {
1260 # DELETE trigger: Just a DELETE command
1261 27 $trigger_sql .= $trigger_dml_stmts{delete_for_delete}.';';
1262 }
1263 81 $trigger_sql .= "\nEND";
1264  
1265 # DOIT!
1266 81 $self->dbh_runner_do($trigger_sql);
1267  
1268 $reversible->add_undo(sub {
1269 0 ▶ $self->dbh_runner_do( "DROP TRIGGER IF EXISTS ".$self->_vars->{trigger_names_quoted}{$trigger_type} );
Subroutine __ANON__: not called CC=1 SCAR=6.9
1270 81 });
1271 }
1272  
1273 27 $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 sub copy_rows {
1284 27 ▶ my $self = shift;
Subroutine copy_rows: called CC=1 SCAR=0.0
Pod documented
1285 27 my $dbh = $self->dbh;
1286 27 my $vars = $self->_vars;
1287  
1288 27 my $progress = $vars->{progress_bar};
1289 27 my $copy_opts = $self->_fill_copy_opts;
1290  
1291 27 $progress->message("Copying all rows to the new table");
1292  
1293 27 DBIx::BatchChunker->construct_and_execute( %$copy_opts );
1294 27 $vars->{new_table_copied} = 1;
1295  
1296 # Analyze the table, since we have a ton of new rows now
1297 27 $progress->message("Analyzing table");
1298 27 $self->_helper->analyze_table( $self->new_table_name );
1299  
1300 27 $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 sub swap_tables {
1310 27 ▶ my $self = shift;
Subroutine swap_tables: called CC=11 SCAR=34.2
Pod documented
1311 27 my $dbh = $self->dbh;
1312 27 my $vars = $self->_vars;
1313  
1314 27 my $progress = $vars->{progress_bar};
1315 27 my $reversible = $self->reversible;
1316 27 my $helper = $self->_helper;
1317  
1318 27 my $catalog = $vars->{catalog};
1319 27 my $schema = $vars->{schema};
1320 27 my $orig_table_name = $self->table_name;
1321 27 my $new_table_name = $self->new_table_name;
1322  
1323 27 ▶ 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
270
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 27 $self->fire_hook('before_swap');
1327  
1328 27 ▶ 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 27 27
Condition33%1/3
$helper->dbms_uses_global_fk_namespace or $helper->child_fks_need_adjusting
l!l&&r!l&&!r
0027
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 $self->dbh_runner(run => set_subname '_fk_parent_info_query', sub {
1335 0 ▶ $fk_hash->{parent} = $self->_fk_info_to_hash( $helper->foreign_key_info(undef, undef, undef, $catalog, $schema, $new_table_name) );
Subroutine __ANON__: not called CC=1 SCAR=6.9
1336 0 });
1337 $self->dbh_runner(run => set_subname '_fk_child_info_query', sub {
1338 0 ▶ $fk_hash->{child} = $self->_fk_info_to_hash( $helper->foreign_key_info($catalog, $schema, $orig_table_name, undef, undef, undef) );
Subroutine __ANON__: not called CC=1 SCAR=6.9
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 }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and $_ ne $orig_table_name and $_ ne $new_table_name 0 0 0
Condition0%0/3
defined $_ and $_ ne $orig_table_name
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ and $_ ne $orig_table_name
defined $_ $_ ne $orig_table_name
Truth table0%0/3
A and B
A= defined $_B= $_ ne $orig_table_name
ABresult
0X0
100
111
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 "_${orig_table_name}_old" => set_subname('_old_table_name_finder', sub {
1359 27 ▶ my ($d, $like_expr) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
1360 27 $like_expr =~ s/([_%])/$escape$1/g;
1361  
1362 27 $d->table_info($catalog, $schema, $like_expr)->fetchrow_array;
1363 27 }),
1364 'SQL_MAXIMUM_TABLE_NAME_LENGTH',
1365 );
1366 27 my $old_table_name_quote = $dbh->quote_identifier($old_table_name);
1367 27 $progress->message("Swapping tables ($new_table_name --> $orig_table_name --> $old_table_name)");
1368  
1369 # Let's swap tables!
1370 27 $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 27 $reversible->clear_undo;
1376 27 $vars->{new_table_swapped} = 1;
1377  
1378 27 $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 sub readd_triggers {
1397 27 ▶ my $self = shift;
Subroutine readd_triggers: called CC=2 SCAR=7.1
Pod documented
1398 27 my $dbh = $self->dbh;
1399 27 my $vars = $self->_vars;
1400  
1401 27 my $progress = $vars->{progress_bar};
1402 27 my $reversible = $self->reversible;
1403 27 my $helper = $self->_helper;
1404  
1405 27 my $orig_table_name = $self->table_name;
1406  
1407 27 $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 27 ▶ if ($vars->{existing_triggers}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vars->{"existing_triggers"}) 0 27 27
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 27 $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 sub drop_old_table {
1433 27 ▶ my $self = shift;
Subroutine drop_old_table: called CC=2 SCAR=7.0
Pod documented
1434 27 my $dbh = $self->dbh;
1435 27 my $vars = $self->_vars;
1436  
1437 27 my $progress = $vars->{progress_bar};
1438 27 my $reversible = $self->reversible;
1439 27 my $helper = $self->_helper;
1440  
1441 27 my $old_table_name = $vars->{old_table_name};
1442 27 my $old_table_name_quote = $dbh->quote_identifier($old_table_name);
1443  
1444 27 $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 27 ▶ if ($helper->child_fks_need_adjusting) {
Branch: 1 branch
BranchTrueFalseTotal
if ($helper->child_fks_need_adjusting) 0 27 27
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 27 $progress->message("Dropping old table $old_table_name");
1467  
1468 27 $self->dbh_runner_do("DROP TABLE $old_table_name_quote");
1469  
1470 27 $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 sub cleanup_foreign_keys {
1480 27 ▶ my $self = shift;
Subroutine cleanup_foreign_keys: called CC=3 SCAR=12.7
Pod documented
1481 27 my $dbh = $self->dbh;
1482 27 my $vars = $self->_vars;
1483  
1484 27 my $progress = $vars->{progress_bar};
1485 27 my $reversible = $self->reversible;
1486 27 my $helper = $self->_helper;
1487  
1488 27 $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 27 ▶ if ($helper->dbms_uses_global_fk_namespace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($helper->dbms_uses_global_fk_namespace) 0 27 27
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 27 ▶ if ($helper->child_fks_need_adjusting) {
Branch: 1 branch
BranchTrueFalseTotal
if ($helper->child_fks_need_adjusting) 0 27 27
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 27 $progress->update;
1525 }
1526  
1527 ### Private methods
1528  
1529 sub _find_new_identifier {
1530 139 ▶ my ($self, $desired_identifier, $finder_sub, $length_info_str) = @_;
Subroutine _find_new_identifier: called CC=6 SCAR=18.0
1531 139 ▶ $length_info_str ||= 'SQL_MAXIMUM_IDENTIFIER_LENGTH';
Condition100%2/2
$length_info_str ||= "SQL_MAXIMUM_IDENTIFIER_LENGTH"
l!l
5584
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 139 state $hash_digits = ['a' .. 'z', '0' .. '9'];
1534  
1535 139 my $hash = join '', map { $hash_digits->[rand @$hash_digits] } 1 .. 10;
1536  
1537 # Test out some potential names
1538 139 my @potential_names = (
1539 $desired_identifier, "_${desired_identifier}",
1540 "${desired_identifier}_${hash}", "_${desired_identifier}_${hash}",
1541 $hash, "_${hash}"
1542 );
1543  
1544 139 ▶ 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
1390
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
1545  
1546 139 my $new_name;
1547 139 foreach my $potential_name (@potential_names) {
1548 139 $potential_name = substr($potential_name, 0, $max_len); # avoid the ID name character limit
1549  
1550 my @results = $self->dbh_runner(run => set_subname '_find_new_identifier_dbh_runner', sub {
1551 139 ▶ $finder_sub->($_, $potential_name);
Subroutine __ANON__: called CC=1 SCAR=0.0
1552 139 });
1553  
1554 # Skip if we found it
1555 139 ▶ next if @results;
Branch: 1 branch
BranchTrueFalseTotal
if @results 0 139 139
1556  
1557 139 $new_name = $potential_name;
1558 139 last;
1559 }
1560  
1561 # This really shouldn't happen...
1562 139 ▶ 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 139 139
1563  
1564 139 return $new_name;
1565 }
1566  
1567 sub _column_list {
1568 55 ▶ my $self = shift;
Subroutine _column_list: called CC=1 SCAR=0.0
1569 55 my $dbh = $self->dbh;
1570 55 my $vars = $self->_vars;
1571  
1572 55 my $catalog = $vars->{catalog};
1573 55 my $schema = $vars->{schema};
1574 55 my $orig_table_name = $self->table_name;
1575 55 my $new_table_name = $self->new_table_name;
1576  
1577 55 my (@old_column_list, @new_column_list);
1578 $self->dbh_runner(run => set_subname '_column_list_runner', sub {
1579 55 ▶ $dbh = $_;
Subroutine __ANON__: called CC=1 SCAR=0.0
1580 @old_column_list =
1581 194 map { $_->{COLUMN_NAME} }
1582 55 @{ $dbh->column_info( $catalog, $schema, $orig_table_name, '%' )->fetchall_arrayref({ COLUMN_NAME => 1 }) }
1583 ;
1584 @new_column_list =
1585 222 map { $_->{COLUMN_NAME} }
1586 55 @{ $dbh->column_info( $catalog, $schema, $new_table_name, '%' )->fetchall_arrayref({ COLUMN_NAME => 1 }) }
1587 ;
1588 55 });
1589  
1590 # We only care about columns that exist in both tables. If a column was added on the
1591 # new table, there's no data to copy. If a column was deleted from the new table, we
1592 # don't care about keeping it.
1593 55 my %new_column_set = map { $_ => 1 } @new_column_list;
1594 55 return grep { $new_column_set{$_} } @old_column_list;
1595 }
1596  
1597 sub _get_idx_hash {
1598 28 ▶ my ($self, $table_name) = @_;
Subroutine _get_idx_hash: called CC=5 SCAR=16.1
1599  
1600 28 my $vars = $self->_vars;
1601 28 my $catalog = $vars->{catalog};
1602 28 my $schema = $vars->{schema};
1603  
1604 my %idxs = (
1605 PRIMARY => {
1606 name => 'PRIMARY',
1607 columns => [ $self->dbh_runner(run => set_subname '_pk_info_query', sub {
1608 28 ▶ $_->primary_key($catalog, $schema, $table_name)
Subroutine __ANON__: called CC=1 SCAR=0.0
1609 28 }) ],
1610 unique => 1,
1611 },
1612 );
1613 28 ▶ delete $idxs{PRIMARY} unless @{ $idxs{PRIMARY}{columns} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$idxs{"PRIMARY"}{"columns"};} 0 28 28
1614  
1615 28 ▶ return \%idxs unless $self->dbh->can('statistics_info');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dbh->can("statistics_info") 0 28 28
1616  
1617 # Sometimes, this still dies, even with the 'can' check (eg: older DBD::mysql drivers)
1618 28 my $index_stats = [];
1619 28 eval {
1620 $index_stats = $self->dbh_runner(run => set_subname '_idx_info_query', sub {
1621 28 ▶ $_->statistics_info($catalog, $schema, $table_name, 0, 1)->fetchall_arrayref({});
Subroutine __ANON__: called CC=1 SCAR=0.0
1622 28 });
1623 };
1624 28 ▶ $index_stats = [] if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 28 28
1625  
1626 28 foreach my $index_name (uniq map { $_->{INDEX_NAME} } @$index_stats) {
1627 62 ▶ my $index_stat = first { $_->{INDEX_NAME} eq $index_name } @$index_stats;
Subroutine __ANON__: called CC=1 SCAR=0.0
1628 my @cols =
1629 96 map { $_->{COLUMN_NAME} }
1630 38 sort { $a->{ORDINAL_POSITION} <=> $b->{ORDINAL_POSITION} }
1631 62 grep { $_->{INDEX_NAME} eq $index_name }
1632 @$index_stats
1633 ;
1634 $idxs{$index_name} = {
1635 name => $index_name,
1636 columns => \@cols,
1637 unique => !$index_stat->{NON_UNIQUE},
1638 62 };
1639 }
1640  
1641 28 return \%idxs;
1642 }
1643  
1644 sub _fk_info_to_hash {
1645 0 ▶ my ($self, $fk_sth) = @_;
Subroutine _fk_info_to_hash: not called CC=24 SCAR=64.0
1646 0 my $vars = $self->_vars;
1647 0 my $dbh = $self->dbh;
1648  
1649 # NOTE: Need to account for alternate ODBC names
1650  
1651 0 my @fk_rows = @{ $fk_sth->fetchall_arrayref({}) };
1652 @fk_rows = sort {
1653 # Sort by FK name, then by the column sequence number
1654 0 $a->{FK_NAME} cmp $b->{FK_NAME} ||
1655 ($a->{KEY_SEQ} // $a->{ORDINAL_POSITION}) <=> ($a->{KEY_SEQ} // $a->{ORDINAL_POSITION})
1656 0 ▶ } @fk_rows;
Branch: 1 branch
BranchTrueFalseTotal
$DBIx::OnlineDDL::a->{"FK_NAME"} cmp $DBIx::OnlineDDL::b->{"FK_NAME"} or ($DBIx::OnlineDDL::a->{"KEY_SEQ"} // $DBIx::OnlineDDL::a->{"ORDINAL_POSITION"}) <=> ($DBIx::OnlineDDL::a->{"KEY_SEQ"} // $DBIx::OnlineDDL::a->{"ORDINAL_POSITION"}) 0 0 0
Condition0%0/6
$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/2
$DBIx::OnlineDDL::a->{"KEY_SEQ"} // $DBIx::OnlineDDL::a->{"ORDINAL_POSITION"}
$DBIx::OnlineDDL::a->{"KEY_SEQ"} $DBIx::OnlineDDL::a->{"ORDINAL_POSITION"}
MC/DC0%0/2
$DBIx::OnlineDDL::a->{"KEY_SEQ"} // $DBIx::OnlineDDL::a->{"ORDINAL_POSITION"}
$DBIx::OnlineDDL::a->{"KEY_SEQ"} $DBIx::OnlineDDL::a->{"ORDINAL_POSITION"}
Truth table0%0/3
A // B
A= $DBIx::OnlineDDL::a->{"KEY_SEQ"}B= $DBIx::OnlineDDL::a->{"ORDINAL_POSITION"}
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $DBIx::OnlineDDL::a->{"KEY_SEQ"}B= $DBIx::OnlineDDL::a->{"ORDINAL_POSITION"}
ABresult
1X1
011
000
1657  
1658 0 my (%fks, %create_table_sql);
1659 0 foreach my $row (@fk_rows) {
1660 # Some of these rows aren't even FKs
1661 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
1662 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
1663  
1664 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
1665 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
1666  
1667 my $key = join( '.',
1668 $row->{PKTABLE_NAME} // $row->{UK_TABLE_CAT},
1669 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
1670 );
1671  
1672 # Since there may be multiple columns per FK, those associated columns are
1673 # arrayrefs.
1674 0 ▶ unless ($fks{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fks{$key}) { } 0 0 0
1675  
1676 $fks{$key} = {
1677 fk_name => $fk_name,
1678  
1679 # The table where the original PK exists
1680 pk_table_name => $row->{PKTABLE_NAME} // $row->{UK_TABLE_CAT},
1681 pk_columns => [ $row->{PKCOLUMN_NAME} // $row->{UK_COLUMN_NAME} ],
1682  
1683 # The table where the FK constraint has been declared
1684 fk_table_name => $fk_table_name,
1685 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
1686 };
1687  
1688 # Sadly, foreign_key_info doesn't always fill in all of the details for the FK, so the
1689 # CREATE TABLE SQL is actually the better record. Fortunately, this is all ANSI SQL.
1690 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
1691 0 my $fk_name_quote_re = '(?:'.join('|',
1692 quotemeta( $dbh->quote_identifier($fk_name) ), quotemeta('"'.$fk_name.'"'), quotemeta($fk_name)
1693 ).')';
1694  
1695 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
1696 CONSTRAINT \s $fk_name_quote_re \s ( # start capture of full SQL
1697 FOREIGN \s KEY \s \( [^\)]+ \) \s # "FOREIGN KEY" plus column list (which we already have above)
1698 REFERENCES \s [^\(]+ \s \( [^\)]+ \) # "REFERENCES" plus table+column list (again, already captured above)
1699 \s? ( [^\)\,]* ) # ON DELETE/UPDATE, DEFER, MATCH, etc.
1700 ) # end capture of full SQL
1701 >isx) {
1702 0 my ($fk_sql, $extra_sql) = ($1, $2);
1703 0 $fk_sql =~ s/^\s+|\s+$//g;
1704  
1705 0 $fks{$key}{fk_sql} = $fk_sql;
1706 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
1707 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
1708 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
1709 0 ▶ $fks{$key}{match} = $1 if $extra_sql =~ /(MATCH \w+)/i;
Branch: 1 branch
BranchTrueFalseTotal
if $extra_sql =~ /(MATCH \w+)/i 0 0 0
1710 }
1711 }
1712 else {
1713 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
1714 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
1715 }
1716 }
1717  
1718 0 return \%fks;
1719 }
1720  
1721 sub _fk_to_sql {
1722 0 ▶ my ($self, $fk) = @_;
Subroutine _fk_to_sql: not called CC=6 SCAR=37.4
1723 0 my $dbh = $self->dbh;
1724  
1725 # Everything after the CONSTRAINT keyword (ANSI SQL)
1726  
1727 0 ▶ if ($fk->{fk_sql}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fk->{"fk_sql"}) 0 0 0
1728 # Already have most of the SQL
1729 return join(' ',
1730 $dbh->quote_identifier($fk->{fk_name}),
1731 $fk->{fk_sql},
1732 0 );
1733 }
1734  
1735 return join(' ',
1736 $dbh->quote_identifier($fk->{fk_name}),
1737 'FOREIGN KEY',
1738 0 '('.join(', ', map { $dbh->quote_identifier($_) } @{ $fk->{fk_columns} }).')',
1739 'REFERENCES',
1740 $dbh->quote_identifier($fk->{pk_table_name}),
1741 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
1742 ( $fk->{match} ? $fk->{match} : () ),
1743 ( $fk->{delete_rule} ? 'ON DELETE '.$fk->{delete_rule} : () ),
1744 ( $fk->{update_rule} ? 'ON UPDATE '.$fk->{update_rule} : () ),
1745 0 ( $fk->{defer} ? $fk->{defer} : () ),
1746 );
1747 }
1748  
1749 #pod =head1 SEE ALSO
1750 #pod
1751 #pod =over
1752 #pod
1753 #pod =item *
1754 #pod
1755 #pod L<Percona's pt-online-schema-change|https://www.percona.com/doc/percona-toolkit/LATEST/pt-online-schema-change.html>
1756 #pod
1757 #pod =item *
1758 #pod
1759 #pod L<GitHub's gh-ost|https://github.com/github/gh-ost>
1760 #pod
1761 #pod =item *
1762 #pod
1763 #pod L<Facebook's OSC|https://www.facebook.com/notes/mysql-at-facebook/online-schema-change-for-mysql/430801045932/>
1764 #pod
1765 #pod =item *
1766 #pod
1767 #pod L<MySQL's Online DDL|https://dev.mysql.com/doc/refman/5.6/en/innodb-online-ddl.html>
1768 #pod
1769 #pod =back
1770 #pod
1771 #pod =head1 WHY YET ANOTHER OSC?
1772 #pod
1773 #pod The biggest reason is that none of the above fully support foreign key constraints.
1774 #pod Percona's C<pt-osc> comes close, but also includes this paragraph:
1775 #pod
1776 #pod Due to a limitation in MySQL, foreign keys will not have the same names after the ALTER
1777 #pod that they did prior to it. The tool has to rename the foreign key when it redefines it,
1778 #pod which adds a leading underscore to the name. In some cases, MySQL also automatically
1779 #pod renames indexes required for the foreign key.
1780 #pod
1781 #pod So, tables swapped with C<pt-osc> are not exactly what they used to be before the swap.
1782 #pod It also had a number of other quirks that just didn't work out for us, related to FKs and
1783 #pod the amount of switches required to make it (semi-)work.
1784 #pod
1785 #pod Additionally, by making DBIx::OnlineDDL its own Perl module, it's a lot easier to run
1786 #pod Perl-based schema changes along side L<DBIx::BatchChunker> without having to switch
1787 #pod between Perl and CLI. If other people want to subclass this module for their own
1788 #pod environment-specific quirks, they have the power to do so, too.
1789 #pod
1790 #pod =cut
1791  
1792 3 1;
1793  
1794 __END__
↑ Summary OnlineDDL/Helper/MySQL.pm »