Oracle/Diff/ForeignKey.pm

filter: Statementstmt 100% 85 / 85 Branchbran 96.4% 27 / 28 Conditioncond 65.1% 28 / 43 MC/DCmcdc 33.3% 8 / 24 Subroutinesub 100% 12 / 12 Podpod 100% 4 / 4 totaltotal 83.6% 164 / 196 CC 36 SCAR 23.4
CC
36
Cov
90%
diff
19.2
create_op
8.0
as_sql
6.0
CRAP 10.4
« Oracle/Introspect/Keys.pm ↑ Summary Oracle/Introspect/Views.pm »
1 package DBIO::Oracle::Diff::ForeignKey;
2 # ABSTRACT: Diff operations for Oracle foreign keys
3  
4 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 30 our $VERSION = '0.900007';
8  
9 30 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 30 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 30 ▶use DBIO::Oracle::Identifier ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 30 __PACKAGE__->mk_diff_accessors(qw(table_name constraint_name fk_info));
15  
16  
17  
18 # A signature of a foreign key that does not depend on the constraint name.
19 113 ▶sub _signature {
Subroutine _signature: called CC=5 SCAR=16.3
20 113 my ($fk) = @_;
21 226 ▶ my $norm = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
22 226 ▶ my $rule = uc( $_[0] // 'NO ACTION' );
Condition100%2/2
$_[0] // "NO ACTION"
l!l
2179
MC/DC100%1/1
$_[0] // "NO ACTION"
$_[0]
Truth table100%2/2
A
A= $_[0]
Aresult
11
00
23 226 ▶ return $rule eq 'RESTRICT' ? 'NO ACTION' : $rule;
Branch: 1 branch
BranchTrueFalseTotal
$rule eq 'RESTRICT' ? : 8 218 226
24 113 };
25 return join "\x1f",
26 113 ▶ join(',', @{ $fk->{from_columns} // [] }),
Condition50%2/4
$fk->{"from_columns"} // []
l!l
1130
$fk->{"to_table"} // ""
l!l
1130
MC/DC0%0/1
$fk->{"from_columns"} // []
$fk->{"from_columns"}
MC/DC0%0/1
$fk->{"to_table"} // ""
$fk->{"to_table"}
Truth table50%1/2
A
A= $fk->{"from_columns"}
Aresult
11
00
Truth table50%1/2
A
A= $fk->{"to_table"}
Aresult
11
00
27 $fk->{to_table} // '',
28 113 ▶ join(',', @{ $fk->{to_columns} // [] }),
Branch: 1 branch
BranchTrueFalseTotal
$fk->{'is_deferrable'} ? : 10 103 113
Condition50%1/2
$fk->{"to_columns"} // []
l!l
1130
MC/DC0%0/1
$fk->{"to_columns"} // []
$fk->{"to_columns"}
Truth table50%1/2
A
A= $fk->{"to_columns"}
Aresult
11
00
29 $norm->($fk->{on_delete}),
30 $norm->($fk->{on_update}),
31 113 $fk->{is_deferrable} ? 1 : 0;
32 }
33  
34  
35 22 ▶sub create_op {
Subroutine create_op: called CC=8 SCAR=20.8
Pod documented
36 22 my ($class, $table, $fk) = @_;
37 22 ▶ my @from = @{ $fk->{from_columns} // [] };
Condition50%1/2
$fk->{"from_columns"} // []
l!l
220
MC/DC0%0/1
$fk->{"from_columns"} // []
$fk->{"from_columns"}
Truth table50%1/2
A
A= $fk->{"from_columns"}
Aresult
11
00
38 my $name = $fk->{constraint_name}
39 // DBIO::Oracle::Identifier::shorten(join '_', 'fk',
40 (DBIO::Oracle::Identifier::split_table($table))[1], @from,
41 22 ▶ (DBIO::Oracle::Identifier::split_table($fk->{to_table}))[1]);
Condition66%2/3
$fk->{"constraint_name"} // DBIO::Oracle::Identifier::shorten(join("_", "fk", (DBIO::Oracle::Identifier::split_table($table))[1], @from, (DBIO::Oracle::Identifier::split_table($fk->{"to_table"}))[1]))
l!l&&r!l&&!r
2020
MC/DC0%0/2
$fk->{"constraint_name"} // DBIO::Oracle::Identifier::shorten(join("_", "fk", (DBIO::Oracle::Identifier::split_table($table))[1], @from, (DBIO::Oracle::Identifier::split_table($fk->{"to_table"}))[1]))
$fk->{"constraint_name"} DBIO::Oracle::Identifier::shorten(join("_", "fk", (DBIO::Oracle::Identifier::split_table($table))[1], @from, (DBIO::Oracle::Identifier::split_table($fk->{"to_table"}))[1]))
Truth table66%2/3
A // B
A= $fk->{"constraint_name"}B= DBIO::Oracle::Identifier::shorten(join("_", "fk", (DBIO::Oracle::Identifier::split_table($table))[1], @from, (DBIO::Oracle::Identifier::split_table($fk->{"to_table"}))[1]))
ABresult
1X1
011
000
42  
43 # Oracle's delete rules are NO ACTION (RESTRICT is the same), CASCADE and
44 # SET NULL and it has no ON UPDATE; anything else is not written in silence.
45 22 ▶ my $where = sprintf "the foreign key '%s' of table '%s' (column%s %s)",
Branch: 1 branch
BranchTrueFalseTotal
@from > 1 ? : 1 21 22
46 $name, $table, (@from > 1 ? 's' : ''), join(', ', @from);
47 22 ▶ my $rule = uc( $fk->{on_delete} // 'NO ACTION' );
Condition100%2/2
$fk->{"on_delete"} // "NO ACTION"
l!l
211
MC/DC100%1/1
$fk->{"on_delete"} // "NO ACTION"
$fk->{"on_delete"}
Truth table100%2/2
A
A= $fk->{"on_delete"}
Aresult
11
00
48 22 ▶ DBIO::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $rule =~ /\A(?:NO ACTION|RESTRICT|CASCADE|SET NULL)\z/ 1 21 22
49 "DBIO::Oracle::Diff: $where has the delete rule '$fk->{on_delete}', "
50 . 'which Oracle does not have (ON DELETE takes CASCADE and SET NULL)')
51 unless $rule =~ /\A(?:NO ACTION|RESTRICT|CASCADE|SET NULL)\z/;
52 DBIO::Exception->throw(
53 "DBIO::Oracle::Diff: $where has the update rule '$fk->{on_update}', "
54 . 'which Oracle does not have (Oracle has no ON UPDATE)')
55 21 ▶ if defined $fk->{on_update} && $fk->{on_update} !~ /\A(?:NO ACTION|RESTRICT)\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fk->{"on_update"} and not $fk->{"on_update"} =~ /\A(?:NO ACTION|RESTRICT)\z/i 2 19 21
Condition100%3/3
defined $fk->{"on_update"} and not $fk->{"on_update"} =~ /\A(?:NO ACTION|RESTRICT)\z/i
!ll&&!rl&&r
5142
MC/DC100%2/2
defined $fk->{"on_update"} and not $fk->{"on_update"} =~ /\A(?:NO ACTION|RESTRICT)\z/i
defined $fk->{"on_update"} not $fk->{"on_update"} =~ /\A(?:NO ACTION|RESTRICT)\z/i
Truth table100%3/3
A and not(B)
A= defined $fk->{"on_update"}B= not $fk->{"on_update"} =~ /\A(?:NO ACTION|RESTRICT)\z/i
ABresult
0X0
100
111
56  
57 19 return $class->new(
58 action => 'create',
59 table_name => $table,
60 constraint_name => $name,
61 fk_info => $fk,
62 );
63 }
64  
65 122 ▶sub diff {
Subroutine diff: called CC=18 SCAR=29.6
Pod documented
66 122 my ($class, $source, $target, $source_tables, $target_tables, $target_columns) = @_;
67 122 ▶ $source ||= {};
Condition50%1/2
$source ||= {}
l!l
1220
MC/DC0%0/1
$source ||= {}
$source
Truth table50%1/2
A
A= $source
Aresult
11
00
68 122 ▶ $target ||= {};
Condition50%1/2
$target ||= {}
l!l
1220
MC/DC0%0/1
$target ||= {}
$target
Truth table50%1/2
A
A= $target
Aresult
11
00
69 122 ▶ $source_tables ||= $source;
Condition33%1/3
$source_tables ||= $source
l!l&&r!l&&!r
12200
MC/DC0%0/2
$source_tables ||= $source
$source_tables $source
Truth table33%1/3
A ||= B
A= $source_tablesB= $source
ABresult
1X1
011
000
70 122 ▶ $target_tables ||= $target;
Condition33%1/3
$target_tables ||= $target
l!l&&r!l&&!r
12200
MC/DC0%0/2
$target_tables ||= $target
$target_tables $target
Truth table33%1/3
A ||= B
A= $target_tablesB= $target
ABresult
1X1
011
000
71 122 ▶ $target_columns ||= {};
Condition100%2/2
$target_columns ||= {}
l!l
1211
MC/DC100%1/1
$target_columns ||= {}
$target_columns
Truth table100%2/2
A
A= $target_columns
Aresult
11
00
72  
73 122 ▶ my $create = sub { $class->create_op(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
74  
75 122 my @ops;
76 122 my %walk = map { $_ => 1 } keys %$source, keys %$target;
77 122 for my $table (sort keys %walk) {
78 55 ▶ next unless exists $target_tables->{$table};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $target_tables->{$table} 5 50 55
79 50 ▶ my @target_fks = @{ $target->{$table} // [] };
Condition100%2/2
$target->{$table} // []
l!l
464
MC/DC100%1/1
$target->{$table} // []
$target->{$table}
Truth table100%2/2
A
A= $target->{$table}
Aresult
11
00
80  
81 # a table only the target has: all of its keys are new
82 50 ▶ unless (exists $source_tables->{$table}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $source_tables->{$table}) 8 42 50
83 8 push @ops, map { $create->($table, $_) } @target_fks;
84 8 next;
85 }
86  
87 42 ▶ my %have = map { _signature($_) => 1 } @{ $source->{$table} // [] };
Condition50%1/2
$source->{$table} // []
l!l
420
MC/DC0%0/1
$source->{$table} // []
$source->{$table}
Truth table50%1/2
A
A= $source->{$table}
Aresult
11
00
88 42 my %want = map { _signature($_) => 1 } @target_fks;
89  
90 # a key over a column the target drops goes with the column
91 my $kept = $target_columns->{$table}
92 42 ▶ ? { map { lc($_->{column_name}) => 1 } @{ $target_columns->{$table} } } : undef;
Branch: 1 branch
BranchTrueFalseTotal
$target_columns->{$table} ? : 42 0 42
93  
94 42 ▶ for my $fk (@{ $source->{$table} // [] }) {
Condition50%1/2
$source->{$table} // []
l!l
420
MC/DC0%0/1
$source->{$table} // []
$source->{$table}
Truth table50%1/2
A
A= $source->{$table}
Aresult
11
00
95 26 ▶ next if $want{ _signature($fk) };
Branch: 1 branch
BranchTrueFalseTotal
if $want{_signature($fk)} 11 15 26
96 15 ▶ next if $kept && grep { !$kept->{ lc $_ } } @{ $fk->{from_columns} // [] };
Branch: 1 branch
BranchTrueFalseTotal
if $kept and grep {not $$kept{lc $_};} @{$fk->{"from_columns"} // [];} 1 14 15
Condition60%3/5
$kept and grep {not $$kept{lc $_};} @{$fk->{"from_columns"} // [];}
!ll&&!rl&&r
0141
$fk->{"from_columns"} // []
l!l
150
MC/DC50%1/2
$kept and grep {not $$kept{lc $_};} @{$fk->{"from_columns"} // [];}
$kept grep {not $$kept{lc $_};} @{$fk->{"from_columns"} // [];}
MC/DC0%0/1
$fk->{"from_columns"} // []
$fk->{"from_columns"}
Truth table66%2/3
A and B
A= $keptB= grep {not $$kept{lc $_};} @{$fk->{"from_columns"} // [];}
ABresult
0X0
100
111
Truth table50%1/2
A
A= $fk->{"from_columns"}
Aresult
11
00
97 push @ops, $class->new(
98 action => 'drop',
99 table_name => $table,
100 constraint_name => $fk->{constraint_name},
101 14 fk_info => $fk,
102 );
103 }
104 42 push @ops, map { $create->($table, $_) } grep { !$have{ _signature($_) } } @target_fks;
105 }
106 119 return @ops;
107 }
108  
109  
110 36 ▶sub as_sql {
Subroutine as_sql: called CC=6 SCAR=17.9
Pod documented
111 36 my ($self) = @_;
112 36 my $q = \&DBIO::Oracle::Identifier::quote;
113 36 my $fk = $self->fk_info;
114  
115 36 ▶ return sprintf 'ALTER TABLE %s DROP CONSTRAINT %s;',
Branch: 1 branch
BranchTrueFalseTotal
if $self->action eq "drop" 15 21 36
116 DBIO::Oracle::Identifier::quote_table($self->table_name), $q->($self->constraint_name)
117 if $self->action eq 'drop';
118  
119 21 ▶ my $rule = uc( $fk->{on_delete} // 'NO ACTION' );
Condition100%2/2
$fk->{"on_delete"} // "NO ACTION"
l!l
201
MC/DC100%1/1
$fk->{"on_delete"} // "NO ACTION"
$fk->{"on_delete"}
Truth table100%2/2
A
A= $fk->{"on_delete"}
Aresult
11
00
120 21 ▶ $rule = 'NO ACTION' if $rule eq 'RESTRICT';
Branch: 1 branch
BranchTrueFalseTotal
if $rule eq "RESTRICT" 1 20 21
121 return sprintf 'ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s%s;',
122 DBIO::Oracle::Identifier::quote_table($self->table_name), $q->($self->constraint_name),
123 22 join(', ', map { $q->($_) } @{ $fk->{from_columns} }),
124 DBIO::Oracle::Identifier::quote_table($fk->{to_table}),
125 22 ▶ join(', ', map { $q->($_) } @{ $fk->{to_columns} }),
Branch: 2 branches
BranchTrueFalseTotal
$rule eq 'NO ACTION' ? : 15 6 21
$fk->{'is_deferrable'} ? : 5 16 21
126 ( $rule eq 'NO ACTION' ? '' : " ON DELETE $rule" ),
127 21 ( $fk->{is_deferrable} ? ' DEFERRABLE' : '' );
128 }
129  
130  
131 1 ▶sub summary {
Subroutine summary: called CC=1 SCAR=0.0
Pod documented
132 1 my ($self) = @_;
133 1 return sprintf ' %sforeign key: %s on %s',
134 $self->summary_prefix, $self->constraint_name, $self->table_name;
135 }
136  
137 30 1;
138  
139 __END__
« Oracle/Introspect/Keys.pm ↑ Summary Oracle/Introspect/Views.pm »