| 1 | package DBIO::Informix::Diff::ForeignKey; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for Informix foreign keys | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 31 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 31 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 31 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 31 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 31 | ▶ | use DBIO::Informix::Util qw(_quote_ident); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 31 | __PACKAGE__->mk_diff_accessors(qw(table_name constraint_name fk_info replaced)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 109 | ▶ | sub fk_name { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fk_name: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 109 | my ($table, $fk) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | return $fk->{constraint_name} // $fk->{fk_name} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 109 | ▶ | // join '_', 'fk', $table, @{ $fk->{from_columns} // [] }, $fk->{to_table}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition85%6/7
($fk->{"constraint_name"} // $fk->{"fk_name"}) // join("_", "fk", $table, @{$fk->{"from_columns"} // [];}, $fk->{"to_table"})
$fk->{"constraint_name"} // $fk->{"fk_name"}
$fk->{"from_columns"} // []
MC/DC100%2/2
($fk->{"constraint_name"} // $fk->{"fk_name"}) // join("_", "fk", $table, @{$fk->{"from_columns"} // [];}, $fk->{"to_table"})
$fk->{"constraint_name"}
$fk->{"fk_name"}
MC/DC0%0/1
$fk->{"from_columns"} // []
$fk->{"from_columns"}
Truth table100%3/3
A // B
A= $fk->{"constraint_name"}B= $fk->{"fk_name"}
Truth table50%1/2
A
A= $fk->{"from_columns"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # The name the model gives a key, as DROP CONSTRAINT needs it (the server's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # own included). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 124 | ▶ | sub _real_name { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_real_name: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 124 | my ($fk) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 124 | ▶ | my $n = $fk->{constraint_name} // $fk->{fk_name}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$fk->{"constraint_name"} // $fk->{"fk_name"}
MC/DC100%2/2
$fk->{"constraint_name"} // $fk->{"fk_name"}
$fk->{"constraint_name"}
$fk->{"fk_name"}
Truth table100%3/3
A // B
A= $fk->{"constraint_name"}B= $fk->{"fk_name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 124 | ▶ | return (defined $n && length $n) ? $n : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $n and length $n
MC/DC50%1/2
defined $n and length $n
defined $n
length $n
Truth table66%2/3
A and B
A= defined $nB= length $n
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # The name that is written on ADD: one the model states, never one the server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # assigned (r<tabid>_<n>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 101 | ▶ | sub _explicit_name { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_explicit_name: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 101 | my ($fk) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 101 | my $n = _real_name($fk); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 101 | ▶ | return undef if !defined $n || $n =~ /\Ar\d+_\d+\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $n or $n =~ /\Ar\d+_\d+\z/
MC/DC100%2/2
not defined $n or $n =~ /\Ar\d+_\d+\z/
not defined $n
$n =~ /\Ar\d+_\d+\z/
Truth table100%3/3
not(A) or B
A= not defined $nB= $n =~ /\Ar\d+_\d+\z/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 51 | return $n; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 609 | ▶ | sub _rule { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_rule: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 609 | my ($v) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 609 | ▶ | return '' if !defined $v || $v eq ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $v or $v eq ""
MC/DC50%1/2
not defined $v or $v eq ""
not defined $v
$v eq ""
Truth table66%2/3
not(A) or B
A= not defined $vB= $v eq ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 84 | $v = uc $v; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 84 | $v =~ s/\s+/ /g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 84 | ▶ | return 'NONE' if $v eq 'R' || $v eq 'RESTRICT' || $v eq 'NO ACTION'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$v eq "R" or $v eq "RESTRICT" or $v eq "NO ACTION"
$v eq "R" or $v eq "RESTRICT"
MC/DC100%3/3
$v eq "R" or $v eq "RESTRICT" or $v eq "NO ACTION"
$v eq "R"
$v eq "RESTRICT"
$v eq "NO ACTION"
Truth table100%4/4
A or B or C
A= $v eq "R"B= $v eq "RESTRICT"C= $v eq "NO ACTION"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 57 | ▶ | return 'CASCADE' if $v eq 'CASCADE' || $v eq 'C'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$v eq "CASCADE" or $v eq "C"
MC/DC100%2/2
$v eq "CASCADE" or $v eq "C"
$v eq "CASCADE"
$v eq "C"
Truth table100%3/3
A or B
A= $v eq "CASCADE"B= $v eq "C"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 13 | return $v; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # Die unless the key uses only what Informix can express. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 109 | ▶ | sub _assert_supported { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_assert_supported: called CC=10 SCAR=23.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 109 | my ($table, $fk) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 109 | my $name = fk_name($table, $fk); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 109 | ▶ | my $del = _rule($fk->{on_delete} // $fk->{del_rule}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$fk->{"on_delete"} // $fk->{"del_rule"}
MC/DC100%2/2
$fk->{"on_delete"} // $fk->{"del_rule"}
$fk->{"on_delete"}
$fk->{"del_rule"}
Truth table100%3/3
A // B
A= $fk->{"on_delete"}B= $fk->{"del_rule"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 109 | ▶ | my $upd = _rule($fk->{on_update} // $fk->{upd_rule}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$fk->{"on_update"} // $fk->{"upd_rule"}
MC/DC100%2/2
$fk->{"on_update"} // $fk->{"upd_rule"}
$fk->{"on_update"}
$fk->{"upd_rule"}
Truth table100%3/3
A // B
A= $fk->{"on_update"}B= $fk->{"upd_rule"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | die sprintf "Informix foreign key %s on table %s: ON DELETE %s is not supported " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | . "(Informix has only ON DELETE CASCADE); refusing to drop it silently\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | $name, $table, $fk->{on_delete} // $fk->{del_rule} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 109 | ▶ | if $del ne '' && $del ne 'NONE' && $del ne 'CASCADE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition88%8/9
$del ne "" and $del ne "NONE" and $del ne "CASCADE"
$del ne "" and $del ne "NONE"
$fk->{"on_delete"} // $fk->{"del_rule"}
MC/DC100%3/3
$del ne "" and $del ne "NONE" and $del ne "CASCADE"
$del ne ""
$del ne "NONE"
$del ne "CASCADE"
MC/DC0%0/2
$fk->{"on_delete"} // $fk->{"del_rule"}
$fk->{"on_delete"}
$fk->{"del_rule"}
Truth table100%4/4
A and B and C
A= $del ne ""B= $del ne "NONE"C= $del ne "CASCADE"
Truth table66%2/3
A // B
A= $fk->{"on_delete"}B= $fk->{"del_rule"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | die sprintf "Informix foreign key %s on table %s: ON UPDATE %s is not supported " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | . "(Informix has no ON UPDATE); refusing to drop it silently\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | $name, $table, $fk->{on_update} // $fk->{upd_rule} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 104 | ▶ | if $upd ne '' && $upd ne 'NONE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$upd ne "" and $upd ne "NONE"
$fk->{"on_update"} // $fk->{"upd_rule"}
MC/DC100%2/2
$upd ne "" and $upd ne "NONE"
$upd ne ""
$upd ne "NONE"
MC/DC0%0/2
$fk->{"on_update"} // $fk->{"upd_rule"}
$fk->{"on_update"}
$fk->{"upd_rule"}
Truth table100%3/3
A and B
A= $upd ne ""B= $upd ne "NONE"
Truth table66%2/3
A // B
A= $fk->{"on_update"}B= $fk->{"upd_rule"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 101 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 65 | ▶ | sub _cascade { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cascade: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 65 | my ($fk) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 65 | ▶ | return _rule($fk->{on_delete} // $fk->{del_rule}) eq 'CASCADE' ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$fk->{"on_delete"} // $fk->{"del_rule"}
MC/DC50%1/2
$fk->{"on_delete"} // $fk->{"del_rule"}
$fk->{"on_delete"}
$fk->{"del_rule"}
Truth table66%2/3
A // B
A= $fk->{"on_delete"}B= $fk->{"del_rule"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 65 | ▶ | sub fk_clause { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fk_clause: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 65 | my ($table, $fk) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 65 | _assert_supported($table, $fk); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 65 | my $name = _explicit_name($fk); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | return sprintf 'FOREIGN KEY (%s) REFERENCES %s(%s)%s%s', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 71 | join(', ', map { _quote_ident($_) } @{ $fk->{from_columns} }), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | _quote_ident($fk->{to_table}), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 65 | ▶ | join(', ', map { _quote_ident($_) } @{ $fk->{to_columns} }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | (_cascade($fk) ? ' ON DELETE CASCADE' : ''), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | (defined $name ? ' CONSTRAINT ' . _quote_ident($name) : ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | # Defaults compare as '', cascade as CASCADE, anything else as itself (so the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | # change is seen and the add refuses it). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 326 | ▶ | sub _view_rule { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_view_rule: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 326 | my ($v) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 326 | my $r = _rule($v); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 326 | ▶ | return $r eq 'NONE' ? '' : $r; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | # Comparison view: Informix knows only cascade or not, and no ON UPDATE. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 163 | ▶ | sub _view { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_view: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 163 | my ($fk) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | from_columns => $fk->{from_columns}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | to_columns => $fk->{to_columns}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | to_table => $fk->{to_table}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | on_delete => _view_rule($fk->{on_delete} // $fk->{del_rule}), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 163 | ▶ | on_update => _view_rule($fk->{on_update} // $fk->{upd_rule}), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
$fk->{"on_delete"} // $fk->{"del_rule"}
$fk->{"on_update"} // $fk->{"upd_rule"}
MC/DC100%2/2
$fk->{"on_delete"} // $fk->{"del_rule"}
$fk->{"on_delete"}
$fk->{"del_rule"}
MC/DC100%2/2
$fk->{"on_update"} // $fk->{"upd_rule"}
$fk->{"on_update"}
$fk->{"upd_rule"}
Truth table100%3/3
A // B
A= $fk->{"on_delete"}B= $fk->{"del_rule"}
Truth table100%3/3
A // B
A= $fk->{"on_update"}B= $fk->{"upd_rule"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | # Identity of a key: what it says, not what it is called. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 163 | ▶ | sub _content_key { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_content_key: called CC=4 SCAR=14.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 163 | my ($fk) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 163 | my $v = _view($fk); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 163 | ▶ | return join "\0", join(',', @{ $v->{from_columns} // [] }), $v->{to_table} // '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$v->{"from_columns"} // []
$v->{"to_table"} // ""
MC/DC0%0/1
$v->{"from_columns"} // []
$v->{"from_columns"}
MC/DC0%0/1
$v->{"to_table"} // ""
$v->{"to_table"}
Truth table50%1/2
A
A= $v->{"from_columns"}
Truth table50%1/2
A
A= $v->{"to_table"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 163 | ▶ | join(',', @{ $v->{to_columns} // [] }), $v->{on_delete}, $v->{on_update}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$v->{"to_columns"} // []
MC/DC0%0/1
$v->{"to_columns"} // []
$v->{"to_columns"}
Truth table50%1/2
A
A= $v->{"to_columns"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 288 | ▶ | sub _keyed { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_keyed: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 288 | my ($fks) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 288 | my %out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 288 | ▶ | for my $table (keys %{ $fks // {} }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$fks // {}
MC/DC0%0/1
$fks // {}
$fks
Truth table50%1/2
A
A= $fks
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 149 | $out{$table} = [ map { +{ %$_, _content_key => _content_key($_) } } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 167 | ▶ | @{ $fks->{$table} // [] } ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$fks->{$table} // []
MC/DC0%0/1
$fks->{$table} // []
$fks->{$table}
Truth table50%1/2
A
A= $fks->{$table}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 288 | return \%out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 44 | ▶ | sub _add { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_add: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 44 | my ($class, $table, $fk) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 44 | _assert_supported($table, $fk); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 36 | return $class->new(action => 'add', table_name => $table, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | constraint_name => _explicit_name($fk), fk_info => $fk); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 23 | ▶ | sub _drop { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_drop: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 23 | my ($class, $table, $fk) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 23 | return $class->new(action => 'drop', table_name => $table, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | constraint_name => _real_name($fk), fk_info => $fk); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 87 | ▶ | sub diff { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
diff: called CC=32 SCAR=35.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 87 | my ($class, $source, $target, $source_tables, $target_tables, $dropped_keys) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | my @ops = $class->diff_nested(_keyed($source), _keyed($target), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | index_by => '_content_key', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | scope => 'both', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | source_tables => $source_tables, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | target_tables => $target_tables, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 36 | ▶ | on_new => sub { $class->_add($_[0], $_[2]) }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 20 | ▶ | on_gone => sub { $class->_drop($_[0], $_[2]) }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 87 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | # a changed key is its old form dropped, then the new one added | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 80 | @ops = ((grep { $_->action eq 'drop' } @ops), (grep { $_->action ne 'drop' } @ops)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | # A key that stays but points at a unique constraint dropped here: the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | # server takes the key along with the constraint (a later DROP CONSTRAINT of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | # the key is -623), so the key is dropped first and added again after | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | # (karr k27). A key the target no longer has is already an ordinary drop. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 80 | ▶ | if ($dropped_keys && %$dropped_keys) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$dropped_keys and %$dropped_keys
MC/DC100%2/2
$dropped_keys and %$dropped_keys
$dropped_keys
%$dropped_keys
Truth table100%3/3
A and B
A= $dropped_keysB= %$dropped_keys
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 17 | my $want = _keyed($target); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 17 | my %gone = map { $_->table_name . "\0" . _content_key($_->fk_info) => 1 } grep { $_->action eq 'drop' } @ops; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 17 | my (@rebuilt_drop, @rebuilt_add); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 17 | for my $table (sort keys %{ _keyed($source) }) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 8 | ▶ | next if !$source_tables || !exists $source_tables->{$table} || !$target_tables || !exists $target_tables->{$table}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%3/9
$source_tables and exists $source_tables->{$table} and $target_tables and exists $target_tables->{$table}
$source_tables and exists $source_tables->{$table} and $target_tables
$source_tables and exists $source_tables->{$table}
MC/DC0%0/4
$source_tables and exists $source_tables->{$table} and $target_tables and exists $target_tables->{$table}
$source_tables
exists $source_tables->{$table}
$target_tables
exists $target_tables->{$table}
Truth table20%1/5
A and B and C and D
A= $source_tablesB= exists $source_tables->{$table}C= $target_tablesD= exists $target_tables->{$table}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 8 | ▶ | my %stays = map { $_->{_content_key} => $_ } @{ $want->{$table} // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$want->{$table} // []
MC/DC100%1/1
$want->{$table} // []
$want->{$table}
Truth table100%2/2
A
A= $want->{$table}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 8 | ▶ | 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}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 9 | my $key = _content_key($fk); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 9 | ▶ | next if $gone{"$table\0$key"} || !exists $stays{$key}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$gone{"$table\000$key"} or not exists $stays{$key}
MC/DC50%1/2
$gone{"$table\000$key"} or not exists $stays{$key}
$gone{"$table\000$key"}
not exists $stays{$key}
Truth table66%2/3
A or not(B)
A= $gone{"$table\000$key"}B= not exists $stays{$key}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 4 | ▶ | next unless $dropped_keys->{ join "\0", $fk->{to_table} // '', sort @{ $fk->{to_columns} // [] } }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
$fk->{"to_table"} // ""
$fk->{"to_columns"} // []
MC/DC0%0/1
$fk->{"to_table"} // ""
$fk->{"to_table"}
MC/DC0%0/1
$fk->{"to_columns"} // []
$fk->{"to_columns"}
Truth table50%1/2
A
A= $fk->{"to_table"}
Truth table50%1/2
A
A= $fk->{"to_columns"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 3 | push @rebuilt_drop, $class->_drop($table, $fk); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 3 | push @rebuilt_add, $class->_add($table, $stays{$key}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 17 | push @ops, @rebuilt_drop, @rebuilt_add; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 17 | @ops = ((grep { $_->action eq 'drop' } @ops), (grep { $_->action ne 'drop' } @ops)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | # Tables only in the target: their keys are added after the CREATE TABLEs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 80 | my $keyed = _keyed($target); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 80 | for my $table (sort keys %$keyed) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 47 | ▶ | next if !$target_tables || !exists $target_tables->{$table}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$target_tables and exists $target_tables->{$table}
MC/DC0%0/2
$target_tables and exists $target_tables->{$table}
$target_tables
exists $target_tables->{$table}
Truth table33%1/3
A and B
A= $target_tablesB= exists $target_tables->{$table}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 47 | ▶ | next if $source_tables && exists $source_tables->{$table}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$source_tables and exists $source_tables->{$table}
MC/DC50%1/2
$source_tables and exists $source_tables->{$table}
$source_tables
exists $source_tables->{$table}
Truth table66%2/3
A and B
A= $source_tablesB= exists $source_tables->{$table}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 5 | push @ops, map { $class->_add($table, $_) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | sort { $a->{_content_key} cmp $b->{_content_key} } @{ $keyed->{$table} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | # A drop whose key comes back in this diff is the first half of a change. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | # "Comes back" is the same table and the same name when the model states | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | # one; the names the server assigns differ from one database to the next, so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | # a key without a stated name comes back as a key over the same columns. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 79 | my (%back_name, %back_cols); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 79 | for my $op (grep { $_->action eq 'add' } @ops) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 36 | ▶ | $back_name{ join "\0", $op->table_name, $op->constraint_name } = 1 if defined $op->constraint_name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 36 | ▶ | $back_cols{ join "\0", $op->table_name, @{ $op->fk_info->{from_columns} // [] } } = 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$op->fk_info->{"from_columns"} // []
MC/DC0%0/1
$op->fk_info->{"from_columns"} // []
$op->fk_info->{"from_columns"}
Truth table50%1/2
A
A= $op->fk_info->{"from_columns"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 79 | for my $op (grep { $_->action eq 'drop' } @ops) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 23 | my $name = $op->constraint_name; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | my $by_name = defined $name && $name !~ /\Ar\d+_\d+\z/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 23 | ▶ | && $back_name{ join "\0", $op->table_name, $name }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
defined $name && !($name =~ /\Ar\d+_\d+\z/) && $back_name{join "\000", $op->table_name, $name}
defined $name and not $name =~ /\Ar\d+_\d+\z/
MC/DC100%3/3
defined $name && !($name =~ /\Ar\d+_\d+\z/) && $back_name{join "\000", $op->table_name, $name}
defined $name
not $name =~ /\Ar\d+_\d+\z/
$back_name{join "\000", $op->table_name, $name}
Truth table100%4/4
A and not(B) && C
A= defined $nameB= not $name =~ /\Ar\d+_\d+\z/C= $back_name{join "\000", $op->table_name, $name}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | my $by_cols = (!defined $name || $name =~ /\Ar\d+_\d+\z/) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 23 | ▶ | && $back_cols{ join "\0", $op->table_name, @{ $op->fk_info->{from_columns} // [] } }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition87%7/8
(!defined($name) || $name =~ /\Ar\d+_\d+\z/) && $back_cols{join "\000", $op->table_name, @{$op->fk_info->{'from_columns'} // [];}}
not defined $name or $name =~ /\Ar\d+_\d+\z/
$op->fk_info->{"from_columns"} // []
MC/DC66%2/3
(!defined($name) || $name =~ /\Ar\d+_\d+\z/) && $back_cols{join "\000", $op->table_name, @{$op->fk_info->{'from_columns'} // [];}}
not defined $name
$name =~ /\Ar\d+_\d+\z/
$back_cols{join "\000", $op->table_name, @{$op->fk_info->{'from_columns'} // [];}}
MC/DC0%0/1
$op->fk_info->{"from_columns"} // []
$op->fk_info->{"from_columns"}
Truth table80%4/5
not(A) or B && C
A= not defined $nameB= $name =~ /\Ar\d+_\d+\z/C= $back_cols{join "\000", $op->table_name, @{$op->fk_info->{'from_columns'} // [];}}
Truth table50%1/2
A
A= $op->fk_info->{"from_columns"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 23 | ▶ | $op->replaced(1) if $by_name || $by_cols; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$by_name or $by_cols
MC/DC100%2/2
$by_name or $by_cols
$by_name
$by_cols
Truth table100%3/3
A or B
A= $by_nameB= $by_cols
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 79 | return @ops; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 34 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 34 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 34 | my $tbl = _quote_ident($self->table_name); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 34 | ▶ | if ($self->action eq 'drop') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 14 | my $name = $self->constraint_name; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 14 | ▶ | die sprintf "Informix foreign key on table %s cannot be dropped: the model holds no name for it\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | $self->table_name unless defined $name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 13 | return sprintf 'ALTER TABLE %s DROP CONSTRAINT %s;', $tbl, _quote_ident($name); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 20 | return sprintf 'ALTER TABLE %s ADD CONSTRAINT %s;', $tbl, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | fk_clause($self->table_name, $self->fk_info); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 5 | ▶ | sub summary { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
summary: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 5 | ▶ | return sprintf '%s fk: %s on %s', $self->summary_prefix, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->constraint_name // fk_name($self->table_name, $self->fk_info)
MC/DC0%0/2
$self->constraint_name // fk_name($self->table_name, $self->fk_info)
$self->constraint_name
fk_name($self->table_name, $self->fk_info)
Truth table33%1/3
A // B
A= $self->constraint_nameB= fk_name($self->table_name, $self->fk_info)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | $self->constraint_name // fk_name($self->table_name, $self->fk_info), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | $self->table_name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 31 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||