File Coverage

blib/lib/DBIx/Class/Schema/Diff/Source.pm
Criterion Covered Total %
statement 23 23 100.0
branch n/a
condition n/a
subroutine 8 8 100.0
pod n/a
total 31 31 100.0


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