| 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"
MC/DC100%1/1
$_[0] // "NO ACTION"
$_[0]
Truth table100%2/2
A
A= $_[0]
| |||||||||||||||||||||||||||||||||||||||
| 23 | 226 | ▶ | return $rule eq 'RESTRICT' ? 'NO ACTION' : $rule; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 24 | 113 | }; | |||||||||||||||||||||||||||||||||||||
| 25 | return join "\x1f", | ||||||||||||||||||||||||||||||||||||||
| 26 | 113 | ▶ | join(',', @{ $fk->{from_columns} // [] }), | ||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$fk->{"from_columns"} // []
$fk->{"to_table"} // ""
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"}
Truth table50%1/2
A
A= $fk->{"to_table"}
| |||||||||||||||||||||||||||||||||||||||
| 27 | $fk->{to_table} // '', | ||||||||||||||||||||||||||||||||||||||
| 28 | 113 | ▶ | join(',', @{ $fk->{to_columns} // [] }), | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$fk->{"to_columns"} // []
MC/DC0%0/1
$fk->{"to_columns"} // []
$fk->{"to_columns"}
Truth table50%1/2
A
A= $fk->{"to_columns"}
| |||||||||||||||||||||||||||||||||||||||
| 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"} // []
MC/DC0%0/1
$fk->{"from_columns"} // []
$fk->{"from_columns"}
Truth table50%1/2
A
A= $fk->{"from_columns"}
| |||||||||||||||||||||||||||||||||||||||
| 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]))
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]))
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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"
MC/DC100%1/1
$fk->{"on_delete"} // "NO ACTION"
$fk->{"on_delete"}
Truth table100%2/2
A
A= $fk->{"on_delete"}
| |||||||||||||||||||||||||||||||||||||||
| 48 | 22 | ▶ | DBIO::Exception->throw( | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $fk->{"on_update"} and not $fk->{"on_update"} =~ /\A(?:NO ACTION|RESTRICT)\z/i
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
| |||||||||||||||||||||||||||||||||||||||
| 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 ||= {}
MC/DC0%0/1
$source ||= {}
$source
Truth table50%1/2
A
A= $source
| |||||||||||||||||||||||||||||||||||||||
| 68 | 122 | ▶ | $target ||= {}; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$target ||= {}
MC/DC0%0/1
$target ||= {}
$target
Truth table50%1/2
A
A= $target
| |||||||||||||||||||||||||||||||||||||||
| 69 | 122 | ▶ | $source_tables ||= $source; | ||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$source_tables ||= $source
MC/DC0%0/2
$source_tables ||= $source
$source_tables
$source
Truth table33%1/3
A ||= B
A= $source_tablesB= $source
| |||||||||||||||||||||||||||||||||||||||
| 70 | 122 | ▶ | $target_tables ||= $target; | ||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$target_tables ||= $target
MC/DC0%0/2
$target_tables ||= $target
$target_tables
$target
Truth table33%1/3
A ||= B
A= $target_tablesB= $target
| |||||||||||||||||||||||||||||||||||||||
| 71 | 122 | ▶ | $target_columns ||= {}; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$target_columns ||= {}
MC/DC100%1/1
$target_columns ||= {}
$target_columns
Truth table100%2/2
A
A= $target_columns
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 79 | 50 | ▶ | my @target_fks = @{ $target->{$table} // [] }; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$target->{$table} // []
MC/DC100%1/1
$target->{$table} // []
$target->{$table}
Truth table100%2/2
A
A= $target->{$table}
| |||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||
| 81 | # a table only the target has: all of its keys are new | ||||||||||||||||||||||||||||||||||||||
| 82 | 50 | ▶ | unless (exists $source_tables->{$table}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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} // []
MC/DC0%0/1
$source->{$table} // []
$source->{$table}
Truth table50%1/2
A
A= $source->{$table}
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||
| 94 | 42 | ▶ | for my $fk (@{ $source->{$table} // [] }) { | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$source->{$table} // []
MC/DC0%0/1
$source->{$table} // []
$source->{$table}
Truth table50%1/2
A
A= $source->{$table}
| |||||||||||||||||||||||||||||||||||||||
| 95 | 26 | ▶ | next if $want{ _signature($fk) }; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 96 | 15 | ▶ | next if $kept && grep { !$kept->{ lc $_ } } @{ $fk->{from_columns} // [] }; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
$kept and grep {not $$kept{lc $_};} @{$fk->{"from_columns"} // [];}
$fk->{"from_columns"} // []
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"} // [];}
Truth table50%1/2
A
A= $fk->{"from_columns"}
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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"
MC/DC100%1/1
$fk->{"on_delete"} // "NO ACTION"
$fk->{"on_delete"}
Truth table100%2/2
A
A= $fk->{"on_delete"}
| |||||||||||||||||||||||||||||||||||||||
| 120 | 21 | ▶ | $rule = 'NO ACTION' if $rule eq 'RESTRICT'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||