OnlineDDL/Helper/Base.pm

filter: Statementstmt 31.9% 45 / 141 Branchbran 10.0% 1 / 10 Conditioncond 3.7% 1 / 27 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 35.2% 12 / 34 Podpod 91.6% 22 / 24 totaltotal 32.4% 81 / 250 CC 20 SCAR 30.1
CC
20
Cov
26%
add_triggers_back_to_table
90.0
remove_fks_from_child_tables_stmts
20.0
add_fks_back_to_child_tables_stmts
20.0
CRAP 20.2
« OnlineDDL/Helper/MySQL.pm ↑ Summary OnlineDDL/Helper/SQLite.pm »
1 package DBIx::OnlineDDL::Helper::Base;
2  
3 6 our $AUTHORITY = 'cpan:GSG';
4 # ABSTRACT: Private OnlineDDL helper for RDBMS-specific code
5 6 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 our $VERSION = 'v1.1.2'; # VERSION
7  
8 6 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 ▶use Types::Standard qw( InstanceOf Num );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 ▶use DBI::Const::GetInfoType;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Sub::Util qw( set_subname );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use version 0.77 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 6 ▶use namespace::clean; # don't export the above
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 #pod =encoding utf8
20 #pod
21 #pod =head1 DESCRIPTION
22 #pod
23 #pod This is a private helper module for any RDBMS-specific code. B<As a private module, any
24 #pod methods or attributes here are subject to change.>
25 #pod
26 #pod You should really be reading documentation for L<DBIx::OnlineDDL>. The documentation
27 #pod here is mainly to benefit any developers who might want to create their own subclass
28 #pod module for their RDBMS and submit it to us. Or fix bugs with the existing helpers.
29 #pod
30 #pod =cut
31  
32 #pod =head1 PRIVATE ATTRIBUTES
33 #pod
34 #pod =head2 online_ddl
35 #pod
36 #pod Points back to the parent L<DBIx::OnlineDDL>. This comes with a bunch of handles to be
37 #pod able to call common methods with fewer keystrokes.
38 #pod
39 #pod =cut
40  
41 6 has online_ddl => (
42 is => 'ro',
43 isa => InstanceOf['DBIx::OnlineDDL'],
44 required => 1,
45 weak_ref => 1,
46 handles => {
47 vars => '_vars',
48 dbh => 'dbh',
49 table_name => 'table_name',
50 new_table_name => 'new_table_name',
51 copy_opts => 'copy_opts',
52 db_timeouts => 'db_timeouts',
53 dbh_runner => 'dbh_runner',
54 dbh_runner_do => 'dbh_runner_do',
55 find_new_identifier => '_find_new_identifier',
56 get_idx_hash => '_get_idx_hash',
57 fk_to_sql => '_fk_to_sql',
58 },
59 );
60  
61 #pod =for Pod::Coverage dbms_name progress
62 #pod
63 #pod =cut
64  
65 # Other "handles"
66 0 ▶sub dbms_name { shift->vars->{dbms_name} } # used for die errors only
Subroutine dbms_name: not called CC=1 SCAR=6.9
Pod undocumented
67 0 ▶sub progress { shift->vars->{progress_bar} }
Subroutine progress: not called CC=1 SCAR=6.9
Pod undocumented
68  
69 #pod =head1 PRIVATE CLASS "ATTRIBUTES"
70 #pod
71 #pod =head2 dbms_uses_global_fk_namespace
72 #pod
73 #pod If true, OnlineDDL will rename the FKs in the new table to make sure they don't conflict,
74 #pod and rename them back after the swap.
75 #pod
76 #pod =cut
77  
78 0 ▶sub dbms_uses_global_fk_namespace { 0 }
Subroutine dbms_uses_global_fk_namespace: not called CC=1 SCAR=6.9
Pod documented
79  
80 #pod =head2 child_fks_need_adjusting
81 #pod
82 #pod If true, OnlineDDL will call helper methods to adjust FKs bound to child tables.
83 #pod
84 #pod =cut
85  
86 0 ▶sub child_fks_need_adjusting { 0 }
Subroutine child_fks_need_adjusting: not called CC=1 SCAR=6.9
Pod documented
87  
88 #pod =head2 null_safe_equals_op
89 #pod
90 #pod This is the operator that the DB uses for NULL-safe equals comparisons. It would match
91 #pod this truth table:
92 #pod
93 #pod 0 <op> 0 --> TRUE
94 #pod 0 <op> 1 --> FALSE
95 #pod 0 <op> NULL --> FALSE (instead of NULL)
96 #pod NULL <op> NULL --> TRUE (instead of NULL)
97 #pod
98 #pod The ANSI SQL version is C<IS NOT DISTINCT FROM>, but others RDBMS typically use something
99 #pod less bulky.
100 #pod
101 #pod =cut
102  
103 0 ▶sub null_safe_equals_op { 'IS NOT DISTINCT FROM' }
Subroutine null_safe_equals_op: not called CC=1 SCAR=6.9
Pod documented
104  
105 #pod =head2 mmver
106 #pod
107 #pod The major/minor version of the currently connected server, converted to "numified" form
108 #pod via L<version>, after parsing out the dotted notation (ie: C<5.7.33> instead of
109 #pod C<5.7.33-36-log>). This allows for version comparisons.
110 #pod
111 #pod =cut
112  
113 6 has mmver => (
114 is => 'lazy',
115 isa => Num,
116 );
117  
118 29 ▶sub _build_mmver {
Subroutine _build_mmver: called CC=1 SCAR=0.0
119 29 my $self = shift;
120 29 my ($mmver) = ($self->dbh->get_info($GetInfoType{SQL_DBMS_VER}) =~ /(\d+\.\d+(?:\.\d+)?)/);
121 29 return version->parse("v$mmver")->numify;
122 }
123  
124 #pod =head1 PRIVATE HELPER METHODS
125 #pod
126 #pod As the base module, all of these methods will use ANSI SQL, since there is no assumption
127 #pod of the type of RDBMS used yet. Some of these methods may just immediately die, as there
128 #pod may not be a (safe) standard way of doing that task.
129 #pod
130 #pod =head2 current_catalog_schema
131 #pod
132 #pod ($catalog, $schema) = $helper->current_catalog_schema;
133 #pod
134 #pod Figure out the currently-selected catalog and schema (database name) from the database.
135 #pod
136 #pod =cut
137  
138 0 ▶sub current_catalog_schema {
Subroutine current_catalog_schema: not called CC=2 SCAR=17.9
Pod documented
139 0 my $self = shift;
140  
141 # Try to guess from the DSN parameters
142 0 my %dsn = map { /^(.+)=(.+)$/; lc($1) => $2; } (split /\;/, $self->dbh->{Name});
143 0 my $catalog = $dsn{catalog};
144 0 ▶ my $schema = $dsn{database} // $dsn{schema};
Condition0%0/3
$dsn{"database"} // $dsn{"schema"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$dsn{"database"} // $dsn{"schema"}
$dsn{"database"} $dsn{"schema"}
Truth table0%0/3
A // B
A= $dsn{"database"}B= $dsn{"schema"}
ABresult
1X1
011
000
145  
146 0 return ($catalog, $schema);
147 }
148  
149 #pod =head2 insert_select_stmt
150 #pod
151 #pod $insert_select_stmt = $helper->insert_select_stmt($column_list_str);
152 #pod
153 #pod Return an C<INSERT...SELECT> statement to copy rows from the old table to the new, in
154 #pod such a way that doesn't cause "duplicate row" errors. This is used by
155 #pod L<DBIx::BatchChunker> for the copy operation, so it will need C<BETWEEN ? AND ?>
156 #pod placeholders.
157 #pod
158 #pod =cut
159  
160 0 ▶sub insert_select_stmt {
Subroutine insert_select_stmt: not called CC=1 SCAR=6.9
Pod documented
161 0 my ($self, $column_list_str) = @_;
162  
163 0 my $dbh = $self->dbh;
164  
165 0 my $orig_table_name = $self->table_name;
166 0 my $new_table_name = $self->new_table_name;
167  
168 0 my $orig_table_name_quote = $dbh->quote_identifier($orig_table_name);
169 0 my $new_table_name_quote = $dbh->quote_identifier($new_table_name);
170  
171 0 my $id_name = $self->copy_opts->{id_name};
172 0 my $old_full_id_name_quote = $dbh->quote_identifier(undef, $orig_table_name, $id_name);
173 0 my $new_full_id_name_quote = $dbh->quote_identifier(undef, $new_table_name, $id_name);
174  
175 # A generic JOIN solution
176 0 return join("\n",
177 "INSERT INTO $new_table_name_quote",
178 "($column_list_str)",
179 "SELECT",
180 " $column_list_str",
181 "FROM",
182 " $orig_table_name_quote",
183 " LEFT JOIN $new_table_name_quote ON (".join(" = ", $old_full_id_name_quote, $new_full_id_name_quote).")",
184 "WHERE",
185 " $old_full_id_name_quote BETWEEN ? AND ? AND",
186 " $new_full_id_name_quote IS NULL",
187 );
188 }
189  
190 #pod =head2 post_connection_stmts
191 #pod
192 #pod @stmts = $helper->post_connection_stmts;
193 #pod
194 #pod These are the SQL statements to run right after a C<$dbh> re-connect, typically session
195 #pod variable set statements.
196 #pod
197 #pod =cut
198  
199 0 ▶sub post_connection_stmts {
Subroutine post_connection_stmts: not called CC=1 SCAR=6.9
Pod documented
200 # No statements by default
201 0 return;
202 }
203  
204 #pod =head2 is_error_retryable
205 #pod
206 #pod $bool = $helper->is_error_retryable($error);
207 #pod
208 #pod Returns true if the specified error string (or exception object from DBIC/D:C:R) is
209 #pod retryable. Retryable errors generally fall under the categories of: lock contentions,
210 #pod lost DB connections, and query interruptions.
211 #pod
212 #pod =cut
213  
214 0 ▶sub is_error_retryable {
Subroutine is_error_retryable: not called CC=1 SCAR=6.9
Pod documented
215 0 warn sprintf "Not sure how to inspect DB errors for %s systems!", shift->dbms_name;
216 0 return 0;
217 }
218  
219 #pod =head2 create_table_sql
220 #pod
221 #pod $sql = $helper->create_table_sql($table_name);
222 #pod
223 #pod Get the C<CREATE TABLE> SQL statement for the specified table. This is RDBMS-specific,
224 #pod since C<information_schema> isn't always available and usually doesn't house all of the
225 #pod details, anyway.
226 #pod
227 #pod =cut
228  
229 0 ▶sub create_table_sql {
Subroutine create_table_sql: not called CC=1 SCAR=6.9
Pod documented
230 0 die sprintf "Not sure how to create a new table for %s systems!", shift->dbms_name;
231 }
232  
233 #pod =head2 rename_fks_in_table_sql
234 #pod
235 #pod $sql = $helper->rename_fks_in_table_sql($table_name, $sql)
236 #pod if $helper->dbms_uses_global_fk_namespace;
237 #pod
238 #pod Given the C<CREATE TABLE> SQL, return the statement with the FKs renamed. This should
239 #pod use C<find_new_identifier> to find a valid name.
240 #pod
241 #pod Only used if L</dbms_uses_global_fk_namespace> is true.
242 #pod
243 #pod =cut
244  
245 0 ▶sub rename_fks_in_table_sql {
Subroutine rename_fks_in_table_sql: not called CC=1 SCAR=6.9
Pod documented
246 0 my ($self, $table_name, $table_sql) = @_;
247  
248 # Don't change it by default
249 0 return $table_sql;
250 }
251  
252 #pod =head2 has_conflicting_triggers_on_table
253 #pod
254 #pod die if $helper->has_conflicting_triggers_on_table($table_name);
255 #pod
256 #pod Return true if triggers exist on the given table that would conflict with the operation.
257 #pod
258 #pod =cut
259  
260 0 ▶sub has_conflicting_triggers_on_table {
Subroutine has_conflicting_triggers_on_table: not called CC=1 SCAR=6.9
Pod documented
261 0 die sprintf "Not sure how to check for table triggers for %s systems!", shift->dbms_name;
262 }
263  
264 #pod =head2 has_triggers_on_table_to_be_copied
265 #pod
266 #pod $helper->has_triggers_on_table_to_be_copied($table_name);
267 #pod
268 #pod Return true if triggers exist on the given table that won't conflict with the operation,
269 #pod but will need to be copied over to the new table.
270 #pod
271 #pod =cut
272  
273 0 ▶sub has_triggers_on_table_to_be_copied {
Subroutine has_triggers_on_table_to_be_copied: not called CC=1 SCAR=6.9
Pod documented
274 0 die sprintf "Not sure how to check for table triggers for %s systems!", shift->dbms_name;
275 }
276  
277 #pod =head2 find_new_trigger_identifier
278 #pod
279 #pod $trigger_name = $helper->find_new_trigger_identifier($trigger_name);
280 #pod
281 #pod Return a free trigger identifier to use in the new trigger, using the inputted name as a
282 #pod base. This should use C<find_new_identifier> to find a valid name.
283 #pod
284 #pod =cut
285  
286 0 ▶sub find_new_trigger_identifier {
Subroutine find_new_trigger_identifier: not called CC=1 SCAR=6.9
Pod documented
287 0 die sprintf "Not sure how to check for table triggers for %s systems!", shift->dbms_name;
288 }
289  
290 #pod =head2 modify_trigger_dml_stmts
291 #pod
292 #pod $helper->modify_trigger_dml_stmts( \%trigger_dml_stmts );
293 #pod
294 #pod Given the DML SQL statements to be plugged into the triggers, mutate the statements,
295 #pod tailored to the RDBMS. The input is a hashref of SQL statements for the following keys:
296 #pod
297 #pod replace # used in the INSERT/UPDATE triggers
298 #pod delete_for_update # used in the UPDATE trigger
299 #pod delete_for_delete # used in the DELETE trigger
300 #pod
301 #pod Since it's already a reference, this method will mutate the SQL strings.
302 #pod
303 #pod =cut
304  
305 28 ▶sub modify_trigger_dml_stmts {
Subroutine modify_trigger_dml_stmts: called CC=1 SCAR=0.0
Pod documented
306 28 my $self = shift;
307  
308 # Don't change them by default
309 28 return @_;
310 }
311  
312 #pod =head2 analyze_table
313 #pod
314 #pod $helper->analyze_table($table_name);
315 #pod
316 #pod Run the DDL statement to re-analyze the table, typically C<ANALYZE TABLE>.
317 #pod
318 #pod =cut
319  
320 0 ▶sub analyze_table {
Subroutine analyze_table: not called CC=1 SCAR=6.9
Pod documented
321 0 my ($self, $table_name) = @_;
322 0 my $table_name_quote = $self->dbh->quote_identifier($table_name);
323 0 $self->dbh_runner_do("ANALYZE TABLE $table_name_quote");
324 }
325  
326 #pod =head2 swap_tables
327 #pod
328 #pod $helper->swap_tables($new_table_name, $orig_table_name, $old_table_name);
329 #pod
330 #pod Runs the SQL to swap the tables in a safe and atomic manner. The default ANSI SQL
331 #pod solution is to run two C<ALTER TABLE> statements in a transaction, but only if the RDBMS
332 #pod supports transactional DDL.
333 #pod
334 #pod =cut
335  
336 28 ▶sub swap_tables {
Subroutine swap_tables: called CC=3 SCAR=11.3
Pod documented
337 28 my ($self, $new_table_name, $orig_table_name, $old_table_name) = @_;
338 28 my $dbh = $self->dbh;
339  
340 # If the RDBMS actually has a value for SQL_TXN_CAPABLE, and it's not SQL_TC_ALL,
341 # then it really doesn't support transactional DDL.
342 28 my $txn_capable = $dbh->get_info( $GetInfoType{SQL_TXN_CAPABLE} );
343 28 my $sql_tc_all = $DBI::Const::GetInfo::ODBC::ReturnValues{SQL_TXN_CAPABLE}{SQL_TC_ALL};
344 28 ▶ if (defined $txn_capable && $txn_capable != $sql_tc_all) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $txn_capable and $txn_capable != $sql_tc_all) 0 28 28
Condition33%1/3
defined $txn_capable and $txn_capable != $sql_tc_all
!ll&&!rl&&r
0280
MC/DC0%0/2
defined $txn_capable and $txn_capable != $sql_tc_all
defined $txn_capable $txn_capable != $sql_tc_all
Truth table33%1/3
A and B
A= defined $txn_capableB= $txn_capable != $sql_tc_all
ABresult
0X0
100
111
345 0 die sprintf "Not sure how to swap tables for %s systems!", shift->dbms_name;
346 }
347  
348 28 my $new_table_name_quote = $dbh->quote_identifier($new_table_name);
349 28 my $orig_table_name_quote = $dbh->quote_identifier($orig_table_name);
350 28 my $old_table_name_quote = $dbh->quote_identifier($old_table_name);
351  
352 28 ▶ $self->dbh_runner(txn => set_subname '_table_swap', sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
353 28 $dbh = $_;
354 28 $dbh->do("ALTER TABLE $orig_table_name_quote RENAME TO $old_table_name_quote");
355 28 $dbh->do("ALTER TABLE $new_table_name_quote RENAME TO $orig_table_name_quote");
356 28 });
357 }
358  
359 #pod =head2 get_trigger_data
360 #pod
361 #pod $helper->get_trigger_data($table_name) if $helper->has_triggers_on_table_to_be_copied($table_name);
362 #pod
363 #pod Acquires trigger data for the given table and stashes it in C<< $helper->vars >> for
364 #pod later use.
365 #pod
366 #pod =cut
367  
368 0 ▶sub get_trigger_data {
Subroutine get_trigger_data: not called CC=1 SCAR=6.9
Pod documented
369 0 die sprintf "Not sure how to check for table triggers for %s systems!", shift->dbms_name;
370 }
371  
372 #pod =head2 add_triggers_back_to_table
373 #pod
374 #pod @stmts = $helper->add_triggers_back_to_table($orig_table_name) if
375 #pod $helper->has_triggers_on_table_to_be_copied($new_table_name);
376 #pod
377 #pod Return a list of statements needed to add triggers to the given table. These will be run through
378 #pod L<DBIx::OnlineDDL/dbh_runner_do>.
379 #pod
380 #pod Only used if L</has_triggers_on_table_to_be_copied> is true.
381 #pod
382 #pod =cut
383  
384 0 ▶sub add_triggers_back_to_table {
Subroutine add_triggers_back_to_table: not called CC=9 SCAR=45.0
Pod documented
385 0 my ($self, $table_name) = @_;
386 0 my $dbh = $self->dbh;
387  
388 0 ▶ my $triggers = $self->vars->{existing_triggers} // return;
Condition0%0/2
$self->vars->{"existing_triggers"} // (return)
l!l
00
MC/DC0%0/1
$self->vars->{"existing_triggers"} // (return)
$self->vars->{"existing_triggers"}
Truth table0%0/2
A
A= $self->vars->{"existing_triggers"}
Aresult
11
00
389  
390 0 my $table_name_quote = $dbh->quote_identifier($table_name);
391  
392 0 my @stmts;
393 0 foreach my $trigger_name (
394 sort {
395 # Sort by: BEFORE/AFTER, INSERT/UPDATE/DELETE, action order (important!), name
396 ( $triggers->{$a}{action_timing} cmp $triggers->{$b}{action_timing} ) ||
397 ( $triggers->{$a}{event_manipulation} cmp $triggers->{$b}{event_manipulation} ) ||
398 0 ▶ (($triggers->{$a}{action_order} // 0) <=> ($triggers->{$b}{action_order} // 0)) ||
Condition0%0/13
$triggers->{$a}{'action_timing'} cmp $triggers->{$b}{'action_timing'} || $triggers->{$a}{'event_manipulation'} cmp $triggers->{$b}{'event_manipulation'} || ($triggers->{$a}{'action_order'} // 0) <=> ($triggers->{$b}{'action_order'} // 0) || $a cmp $b
l!l&&r!l&&!r
000
$triggers->{$a}{"action_timing"} cmp $triggers->{$b}{"action_timing"} or $triggers->{$a}{"event_manipulation"} cmp $triggers->{$b}{"event_manipulation"} or ($triggers->{$a}{"action_order"} // 0) <=> ($triggers->{$b}{"action_order"} // 0)
l!l&&r!l&&!r
000
$triggers->{$a}{"action_timing"} cmp $triggers->{$b}{"action_timing"} or $triggers->{$a}{"event_manipulation"} cmp $triggers->{$b}{"event_manipulation"}
l!l&&r!l&&!r
000
$triggers->{$a}{"action_order"} // 0
l!l
00
$triggers->{$b}{"action_order"} // 0
l!l
00
MC/DC0%0/4
$triggers->{$a}{'action_timing'} cmp $triggers->{$b}{'action_timing'} || $triggers->{$a}{'event_manipulation'} cmp $triggers->{$b}{'event_manipulation'} || ($triggers->{$a}{'action_order'} // 0) <=> ($triggers->{$b}{'action_order'} // 0) || $a cmp $b
$triggers->{$a}{"action_timing"} cmp $triggers->{$b}{"action_timing"} $triggers->{$a}{"event_manipulation"} cmp $triggers->{$b}{"event_manipulation"} $triggers->{$a}{"action_order"} $a cmp $b
MC/DC0%0/1
$triggers->{$b}{"action_order"} // 0
$triggers->{$b}{"action_order"}
Truth table0%0/5
A or B or C || D
A= $triggers->{$a}{"action_timing"} cmp $triggers->{$b}{"action_timing"}B= $triggers->{$a}{"event_manipulation"} cmp $triggers->{$b}{"event_manipulation"}C= $triggers->{$a}{"action_order"}D= $a cmp $b
ABCDresult
1XXX1
01XX1
001X1
00011
00000
Truth table0%0/2
A
A= $triggers->{$b}{"action_order"}
Aresult
11
00
399 ($a cmp $b)
400 }
401 keys %$triggers
402 ) {
403 0 my $trigger = $triggers->{$trigger_name};
404  
405 0 my $trigger_name_quote = $dbh->quote_identifier($trigger_name);
406 0 push @stmts, "DROP TRIGGER IF EXISTS $trigger_name_quote";
407  
408 0 my $sql = 'CREATE ';
409 0 ▶ if ($trigger->{definer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trigger->{"definer"}) 0 0 0
410 my $definer_quote =
411 join '@',
412 0 map { $dbh->quote_identifier($_) }
413 0 split(/\@/, $trigger->{definer}, 2)
414 ;
415 0 $sql .= "DEFINER = $definer_quote ";
416 }
417 0 $sql .= "TRIGGER $trigger_name_quote ";
418 $sql .= join(' ',
419 $trigger->{action_timing},
420 $trigger->{event_manipulation},
421 'ON', $table_name_quote,
422 'FOR EACH', $trigger->{action_orientation},
423 0 );
424 0 $sql .= "\n".$trigger->{action_statement};
425  
426 0 push @stmts, $sql;
427 }
428  
429 0 return @stmts;
430 }
431  
432 #pod =head2 foreign_key_info
433 #pod
434 #pod my $sth = $helper->foreign_key_info(
435 #pod $pk_catalog, $pk_schema, $pk_table_name,
436 #pod $fk_catalog, $fk_schema, $fk_table_name
437 #pod );
438 #pod
439 #pod Returns a statement handle in the same manner as a L<DBI/foreign_key_info> call. In the
440 #pod default case, this is just that call, but certain implementations may need it to be
441 #pod overloaded or overridden.
442 #pod
443 #pod =cut
444  
445 0 ▶sub foreign_key_info {
Subroutine foreign_key_info: not called CC=1 SCAR=6.9
Pod documented
446 0 my $self = shift;
447 0 return $self->dbh->foreign_key_info(@_);
448 }
449  
450 #pod =head2 remove_fks_from_child_tables_stmts
451 #pod
452 #pod @stmts = $helper->remove_fks_from_child_tables_stmts if $helper->child_fks_need_adjusting;
453 #pod
454 #pod Return a list of statements needed to remove FKs from the child tables. These will be
455 #pod run through L<DBIx::OnlineDDL/dbh_runner_do>.
456 #pod
457 #pod Only used if L</child_fks_need_adjusting> is true.
458 #pod
459 #pod =cut
460  
461 0 ▶sub remove_fks_from_child_tables_stmts {
Subroutine remove_fks_from_child_tables_stmts: not called CC=4 SCAR=30.0
Pod documented
462 0 my $self = shift;
463 0 my $dbh = $self->dbh;
464 0 my $fk_hash = $self->vars->{foreign_keys}{definitions};
465  
466 0 my @stmts;
467 0 foreach my $tbl_fk_name (sort keys %{$fk_hash->{child}}) {
468 0 my $fk = $fk_hash->{child}{$tbl_fk_name};
469  
470 # Ignore self-joined FKs
471 0 ▶ next if $fk->{fk_table_name} eq $self->table_name || $fk->{fk_table_name} eq $self->new_table_name;
Branch: 1 branch
BranchTrueFalseTotal
if $fk->{"fk_table_name"} eq $self->table_name or $fk->{"fk_table_name"} eq $self->new_table_name 0 0 0
Condition0%0/3
$fk->{"fk_table_name"} eq $self->table_name or $fk->{"fk_table_name"} eq $self->new_table_name
l!l&&r!l&&!r
000
MC/DC0%0/2
$fk->{"fk_table_name"} eq $self->table_name or $fk->{"fk_table_name"} eq $self->new_table_name
$fk->{"fk_table_name"} eq $self->table_name $fk->{"fk_table_name"} eq $self->new_table_name
Truth table0%0/3
A or B
A= $fk->{"fk_table_name"} eq $self->table_nameB= $fk->{"fk_table_name"} eq $self->new_table_name
ABresult
1X1
011
000
472  
473 # ANSI SQL, of course
474 push @stmts, join(' ',
475 'ALTER TABLE',
476 $dbh->quote_identifier( $fk->{fk_table_name} ),
477 'DROP CONSTRAINT',
478 0 $dbh->quote_identifier( $fk->{fk_name} ),
479 );
480 }
481  
482 0 return @stmts;
483 }
484  
485 #pod =head2 rename_fks_back_to_original_stmts
486 #pod
487 #pod @stmts = $helper->rename_fks_back_to_original_stmts if $helper->dbms_uses_global_fk_namespace;
488 #pod
489 #pod Return a list of statements needed to rename the FKs back to their original names. These will be
490 #pod run through L<DBIx::OnlineDDL/dbh_runner_do>.
491 #pod
492 #pod Only used if L</dbms_uses_global_fk_namespace> is true.
493 #pod
494 #pod =cut
495  
496 0 ▶sub rename_fks_back_to_original_stmts {
Subroutine rename_fks_back_to_original_stmts: not called CC=3 SCAR=24.8
Pod documented
497 0 my $self = shift;
498 0 my $dbh = $self->dbh;
499 0 my $fks = $self->vars->{foreign_keys};
500 0 my $fk_hash = $fks->{definitions};
501  
502 0 my $table_name = $self->table_name;
503  
504 0 my @stmts;
505 0 foreach my $tbl_fk_name (sort keys %{$fk_hash->{parent}}) {
506 0 my $fk = $fk_hash->{parent}{$tbl_fk_name};
507  
508 0 my $changed_fk_name = $fk->{fk_name};
509 0 my $orig_fk_name = $fks->{orig_names}{"$table_name.$changed_fk_name"};
510  
511 0 ▶ unless ($orig_fk_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($orig_fk_name) 0 0 0
512 0 $self->progress->message("WARNING: Did not find original FK name for $table_name.$changed_fk_name!");
513 0 next;
514 }
515  
516 # _fk_to_sql uses this directly, so just change it at the $fk hashref
517 0 $fk->{fk_name} = $orig_fk_name;
518  
519 0 push @stmts, join("\n",
520 "ALTER TABLE ".$dbh->quote_identifier($table_name),
521 " DROP CONSTRAINT ".$dbh->quote_identifier( $changed_fk_name ).',',
522 " ADD CONSTRAINT ".$self->fk_to_sql($fk)
523 );
524 }
525  
526 0 return @stmts;
527 }
528  
529 #pod =head2 add_fks_back_to_child_tables_stmts
530 #pod
531 #pod @stmts = $helper->add_fks_back_to_child_tables_stmts if $helper->child_fks_need_adjusting;
532 #pod
533 #pod Return a list of statements needed to add FKs back to the child tables. These will be
534 #pod run through L<DBIx::OnlineDDL/dbh_runner_do>.
535 #pod
536 #pod Only used if L</child_fks_need_adjusting> is true.
537 #pod
538 #pod =cut
539  
540 0 ▶sub add_fks_back_to_child_tables_stmts {
Subroutine add_fks_back_to_child_tables_stmts: not called CC=4 SCAR=30.0
Pod documented
541 0 my $self = shift;
542 0 my $dbh = $self->dbh;
543 0 my $fk_hash = $self->vars->{foreign_keys}{definitions};
544  
545 0 my @stmts;
546 0 foreach my $tbl_fk_name (sort keys %{$fk_hash->{child}}) {
547 0 my $fk = $fk_hash->{child}{$tbl_fk_name};
548  
549 # Ignore self-joined FKs
550 0 ▶ next if $fk->{fk_table_name} eq $self->table_name || $fk->{fk_table_name} eq $self->new_table_name;
Branch: 1 branch
BranchTrueFalseTotal
if $fk->{"fk_table_name"} eq $self->table_name or $fk->{"fk_table_name"} eq $self->new_table_name 0 0 0
Condition0%0/3
$fk->{"fk_table_name"} eq $self->table_name or $fk->{"fk_table_name"} eq $self->new_table_name
l!l&&r!l&&!r
000
MC/DC0%0/2
$fk->{"fk_table_name"} eq $self->table_name or $fk->{"fk_table_name"} eq $self->new_table_name
$fk->{"fk_table_name"} eq $self->table_name $fk->{"fk_table_name"} eq $self->new_table_name
Truth table0%0/3
A or B
A= $fk->{"fk_table_name"} eq $self->table_nameB= $fk->{"fk_table_name"} eq $self->new_table_name
ABresult
1X1
011
000
551  
552 push @stmts, join(' ',
553 "ALTER TABLE",
554 0 $dbh->quote_identifier( $fk->{fk_table_name} ),
555 "ADD CONSTRAINT",
556 $self->fk_to_sql($fk),
557 );
558 }
559  
560 0 return @stmts;
561 }
562  
563 #pod =head2 post_fk_add_cleanup_stmts
564 #pod
565 #pod @stmts = $helper->post_fk_add_cleanup_stmts if $helper->child_fks_need_adjusting;
566 #pod
567 #pod Return a list of clean up statements to run after the FKs are re-added back to the child
568 #pod tables. These will be run through L<DBIx::OnlineDDL/dbh_runner_do>.
569 #pod
570 #pod Only used if L</child_fks_need_adjusting> is true. The base method does nothing.
571 #pod
572 #pod =cut
573  
574 0 ▶sub post_fk_add_cleanup_stmts { return () }
Subroutine post_fk_add_cleanup_stmts: not called CC=1 SCAR=6.9
Pod documented
575  
576 6 1;
577  
578 __END__
« OnlineDDL/Helper/MySQL.pm ↑ Summary OnlineDDL/Helper/SQLite.pm »