Diff/SchemaData.pm

filter: Statementstmt 95.4% 126 / 132 Branchbran 73.8% 31 / 42 Conditioncond 88.8% 8 / 9 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 90.6% 29 / 32 Podpod 57.1% 4 / 7 totaltotal 88.9% 202 / 227 CC 30 SCAR 12.5
CC
30
Cov
90%
_localize_deep_namespace_strings
7.0
__prune_private_col_attrs
6.0
dump_json_file
5.3
CRAP 3.5
« Diff/Source.pm ↑ Summary Diff/InfoPacket.pm »
1 package DBIx::Class::Schema::Diff::SchemaData;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: Data representation of schema for diffing
6  
7 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 with 'DBIx::Class::Schema::Diff::Role::Common';
9  
10 5 ▶use Types::Standard qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Module::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Path::Class qw(file);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Clone 'clone';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Digest::SHA1;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use Data::Dumper::Concise;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 has 'schema', is => 'ro', isa => Maybe[InstanceOf[
22 'DBIx::Class::Schema'
23 9 ▶]], coerce => \&_coerce_schema, default => sub {undef};
Subroutine __ANON__: called CC=1 SCAR=0.0
24  
25 has 'data', is => 'ro', lazy => 1, default => sub {
26 43 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.1
27
28 43 ▶ die "You must supply a schema or an existing data packet"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->schema 0 43 43
29 unless ($self->schema);
30
31 43 return $self->_gen_data( $self->schema );
32  
33 5 }, isa => HashRef, coerce => \&_coerce_deep_unsafe_refs;
34  
35  
36 sub BUILD {
37 52 ▶ my $self = shift;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
38  
39 # initialize:
40 52 $self->data;
41 }
42  
43 sub sources {
44 106 ▶ my $self = shift;
Subroutine sources: called CC=2 SCAR=7.1
Pod undocumented
45 106 ▶ return sort keys %{ $self->data->{sources} || {} };
Branch: 1 branch
BranchTrueFalseTotal
$self->data->{"sources"} or {} 0 106 106
46 }
47  
48 sub source {
49 1034 ▶ my ($self, $name) = @_;
Subroutine source: called CC=1 SCAR=0.0
Pod undocumented
50 1034 return $self->data->{sources}{$name};
51 }
52  
53 sub dump_json {
54 0 ▶ my $self = shift;
Subroutine dump_json: not called CC=1 SCAR=6.9
Pod documented
55 0 JSON::to_json( $self->data => { pretty => 1 });
56 }
57  
58 sub dump_json_file {
59 1 ▶ my ($self, $path) = @_;
Subroutine dump_json_file: called CC=5 SCAR=16.7
Pod documented
60 1 ▶ die "Filename required" unless ($path);
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 1 1
61 1 my $file = file($path)->absolute;
62
63 1 ▶ die "Target file '$file' already exists." if (-e $file);
Branch: 1 branch
BranchTrueFalseTotal
if -e $file 0 1 1
64
65 1 my $out_fh;
66 1 ▶ open $out_fh, '>', $file or die "Could not open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $out_fh, ">", $file or die "Could not open ${file}: $!" 0 1 1
67 1 print $out_fh JSON::to_json( $self->data, { pretty => 1 });
68 1 close $out_fh;
69 1 ▶ return -f $file ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
-f $file ? : 1 0 1
70 }
71  
72  
73 sub _gen_data {
74 43 ▶ my ($self, $schema) = @_;
Subroutine _gen_data: called CC=1 SCAR=0.0
75
76 my $data = {
77 schema_class => (blessed $schema),
78 sources => {
79 map {
80 43 my $Source = $schema->source($_);
81 $_ => {
82
83 columns => {
84 map {
85 5635 $_ => $Source->column_info($_)
86 } $Source->columns
87 },
88
89 relationships => {
90 map {
91 1849 $_ => $Source->relationship_info($_)
92 } $Source->relationships
93 },
94
95 constraints => {
96 map {
97 989 $_ => { columns => [$Source->unique_constraint_columns($_)] }
98 } $Source->unique_constraint_names
99 },
100
101 table_name => $Source->from,
102
103 isa => mro::get_linear_isa( $schema->class( $Source->source_name ) ),
104  
105 }
106 } $schema->sources
107 }
108 };
109
110 43 return $self->_localize_deep_namespace_strings($data,$data->{schema_class});
111 }
112  
113  
114 sub _coerce_schema {
115 52 ▶ my ($v) = @_;
Subroutine _coerce_schema: called CC=4 SCAR=13.9
116 52 ▶ return $v if (!$v || ref $v);
Branch: 1 branch
BranchTrueFalseTotal
if not $v or ref $v 38 14 52
Condition100%3/3
not $v or ref $v
l!l&&r!l&&!r
92914
MC/DC100%2/2
not $v or ref $v
not $v ref $v
Truth table100%3/3
not(A) or B
A= not $vB= ref $v
ABresult
1X1
011
000
117
118 # Its a class name:
119 14 Module::Runtime::require_module($v);
120 14 ▶ return $v->can('connect') ? $v->connect('dbi:SQLite::memory:','','') : $v;
Branch: 1 branch
BranchTrueFalseTotal
$v->can('connect') ? : 14 0 14
121 }
122  
123  
124 sub _coerce_deep_unsafe_refs {
125 105945 ▶ my ($v) = @_;
Subroutine _coerce_deep_unsafe_refs: called CC=5 SCAR=16.1
126 105945 ▶ my $rt = ref($v) or return $v;
Branch: 1 branch
BranchTrueFalseTotal
my $rt = ref $v or return $v 77149 28796 105945
127
128 28796 ▶ if($rt eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if ($rt eq 'HASH') { } 23854 4942 28796
elsif ($rt eq 'ARRAY') { } 2663 2279 4942
elsif ($rt eq 'CODE') { } 1634 645 2279
129 23854 return { map { $_ => &_coerce_deep_unsafe_refs($v->{$_}) } keys %$v };
130 }
131 elsif($rt eq 'ARRAY') {
132 2663 return [ map { &_coerce_deep_unsafe_refs($_) } @$v ];
133 }
134 elsif($rt eq 'CODE') {
135 # TODO: we don't have to do this, we could let it through
136 # to be stringified, but for now, we're not trying to compare
137 # CodeRef contents
138 1634 return 'sub { "DUMMY" }';
139 }
140 else {
141 # For all other refs, stringify them with Dumper. These will still
142 # be just as useful for diff/compare. This makes them safe for JSON, etc
143 645 my $str = Dumper($v);
144 # strip newlines:
145 645 $str =~ s/\r?\n//g;
146 # normalize whitespace:
147 645 $str =~ s/\s+/ /g;
148 645 return $str;
149 }
150 }
151  
152 sub _localize_deep_namespace_strings {
153 88276 ▶ my ($self, $v, $ns) = @_;
Subroutine _localize_deep_namespace_strings: called CC=7 SCAR=19.5
154 88276 my $rt = ref($v);
155 88276 ▶ if($rt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rt) { } 24308 63968 88276
156 24308 ▶ if($rt eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($rt eq 'HASH') { } 19793 4515 24308
elsif ($rt eq 'ARRAY') { } 2236 2279 4515
157 return { map {
158 19793 $_ => $self->_localize_deep_namespace_strings($v->{$_},$ns)
159 } keys %$v };
160 }
161 elsif($rt eq 'ARRAY') {
162 return [ map {
163 2236 $self->_localize_deep_namespace_strings($_,$ns)
164 } @$v ];
165 }
166 else {
167 2279 return $v;
168 }
169 }
170 else {
171 # swap the namespace prefix string for literal '{schema_class}':
172 63968 ▶ $v =~ s/^${ns}/\{schema_class\}/ if($v && $ns && $v ne $ns);
Branch: 1 branch
BranchTrueFalseTotal
if $v and $ns and $v ne $ns 56474 7494 63968
Condition83%5/6
$v and $ns and $v ne $ns
!ll&&!rl&&r
74514356474
$v and $ns
!ll&&!rl&&r
7451056517
MC/DC66%2/3
$v and $ns and $v ne $ns
$v $ns $v ne $ns
Truth table75%3/4
A and B and C
A= $vB= $nsC= $v ne $ns
ABCresult
0XX0
10X0
1100
1111
173 63968 return $v;
174 }
175 }
176  
177  
178 sub prune {
179 2 ▶ my ($self,@keywords) = @_;
Subroutine prune: called CC=4 SCAR=14.0
Pod documented
180 2 ▶ die "must supply at least 1 prune keyword" unless (scalar(@keywords) > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @keywords > 0 0 2 2
181
182 2 my $data = clone( $self->data );
183
184 my @meths = map {
185 2 my $meth = join('_','__prune',$_);
186 3 ▶ $self->can($meth) or die "Bad prune keyword '$_' (no such method '$meth')";
Branch: 1 branch
BranchTrueFalseTotal
$self->can($meth) or die "Bad prune keyword '${_}' (no such method '${meth}')" 0 3 3
187 3 $meth
188 } @keywords;
189
190 2 $self->$_($data) for (@meths);
191  
192 2 __PACKAGE__->new({ data => $data })
193 }
194  
195  
196 sub __prune_isa {
197 1 ▶ my ($self, $data) = @_;
Subroutine __prune_isa: called CC=1 SCAR=0.0
198 1 $self->_prune_whole_source_key('isa',$data)
199 }
200  
201 sub __prune_constraints {
202 1 ▶ my ($self, $data) = @_;
Subroutine __prune_constraints: called CC=1 SCAR=0.0
203 1 $self->_prune_whole_source_key('constraints',$data)
204 }
205  
206 sub __prune_relationships {
207 0 ▶ my ($self, $data) = @_;
Subroutine __prune_relationships: not called CC=1 SCAR=6.9
208 0 $self->_prune_whole_source_key('relationships',$data)
209 }
210  
211 sub __prune_columns {
212 0 ▶ my ($self, $data) = @_;
Subroutine __prune_columns: not called CC=1 SCAR=6.9
213 0 $self->_prune_whole_source_key('columns',$data)
214 }
215  
216  
217 sub __prune_private_col_attrs {
218 1 ▶ my ($self, $data) = @_;
Subroutine __prune_private_col_attrs: called CC=6 SCAR=18.0
219
220 1 for my $rsrcData (values %{ $data->{sources} }) {
221 23 ▶ if(my $columns = $rsrcData->{columns}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $columns = $rsrcData->{"columns"}) 23 0 23
222 23 for my $attrs (values %$columns) {
223 # delete all keys starting with underscore '_'
224 131 ▶ $_ =~ /^_/ and delete $attrs->{$_} for (keys %$attrs);
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /^_/ and delete $attrs->{$_} 38 454 492
225 }
226 }
227 }
228
229 $data
230 1 }
231  
232  
233 sub _prune_whole_source_key {
234 2 ▶ my ($self, $key, $data) = @_;
Subroutine _prune_whole_source_key: called CC=3 SCAR=11.1
235
236 2 for my $rsrcData (values %{ $data->{sources} }) {
237 46 ▶ delete $rsrcData->{$key} if exists $rsrcData->{$key}
Branch: 1 branch
BranchTrueFalseTotal
if exists $rsrcData->{$key} 46 0 46
238 }
239
240 $data
241 2 }
242  
243  
244 sub fingerprint {
245 3 ▶ my $self = shift;
Subroutine fingerprint: called CC=1 SCAR=0.0
Pod documented
246 3 my $sum = Digest::SHA1->new->add( $self->_string_for_signature )->hexdigest;
247 3 join('-', 'schemsum', substr($sum,0,15) )
248 }
249  
250  
251 # So far this is the only thing I could find to produce a consistent string value across all
252 # Travis tested perls (5.10,5.12,5.14,5.16,5.18,5.20,5.22,5.24,5.26)
253 sub _string_for_signature {
254 3 ▶ my $self = shift;
Subroutine _string_for_signature: called CC=1 SCAR=0.0
255
256 3 local $Data::Dumper::Maxdepth = 0;
257 3 Data::Dumper->new([ $self->data->{sources} ])
258 ->Purity(0)
259 ->Terse(1)
260 ->Indent(0)
261 ->Useqq(1)
262 ->Sortkeys(1)
263 ->Dump()
264 }
265  
266  
267 5 1;
268  
269  
270 __END__
271  
272 =head1 NAME
273  
274 DBIx::Class::Schema::Diff::SchemaDiff - Data representation of schema for diffing
275  
276 =head1 SYNOPSIS
277  
278 use DBIx::Class::Schema::Diff::SchemaData;
279
280 my $SD = DBIx::Class::Schema::Diff::SchemaData->new(
281 schema => 'My::Schema'
282 );
283
284 $SD = DBIx::Class::Schema::Diff::SchemaData->new(
285 schema => $schema
286 );
287
288 # Get hashref representation of schema:
289 my $hashref = $SD->data;
290
291 my $SD2 = DBIx::Class::Schema::Diff::SchemaData->new(
292 data => $hashref
293 );
294
295 # Dump as JSON to file:
296 $SD2->dump_json_file('/path/to/schema_data.json');
297
298  
299 =head1 DESCRIPTION
300  
301 Object class representing a DBIx::Class::Schema. The C<old_schema> and C<new_schema> build
302 params in L<DBIx::Class::Schema::Diff> are dynamically coerced into objects of this class.
303  
304 This class can also be instantiated directly to dump/save the current schema data for
305 later use by L<DBIx::Class::Schema::Diff> (See its SYNOPSIS).
306  
307 =head1 METHODS
308  
309 =head2 new
310  
311 Create a new DBIx::Class::Schema::Diff::SchemaData instance. The following build options are supported:
312  
313 =over 4
314  
315 =item schema
316  
317 DBIx::Class schema supplied as a class name or connected object instance. If this param is supplied
318 it will be used to build C<data>.
319  
320 =item data
321  
322 Can be supplied instead of C<schema> to use previously generated data.
323  
324 =back
325  
326 =head2 dump_json
327  
328 Returns C<data> as a serialized JSON string.
329  
330 =head2 dump_json_file
331  
332 Writes output of C<dump_json()> to the supplied filename as long as it doesn't already exists. If the
333 file already exists, an exception is thrown.
334  
335 =head2 prune
336  
337 Accepts a list of one or more prune C<keywords> and returns a new C<SchemaData> object with the
338 specified information pruned/stripped from the C<data>. Currently supported prune keywords:
339  
340 =over 4
341  
342 =item isa
343  
344 =item constraints
345  
346 =item relationships
347  
348 =item columns
349  
350 =item private_col_attrs
351  
352 =back
353  
354 =head2 fingerprint
355  
356 Returns a sha1-based fingerprint string of the current data. Note that C<prune> will result in
357 different fingerprints. An example fingerprint is C<schemsum-448d754e40e09e0>. The 'schemsum' prefix
358 is just for fun (and also provides an easy way to eyeball these values down the road).
359  
360  
361 =head1 SEE ALSO
362  
363 =over
364  
365 =item *
366  
367 L<DBIx::Class::Schema::Diff>
368  
369 =back
370  
371 =head1 AUTHOR
372  
373 Henry Van Styn <vanstyn@cpan.org>
374  
375 =head1 COPYRIGHT AND LICENSE
376  
377 This software is copyright (c) 2014 by IntelliTree Solutions llc.
378  
379 This is free software; you can redistribute it and/or modify it under
380 the same terms as the Perl 5 programming language system itself.
381  
382 =cut
« Diff/Source.pm ↑ Summary Diff/InfoPacket.pm »