| 1 | package DBIO::PostgreSQL::Diff::ForeignKey; | ||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for PostgreSQL foreign keys | ||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||
| 4 | 104 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 5 | 104 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||
| 7 | 104 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||
| 9 | 104 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||
| 11 | 104 | ▶ | use DBIO::PostgreSQL::Diff::NoStatement (); | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 12 | 104 | ▶ | use DBIO::PostgreSQL::Introspect (); | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 13 | 104 | ▶ | use DBIO::PostgreSQL::Util qw(quote_ident quote_qualified deferrable_clause); | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||
| 15 | 104 | __PACKAGE__->mk_diff_accessors(qw( | |||||||||||||||||||||||||||||||||||||||||||
| 16 | table_key schema_name table_name constraint_name fk_info | ||||||||||||||||||||||||||||||||||||||||||||
| 17 | replaced not_valid existing_table ahead_of_table | ||||||||||||||||||||||||||||||||||||||||||||
| 18 | )); | ||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||
| 31 | 202 | ▶ | sub _action { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_action: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||
| 32 | 202 | my ($action) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 202 | ▶ | $action = uc($action // 'NO ACTION'); | ||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$action // "NO ACTION"
MC/DC100%1/1
$action // "NO ACTION"
$action
Truth table100%2/2
A
A= $action
| |||||||||||||||||||||||||||||||||||||||||||||
| 34 | 202 | $action =~ s/\s+/ /g; | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 202 | return $action; | |||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | 93 | ▶ | sub _match { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_match: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||
| 39 | 93 | my ($match) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 93 | ▶ | return uc($match || 'SIMPLE'); | ||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$match || 'SIMPLE'
MC/DC100%1/1
$match || 'SIMPLE'
$match
Truth table100%2/2
A
A= $match
| |||||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||
| 43 | # The timing of a key as the model says it: (deferrable, initially_deferred). A | ||||||||||||||||||||||||||||||||||||||||||||
| 44 | # model that has only the older is_deferrable is read as deferrable. | ||||||||||||||||||||||||||||||||||||||||||||
| 45 | 159 | ▶ | sub _timing { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_timing: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||
| 46 | 159 | my ($info) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 159 | ▶ | return (($info->{deferrable} // $info->{is_deferrable}) ? 1 : 0, $info->{initially_deferred} ? 1 : 0); | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$info->{"deferrable"} // $info->{"is_deferrable"}
MC/DC100%2/2
$info->{"deferrable"} // $info->{"is_deferrable"}
$info->{"deferrable"}
$info->{"is_deferrable"}
Truth table100%3/3
A // B
A= $info->{"deferrable"}B= $info->{"is_deferrable"}
| |||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||
| 50 | # The fields of a key that make it another key. Validity is not one: it is the | ||||||||||||||||||||||||||||||||||||||||||||
| 51 | # same key, validated or not (_invalid). | ||||||||||||||||||||||||||||||||||||||||||||
| 52 | 61 | ▶ | sub _redefined { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_redefined: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||||||||||||||||||
| 53 | 61 | my ($old, $new) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 61 | ▶ | return 1 if join("\0", _timing($old)) ne join("\0", _timing($new)); | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 55 | 49 | for my $field (qw( remote_schema remote_table )) { | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 94 | ▶ | return 1 if ($old->{$field} // '') ne ($new->{$field} // ''); | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
$old->{$field} // ""
$new->{$field} // ""
MC/DC0%0/1
$old->{$field} // ""
$old->{$field}
MC/DC0%0/1
$new->{$field} // ""
$new->{$field}
Truth table50%1/2
A
A= $old->{$field}
Truth table50%1/2
A
A= $new->{$field}
| |||||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 58 | 43 | for my $field (qw( local_columns remote_columns on_delete_columns )) { | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 123 | ▶ | return 1 if join("\0", @{ $old->{$field} // [] }) ne join("\0", @{ $new->{$field} // [] }); | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%4/4
$old->{$field} // []
$new->{$field} // []
MC/DC100%1/1
$old->{$field} // []
$old->{$field}
MC/DC100%1/1
$new->{$field} // []
$new->{$field}
Truth table100%2/2
A
A= $old->{$field}
Truth table100%2/2
A
A= $new->{$field}
| |||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 61 | 35 | for my $field (qw( on_delete on_update )) { | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 64 | ▶ | return 1 if _action($old->{$field}) ne _action($new->{$field}); | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 64 | 27 | ▶ | return 1 if _match($old->{match}) ne _match($new->{match}); | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 65 | 25 | return 0; | |||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||
| 68 | # The key the database holds is NOT VALID and the target's is valid (a model | ||||||||||||||||||||||||||||||||||||||||||||
| 69 | # that says nothing of it holds a valid key: the target is what the install | ||||||||||||||||||||||||||||||||||||||||||||
| 70 | # built). | ||||||||||||||||||||||||||||||||||||||||||||
| 71 | 22 | ▶ | sub _invalid { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_invalid: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||
| 72 | 22 | my ($old, $new) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 22 | ▶ | return (!($old->{is_valid} // 1) && ($new->{is_valid} // 1)) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%7/7
not $old->{"is_valid"} // 1 and $new->{"is_valid"} // 1
$old->{"is_valid"} // 1
$new->{"is_valid"} // 1
MC/DC0%0/2
not $old->{"is_valid"} // 1 and $new->{"is_valid"} // 1
$old->{"is_valid"}
$new->{"is_valid"}
Truth table33%1/3
not(A) and B
A= $old->{"is_valid"}B= $new->{"is_valid"}
| |||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||
| 76 | 40 | ▶ | sub _differs { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_differs: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||
| 77 | 40 | my ($old, $new) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 40 | ▶ | return _redefined($old, $new) || _invalid($old, $new); | ||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
_redefined($old, $new) || _invalid($old, $new)
MC/DC100%2/2
_redefined($old, $new) || _invalid($old, $new)
_redefined($old, $new)
_invalid($old, $new)
Truth table100%3/3
A || B
A= _redefined($old, $new)B= _invalid($old, $new)
| |||||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||
| 81 | 69 | ▶ | sub diff { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
diff: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 82 | 69 | my ($class, $source, $target, $source_tables, $target_tables) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 69 | ▶ | $source //= {}; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$source //= {}
MC/DC0%0/1
$source //= {}
$source
Truth table50%1/2
A
A= $source
| |||||||||||||||||||||||||||||||||||||||||||||
| 84 | 69 | ▶ | $target //= {}; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$target //= {}
MC/DC0%0/1
$target //= {}
$target
Truth table50%1/2
A
A= $target
| |||||||||||||||||||||||||||||||||||||||||||||
| 85 | 69 | ▶ | $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
| |||||||||||||||||||||||||||||||||||||||||||||
| 86 | 69 | ▶ | $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
| |||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||
| 88 | # Every table that had a key is walked, though the target has no entry for | ||||||||||||||||||||||||||||||||||||||||||||
| 89 | # it (the introspection files none for a table without a key): an empty one, | ||||||||||||||||||||||||||||||||||||||||||||
| 90 | # so that the key the target no longer has is seen. | ||||||||||||||||||||||||||||||||||||||||||||
| 91 | # | ||||||||||||||||||||||||||||||||||||||||||||
| 92 | # Only tables the target has are looked at. The keys of a table that is | ||||||||||||||||||||||||||||||||||||||||||||
| 93 | # dropped go with the table, and a section that names a table its model | ||||||||||||||||||||||||||||||||||||||||||||
| 94 | # lacks is no more than that. The exception is a key between two tables | ||||||||||||||||||||||||||||||||||||||||||||
| 95 | # that are both dropped (below): DROP TABLE finds a key of the other table | ||||||||||||||||||||||||||||||||||||||||||||
| 96 | # on the table it drops. | ||||||||||||||||||||||||||||||||||||||||||||
| 97 | 69 | my $all_source = $source; | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 69 | my %source = map { $_ => $source->{$_} } grep { exists $target_tables->{$_} } keys %$source; | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 56 | my %target = ((map { $_ => [] } keys %source), | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 69 | map { $_ => $target->{$_} } grep { exists $target_tables->{$_} } keys %$target); | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 69 | $source = \%source; | |||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||
| 103 | 69 | ▶ | my $op = sub { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 104 | 69 | my ($action, $table_key, $name, $info, %more) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 105 | my ($schema, $table) = DBIO::PostgreSQL::Introspect->key_names( | ||||||||||||||||||||||||||||||||||||||||||||
| 106 | 69 | $table_key, table_name => $source_tables->{$table_key}, $target_tables->{$table_key}); | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 69 | return $class->new( | |||||||||||||||||||||||||||||||||||||||||||
| 108 | action => $action, table_key => $table_key, schema_name => $schema, table_name => $table, | ||||||||||||||||||||||||||||||||||||||||||||
| 109 | constraint_name => $name, fk_info => $info, %more); | ||||||||||||||||||||||||||||||||||||||||||||
| 110 | 69 | }; | |||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||
| 112 | my @ops = $class->diff_nested( | ||||||||||||||||||||||||||||||||||||||||||||
| 113 | $source, \%target, | ||||||||||||||||||||||||||||||||||||||||||||
| 114 | index_by => 'constraint_name', | ||||||||||||||||||||||||||||||||||||||||||||
| 115 | scope => 'all', | ||||||||||||||||||||||||||||||||||||||||||||
| 116 | source_tables => $source_tables, | ||||||||||||||||||||||||||||||||||||||||||||
| 117 | target_tables => $target_tables, | ||||||||||||||||||||||||||||||||||||||||||||
| 118 | changed_when => \&_differs, | ||||||||||||||||||||||||||||||||||||||||||||
| 119 | 20 | ▶ | on_new => sub { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||
| 120 | 20 | my ($table_key, $name, $info) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 121 | return $op->('create', $table_key, $name, $info, | ||||||||||||||||||||||||||||||||||||||||||||
| 122 | 20 | ▶ | existing_table => (exists $source_tables->{$table_key} ? 1 : 0)); | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 123 | }, | ||||||||||||||||||||||||||||||||||||||||||||
| 124 | 21 | ▶ | on_changed => sub { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||||
| 125 | 21 | my ($table_key, $name, $old, $new) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 21 | ▶ | return $op->('validate', $table_key, $name, $new) unless _redefined($old, $new); | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 127 | return ( | ||||||||||||||||||||||||||||||||||||||||||||
| 128 | $op->('drop', $table_key, $name, $old, replaced => 1), | ||||||||||||||||||||||||||||||||||||||||||||
| 129 | $op->('create', $table_key, $name, $new, | ||||||||||||||||||||||||||||||||||||||||||||
| 130 | 18 | ▶ | existing_table => (exists $source_tables->{$table_key} ? 1 : 0)), | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 131 | ); | ||||||||||||||||||||||||||||||||||||||||||||
| 132 | }, | ||||||||||||||||||||||||||||||||||||||||||||
| 133 | 8 | ▶ | on_gone => sub { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 134 | 8 | my ($table_key, $name, $info) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 8 | return $op->('drop', $table_key, $name, $info); | |||||||||||||||||||||||||||||||||||||||||||
| 136 | }, | ||||||||||||||||||||||||||||||||||||||||||||
| 137 | 69 | ); | |||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||
| 139 | # The keys that tie tables dropped together in a circle: no order of the | ||||||||||||||||||||||||||||||||||||||||||||
| 140 | # drops takes them. | ||||||||||||||||||||||||||||||||||||||||||||
| 141 | 69 | for my $key (grep { $_->{cyclic} } $class->dropped_together($all_source, $source_tables, $target_tables)) { | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 2 | push @ops, $op->('drop', $key->{holder}, $key->{info}{constraint_name}, $key->{info}, ahead_of_table => 1); | |||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||
| 145 | 69 | return @ops; | |||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||
| 149 | 3689 | ▶ | sub dropped_together { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dropped_together: called CC=13 SCAR=25.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 150 | 3689 | my ($class, $fks, $source_tables, $target_tables) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 3689 | ▶ | $fks //= {}; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$fks //= {}
MC/DC100%1/1
$fks //= {}
$fks
Truth table100%2/2
A
A= $fks
| |||||||||||||||||||||||||||||||||||||||||||||
| 152 | 3689 | ▶ | $source_tables //= {}; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$source_tables //= {}
MC/DC0%0/1
$source_tables //= {}
$source_tables
Truth table50%1/2
A
A= $source_tables
| |||||||||||||||||||||||||||||||||||||||||||||
| 153 | 3689 | ▶ | $target_tables //= {}; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$target_tables //= {}
MC/DC0%0/1
$target_tables //= {}
$target_tables
Truth table50%1/2
A
A= $target_tables
| |||||||||||||||||||||||||||||||||||||||||||||
| 154 | 3689 | my %dropped = map { $_ => 1 } grep { !exists $target_tables->{$_} } keys %$source_tables; | |||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||
| 156 | 3689 | my @keys; | |||||||||||||||||||||||||||||||||||||||||||
| 157 | 3689 | for my $holder (sort keys %$fks) { | |||||||||||||||||||||||||||||||||||||||||||
| 158 | 126 | ▶ | next unless $dropped{$holder}; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 159 | 12 | for my $info (@{ $fks->{$holder} }) { | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 16 | ▶ | next unless defined $info->{remote_schema} && defined $info->{remote_table}; # a key that names no table | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $info->{"remote_schema"} and defined $info->{"remote_table"}
MC/DC0%0/2
defined $info->{"remote_schema"} and defined $info->{"remote_table"}
defined $info->{"remote_schema"}
defined $info->{"remote_table"}
Truth table33%1/3
A and B
A= defined $info->{"remote_schema"}B= defined $info->{"remote_table"}
| |||||||||||||||||||||||||||||||||||||||||||||
| 161 | 16 | my $referenced = DBIO::PostgreSQL::Introspect->qualified_key($info->{remote_schema}, $info->{remote_table}); | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 16 | ▶ | next if $referenced eq $holder || !$dropped{$referenced}; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$referenced eq $holder or not $dropped{$referenced}
MC/DC100%2/2
$referenced eq $holder or not $dropped{$referenced}
$referenced eq $holder
not $dropped{$referenced}
Truth table100%3/3
A or not(B)
A= $referenced eq $holderB= not $dropped{$referenced}
| |||||||||||||||||||||||||||||||||||||||||||||
| 163 | 6 | push @keys, { holder => $holder, referenced => $referenced, info => $info }; | |||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||
| 167 | 3689 | my %to; | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 3689 | push @{ $to{ $_->{holder} } }, $_->{referenced} for @keys; | |||||||||||||||||||||||||||||||||||||||||||
| 169 | 6 | ▶ | my $reaches = sub { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||
| 170 | 6 | my ($from, $goal) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 6 | my (%seen, @todo) = (); | |||||||||||||||||||||||||||||||||||||||||||
| 172 | 6 | @todo = ($from); | |||||||||||||||||||||||||||||||||||||||||||
| 173 | 6 | ▶ | while (defined(my $next = shift @todo)) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 174 | 10 | ▶ | return 1 if $next eq $goal; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 175 | 6 | ▶ | next if $seen{$next}++; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 176 | 6 | ▶ | push @todo, @{ $to{$next} // [] }; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$to{$next} // []
MC/DC100%1/1
$to{$next} // []
$to{$next}
Truth table100%2/2
A
A= $to{$next}
| |||||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 178 | 2 | return 0; | |||||||||||||||||||||||||||||||||||||||||||
| 179 | 3689 | }; | |||||||||||||||||||||||||||||||||||||||||||
| 180 | 3689 | $_->{cyclic} = $reaches->($_->{referenced}, $_->{holder}) for @keys; | |||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||||
| 182 | 3689 | return @keys; | |||||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||
| 186 | 69 | ▶ | sub split_outside { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
split_outside: called CC=15 SCAR=27.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 187 | 69 | my ($class, $source, $target, $source_tables, $target_tables) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 188 | 69 | ▶ | $source //= {}; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$source //= {}
MC/DC0%0/1
$source //= {}
$source
Truth table50%1/2
A
A= $source
| |||||||||||||||||||||||||||||||||||||||||||||
| 189 | 69 | ▶ | $target //= {}; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$target //= {}
MC/DC0%0/1
$target //= {}
$target
Truth table50%1/2
A
A= $target
| |||||||||||||||||||||||||||||||||||||||||||||
| 190 | 69 | ▶ | $source_tables //= {}; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$source_tables //= {}
MC/DC0%0/1
$source_tables //= {}
$source_tables
Truth table50%1/2
A
A= $source_tables
| |||||||||||||||||||||||||||||||||||||||||||||
| 191 | 69 | ▶ | $target_tables //= {}; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$target_tables //= {}
MC/DC0%0/1
$target_tables //= {}
$target_tables
Truth table50%1/2
A
A= $target_tables
| |||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||
| 193 | 59 | ▶ | my $outside = sub { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||
| 194 | 59 | my ($info) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 195 | 59 | my $key = DBIO::PostgreSQL::Introspect->qualified_key($info->{remote_schema}, $info->{remote_table}); | |||||||||||||||||||||||||||||||||||||||||||
| 196 | 59 | ▶ | return !exists $source_tables->{$key} && !exists $target_tables->{$key}; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
exists $source_tables->{$key} || exists $target_tables->{$key}
MC/DC100%2/2
exists $source_tables->{$key} || exists $target_tables->{$key}
exists $source_tables->{$key}
exists $target_tables->{$key}
Truth table100%3/3
A || B
A= exists $source_tables->{$key}B= exists $target_tables->{$key}
| |||||||||||||||||||||||||||||||||||||||||||||
| 197 | 69 | }; | |||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||||
| 199 | 69 | my (%source, %target, @kept); | |||||||||||||||||||||||||||||||||||||||||||
| 200 | 69 | for my $table_key (sort keys %$source) { | |||||||||||||||||||||||||||||||||||||||||||
| 201 | 62 | my @in; | |||||||||||||||||||||||||||||||||||||||||||
| 202 | 62 | for my $info (@{ $source->{$table_key} }) { | |||||||||||||||||||||||||||||||||||||||||||
| 203 | my $redefined = grep { $_->{constraint_name} eq $info->{constraint_name} | ||||||||||||||||||||||||||||||||||||||||||||
| 204 | 59 | ▶ | && ($_->{remote_schema} // "") . "\0" . $_->{remote_table} ne ($info->{remote_schema} // "") . "\0" . $info->{remote_table} } @{ $target->{$table_key} // [] }; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition66%6/9
$_->{'constraint_name'} eq $info->{'constraint_name'} && ($_->{'remote_schema'} // '') . "\000" . $_->{'remote_table'} ne ($info->{'remote_schema'} // '') . "\000" . $info->{'remote_table'}
$_->{"remote_schema"} // ""
$info->{"remote_schema"} // ""
$target->{$table_key} // []
MC/DC0%0/2
$_->{'constraint_name'} eq $info->{'constraint_name'} && ($_->{'remote_schema'} // '') . "\000" . $_->{'remote_table'} ne ($info->{'remote_schema'} // '') . "\000" . $info->{'remote_table'}
$_->{'constraint_name'} eq $info->{'constraint_name'}
$_->{"remote_schema"}
MC/DC0%0/1
$info->{"remote_schema"} // ""
$info->{"remote_schema"}
MC/DC100%1/1
$target->{$table_key} // []
$target->{$table_key}
Truth table33%1/3
A && B
A= $_->{'constraint_name'} eq $info->{'constraint_name'}B= $_->{"remote_schema"}
Truth table50%1/2
A
A= $info->{"remote_schema"}
Truth table100%2/2
A
A= $target->{$table_key}
| |||||||||||||||||||||||||||||||||||||||||||||
| 205 | 59 | ▶ | if ($outside->($info) && !$redefined) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
&$outside($info) and not $redefined
MC/DC100%2/2
&$outside($info) and not $redefined
&$outside($info)
not $redefined
Truth table100%3/3
A and not(B)
A= &$outside($info)B= not $redefined
| |||||||||||||||||||||||||||||||||||||||||||||
| 206 | # A table the target lacks goes with its keys; nothing is kept there. | ||||||||||||||||||||||||||||||||||||||||||||
| 207 | 3 | ▶ | next unless exists $target_tables->{$table_key}; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 208 | my ($schema, $table) = DBIO::PostgreSQL::Introspect->key_names( | ||||||||||||||||||||||||||||||||||||||||||||
| 209 | 3 | $table_key, table_name => $source_tables->{$table_key}, $target_tables->{$table_key}); | |||||||||||||||||||||||||||||||||||||||||||
| 210 | push @kept, $class->new( | ||||||||||||||||||||||||||||||||||||||||||||
| 211 | action => 'kept', table_key => $table_key, schema_name => $schema, table_name => $table, | ||||||||||||||||||||||||||||||||||||||||||||
| 212 | 3 | constraint_name => $info->{constraint_name}, fk_info => $info); | |||||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 214 | 56 | else { push @in, $info } | |||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 216 | 62 | $source{$table_key} = \@in; | |||||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 218 | # The target's key of the same name is not compared with the one kept. A key | ||||||||||||||||||||||||||||||||||||||||||||
| 219 | # only the target has is created, wherever it points. | ||||||||||||||||||||||||||||||||||||||||||||
| 220 | 69 | my %kept = map { ($_->table_key . "\0" . $_->constraint_name) => 1 } @kept; | |||||||||||||||||||||||||||||||||||||||||||
| 221 | 69 | for my $table_key (keys %$target) { | |||||||||||||||||||||||||||||||||||||||||||
| 222 | 66 | $target{$table_key} = [ grep { !$kept{ $table_key . "\0" . $_->{constraint_name} } } @{ $target->{$table_key} } ]; | |||||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 224 | 69 | return (\%source, \%target, @kept); | |||||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||
| 228 | 8 | ▶ | sub with_validation { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
with_validation: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 229 | 8 | my ($class, @ops) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 230 | 8 | my @validate; | |||||||||||||||||||||||||||||||||||||||||||
| 231 | 8 | for my $op (@ops) { | |||||||||||||||||||||||||||||||||||||||||||
| 232 | 10 | ▶ | next unless $op->action eq 'create' && $op->existing_table; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$op->action eq "create" and $op->existing_table
MC/DC100%2/2
$op->action eq "create" and $op->existing_table
$op->action eq "create"
$op->existing_table
Truth table100%3/3
A and B
A= $op->action eq "create"B= $op->existing_table
| |||||||||||||||||||||||||||||||||||||||||||||
| 233 | 7 | $op->not_valid(1); | |||||||||||||||||||||||||||||||||||||||||||
| 234 | 7 | push @validate, $class->new( | |||||||||||||||||||||||||||||||||||||||||||
| 235 | action => 'validate', table_key => $op->table_key, schema_name => $op->schema_name, | ||||||||||||||||||||||||||||||||||||||||||||
| 236 | table_name => $op->table_name, constraint_name => $op->constraint_name, fk_info => $op->fk_info); | ||||||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 238 | 8 | return (@ops, @validate); | |||||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||
| 242 | 74 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 243 | 74 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 244 | 74 | my $table = quote_qualified( | |||||||||||||||||||||||||||||||||||||||||||
| 245 | DBIO::PostgreSQL::Introspect->key_names($self->table_key, table_name => $self)); | ||||||||||||||||||||||||||||||||||||||||||||
| 246 | 74 | my $name = quote_ident($self->constraint_name); | |||||||||||||||||||||||||||||||||||||||||||
| 247 | 74 | my $action = $self->action; | |||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||
| 249 | 74 | ▶ | if ($action eq 'drop') { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||
| 250 | 27 | return sprintf 'ALTER TABLE %s DROP CONSTRAINT %s;', $table, $name; | |||||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 252 | elsif ($action eq 'validate') { | ||||||||||||||||||||||||||||||||||||||||||||
| 253 | 10 | return sprintf 'ALTER TABLE %s VALIDATE CONSTRAINT %s;', $table, $name; | |||||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 255 | elsif ($action eq 'create') { | ||||||||||||||||||||||||||||||||||||||||||||
| 256 | 37 | my $info = $self->fk_info; | |||||||||||||||||||||||||||||||||||||||||||
| 257 | my $sql = sprintf 'ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)', | ||||||||||||||||||||||||||||||||||||||||||||
| 258 | $table, $name, | ||||||||||||||||||||||||||||||||||||||||||||
| 259 | 38 | join(', ', map { quote_ident($_) } @{ $info->{local_columns} }), | |||||||||||||||||||||||||||||||||||||||||||
| 260 | quote_qualified($info->{remote_schema}, $info->{remote_table}), | ||||||||||||||||||||||||||||||||||||||||||||
| 261 | 37 | join(', ', map { quote_ident($_) } @{ $info->{remote_columns} }); | |||||||||||||||||||||||||||||||||||||||||||
| 262 | 37 | ▶ | $sql .= ' MATCH ' . _match($info->{match}) unless _match($info->{match}) eq 'SIMPLE'; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 263 | 37 | for my $event (qw( delete update )) { | |||||||||||||||||||||||||||||||||||||||||||
| 264 | 74 | my $word = _action($info->{"on_$event"}); | |||||||||||||||||||||||||||||||||||||||||||
| 265 | 74 | ▶ | next if $word eq 'NO ACTION'; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 266 | 21 | $sql .= sprintf ' ON %s %s', uc $event, $word; | |||||||||||||||||||||||||||||||||||||||||||
| 267 | 21 | ▶ | my @cols = @{ $info->{"on_${event}_columns"} // [] }; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$$info{"on_${event}_columns"} // []
MC/DC100%1/1
$$info{"on_${event}_columns"} // []
$$info{"on_${event}_columns"}
Truth table100%2/2
A
A= $$info{"on_${event}_columns"}
| |||||||||||||||||||||||||||||||||||||||||||||
| 268 | 21 | ▶ | $sql .= ' (' . join(', ', map { quote_ident($_) } @cols) . ')' if @cols; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 270 | 37 | $sql .= deferrable_clause(_timing($info)); | |||||||||||||||||||||||||||||||||||||||||||
| 271 | 37 | ▶ | $sql .= ' NOT VALID' if $self->not_valid; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 272 | 37 | return "$sql;"; | |||||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | DBIO::PostgreSQL::Diff::NoStatement->throw($self, 'foreign key ' . $self->constraint_name); | |||||||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||
| 278 | 21 | ▶ | sub summary { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
summary: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 279 | 21 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 280 | 21 | ▶ | my $info = $self->fk_info // {}; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->fk_info // {}
MC/DC0%0/1
$self->fk_info // {}
$self->fk_info
Truth table50%1/2
A
A= $self->fk_info
| |||||||||||||||||||||||||||||||||||||||||||||
| 281 | 21 | my $on = $self->table_key; | |||||||||||||||||||||||||||||||||||||||||||
| 282 | 21 | my $to = DBIO::PostgreSQL::Introspect->qualified_key($info->{remote_schema}, $info->{remote_table}); | |||||||||||||||||||||||||||||||||||||||||||
| 283 | 21 | my $action = $self->action; | |||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||
| 285 | 21 | ▶ | return sprintf ' ~fk: %s (on %s) validate', $self->constraint_name, $on if $action eq 'validate'; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 286 | 18 | ▶ | return sprintf ' =fk: %s (on %s -> %s) -- kept (target outside the deploy)', $self->constraint_name, $on, $to | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 287 | if $action eq 'kept'; | ||||||||||||||||||||||||||||||||||||||||||||
| 288 | 15 | ▶ | my $head = sprintf ' %s %s (on %s -> %s)', ($action eq 'create' ? '+fk:' : '-fk:'), $self->constraint_name, $on, $to; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 289 | 15 | ▶ | return $head . ($self->not_valid ? ' NOT VALID' : '') if $action eq 'create'; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||
| 290 | 11 | ▶ | return $head . ' -- dropped ahead of its table' if $self->ahead_of_table; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 291 | 7 | ▶ | return $head . ($self->replaced ? ' -- changed, created again' : ' -- not declared by the schema, dropped'); | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||||
| 295 | 104 | 1; | |||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||||
| 297 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||