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   38 use strict;
  5         12  
  5         177  
3 5     5   29 use warnings;
  5         20  
  5         175  
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   30 use Moo;
  5         15  
  5         41  
27             with 'DBIx::Class::Schema::Diff::Role::Common';
28              
29 5     5   2181 use Types::Standard qw(:all);
  5         13  
  5         88  
30 5     5   242650 use Module::Runtime;
  5         16  
  5         57  
31 5     5   351 use Try::Tiny;
  5         17  
  5         385  
32              
33 5     5   3633 use DBIx::Class::Schema::Diff::Source;
  5         22  
  5         217  
34 5     5   2977 use DBIx::Class::Schema::Diff::SchemaData;
  5         22  
  5         2757  
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 133 my $self = shift;
49            
50 53 50       300 return $self->new_schema->sources if ($self->new_schema_only);
51            
52 53         327 my ($o,$n) = ($self->old_schema,$self->new_schema);
53            
54             # List of all sources in old, new, or both:
55 53         315 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   887 my $self = shift;
82 517         882 my $name = shift;
83            
84             DBIx::Class::Schema::Diff::Source->new( $self->new_schema_only
85 517     517   19504 ? ( diff_added => 1 ) : ( old_source => scalar try{$self->old_schema->source($name)} ),
86 517     517   26121 new_source => scalar try{$self->new_schema->source($name)},
87 517 50       3213 name => $name, _schema_diff => $self
88             )
89             }
90              
91              
92             1;
93              
94             __END__