| 1 | package DBIO::Diff::Op; | ||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Base class for DBIO driver diff operation objects | ||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||
| 7 | 6 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use base 'Class::Accessor::Grouped'; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||
| 13 | 6 | __PACKAGE__->mk_group_accessors(simple => 'action'); | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||
| 16 | 53 | ▶ | sub new { | ||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 17 | 53 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||
| 18 | 53 | return bless { %args }, $class; | |||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||
| 22 | 4 | ▶ | sub mk_diff_accessors { | ||||||||||||||||||||||||||||
|
Subroutine
mk_diff_accessors: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 23 | 4 | my ($class, @names) = @_; | |||||||||||||||||||||||||||||
| 24 | 4 | $class->mk_group_accessors(simple => @names); | |||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||
| 27 | 6 | my %DEFAULT_PREFIX = ( | |||||||||||||||||||||||||||||
| 28 | create => '+', add => '+', | ||||||||||||||||||||||||||||||
| 29 | drop => '-', | ||||||||||||||||||||||||||||||
| 30 | alter => '~', modify => '~', change => '~', | ||||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||
| 34 | 6 | ▶ | sub summary_prefix { | ||||||||||||||||||||||||||||
|
Subroutine
summary_prefix: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 35 | 6 | my ($self, %map) = @_; | |||||||||||||||||||||||||||||
| 36 | 6 | ▶ | my $action = $self->action // ''; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$self->action // ""
MC/DC0%0/1
$self->action // ""
$self->action
Truth table50%1/2
A
A= $self->action
| |||||||||||||||||||||||||||||||
| 37 | 6 | ▶ | return $map{$action} // $DEFAULT_PREFIX{$action} // '~'; | ||||||||||||||||||||||||||||
|
Condition100%5/5
($map{$action} // $DEFAULT_PREFIX{$action}) // "~"
$map{$action} // $DEFAULT_PREFIX{$action}
MC/DC100%2/2
($map{$action} // $DEFAULT_PREFIX{$action}) // "~"
$map{$action}
$DEFAULT_PREFIX{$action}
Truth table100%3/3
A // B
A= $map{$action}B= $DEFAULT_PREFIX{$action}
| |||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||
| 41 | # Internal: stable topological order of @names; a name comes after the names | ||||||||||||||||||||||||||||||
| 42 | # it depends on. Names caught in a cycle follow alphabetically at the end. | ||||||||||||||||||||||||||||||
| 43 | 18 | ▶ | sub _order_by_dependency { | ||||||||||||||||||||||||||||
|
Subroutine
_order_by_dependency: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||||
| 44 | 18 | my ($depends, @names) = @_; | |||||||||||||||||||||||||||||
| 45 | 18 | @names = sort @names; | |||||||||||||||||||||||||||||
| 46 | 18 | ▶ | return @names unless $depends; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||
| 48 | 12 | my %in = map { $_ => 1 } @names; | |||||||||||||||||||||||||||||
| 49 | 12 | my %deps; | |||||||||||||||||||||||||||||
| 50 | 12 | for my $n (@names) { | |||||||||||||||||||||||||||||
| 51 | 21 | ▶ | my $d = ref($depends) eq 'CODE' ? $depends->($n) : $depends->{$n}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 52 | 21 | ▶ | $deps{$n} = [ grep { $_ ne $n && $in{$_} } @{ $d || [] } ]; | ||||||||||||||||||||||||||||
|
Condition80%4/5
$_ ne $n && $in{$_}
$d || []
MC/DC50%1/2
$_ ne $n && $in{$_}
$_ ne $n
$in{$_}
MC/DC100%1/1
$d || []
$d
Truth table66%2/3
A && B
A= $_ ne $nB= $in{$_}
Truth table100%2/2
A
A= $d
| |||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||
| 55 | 12 | my (@ordered, %done); | |||||||||||||||||||||||||||||
| 56 | 12 | my @rest = @names; | |||||||||||||||||||||||||||||
| 57 | 12 | ▶ | while (@rest) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 58 | 19 | my $at; | |||||||||||||||||||||||||||||
| 59 | 19 | for my $i (0 .. $#rest) { | |||||||||||||||||||||||||||||
| 60 | 32 | ▶ | next if grep { !$done{$_} } @{ $deps{ $rest[$i] } }; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 61 | 18 | $at = $i; | |||||||||||||||||||||||||||||
| 62 | 18 | last; | |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||
| 64 | 19 | ▶ | last unless defined $at; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 65 | 18 | my ($n) = splice @rest, $at, 1; | |||||||||||||||||||||||||||||
| 66 | 18 | $done{$n} = 1; | |||||||||||||||||||||||||||||
| 67 | 18 | push @ordered, $n; | |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||
| 69 | 12 | return (@ordered, @rest); | |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||
| 72 | 9 | ▶ | sub diff_toplevel { | ||||||||||||||||||||||||||||
|
Subroutine
diff_toplevel: called CC=9 SCAR=22.2
Pod
documented
| |||||||||||||||||||||||||||||||
| 73 | 9 | my ($class, $source, $target, %cb) = @_; | |||||||||||||||||||||||||||||
| 74 | 9 | ▶ | $source ||= {}; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$source ||= {}
MC/DC0%0/1
$source ||= {}
$source
Truth table50%1/2
A
A= $source
| |||||||||||||||||||||||||||||||
| 75 | 9 | ▶ | $target ||= {}; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$target ||= {}
MC/DC0%0/1
$target ||= {}
$target
Truth table50%1/2
A
A= $target
| |||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||
| 77 | 9 | my @ops; | |||||||||||||||||||||||||||||
| 78 | my @create = _order_by_dependency($cb{depends}, | ||||||||||||||||||||||||||||||
| 79 | 9 | grep { !exists $source->{$_} } keys %$target); | |||||||||||||||||||||||||||||
| 80 | 9 | for my $name (@create) { | |||||||||||||||||||||||||||||
| 81 | 18 | ▶ | push @ops, $cb{create}->($name) if $cb{create}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||
| 84 | 9 | ▶ | my $drop_depends = $cb{source_depends} || $cb{depends}; | ||||||||||||||||||||||||||||
|
Condition100%3/3
$cb{'source_depends'} || $cb{'depends'}
MC/DC100%2/2
$cb{'source_depends'} || $cb{'depends'}
$cb{'source_depends'}
$cb{'depends'}
Truth table100%3/3
A || B
A= $cb{'source_depends'}B= $cb{'depends'}
| |||||||||||||||||||||||||||||||
| 85 | my @drop = _order_by_dependency($drop_depends, | ||||||||||||||||||||||||||||||
| 86 | 9 | grep { !exists $target->{$_} } keys %$source); | |||||||||||||||||||||||||||||
| 87 | 9 | ▶ | @drop = reverse @drop if $drop_depends; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 88 | 9 | for my $name (@drop) { | |||||||||||||||||||||||||||||
| 89 | 13 | ▶ | push @ops, $cb{drop}->($name) if $cb{drop}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||
| 91 | 9 | return @ops; | |||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||
| 94 | # Internal: normalize a per-table member collection into { name => $member }. | ||||||||||||||||||||||||||||||
| 95 | # A collection that is already a hashref is returned as-is; an arrayref is | ||||||||||||||||||||||||||||||
| 96 | # keyed by the field named in $index_by. | ||||||||||||||||||||||||||||||
| 97 | 20 | ▶ | sub _members_by_key { | ||||||||||||||||||||||||||||
|
Subroutine
_members_by_key: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||
| 98 | 20 | my ($members, $index_by) = @_; | |||||||||||||||||||||||||||||
| 99 | 20 | ▶ | return {} unless defined $members; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 100 | 19 | ▶ | if (ref($members) eq 'HASH') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 101 | 11 | return $members; | |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||
| 103 | 8 | my %by; | |||||||||||||||||||||||||||||
| 104 | 8 | $by{ $_->{$index_by} } = $_ for @{ $members }; | |||||||||||||||||||||||||||||
| 105 | 8 | return \%by; | |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||
| 109 | 9 | ▶ | sub diff_nested { | ||||||||||||||||||||||||||||
|
Subroutine
diff_nested: called CC=32 SCAR=35.5
Pod
documented
| |||||||||||||||||||||||||||||||
| 110 | 9 | my ($class, $source, $target, %opt) = @_; | |||||||||||||||||||||||||||||
| 111 | 9 | ▶ | $source ||= {}; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$source ||= {}
MC/DC0%0/1
$source ||= {}
$source
Truth table50%1/2
A
A= $source
| |||||||||||||||||||||||||||||||
| 112 | 9 | ▶ | $target ||= {}; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$target ||= {}
MC/DC0%0/1
$target ||= {}
$target
Truth table50%1/2
A
A= $target
| |||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||
| 114 | 9 | my $index_by = $opt{index_by}; | |||||||||||||||||||||||||||||
| 115 | 9 | ▶ | my $scope = $opt{scope} || 'both'; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$opt{'scope'} || 'both'
MC/DC0%0/1
$opt{'scope'} || 'both'
$opt{'scope'}
Truth table50%1/2
A
A= $opt{'scope'}
| |||||||||||||||||||||||||||||||
| 116 | 9 | my $skip = $opt{skip}; | |||||||||||||||||||||||||||||
| 117 | 9 | my $changed = $opt{changed_when}; | |||||||||||||||||||||||||||||
| 118 | 9 | ▶ | my $src_tables = $opt{source_tables} || $source; | ||||||||||||||||||||||||||||
|
Condition66%2/3
$opt{'source_tables'} || $source
MC/DC0%0/2
$opt{'source_tables'} || $source
$opt{'source_tables'}
$source
Truth table66%2/3
A || B
A= $opt{'source_tables'}B= $source
| |||||||||||||||||||||||||||||||
| 119 | 9 | ▶ | my $tgt_tables = $opt{target_tables} || $target; | ||||||||||||||||||||||||||||
|
Condition66%2/3
$opt{'target_tables'} || $target
MC/DC0%0/2
$opt{'target_tables'} || $target
$opt{'target_tables'}
$target
Truth table66%2/3
A || B
A= $opt{'target_tables'}B= $target
| |||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||
| 121 | 9 | my @ops; | |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||
| 123 | # Under 'both' the tables of the source count too: a table that lost its | ||||||||||||||||||||||||||||||
| 124 | # last member is missing from the target hash, but its on_gone is due. | ||||||||||||||||||||||||||||||
| 125 | 9 | my %walk = map { $_ => 1 } keys %$target; | |||||||||||||||||||||||||||||
| 126 | 9 | ▶ | if ($scope eq 'both') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 127 | 7 | $walk{$_} = 1 for keys %$source; | |||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||
| 130 | 9 | for my $table (sort keys %walk) { | |||||||||||||||||||||||||||||
| 131 | 12 | ▶ | if ($scope eq 'both') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 132 | 10 | ▶ | next unless exists $src_tables->{$table} && exists $tgt_tables->{$table}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $src_tables->{$table} and exists $tgt_tables->{$table}
MC/DC100%2/2
exists $src_tables->{$table} and exists $tgt_tables->{$table}
exists $src_tables->{$table}
exists $tgt_tables->{$table}
Truth table100%3/3
A and B
A= exists $src_tables->{$table}B= exists $tgt_tables->{$table}
| |||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||
| 135 | 9 | my $src = _members_by_key($source->{$table}, $index_by); | |||||||||||||||||||||||||||||
| 136 | 9 | my $tgt = _members_by_key($target->{$table}, $index_by); | |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||
| 138 | 9 | for my $name (sort keys %$tgt) { | |||||||||||||||||||||||||||||
| 139 | 11 | my $new = $tgt->{$name}; | |||||||||||||||||||||||||||||
| 140 | 11 | ▶ | next if $skip && $skip->($new); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$skip and &$skip($new)
MC/DC0%0/2
$skip and &$skip($new)
$skip
&$skip($new)
Truth table66%2/3
A and B
A= $skipB= &$skip($new)
| |||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||
| 142 | 11 | ▶ | if (!exists $src->{$name}) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 143 | 2 | ▶ | push @ops, $opt{on_new}->($table, $name, $new) if $opt{on_new}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 144 | 2 | next; | |||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||
| 147 | 9 | my $old = $src->{$name}; | |||||||||||||||||||||||||||||
| 148 | 9 | ▶ | if (!$changed || $changed->($old, $new)) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $changed or &$changed($old, $new)
MC/DC50%1/2
not $changed or &$changed($old, $new)
not $changed
&$changed($old, $new)
Truth table66%2/3
not(A) or B
A= not $changedB= &$changed($old, $new)
| |||||||||||||||||||||||||||||||
| 149 | 5 | ▶ | push @ops, $opt{on_changed}->($table, $name, $old, $new) if $opt{on_changed}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||
| 153 | 9 | for my $name (sort keys %$src) { | |||||||||||||||||||||||||||||
| 154 | 15 | my $old = $src->{$name}; | |||||||||||||||||||||||||||||
| 155 | 15 | ▶ | next if $skip && $skip->($old); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$skip and &$skip($old)
MC/DC100%2/2
$skip and &$skip($old)
$skip
&$skip($old)
Truth table100%3/3
A and B
A= $skipB= &$skip($old)
| |||||||||||||||||||||||||||||||
| 156 | 14 | ▶ | next if exists $tgt->{$name}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 157 | 5 | ▶ | push @ops, $opt{on_gone}->($table, $name, $old) if $opt{on_gone}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||
| 161 | 9 | ▶ | if ($scope eq 'all') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 162 | 2 | for my $table (sort keys %$source) { | |||||||||||||||||||||||||||||
| 163 | 4 | ▶ | next if exists $target->{$table}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 164 | 2 | my $src = _members_by_key($source->{$table}, $index_by); | |||||||||||||||||||||||||||||
| 165 | 2 | for my $name (sort keys %$src) { | |||||||||||||||||||||||||||||
| 166 | 2 | my $old = $src->{$name}; | |||||||||||||||||||||||||||||
| 167 | 2 | ▶ | next if $skip && $skip->($old); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$skip and &$skip($old)
MC/DC0%0/2
$skip and &$skip($old)
$skip
&$skip($old)
Truth table66%2/3
A and B
A= $skipB= &$skip($old)
| |||||||||||||||||||||||||||||||
| 168 | 2 | ▶ | push @ops, $opt{on_gone}->($table, $name, $old) if $opt{on_gone}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||
| 173 | 9 | return @ops; | |||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||
| 177 | 9 | ▶ | sub should_emit_if_exists { | ||||||||||||||||||||||||||||
|
Subroutine
should_emit_if_exists: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||
| 178 | 9 | my ($storage) = @_; | |||||||||||||||||||||||||||||
| 179 | 9 | ▶ | return 0 unless $storage; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 180 | 8 | ▶ | return 0 unless Scalar::Util::blessed($storage) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($storage) and $storage->can("_use_supports_if_exists")
MC/DC100%2/2
builtin::blessed($storage) and $storage->can("_use_supports_if_exists")
builtin::blessed($storage)
$storage->can("_use_supports_if_exists")
Truth table100%3/3
A and B
A= builtin::blessed($storage)B= $storage->can("_use_supports_if_exists")
| |||||||||||||||||||||||||||||||
| 181 | && $storage->can('_use_supports_if_exists'); | ||||||||||||||||||||||||||||||
| 182 | 6 | ▶ | return $storage->_use_supports_if_exists ? 1 : 0; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||
| 185 | 6 | 1; | |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||
| 187 | __END__ | ||||||||||||||||||||||||||||||