File Coverage

blib/lib/DBIx/Class/Schema/Diff/Schema.pm
Criterion Covered Total %
statement 33 34 97.0
branch 2 4 50.0
condition n/a
subroutine 12 13 92.3
pod 0 1 0.0
total 47 52 90.3


line stmt bran cond sub pod time code
1             package DBIx::Class::Schema::Diff::Schema;
2 5     5   39 use strict;
  5         17  
  5         162  
3 5     5   41 use warnings;
  5         12  
  5         172  
4              
5             # ABSTRACT: Simple Diffing of DBIC Schemas
6             # VERSION
7              
8             #
9             # TODO (#2)
10             #
11             # The structure/design of this class (+ Source and InfoPacket)
12             # made more sense before adding the 'SchemaData' feature (#1)
13             # when these classes were handling both the data extraction from
14             # DBIC and the diffing tasks of the individual section of data.
15             # Now the data extraction work is done in SchemaData leaving only
16             # simple static hashrefs to be dealt with here. These classes should
17             # probably now be consolidated and generalized. I'm not sure
18             # how important this is though since everything is already working.
19             # Later on, if more types of data need to be diffed besides the
20             # 5 current ones (columns,relationships,constraints,table_name,isa),
21             # or it is useful to make that more dynamic, such as to be able to
22             # add more data-points on the fly, then taking the time for
23             # refactoring these will make more sense... (2014-04-14 by vanstyn)
24             #
25              
26 5     5   43 use Moo;
  5         14  
  5         41  
27             with 'DBIx::Class::Schema::Diff::Role::Common';
28              
29 5     5   2074 use Types::Standard qw(:all);
  5         13  
  5         100  
30 5     5   245965 use Module::Runtime;
  5         14  
  5         63  
31 5     5   308 use Try::Tiny;
  5         16  
  5         382  
32              
33 5     5   3413 use DBIx::Class::Schema::Diff::Source;
  5         21  
  5         215  
34 5     5   2900 use DBIx::Class::Schema::Diff::SchemaData;
  5         26  
  5         2685  
35              
36             has 'new_schema_only', is => 'ro', isa => Bool, default => sub { 0 };
37              
38             has 'old_schema', required => 1, is => 'ro', isa => InstanceOf[
39             'DBIx::Class::Schema::Diff::SchemaData'
40             ], coerce => \&_coerce_schema_data;
41              
42             has 'new_schema', required => 1, is => 'ro', isa => InstanceOf[
43             'DBIx::Class::Schema::Diff::SchemaData'
44             ], coerce => \&_coerce_schema_data;
45              
46              
47             sub all_source_names {
48 53     53 0 142 my $self = shift;
49            
50 53 50       262 return $self->new_schema->sources if ($self->new_schema_only);
51            
52 53         342 my ($o,$n) = ($self->old_schema,$self->new_schema);
53            
54             # List of all sources in old, new, or both:
55 53         268 return uniq($o->sources,$n->sources);
56             }
57              
58             has 'sources', is => 'ro', lazy => 1, default => sub {
59             my $self = shift;
60             return { map { $_ => $self->_new_Diff_Source($_) } $self->all_source_names };
61             }, init_arg => undef, isa => HashRef;
62              
63              
64             has 'diff', is => 'ro', lazy => 1, default => sub {
65             my $self = shift;
66            
67             # TODO: handle added/deleted/changed at this level, too...
68             my $diff = { map {
69             $_->diff ? ($_->name => $_->diff) : ()
70             } values %{$self->sources} };
71            
72             return undef unless (keys %$diff > 0);
73             return $diff;
74            
75             }, init_arg => undef, isa => Maybe[HashRef];
76              
77 0     0   0 sub _schema_diff { (shift) }
78              
79              
80             sub _new_Diff_Source {
81 517     517   931 my $self = shift;
82 517         846 my $name = shift;
83            
84             DBIx::Class::Schema::Diff::Source->new( $self->new_schema_only
85 517     517   19881 ? ( diff_added => 1 ) : ( old_source => scalar try{$self->old_schema->source($name)} ),
86 517     517   27786 new_source => scalar try{$self->new_schema->source($name)},
87 517 50       3485 name => $name, _schema_diff => $self
88             )
89             }
90              
91              
92             1;
93              
94             __END__