Diff/SchemaData.pm

filter: Statementstmt 97.6% 83 / 85 Branchbran 80.0% 24 / 30 Conditioncond 88.8% 8 / 9 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 95.0% 19 / 20 Podpod 40.0% 2 / 5 totaltotal 90.9% 140 / 154 CC 19 SCAR 12.3
CC
19
Cov
93%
_localize_deep_namespace_strings
7.0
_coerce_deep_unsafe_refs
5.0
dump_json_file
4.2
CRAP 3.4
« Diff/Source.pm ↑ Summary Diff.pm »
1 package DBIx::Class::Schema::Diff::SchemaData;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: Data representation of schema for diffing
6  
7 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 with 'DBIx::Class::Schema::Diff::Role::Common';
9  
10 4 ▶use Types::Standard qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Module::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Data::Dumper::Concise;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Path::Class qw(file);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use JSON::DWIW;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 has 'schema', is => 'ro', isa => Maybe[InstanceOf[
18 'DBIx::Class::Schema'
19 7 ▶]], coerce => \&_coerce_schema, default => sub {undef};
Subroutine __ANON__: called CC=1 SCAR=0.0
20  
21 has 'data', is => 'ro', lazy => 1, default => sub {
22 42 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.1
23
24 42 ▶ die "You must supply a schema or an existing data packet"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->schema 0 42 42
25 unless ($self->schema);
26
27 42 return $self->_gen_data( $self->schema );
28  
29 4 }, isa => HashRef, coerce => \&_coerce_deep_unsafe_refs;
30  
31  
32 sub BUILD {
33 49 ▶ my $self = shift;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
34  
35 # initialize:
36 49 $self->data;
37 }
38  
39 sub sources {
40 104 ▶ my $self = shift;
Subroutine sources: called CC=2 SCAR=7.1
Pod undocumented
41 104 ▶ return sort keys %{ $self->data->{sources} || {} };
Branch: 1 branch
BranchTrueFalseTotal
$self->data->{"sources"} or {} 0 104 104
42 }
43  
44 sub source {
45 1034 ▶ my ($self, $name) = @_;
Subroutine source: called CC=1 SCAR=0.0
Pod undocumented
46 1034 return $self->data->{sources}{$name};
47 }
48  
49 sub dump_json {
50 0 ▶ my $self = shift;
Subroutine dump_json: not called CC=1 SCAR=6.9
Pod documented
51 0 JSON::DWIW->to_json( $self->data => { pretty => 1 });
52 }
53  
54 sub dump_json_file {
55 1 ▶ my ($self, $path) = @_;
Subroutine dump_json_file: called CC=4 SCAR=14.5
Pod documented
56 1 ▶ die "Filename required" unless ($path);
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 1 1
57 1 my $file = file($path)->absolute;
58
59 1 ▶ die "Target file '$file' already exists." if (-e $file);
Branch: 1 branch
BranchTrueFalseTotal
if -e $file 0 1 1
60
61 1 JSON::DWIW->to_json_file( $self->data, $file, { pretty => 1 });
62 1 ▶ return -f $file ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
-f $file ? : 1 0 1
63 }
64  
65  
66 sub _gen_data {
67 42 ▶ my ($self, $schema) = @_;
Subroutine _gen_data: called CC=1 SCAR=0.0
68
69 my $data = {
70 schema_class => (blessed $schema),
71 sources => {
72 map {
73 42 my $Source = $schema->source($_);
74 $_ => {
75
76 columns => {
77 map {
78 5504 $_ => $Source->column_info($_)
79 } $Source->columns
80 },
81
82 relationships => {
83 map {
84 1806 $_ => $Source->relationship_info($_)
85 } $Source->relationships
86 },
87
88 constraints => {
89 map {
90 966 $_ => { columns => [$Source->unique_constraint_columns($_)] }
91 } $Source->unique_constraint_names
92 },
93
94 table_name => $Source->from,
95
96 isa => mro::get_linear_isa( $schema->class( $Source->source_name ) ),
97  
98 }
99 } $schema->sources
100 }
101 };
102
103 42 return $self->_localize_deep_namespace_strings($data,$data->{schema_class});
104 }
105  
106  
107 sub _coerce_schema {
108 49 ▶ my ($v) = @_;
Subroutine _coerce_schema: called CC=4 SCAR=13.9
109 49 ▶ return $v if (!$v || ref $v);
Branch: 1 branch
BranchTrueFalseTotal
if not $v or ref $v 36 13 49
Condition100%3/3
not $v or ref $v
l!l&&r!l&&!r
72913
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
110
111 # Its a class name:
112 13 Module::Runtime::require_module($v);
113 13 ▶ return $v->can('connect') ? $v->connect('dbi:SQLite::memory:','','') : $v;
Branch: 1 branch
BranchTrueFalseTotal
$v->can('connect') ? : 13 0 13
114 }
115  
116  
117 sub _coerce_deep_unsafe_refs {
118 100594 ▶ my ($v) = @_;
Subroutine _coerce_deep_unsafe_refs: called CC=5 SCAR=16.1
119 100594 ▶ my $rt = ref($v) or return $v;
Branch: 1 branch
BranchTrueFalseTotal
my $rt = ref $v or return $v 73267 27327 100594
120
121 27327 ▶ if($rt eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if ($rt eq 'HASH') { } 22553 4774 27327
elsif ($rt eq 'ARRAY') { } 2548 2226 4774
elsif ($rt eq 'CODE') { } 1596 630 2226
122 22553 return { map { $_ => &_coerce_deep_unsafe_refs($v->{$_}) } keys %$v };
123 }
124 elsif($rt eq 'ARRAY') {
125 2548 return [ map { &_coerce_deep_unsafe_refs($_) } @$v ];
126 }
127 elsif($rt eq 'CODE') {
128 # TODO: we don't have to do this, we could let it through
129 # to be stringified, but for now, we're not trying to compare
130 # CodeRef contents
131 1596 return 'sub { "DUMMY" }';
132 }
133 else {
134 # For all other refs, stringify them with Dumper. These will still
135 # be just as useful for diff/compare. This makes them safe for JSON, etc
136 630 my $str = Dumper($v);
137 # strip newlines:
138 630 $str =~ s/\r?\n//g;
139 # normalize whitespace:
140 630 $str =~ s/\s+/ /g;
141 630 return $str;
142 }
143 }
144  
145 sub _localize_deep_namespace_strings {
146 86223 ▶ my ($self, $v, $ns) = @_;
Subroutine _localize_deep_namespace_strings: called CC=7 SCAR=19.5
147 86223 my $rt = ref($v);
148 86223 ▶ if($rt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rt) { } 23743 62480 86223
149 23743 ▶ if($rt eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($rt eq 'HASH') { } 19333 4410 23743
elsif ($rt eq 'ARRAY') { } 2184 2226 4410
150 return { map {
151 19333 $_ => $self->_localize_deep_namespace_strings($v->{$_},$ns)
152 } keys %$v };
153 }
154 elsif($rt eq 'ARRAY') {
155 return [ map {
156 2184 $self->_localize_deep_namespace_strings($_,$ns)
157 } @$v ];
158 }
159 else {
160 2226 return $v;
161 }
162 }
163 else {
164 # swap the namespace prefix string for literal '{schema_class}':
165 62480 ▶ $v =~ s/^${ns}/\{schema_class\}/ if($v && $ns && $v ne $ns);
Branch: 1 branch
BranchTrueFalseTotal
if $v and $ns and $v ne $ns 55160 7320 62480
Condition83%5/6
$v and $ns and $v ne $ns
!ll&&!rl&&r
72784255160
$v and $ns
!ll&&!rl&&r
7278055202
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
166 62480 return $v;
167 }
168 }
169  
170 4 1;
171  
172  
173 __END__
« Diff/Source.pm ↑ Summary Diff.pm »