| 1 | package DBIx::Class::Schema::Diff::Source; | ||||||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | 4 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 4 | with 'DBIx::Class::Schema::Diff::Role::Common'; | |||||||||||||
| 7 | |||||||||||||||
| 8 | 4 | ▶ | use Types::Standard qw(:all); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 4 | ▶ | use Try::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 4 | ▶ | use List::MoreUtils qw(uniq); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 4 | ▶ | use DBIx::Class::Schema::Diff::InfoPacket; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 4 | has 'name', required => 1, is => 'ro', isa => Str; | |||||||||||||
| 15 | 4 | has 'old_source', required => 1, is => 'ro', isa => Maybe[HashRef]; | |||||||||||||
| 16 | 4 | has 'new_source', required => 1, is => 'ro', isa => Maybe[HashRef]; | |||||||||||||
| 17 | |||||||||||||||
| 18 | 4 | has '_schema_diff', required => 1, is => 'ro', isa => InstanceOf[ | |||||||||||||
| 19 | 'DBIx::Class::Schema::Diff::Schema' | ||||||||||||||
| 20 | ]; | ||||||||||||||
| 21 | |||||||||||||||
| 22 | has 'added', is => 'ro', lazy => 1, default => sub { | ||||||||||||||
| 23 | 517 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||
| 24 | 517 | ▶ | defined $self->new_source && ! defined $self->old_source | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 25 | 4 | }, init_arg => undef, isa => Bool; | |||||||||||||
| 26 | |||||||||||||||
| 27 | has 'deleted', is => 'ro', lazy => 1, default => sub { | ||||||||||||||
| 28 | 506 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||
| 29 | 506 | ▶ | defined $self->old_source && ! defined $self->new_source | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 30 | 4 | }, init_arg => undef, isa => Bool; | |||||||||||||
| 31 | |||||||||||||||
| 32 | |||||||||||||||
| 33 | has 'columns', is => 'ro', lazy => 1, default => sub { | ||||||||||||||
| 34 | 495 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||
| 35 | |||||||||||||||
| 36 | 495 | my ($o,$n) = ($self->old_source,$self->new_source); | |||||||||||||
| 37 | |||||||||||||||
| 38 | # List of all columns in old, new, or both: | ||||||||||||||
| 39 | 495 | ▶ | my @columns = uniq(try{keys %{$o->{columns}}}, try{keys %{$n->{columns}}}); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 40 | |||||||||||||||
| 41 | return { | ||||||||||||||
| 42 | 495 | map { $_ => DBIx::Class::Schema::Diff::InfoPacket->new( | |||||||||||||
| 43 | name => $_, | ||||||||||||||
| 44 | old_info => $o ? $o->{columns}{$_} : undef, | ||||||||||||||
| 45 | 2862 | ▶ | new_info => $n ? $n->{columns}{$_} : undef, | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 46 | _source_diff => $self, | ||||||||||||||
| 47 | ) } @columns | ||||||||||||||
| 48 | }; | ||||||||||||||
| 49 | |||||||||||||||
| 50 | 4 | }, init_arg => undef, isa => HashRef; | |||||||||||||
| 51 | |||||||||||||||
| 52 | |||||||||||||||
| 53 | has 'relationships', is => 'ro', lazy => 1, default => sub { | ||||||||||||||
| 54 | 495 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||
| 55 | |||||||||||||||
| 56 | 495 | my ($o,$n) = ($self->old_source,$self->new_source); | |||||||||||||
| 57 | |||||||||||||||
| 58 | # List of all relationships in old, new, or both: | ||||||||||||||
| 59 | 495 | ▶ | my @rels = uniq(try{keys %{$o->{relationships}}}, try{keys %{$n->{relationships}}}); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 60 | |||||||||||||||
| 61 | return { | ||||||||||||||
| 62 | 495 | map { $_ => DBIx::Class::Schema::Diff::InfoPacket->new( | |||||||||||||
| 63 | name => $_, | ||||||||||||||
| 64 | old_info => $o ? $o->{relationships}{$_} : undef, | ||||||||||||||
| 65 | 957 | ▶ | new_info => $n ? $n->{relationships}{$_} : undef, | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 66 | _source_diff => $self, | ||||||||||||||
| 67 | ) } @rels | ||||||||||||||
| 68 | }; | ||||||||||||||
| 69 | |||||||||||||||
| 70 | 4 | }, init_arg => undef, isa => HashRef; | |||||||||||||
| 71 | |||||||||||||||
| 72 | |||||||||||||||
| 73 | has 'constraints', is => 'ro', lazy => 1, default => sub { | ||||||||||||||
| 74 | 495 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||
| 75 | |||||||||||||||
| 76 | 495 | my ($o,$n) = ($self->old_source,$self->new_source); | |||||||||||||
| 77 | |||||||||||||||
| 78 | # List of all unique_constraint_names in old, new, or both: | ||||||||||||||
| 79 | 495 | ▶ | my @consts = uniq(try{keys %{$o->{constraints}}}, try{keys %{$n->{constraints}}}); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 80 | |||||||||||||||
| 81 | return { | ||||||||||||||
| 82 | 495 | map { $_ => DBIx::Class::Schema::Diff::InfoPacket->new( | |||||||||||||
| 83 | name => $_, | ||||||||||||||
| 84 | old_info => $o ? $o->{constraints}{$_} : undef, | ||||||||||||||
| 85 | 418 | ▶ | new_info => $n ? $n->{constraints}{$_} : undef, | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 86 | _source_diff => $self, | ||||||||||||||
| 87 | ) } @consts | ||||||||||||||
| 88 | }; | ||||||||||||||
| 89 | |||||||||||||||
| 90 | 4 | }, init_arg => undef, isa => HashRef; | |||||||||||||
| 91 | |||||||||||||||
| 92 | |||||||||||||||
| 93 | has 'isa_diff', is => 'ro', lazy => 1, default => sub { | ||||||||||||||
| 94 | 495 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||
| 95 | |||||||||||||||
| 96 | 495 | my ($o,$n) = ($self->old_source,$self->new_source); | |||||||||||||
| 97 | |||||||||||||||
| 98 | 495 | ▶ | my $o_isa = $o ? $o->{isa} : []; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 99 | 495 | ▶ | my $n_isa = $n ? $n->{isa} : []; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 100 | |||||||||||||||
| 101 | 495 | my $AD = Array::Diff->diff($o_isa,$n_isa); | |||||||||||||
| 102 | my $diff = [ | ||||||||||||||
| 103 | 1 | (map {'-'.$_} @{$AD->deleted}), | |||||||||||||
| 104 | 495 | (map {'+'.$_} @{$AD->added}) | |||||||||||||
| 105 | ]; | ||||||||||||||
| 106 | |||||||||||||||
| 107 | 495 | ▶ | return scalar(@$diff) > 0 ? $diff : undef; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 108 | |||||||||||||||
| 109 | 4 | }, init_arg => undef, isa => Maybe[ArrayRef]; | |||||||||||||
| 110 | |||||||||||||||
| 111 | |||||||||||||||
| 112 | |||||||||||||||
| 113 | has 'diff', is => 'ro', lazy => 1, default => sub { | ||||||||||||||
| 114 | 517 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
__ANON__: called CC=14 SCAR=26.4
| |||||||||||||||
| 115 | |||||||||||||||
| 116 | # There is no reason to diff in the case of added/deleted: | ||||||||||||||
| 117 | 517 | ▶ | return { _event => 'added' } if ($self->added); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 118 | 506 | ▶ | return { _event => 'deleted' } if ($self->deleted); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 119 | |||||||||||||||
| 120 | 495 | my $diff = {}; | |||||||||||||
| 121 | |||||||||||||||
| 122 | $diff->{columns} = { map { | ||||||||||||||
| 123 | 2862 | ▶ | $_->diff ? ($_->name => $_->diff) : () | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 124 | 495 | } values %{$self->columns} }; | |||||||||||||
| 125 | 495 | ▶ | delete $diff->{columns} unless (keys %{$diff->{columns}} > 0); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 126 | |||||||||||||||
| 127 | $diff->{relationships} = { map { | ||||||||||||||
| 128 | 957 | ▶ | $_->diff ? ($_->name => $_->diff) : () | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 129 | 495 | } values %{$self->relationships} }; | |||||||||||||
| 130 | 495 | ▶ | delete $diff->{relationships} unless (keys %{$diff->{relationships}} > 0); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 131 | |||||||||||||||
| 132 | $diff->{constraints} = { map { | ||||||||||||||
| 133 | 418 | ▶ | $_->diff ? ($_->name => $_->diff) : () | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 134 | 495 | } values %{$self->constraints} }; | |||||||||||||
| 135 | 495 | ▶ | delete $diff->{constraints} unless (keys %{$diff->{constraints}} > 0); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 136 | |||||||||||||||
| 137 | 495 | ▶ | my $o_tbl = try{$self->old_source->{table_name}} || ''; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Condition50%1/2
&try(sub {
$self->old_source->{'table_name'};
}
) || ''
MC/DC0%0/1
&try(sub {
$self->old_source->{'table_name'};
}
) || ''
&try(sub {
$self->old_source->{'table_name'};
}
)
Truth table50%1/2
A
A= &try(sub {
$self->old_source->{'table_name'};
}
)
| |||||||||||||||
| 138 | 495 | ▶ | my $n_tbl = try{$self->new_source->{table_name}} || ''; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Condition50%1/2
&try(sub {
$self->new_source->{'table_name'};
}
) || ''
MC/DC0%0/1
&try(sub {
$self->new_source->{'table_name'};
}
) || ''
&try(sub {
$self->new_source->{'table_name'};
}
)
Truth table50%1/2
A
A= &try(sub {
$self->new_source->{'table_name'};
}
)
| |||||||||||||||
| 139 | 495 | ▶ | $diff->{table_name} = $n_tbl unless ($o_tbl eq $n_tbl); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 140 | |||||||||||||||
| 141 | 495 | ▶ | $diff->{isa} = $self->isa_diff if ($self->isa_diff); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 142 | |||||||||||||||
| 143 | # TODO: other data points TDB | ||||||||||||||
| 144 | # ... | ||||||||||||||
| 145 | |||||||||||||||
| 146 | # No changes: | ||||||||||||||
| 147 | 495 | ▶ | return undef unless (keys %$diff > 0); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 148 | |||||||||||||||
| 149 | 69 | $diff->{_event} = 'changed'; | |||||||||||||
| 150 | 69 | return $diff; | |||||||||||||
| 151 | |||||||||||||||
| 152 | 4 | }, init_arg => undef, isa => Maybe[HashRef]; | |||||||||||||
| 153 | |||||||||||||||
| 154 | |||||||||||||||
| 155 | 4 | 1; | |||||||||||||
| 156 | |||||||||||||||
| 157 | |||||||||||||||
| 158 | __END__ | ||||||||||||||