AuditAny.pm

filter: Statementstmt 70.9% 281 / 396 Branchbran 37.5% 54 / 144 Conditioncond 28.5% 14 / 49 MC/DCmcdc 3.4% 1 / 29 Subroutinesub 75.0% 60 / 80 Podpod 94.7% 18 / 19 totaltotal 59.6% 428 / 717 CC 114 SCAR 33.5
CC
114
Cov
59%
track
91.1
__ANON__
72.0
audit_take_snapshot
72.0
CRAP 28.4
« AuditAny/Role/Storage.pm ↑ Summary AuditAny/AuditContext/Change.pm »
1 package DBIx::Class::AuditAny;
2 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: Flexible change tracking framework for DBIx::Class
6  
7 14 our $VERSION = '0.200200';
8  
9 14 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 14 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use MooX::Types::MooseLike::Base 0.19 qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 14 ▶use Class::MOP;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 14 ▶use Class::MOP::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 14 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 14 ▶use DBIx::Class::AuditAny::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 14 ▶use DBIx::Class::AuditAny::Util::BuiltinDatapoints;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 14 ▶use DBIx::Class::AuditAny::Role::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 14 ▶use Term::ANSIColor qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 17 ▶has 'time_zone', is => 'ro', isa => Str, default => sub{'local'};
Subroutine __ANON__: called CC=1 SCAR=0.0
24 15 ▶sub get_dt { DateTime->now( time_zone => (shift)->time_zone ) }
Subroutine get_dt: called CC=1 SCAR=0.0
Pod documented
25  
26 14 has 'schema', is => 'ro', required => 1, isa => InstanceOf['DBIx::Class::Schema']; #<--- This won't go back to Moose
27 15 ▶has 'track_immutable', is => 'ro', isa => Bool, default => sub{0};
Subroutine __ANON__: called CC=1 SCAR=0.0
28 17 ▶has 'track_actions', is => 'ro', isa => ArrayRef, default => sub { [qw(insert update delete)] };
Subroutine __ANON__: called CC=1 SCAR=0.0
29 17 ▶has 'allow_multiple_auditors', is => 'ro', isa => Bool, default => sub{0};
Subroutine __ANON__: called CC=1 SCAR=0.0
30  
31 17 ▶has 'source_context_class', is => 'ro', default => sub{'AuditContext::Source'};
Subroutine __ANON__: called CC=1 SCAR=0.0
32 17 ▶has 'change_context_class', is => 'ro', default => sub{'AuditContext::Change'};
Subroutine __ANON__: called CC=1 SCAR=0.0
33 17 ▶has 'changeset_context_class', is => 'ro', default => sub{'AuditContext::ChangeSet'};
Subroutine __ANON__: called CC=1 SCAR=0.0
34 17 ▶has 'column_context_class', is => 'ro', default => sub{'AuditContext::Column'};
Subroutine __ANON__: called CC=1 SCAR=0.0
35 17 ▶has 'default_datapoint_class', is => 'ro', default => sub{'DataPoint'};
Subroutine __ANON__: called CC=1 SCAR=0.0
36 14 has 'collector_class', is => 'ro', isa => Str;
37  
38 305 ▶around $_ => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
39 305 my $orig = shift; my $self = shift;
40 305 resolve_localclass $self->$orig(@_);
41 14 } for qw(
42 source_context_class change_context_class
43 changeset_context_class column_context_class
44 default_datapoint_class collector_class
45 );
46  
47 0 ▶has 'collector_params', is => 'ro', isa => HashRef, default => sub {{}};
Subroutine __ANON__: not called CC=1 SCAR=1.2
48 17 ▶has 'primary_key_separator', is => 'ro', isa => Str, default => sub{'|~|'};
Subroutine __ANON__: called CC=1 SCAR=0.0
49 15 ▶has 'datapoint_configs', is => 'ro', isa => ArrayRef[HashRef], default => sub {[]};
Subroutine __ANON__: called CC=1 SCAR=0.0
50 17 ▶has 'auto_include_user_defined_datapoints', is => 'ro', isa => Bool, default => sub{1};
Subroutine __ANON__: called CC=1 SCAR=0.0
51 7 ▶has 'rename_datapoints', is => 'ro', isa => Maybe[HashRef[Str]], default => sub{undef};
Subroutine __ANON__: called CC=1 SCAR=0.0
52 17 ▶has 'disable_datapoints', is => 'ro', isa => ArrayRef, default => sub {[]};
Subroutine __ANON__: called CC=1 SCAR=0.0
53 17 ▶has 'record_empty_changes', is => 'ro', isa => Bool, default => sub{0};
Subroutine __ANON__: called CC=1 SCAR=0.0
54  
55 has 'datapoints', is => 'ro', isa => ArrayRef[Str],
56 6 ▶ default => sub{[qw(
Subroutine __ANON__: called CC=1 SCAR=0.0
57 change_ts
58 action
59 source
60 pri_key_value
61 column_name
62 old_value
63 new_value
64 14 )]};
65  
66 17 ▶has 'collector', is => 'ro', lazy => 1, default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
67 17 my $self = shift;
68 return ($self->collector_class)->new(
69 17 %{$self->collector_params},
70 AuditObj => $self
71 );
72 14 };
73  
74 # Any sources within the tracked schema that the collector is writing to; these
75 # sources are not allowed to be tracked because it would create infinite recursion:
76 4 ▶has 'log_sources', is => 'ro', isa => ArrayRef[Str], lazy => 1, init_arg => undef, default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
77 4 my $self = shift;
78 4 return $self->collector->writes_bound_schema_sources;
79 14 };
80  
81 17 ▶has 'tracked_action_functions', is => 'ro', isa => HashRef, default => sub {{}};
Subroutine __ANON__: called CC=1 SCAR=0.0
82 17 ▶has 'tracked_sources', is => 'ro', isa => HashRef[Str], default => sub {{}};
Subroutine __ANON__: called CC=1 SCAR=0.0
83 17 ▶has 'calling_action_function', is => 'ro', isa => HashRef[Bool], default => sub {{}};
Subroutine __ANON__: called CC=1 SCAR=0.0
84 17 ▶has 'active_changeset', is => 'rw', isa => Maybe[Object], default => sub{undef};
Subroutine __ANON__: called CC=1 SCAR=0.0
85 17 ▶has 'auto_finish', is => 'rw', isa => Bool, default => sub{0};
Subroutine __ANON__: called CC=1 SCAR=0.0
86  
87 0 ▶has 'track_init_args', is => 'ro', isa => Maybe[HashRef], default => sub{undef};
Subroutine __ANON__: not called CC=1 SCAR=1.2
88 14 has 'build_init_args', is => 'ro', isa => HashRef, required => 1;
89  
90 17 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=3 SCAR=11.6
91 17 my $orig = shift;
92 17 my $class = shift;
93 0 ▶ my %opts = (ref($_[0]) eq 'HASH') ? %{ $_[0] } : @_; # <-- arg as hash or hashref
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 17 17
94  
95 17 ▶ die 'Cannot specify build_init_args in new()' if (exists $opts{build_init_args});
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"build_init_args"} 0 17 17
96 17 $opts{build_init_args} = { %opts };
97 17 return $class->$orig(%opts);
98 14 };
99  
100 17 ▶sub track {
Subroutine track: called CC=28 SCAR=45.1
Pod documented
101 17 my $class = shift;
102 0 ▶ my %opts = (ref($_[0]) eq 'HASH') ? %{ $_[0] } : @_; # <-- arg as hash or hashref
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 17 17
103 17 ▶ die "track cannot be called on object instances" if (ref $class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 17 17
104
105 # Record the track init arguments:
106 17 $opts{track_init_args} = { %opts };
107
108 17 ▶ my $sources = exists $opts{track_sources} ? delete $opts{track_sources} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'track_sources'} ? : 0 17 17
109 17 ▶ die 'track_sources must be an arrayref' if ($sources and ! ref($sources) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if $sources and !ref($sources) eq "ARRAY" 0 17 17
Condition33%1/3
$sources and !ref($sources) eq "ARRAY"
!ll&&!rl&&r
1700
MC/DC0%0/2
$sources and !ref($sources) eq "ARRAY"
$sources !ref($sources) eq "ARRAY"
Truth table33%1/3
A and not(B)
A= $sourcesB= !ref($sources) eq "ARRAY"
ABresult
0X0
100
111
110 17 ▶ my $track_all = exists $opts{track_all_sources} ? delete $opts{track_all_sources} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'track_all_sources'} ? : 17 0 17
111 17 ▶ die "track_sources and track_all_sources are incompatible" if ($sources && $track_all);
Branch: 1 branch
BranchTrueFalseTotal
if $sources and $track_all 0 17 17
Condition33%1/3
$sources and $track_all
!ll&&!rl&&r
1700
MC/DC0%0/2
$sources and $track_all
$sources $track_all
Truth table33%1/3
A and B
A= $sourcesB= $track_all
ABresult
0X0
100
111
112
113 17 ▶ my $init_sources = exists $opts{init_sources} ? delete $opts{init_sources} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'init_sources'} ? : 0 17 17
114 17 ▶ die 'init_sources must be an arrayref' if ($init_sources and ! ref($init_sources) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if $init_sources and !ref($init_sources) eq "ARRAY" 0 17 17
Condition33%1/3
$init_sources and !ref($init_sources) eq "ARRAY"
!ll&&!rl&&r
1700
MC/DC0%0/2
$init_sources and !ref($init_sources) eq "ARRAY"
$init_sources !ref($init_sources) eq "ARRAY"
Truth table33%1/3
A and not(B)
A= $init_sourcesB= !ref($init_sources) eq "ARRAY"
ABresult
0X0
100
111
115 17 ▶ my $init_all = exists $opts{init_all_sources} ? delete $opts{init_all_sources} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'init_all_sources'} ? : 0 17 17
116 17 ▶ die "init_sources and init_all_sources are incompatible" if ($init_sources && $init_all);
Branch: 1 branch
BranchTrueFalseTotal
if $init_sources and $init_all 0 17 17
Condition33%1/3
$init_sources and $init_all
!ll&&!rl&&r
1700
MC/DC0%0/2
$init_sources and $init_all
$init_sources $init_all
Truth table33%1/3
A and B
A= $init_sourcesB= $init_all
ABresult
0X0
100
111
117
118 17 ▶ my $collect = exists $opts{collect} ? delete $opts{collect} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'collect'} ? : 2 15 17
119 17 ▶ if ($collect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($collect) 2 15 17
120 die "'collect' cannot be used with 'collector_params', 'collector_class' or 'collector'"
121 2 ▶ if ($opts{collector_params} || $opts{collector_class} || $opts{collector});
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"collector_params"} or $opts{"collector_class"} or $opts{"collector"} 0 2 2
Condition33%2/6
$opts{"collector_params"} or $opts{"collector_class"} or $opts{"collector"}
l!l&&r!l&&!r
002
$opts{"collector_params"} or $opts{"collector_class"}
l!l&&r!l&&!r
002
MC/DC0%0/3
$opts{"collector_params"} or $opts{"collector_class"} or $opts{"collector"}
$opts{"collector_params"} $opts{"collector_class"} $opts{"collector"}
Truth table25%1/4
A or B or C
A= $opts{"collector_params"}B= $opts{"collector_class"}C= $opts{"collector"}
ABCresult
1XX1
01X1
0011
0000
122
123 2 $opts{collector_class} = 'Collector::Code';
124 2 $opts{collector_params} = { collect_coderef => $collect };
125 }
126
127 17 ▶ if($opts{collector}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"collector"}) 0 17 17
128 die "'collector' cannot be used with 'collector_params', 'collector_class' or 'collect'"
129 0 ▶ if ($opts{collector_params} || $opts{collector_class} || $opts{collect});
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"collector_params"} or $opts{"collector_class"} or $opts{"collect"} 0 0 0
Condition0%0/6
$opts{"collector_params"} or $opts{"collector_class"} or $opts{"collect"}
l!l&&r!l&&!r
000
$opts{"collector_params"} or $opts{"collector_class"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$opts{"collector_params"} or $opts{"collector_class"} or $opts{"collect"}
$opts{"collector_params"} $opts{"collector_class"} $opts{"collect"}
Truth table0%0/4
A or B or C
A= $opts{"collector_params"}B= $opts{"collector_class"}C= $opts{"collect"}
ABCresult
1XX1
01X1
0011
0000
130 }
131
132 17 my $self = $class->new(%opts);
133
134 4 ▶ $self->track_sources(@$sources) if ($sources);
Branch: 1 branch
BranchTrueFalseTotal
if $sources 0 4 4
135 4 ▶ $self->track_all_sources if ($track_all);
Branch: 1 branch
BranchTrueFalseTotal
if $track_all 4 0 4
136
137 4 ▶ $self->init_sources(@$init_sources) if ($init_sources);
Branch: 1 branch
BranchTrueFalseTotal
if $init_sources 0 4 4
138 4 ▶ $self->init_all_sources if ($init_all);
Branch: 1 branch
BranchTrueFalseTotal
if $init_all 0 4 4
139 4 return $self;
140 }
141  
142  
143 17 ▶sub _get_datapoint_configs {
Subroutine _get_datapoint_configs: called CC=2 SCAR=6.9
144 17 my $self = shift;
145
146 17 my @configs = DBIx::Class::AuditAny::Util::BuiltinDatapoints->all_configs;
147
148 # strip out any being redefined:
149 17 my %cust = map {$_->{name}=>1} @{$self->datapoint_configs};
150 17 @configs = grep { !$cust{$_->{name}} } @configs;
151
152 # Set flag to mark the configs that were user defined
153 17 $_->{user_defined} = 1 for (@{$self->datapoint_configs});
154
155 17 push @configs, @{$self->datapoint_configs};
156
157 17 return @configs;
158 }
159  
160 17 ▶has '_datapoints', is => 'ro', isa => HashRef, default => sub {{}};
Subroutine __ANON__: called CC=1 SCAR=0.0
161 17 ▶has '_datapoints_context', is => 'ro', isa => HashRef, default => sub {{}};
Subroutine __ANON__: called CC=1 SCAR=0.0
162  
163 # Also index datapoints by 'original_name' which will be different from 'name'
164 # whenever 'rename_datapoints' has been applied
165 17 ▶has '_datapoints_orig_names', is => 'ro', isa => HashRef, default => sub {{}};
Subroutine __ANON__: called CC=1 SCAR=0.0
166 13 ▶sub get_datapoint_orig { (shift)->_datapoints_orig_names->{(shift)} }
Subroutine get_datapoint_orig: called CC=1 SCAR=0.0
Pod documented
167  
168 178 ▶sub add_datapoints {
Subroutine add_datapoints: called CC=7 SCAR=20.4
Pod documented
169 178 my $self = shift;
170 178 my $class = $self->default_datapoint_class;
171 178 foreach my $cnf (@_) {
172 178 ▶ die "'$cnf' not expected ref" unless (ref $cnf);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cnf 0 178 178
173 178 ▶ $class = delete $cnf->{class} if ($cnf->{class});
Branch: 1 branch
BranchTrueFalseTotal
if $cnf->{"class"} 0 178 178
174 178 ▶ my $DataPoint = ref($cnf) eq $class ? $cnf : $class->new($cnf);
Branch: 1 branch
BranchTrueFalseTotal
ref $cnf eq $class ? : 0 178 178
175 178 ▶ die "Error creating datapoint object" unless (ref($DataPoint) eq $class);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $DataPoint eq $class 0 178 178
176 178 ▶ die "Duplicate datapoint name '" . $DataPoint->name . "'" if ($self->_datapoints->{$DataPoint->name});
Branch: 1 branch
BranchTrueFalseTotal
if $self->_datapoints->{$DataPoint->name} 0 178 178
177 178 $self->_datapoints->{$DataPoint->name} = $DataPoint;
178 178 $self->_datapoints_context->{$DataPoint->context}->{$DataPoint->name} = $DataPoint;
179 178 $self->_datapoints_orig_names->{$DataPoint->original_name} = $DataPoint;
180 }
181 }
182 0 ▶sub all_datapoints { values %{(shift)->_datapoints} }
Subroutine all_datapoints: not called CC=1 SCAR=6.9
Pod documented
183  
184 45 ▶sub get_context_datapoints {
Subroutine get_context_datapoints: called CC=1 SCAR=0.0
Pod documented
185 45 my $self = shift;
186 45 my @contexts = grep { exists $self->_datapoints_context->{$_} } @_;
187 45 return map { values %{$self->_datapoints_context->{$_}} } @contexts;
188 }
189  
190 0 ▶sub get_context_datapoint_names {
Subroutine get_context_datapoint_names: not called CC=1 SCAR=6.9
Pod documented
191 0 my $self = shift;
192 0 return map { $_->name } $self->get_context_datapoints(@_);
193 }
194  
195  
196 0 ▶sub local_datapoint_data { (shift)->base_datapoint_values }
Subroutine local_datapoint_data: not called CC=1 SCAR=6.9
Pod documented
197 0 ▶has 'base_datapoint_values', is => 'ro', isa => HashRef, lazy => 1, default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
198 0 my $self = shift;
199 0 return { map { $_->name => $_->get_value($self) } $self->get_context_datapoints('base') };
200 14 };
201  
202 17 ▶sub _init_datapoints {
Subroutine _init_datapoints: called CC=14 SCAR=26.7
203 17 my $self = shift;
204
205 17 my @configs = $self->_get_datapoint_configs;
206
207 17 ▶ if($self->rename_datapoints) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->rename_datapoints) 10 7 17
208 10 my $rename = $self->rename_datapoints;
209
210 10 ▶ @{$self->datapoints} = map { $rename->{$_} || $_ } @{$self->datapoints};
Condition66%2/3
$rename->{$_} || $_
l!l&&r!l&&!r
48620
MC/DC0%0/2
$rename->{$_} || $_
$rename->{$_} $_
Truth table66%2/3
A || B
A= $rename->{$_}B= $_
ABresult
1X1
011
000
211
212 10 foreach my $cnf (@configs) {
213 224 ▶ next unless (exists $rename->{$cnf->{name}});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$rename{$cnf->{"name"}} 164 60 224
214 60 $cnf->{original_name} = $cnf->{name};
215 60 $cnf->{name} = $rename->{$cnf->{name}};
216 }
217 }
218
219 17 my %seen = ();
220 17 ▶ $seen{$_}++ and die "Duplicate datapoint name '$_'" for (@{$self->datapoints});
Branch: 1 branch
BranchTrueFalseTotal
$seen{$_}++ and die "Duplicate datapoint name '${_}'" 0 174 174
221
222 0 my %disable = map {$_=>1} @{$self->disable_datapoints};
223 17 my %activ = map {$_=>1} grep { !$disable{$_} } @{$self->datapoints};
224
225 17 ▶ if($self->auto_include_user_defined_datapoints) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->auto_include_user_defined_datapoints) 17 0 17
226 17 ▶ $activ{$_->{name}} = 1 for(grep { $_->{name} && $_->{user_defined} } @configs);
Condition66%2/3
$_->{'name'} && $_->{'user_defined'}
!ll&&!rl&&r
03744
MC/DC50%1/2
$_->{'name'} && $_->{'user_defined'}
$_->{'name'} $_->{'user_defined'}
Truth table66%2/3
A && B
A= $_->{'name'}B= $_->{'user_defined'}
ABresult
0X0
100
111
227 }
228
229 17 foreach my $cnf (@configs) {
230 # Do this just to throw the exception for no name:
231 378 ▶ $self->add_datapoints($cnf) unless ($cnf->{name});
Branch: 1 branch
BranchTrueFalseTotal
unless $cnf->{"name"} 0 378 378
232
233 378 ▶ next unless $activ{$cnf->{name}};
Branch: 1 branch
BranchTrueFalseTotal
unless $activ{$cnf->{"name"}} 200 178 378
234 178 delete $activ{$cnf->{name}};
235 178 $self->add_datapoints({%$cnf, AuditObj => $self});
236 }
237
238 17 ▶ die "Unknown datapoint(s) specified (" . join(',',keys %activ) . ')'
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %activ > 0 0 17 17
239 if (scalar(keys %activ) > 0);
240 }
241  
242  
243 17 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
244 17 my $self = shift;
245
246 # init all classes first:
247 17 $self->change_context_class;
248 17 $self->changeset_context_class;
249 17 $self->source_context_class;
250 17 $self->column_context_class;
251 17 $self->default_datapoint_class;
252
253 17 $self->_init_datapoints;
254 17 $self->_bind_schema;
255
256 # init collector object:
257 17 $self->collector;
258 }
259  
260  
261 17 ▶sub _init_apply_schema_class {
Subroutine _init_apply_schema_class: called CC=3 SCAR=11.3
262 17 my $self = shift;
263 17 ▶ die "schema is not a reference" unless (ref $self->schema);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->schema 0 17 17
264
265 Moo::Role->apply_roles_to_object($self->schema,'DBIx::Class::AuditAny::Role::Schema')
266 17 ▶ unless try{$self->schema->does('DBIx::Class::AuditAny::Role::Schema')};
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless &try(sub { $self->schema->does("DBIx::Class::AuditAny::Role::Schema"); } ) 17 0 17
267
268 # Important!
269 17 $self->schema->_apply_storage_role;
270 }
271  
272  
273  
274  
275  
276 17 ▶sub _bind_schema {
Subroutine _bind_schema: called CC=5 SCAR=17.8
277 17 my $self = shift;
278 17 $self->_init_apply_schema_class;
279
280 17 ▶ die "Supplied Schema instance already has a bound Auditor - to allow multple " .
Branch: 1 branch
BranchTrueFalseTotal
if $self->schema->auditor_count > 0 and not $self->allow_multiple_auditors 0 17 17
Condition33%1/3
$self->schema->auditor_count > 0 and not $self->allow_multiple_auditors
!ll&&!rl&&r
1700
MC/DC0%0/2
$self->schema->auditor_count > 0 and not $self->allow_multiple_auditors
$self->schema->auditor_count > 0 not $self->allow_multiple_auditors
Truth table33%1/3
A and not(B)
A= $self->schema->auditor_count > 0B= not $self->allow_multiple_auditors
ABresult
0X0
100
111
281 "Auditors, set 'allow_multiple_auditors' to true"
282 if($self->schema->auditor_count > 0 and ! $self->allow_multiple_auditors);
283
284 17 ▶ $_ == $self and return for($self->schema->auditors);
Branch: 1 branch
BranchTrueFalseTotal
$_ == $self and return 0 17 17
285
286 17 return $self->schema->add_auditor($self);
287 }
288  
289  
290  
291  
292 4 ▶sub track_sources {
Subroutine track_sources: called CC=5 SCAR=16.3
Pod documented
293 4 my ($self,@sources) = @_;
294
295 4 foreach my $name (@sources) {
296 21 ▶ my $Source = $self->schema->source($name) or die "Bad Result Source name '$name'";
Branch: 1 branch
BranchTrueFalseTotal
my $Source = $self->schema->source($name) or die "Bad Result Source name '${name}'" 21 0 21
297
298 21 my $class = $self->source_context_class;
299 21 my $AuditSourceContext = $class->new(
300 AuditObj => $self,
301 ResultSource => $Source
302 );
303
304 21 my $source_name = $AuditSourceContext->source;
305
306 21 my %log_sources = map {$_=>1} @{$self->log_sources};
307 die "The Log Source (" . $source_name . ") cannot track itself!!"
308 21 ▶ if ($log_sources{$source_name});
Branch: 1 branch
BranchTrueFalseTotal
if $log_sources{$source_name} 0 21 21
309  
310 # Skip sources we've already setup:
311 21 ▶ return if ($self->tracked_sources->{$source_name});
Branch: 1 branch
BranchTrueFalseTotal
if $self->tracked_sources->{$source_name} 0 21 21
312
313 21 $self->_add_row_trackers_methods($AuditSourceContext);
314 21 $self->tracked_sources->{$source_name} = $AuditSourceContext;
315 }
316 }
317  
318 4 ▶sub track_all_sources {
Subroutine track_all_sources: called CC=1 SCAR=0.0
Pod documented
319 4 my ($self,@exclude) = @_;
320 #$class->_init;
321
322 4 push @exclude, @{$self->log_sources};
323
324 4 my %excl = map {$_=>1} @exclude;
325 4 return $self->track_sources(grep { !$excl{$_} } $self->schema->sources);
326 }
327  
328 # This is the original, Row-based solution for initializing existing data. This
329 # is going to be refactored and replaced, but with what has not been decided yet
330 # See also _add_additional_row_methods() below
331 0 ▶sub init_sources {
Subroutine init_sources: not called CC=1 SCAR=6.9
Pod documented
332 0 my ($self,@sources) = @_;
333
334 0 ▶ $self->schema->txn_do(sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
335
336 0 foreach my $name (@sources) {
337 0 ▶ my $SourceContext = $self->tracked_sources->{$name}
Branch: 1 branch
BranchTrueFalseTotal
my $SourceContext = $self->tracked_sources->{$name} or die "Source '${name}' is not being tracked" 0 0 0
338 or die "Source '$name' is not being tracked";
339
340 0 print STDERR "\n";
341
342 0 my $msg = "Initializing Audit Records for $name: ";
343 0 print STDERR $msg . "\r";
344
345 0 my $Rs = $SourceContext->ResultSource->resultset;
346 0 my $total = $Rs->count;
347 0 my $count = 0;
348 0 foreach my $Row ($Rs->all) {
349 0 print STDERR $msg . ++$count . '/' . $total . "\r";
350 0 $Row->audit_init($self);
351 }
352 }
353
354 0 print STDERR "\n\n";
355 0 });
356 }
357  
358 0 ▶sub init_all_sources {
Subroutine init_all_sources: not called CC=1 SCAR=6.9
Pod documented
359 0 my $self = shift;
360 0 $self->init_sources(keys %{$self->tracked_sources});
361 }
362  
363  
364 14 our $NESTED_CALL = 0;
365 21 ▶sub _add_row_trackers_methods {
Subroutine _add_row_trackers_methods: called CC=3 SCAR=11.0
366 21 my $self = shift;
367 21 my $AuditSourceContext = shift;
368
369 21 my $source_name = $AuditSourceContext->source;
370 21 my $result_class = $self->schema->class($source_name);
371
372 21 foreach my $action (@{$self->track_actions}) {
373 63 my $func_name = $source_name . '::' . $action;
374 63 ▶ return if $self->tracked_action_functions->{$func_name}++;
Branch: 1 branch
BranchTrueFalseTotal
if $self->tracked_action_functions->{$func_name}++ 0 63 63
375 }
376
377 21 $self->_add_additional_row_methods($result_class);
378 }
379  
380  
381  
382 # TODO/FIXME: This needs to be refactored to use a cleaner API. Probably
383 # totally different (this code is leftover from before the switch to the
384 # Storage Role API)
385 21 ▶sub _add_additional_row_methods {
Subroutine _add_additional_row_methods: called CC=6 SCAR=32.8
386 21 my $self = shift;
387 21 my $result_class = shift;
388
389 21 my $meta = Class::MOP::Class->initialize($result_class);
390 21 my $immutable = $meta->is_immutable;
391
392 21 ▶ die "Won't add tracker/modifier method to immutable Result Class '$result_class' " .
Branch: 1 branch
BranchTrueFalseTotal
if $immutable and not $self->track_immutable 0 21 21
Condition33%1/3
$immutable and not $self->track_immutable
!ll&&!rl&&r
2100
MC/DC0%0/2
$immutable and not $self->track_immutable
$immutable not $self->track_immutable
Truth table33%1/3
A and not(B)
A= $immutableB= not $self->track_immutable
ABresult
0X0
100
111
393 '(hint: did you forget to remove __PACKAGE__->meta->make_immutable ??)' .
394 ' - to force/override, set "track_immutable" to true.'
395 if ($immutable && !$self->track_immutable);
396
397 # Tempory turn mutable back on, saving any immutable_options, first:
398 21 my %immut_opts = ();
399 21 ▶ if($immutable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($immutable) 0 21 21
400 0 %immut_opts = $meta->immutable_options;
401 0 $meta->make_mutable;
402 }
403
404 21 ▶ return if ($meta->has_method('audit_take_snapshot'));
Branch: 1 branch
BranchTrueFalseTotal
if $meta->has_method("audit_take_snapshot") 3 18 21
405
406 0 ▶ $meta->add_method( audit_take_snapshot => sub {
Subroutine audit_take_snapshot: not called CC=8 SCAR=42.8
Subroutine __ANON__: not called CC=8 SCAR=42.8
407 0 my $Row = shift;
408 0 ▶ my $AuditObj = shift or die "AuditObj not supplied in argument.";
Branch: 2 branches
BranchTrueFalseTotal
my $AuditObj = shift() or die "AuditObj not supplied in argument." 0 0 0
my $AuditObj = shift() or die "AuditObj not supplied in argument." 0 0 0
409
410 0 ▶ my $Auditors = $Row->result_source->schema->auditors || [];
Condition0%0/4
$Row->result_source->schema->auditors || []
l!l
00
$Row->result_source->schema->auditors || []
l!l
00
MC/DC0%0/1
$Row->result_source->schema->auditors || []
$Row->result_source->schema->auditors
MC/DC0%0/1
$Row->result_source->schema->auditors || []
$Row->result_source->schema->auditors
Truth table0%0/2
A
A= $Row->result_source->schema->auditors
Aresult
11
00
Truth table0%0/2
A
A= $Row->result_source->schema->auditors
Aresult
11
00
411 0 my $found = 0;
412 0 ▶ $_ == $AuditObj and $found = 1 for (@$Auditors);
Branch: 2 branches
BranchTrueFalseTotal
$_ == $AuditObj and $found = 1 0 0 0
$_ == $AuditObj and $found = 1 0 0 0
413 0 ▶ die "Supplied AuditObj is not an active Auditor on this Row's schema instance"
Branch: 2 branches
BranchTrueFalseTotal
unless $found 0 0 0
unless $found 0 0 0
414 unless ($found);
415
416 0 my $source_name = $Row->result_source->source_name;
417 0 ▶ my $SourceContext = $AuditObj->tracked_sources->{$source_name}
Branch: 2 branches
BranchTrueFalseTotal
my $SourceContext = $AuditObj->tracked_sources->{$source_name} or die "Source '${source_name}' is not being tracked by the supplied Auditor" 0 0 0
my $SourceContext = $AuditObj->tracked_sources->{$source_name} or die "Source '${source_name}' is not being tracked by the supplied Auditor" 0 0 0
418 or die "Source '$source_name' is not being tracked by the supplied Auditor";
419
420 0 ▶ unless ($AuditObj->active_changeset) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($AuditObj->active_changeset) 0 0 0
unless ($AuditObj->active_changeset) 0 0 0
421 0 $AuditObj->start_changeset;
422 0 $AuditObj->auto_finish(1);
423 }
424
425 0 my $class = $AuditObj->change_context_class;
426 0 my $ChangeContext = $class->new(
427 AuditObj => $AuditObj,
428 SourceContext => $SourceContext,
429 ChangeSetContext => $AuditObj->active_changeset,
430 Row => $Row,
431 new_columns => { $Row->get_columns },
432 action => 'select'
433 );
434 0 $ChangeContext->record;
435 0 $AuditObj->record_changes($ChangeContext);
436 0 return $Row;
437 18 });
438
439 0 ▶ $meta->add_method( audit_init => sub {
Subroutine audit_init: not called CC=7 SCAR=40.3
440 0 my $Row = shift;
441 0 ▶ my $AuditObj = shift or die "AuditObj not supplied in argument.";
Branch: 1 branch
BranchTrueFalseTotal
my $AuditObj = shift() or die "AuditObj not supplied in argument." 0 0 0
442
443 0 ▶ my $Auditors = $Row->result_source->schema->auditors || [];
Condition0%0/2
$Row->result_source->schema->auditors || []
l!l
00
MC/DC0%0/1
$Row->result_source->schema->auditors || []
$Row->result_source->schema->auditors
Truth table0%0/2
A
A= $Row->result_source->schema->auditors
Aresult
11
00
444 0 my $found = 0;
445 0 ▶ $_ == $AuditObj and $found = 1 for (@$Auditors);
Branch: 1 branch
BranchTrueFalseTotal
$_ == $AuditObj and $found = 1 0 0 0
446 0 ▶ die "Supplied AuditObj is not an active Auditor on this Row's schema instance"
Branch: 1 branch
BranchTrueFalseTotal
unless $found 0 0 0
447 unless ($found);
448
449 0 my $Collector = $AuditObj->collector;
450 0 ▶ return $Row->audit_take_snapshot($AuditObj) unless ($Collector->has_full_row_stored($Row));
Branch: 1 branch
BranchTrueFalseTotal
unless $Collector->has_full_row_stored($Row) 0 0 0
451 0 return $Row;
452 18 });
453
454 # Restore immutability to the way to was:
455 18 ▶ $meta->make_immutable(%immut_opts) if ($immutable);
Branch: 1 branch
BranchTrueFalseTotal
if $immutable 0 18 18
456 }
457  
458  
459 ##########
460 ##########
461  
462 # Starts a new changeset if there isn't one active:
463 0 ▶sub start_unless_changeset {
Subroutine start_unless_changeset: not called CC=2 SCAR=17.9
Pod documented
464 0 my $self = shift;
465 0 ▶ return $self->active_changeset ? undef : $self->start_changeset;
Branch: 1 branch
BranchTrueFalseTotal
$self->active_changeset ? : 0 0 0
466 }
467  
468 0 ▶sub start_changeset {
Subroutine start_changeset: not called CC=2 SCAR=17.9
Pod documented
469 0 my $self = shift;
470 0 ▶ die "Cannot start_changeset because a changeset is already active" if ($self->active_changeset);
Branch: 1 branch
BranchTrueFalseTotal
if $self->active_changeset 0 0 0
471
472 0 my $class = $self->changeset_context_class;
473 0 $self->active_changeset($class->new( AuditObj => $self ));
474 0 return $self->active_changeset;
475 }
476  
477 0 ▶sub finish_if_changeset {
Subroutine finish_if_changeset: not called CC=2 SCAR=17.9
Pod documented
478 0 my $self = shift;
479 0 ▶ return $self->active_changeset ? $self->finish_changeset : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->active_changeset ? : 0 0 0
480 }
481  
482 17 ▶has '_finishing_changeset', is => 'rw', isa => Bool, default => sub{0};
Subroutine __ANON__: called CC=1 SCAR=0.0
483 0 ▶sub finish_changeset {
Subroutine finish_changeset: not called CC=6 SCAR=37.4
Pod documented
484 0 my $self = shift;
485 0 ▶ die "Cannot finish_changeset because there isn't one active" unless ($self->active_changeset);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->active_changeset 0 0 0
486
487 # Protect against deep recursion. This is needed for cases where the collector
488 # is writing to tables within the tracked schema
489 0 ▶ return if ($self->_finishing_changeset);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_finishing_changeset 0 0 0
490 0 $self->_finishing_changeset(1);
491
492 0 ▶ unless($self->record_empty_changes) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->record_empty_changes) 0 0 0
493 0 my $count_cols = 0;
494 $count_cols = $count_cols + scalar($_->all_column_changes)
495 0 for (@{$self->active_changeset->changes});
496 0 ▶ unless ($count_cols > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($count_cols > 0) 0 0 0
497 0 $self->clear_changeset;
498 0 return 1;
499 }
500 }
501
502 0 $self->active_changeset->finish;
503
504 #####
505 0 $self->collector->record_changes($self->active_changeset);
506 #####
507
508 0 $self->clear_changeset;
509 0 return 1;
510 }
511  
512 0 ▶sub _exception_cleanup {
Subroutine _exception_cleanup: not called CC=1 SCAR=6.9
513 0 my $self = shift;
514 0 my $err = shift;
515 0 $self->clear_changeset;
516 0 $self->_current_change_group([]);
517 }
518  
519 0 ▶sub clear_changeset {
Subroutine clear_changeset: not called CC=1 SCAR=6.9
Pod documented
520 0 my $self = shift;
521 0 $self->active_changeset(undef);
522 0 $self->auto_finish(0);
523 0 $self->_finishing_changeset(0);
524 }
525  
526 0 ▶sub record_changes {
Subroutine record_changes: not called CC=3 SCAR=24.8
Pod documented
527 0 my ($self, @ChangeContexts) = @_;
528
529 0 my $local_changeset = $self->start_unless_changeset;
530
531 0 $self->active_changeset->add_changes($_) for (@ChangeContexts);
532
533 0 ▶ $self->finish_changeset if ($local_changeset);
Branch: 1 branch
BranchTrueFalseTotal
if $local_changeset 0 0 0
534 }
535  
536  
537 ## Change 'group' vs Change 'set'
538 #
539 # I am using the term 'group' (to distinguish from 'set') to represent a group
540 # of changes (rows) that are being changed within a single query/sql statement.
541 # (vs. set which is any number of query/sql statements grouped in a transaction)
542 # This should only happen from making changes via ResultSet objects instead of
543 # Row objects, and in these cases we normalize these into individual (row) changes
544 # TODO: should ChangeGroup be made into a 6th Context? For now, I think no because
545 # it is overkill.
546 ##
547  
548 # -- This is a glorified tmp variable used just to allow groups of changes
549 # to be associated with the correct auditor. TODO: This is probably a
550 # poor solution to a complex scoping problem. This exposes us to the
551 # risk of processing stale data, so we have to be sure (manually) to keep
552 # this clear/empty outside its *very* short lifespan, by regularly resetting it
553 17 ▶has '_current_change_group', is => 'rw', isa => ArrayRef[Object], default => sub{[]};
Subroutine __ANON__: called CC=1 SCAR=0.0
554 # --
555  
556 4 ▶sub _start_current_change_group {
Subroutine _start_current_change_group: called CC=4 SCAR=14.0
557 4 my ($self, $Source, $nested, $action, @changes) = @_;
558
559 4 ▶ my $Group = $self->_current_change_group || [];
Condition50%1/2
$self->_current_change_group || []
l!l
40
MC/DC0%0/1
$self->_current_change_group || []
$self->_current_change_group
Truth table50%1/2
A
A= $self->_current_change_group
Aresult
11
00
560 4 ▶ $Group = [] unless ($nested);
Branch: 1 branch
BranchTrueFalseTotal
unless $nested 4 0 4
561
562 4 $self->_current_change_group($Group); # just for good measure
563
564 4 my $source_name = $Source->source_name;
565 4 my $func_name = $source_name . '::' . $action;
566
567 4 ▶ return () unless ($self->tracked_action_functions->{$func_name});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->tracked_action_functions->{$func_name} 0 4 4
568
569 my @ChangeContexts = map {
570 4 $self->_new_change_context(
571 AuditObj => $self,
572 4 SourceContext => $self->tracked_sources->{$source_name},
573 ChangeSetContext => $self->active_changeset, # could be undef
574 action => $action,
575 $self->_validated_change_hash($_)
576 )
577 } @changes;
578
579 4 push @$Group, @ChangeContexts;
580 4 $self->_current_change_group($Group);
581 4 return @ChangeContexts;
582 }
583  
584 4 ▶sub _validated_change_hash {
Subroutine _validated_change_hash: called CC=5 SCAR=17.0
585 4 my ($self, $data) = @_;
586
587 4 require Data::Dumper::Concise;
588
589 4 ▶ die "change data must be a HashRef:\n" .
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 0 4 4
590 Data::Dumper::Concise::Dumper($data) unless (ref($data) eq 'HASH');
591
592 4 my %allowed_keys = map {$_=>1} qw(old_columns to_columns new_columns);
593
594 $allowed_keys{$_} && ref($data->{$_}) eq 'HASH' or
595 die "Bad data in change hash:\n" . Data::Dumper::Concise::Dumper($data)
596 4 ▶ for (grep { $_ ne 'condition' } keys %$data);
Branch: 1 branch
BranchTrueFalseTotal
$allowed_keys{$_} and ref $data->{$_} eq "HASH" or die "Bad data in change hash:\n" . Data::Dumper::Concise::Dumper($data) 4 0 4
Condition33%1/3
$allowed_keys{$_} and ref $data->{$_} eq "HASH"
!ll&&!rl&&r
004
MC/DC0%0/2
$allowed_keys{$_} and ref $data->{$_} eq "HASH"
$allowed_keys{$_} ref $data->{$_} eq "HASH"
Truth table33%1/3
A and B
A= $allowed_keys{$_}B= ref $data->{$_} eq "HASH"
ABresult
0X0
100
111
597  
598 4 return %$data;
599 }
600  
601  
602 0 ▶sub _finish_current_change_group {
Subroutine _finish_current_change_group: not called CC=2 SCAR=17.9
603 0 my $self = shift;
604 0 ▶ $self->record_changes(@{$self->_current_change_group || []});
Condition0%0/2
$self->_current_change_group || []
l!l
00
MC/DC0%0/1
$self->_current_change_group || []
$self->_current_change_group
Truth table0%0/2
A
A= $self->_current_change_group
Aresult
11
00
605 0 $self->_current_change_group([]); #<-- critical to reset!
606 }
607  
608 # factory-like helper:
609 4 ▶sub _new_change_context {
Subroutine _new_change_context: called CC=1 SCAR=0.0
610 4 my $self = shift;
611 4 my $class = $self->change_context_class;
612 4 return $class->new(@_);
613 }
614  
615  
616  
617 14 1;
618  
619  
620 __END__
« AuditAny/Role/Storage.pm ↑ Summary AuditAny/AuditContext/Change.pm »