File Coverage

blib/lib/DBIx/Class/Schema/Diff/Source.pm
Criterion Covered Total %
statement 21 21 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 28 28 100.0


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