blib/lib/DBIO/ChangeLog.pm

filter: Statementstmt 96.6% 114 / 118 Branchbran 76.1% 32 / 42 Conditioncond 64.5% 20 / 31 MC/DCmcdc 41.1% 7 / 17 Subroutinesub 95.8% 23 / 24 Podpod 100% 11 / 11 totaltotal 85.1% 207 / 243 CC 37 SCAR 16.9
CC
37
Cov
87%
update
14.1
_changelog_is_tracked
5.6
delete
5.1
CRAP 5.4
« blib/lib/DBIO/Relationship/CascadeActions.pm ↑ Summary blib/lib/DBIO/ChangeLog/Table.pm »
1 package DBIO::ChangeLog;
2 # ABSTRACT: Row-level change tracking component
3  
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 our $VERSION = '0.900007';
8  
9 11 ▶use base 'DBIO::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 11 my $json = JSON::MaybeXS->new->utf8->canonical->allow_nonref->convert_blessed;
15  
16  
17  
18 11 ▶sub add_columns {
Subroutine add_columns: called CC=5 SCAR=16.2
Pod documented
19 11 my ($self, @cols) = @_;
20 11 my @columns;
21  
22 11 ▶ while (my $col = shift @cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $col = shift @cols) 35 11 46
23 35 ▶ my $info = ref $cols[0] ? shift @cols : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $cols[0] ? : 35 0 35
24  
25 # a false value becomes the marker; a true one stays in the column info, so
26 # a component further down the chain still sees that the column said so
27 35 ▶ if (exists $info->{changelog} and !$info->{changelog}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $info->{"changelog"} and not $info->{"changelog"}) 4 31 35
Condition66%2/3
exists $info->{"changelog"} and not $info->{"changelog"}
!ll&&!rl&&r
3104
MC/DC50%1/2
exists $info->{"changelog"} and not $info->{"changelog"}
exists $info->{"changelog"} not $info->{"changelog"}
Truth table66%2/3
A and not(B)
A= exists $info->{"changelog"}B= not $info->{"changelog"}
ABresult
0X0
100
111
28 4 delete $info->{changelog};
29 4 $info->{_changelog_exclude} = 1;
30 }
31  
32 35 push @columns, $col => $info;
33 }
34  
35 11 return $self->next::method(@columns);
36 }
37  
38  
39 0 ▶sub changelog_column_definitions {
Subroutine changelog_column_definitions: not called CC=1 SCAR=6.9
Pod documented
40 0 return ( changes => { data_type => 'text' } );
41 }
42  
43  
44 41 ▶sub changelog_serialize_changes {
Subroutine changelog_serialize_changes: called CC=1 SCAR=0.0
Pod documented
45 41 my ($self, $changes) = @_;
46 41 return $json->encode($changes);
47 }
48  
49  
50 1 ▶sub changelog_deserialize_changes {
Subroutine changelog_deserialize_changes: called CC=2 SCAR=7.1
Pod documented
51 1 my ($self, $raw) = @_;
52 1 ▶ return undef unless defined $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw 0 1 1
53 1 return $json->decode($raw);
54 }
55  
56  
57 40 ▶sub changelog_write_entry {
Subroutine changelog_write_entry: called CC=1 SCAR=0.0
Pod documented
58 40 my ($self, $entry) = @_;
59  
60 40 my $schema = $self->result_source->schema;
61 40 my $source_name = $self->result_source->source_name;
62 40 my $cl_source_name = $source_name . '_ChangeLog';
63  
64 40 $schema->resultset($cl_source_name)->create($entry);
65 }
66  
67  
68 39 ▶sub changelog_notify { }
Subroutine changelog_notify: called CC=1 SCAR=0.0
Pod documented
69  
70 # ---- Internal helpers ----
71  
72 45 ▶sub _changelog_serialize_pk {
Subroutine _changelog_serialize_pk: called CC=3 SCAR=11.0
73 45 my ($self) = @_;
74 45 my @pk_cols = $self->result_source->primary_columns;
75 45 ▶ if (@pk_cols == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pk_cols == 1) 43 2 45
76 43 ▶ return '' . ($self->get_column($pk_cols[0]) // '');
Condition100%2/2
$self->get_column($pk_cols[0]) // ""
l!l
421
MC/DC100%1/1
$self->get_column($pk_cols[0]) // ""
$self->get_column($pk_cols[0])
Truth table100%2/2
A
A= $self->get_column($pk_cols[0])
Aresult
11
00
77 }
78 2 return $json->encode([ map { $self->get_column($_) } @pk_cols ]);
79 }
80  
81 86 ▶sub _changelog_is_disabled {
Subroutine _changelog_is_disabled: called CC=3 SCAR=11.0
82 86 my ($self) = @_;
83 86 my $schema = $self->result_source->schema;
84 86 ▶ return 0 unless $schema->can('changelog_disabled');
Branch: 1 branch
BranchTrueFalseTotal
unless $schema->can("changelog_disabled") 0 86 86
85 86 ▶ return $schema->changelog_disabled ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$schema->changelog_disabled ? : 4 82 86
86 }
87  
88 85 ▶sub _changelog_is_tracked {
Subroutine _changelog_is_tracked: called CC=4 SCAR=17.3
89 85 my ($self) = @_;
90 85 my $schema = $self->result_source->schema;
91 85 ▶ return 1 unless $schema->can('changelog_sources');
Branch: 1 branch
BranchTrueFalseTotal
unless $schema->can("changelog_sources") 0 85 85
92 85 my $sources = $schema->changelog_sources;
93 85 ▶ return 1 unless $sources && @$sources;
Branch: 1 branch
BranchTrueFalseTotal
unless $sources and @$sources 85 0 85
Condition33%1/3
$sources and @$sources
!ll&&!rl&&r
8500
MC/DC0%0/2
$sources and @$sources
$sources @$sources
Truth table33%1/3
A and B
A= $sourcesB= @$sources
ABresult
0X0
100
111
94 0 my $source_name = $self->result_source->source_name;
95 0 return grep { $_ eq $source_name } @$sources;
96 }
97  
98 40 ▶sub _changelog_current_changeset_id {
Subroutine _changelog_current_changeset_id: called CC=2 SCAR=7.0
99 40 my ($self) = @_;
100 40 my $schema = $self->result_source->schema;
101 40 ▶ return undef unless $schema->can('_changelog_current_changeset_id');
Branch: 1 branch
BranchTrueFalseTotal
unless $schema->can("_changelog_current_changeset_id") 0 40 40
102 40 return $schema->_changelog_current_changeset_id;
103 }
104  
105 40 ▶sub _changelog_get_timestamp {
Subroutine _changelog_get_timestamp: called CC=1 SCAR=0.0
106 40 my ($self) = @_;
107 # Use a simple ISO 8601 string; drivers can override
108 40 require POSIX;
109 40 return POSIX::strftime('%Y-%m-%d %H:%M:%S', localtime);
110 }
111  
112 38 ▶sub _changelog_excluded_columns {
Subroutine _changelog_excluded_columns: called CC=1 SCAR=0.0
113 38 my ($self) = @_;
114 38 my $info = $self->result_source->columns_info;
115 38 return { map { $_ => 1 } grep { $info->{$_}{_changelog_exclude} } keys %$info };
116 }
117  
118 26 ▶sub _changelog_filtered_columns {
Subroutine _changelog_filtered_columns: called CC=2 SCAR=6.9
119 26 my ($self, %cols) = @_;
120 26 my $exclude = $self->_changelog_excluded_columns;
121 26 delete $cols{$_} for keys %$exclude;
122 26 return %cols;
123 }
124  
125 41 ▶sub _changelog_record {
Subroutine _changelog_record: called CC=3 SCAR=11.0
126 41 my ($self, $event, $changes) = @_;
127  
128 41 ▶ return if $self->_changelog_is_disabled;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_changelog_is_disabled 1 40 41
129 40 ▶ return unless $self->_changelog_is_tracked;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_changelog_is_tracked 0 40 40
130  
131 40 my $entry = {
132 changeset_id => $self->_changelog_current_changeset_id,
133 row_id => $self->_changelog_serialize_pk,
134 event => $event,
135 changes => $self->changelog_serialize_changes($changes),
136 created_at => $self->_changelog_get_timestamp,
137 };
138  
139 40 $self->changelog_write_entry($entry);
140 40 $self->changelog_notify($event, $entry);
141 }
142  
143 # ---- Overrides ----
144  
145  
146 20 ▶sub insert {
Subroutine insert: called CC=3 SCAR=11.0
Pod documented
147 20 my ($self, @args) = @_;
148  
149 20 my $result = $self->next::method(@args);
150  
151 20 ▶ if ($self->_changelog_is_tracked && !$self->_changelog_is_disabled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_changelog_is_tracked and not $self->_changelog_is_disabled) 18 2 20
Condition66%2/3
$self->_changelog_is_tracked and not $self->_changelog_is_disabled
!ll&&!rl&&r
0218
MC/DC50%1/2
$self->_changelog_is_tracked and not $self->_changelog_is_disabled
$self->_changelog_is_tracked not $self->_changelog_is_disabled
Truth table66%2/3
A and not(B)
A= $self->_changelog_is_trackedB= not $self->_changelog_is_disabled
ABresult
0X0
100
111
152 18 my %cols = $self->_changelog_filtered_columns($self->get_columns);
153 18 $self->_changelog_record('insert', \%cols);
154 }
155  
156 20 return $result;
157 }
158  
159  
160 11 ▶sub update {
Subroutine update: called CC=13 SCAR=26.4
Pod documented
161 11 my ($self, $upd) = @_;
162  
163 # Apply the update values so get_dirty_columns sees them
164 11 ▶ $self->set_inflated_columns($upd) if $upd;
Branch: 1 branch
BranchTrueFalseTotal
if $upd 11 0 11
165  
166 11 my %dirty = $self->get_dirty_columns;
167  
168 # Filter out excluded columns
169 11 my $exclude = $self->_changelog_excluded_columns;
170 11 delete $dirty{$_} for keys %$exclude;
171  
172 # Capture old values before the update
173 11 my %old_values;
174 11 ▶ if (%dirty && $self->_changelog_is_tracked && !$self->_changelog_is_disabled) {
Branch: 1 branch
BranchTrueFalseTotal
if (%dirty and $self->_changelog_is_tracked and not $self->_changelog_is_disabled) 8 3 11
Condition66%4/6
%dirty and $self->_changelog_is_tracked and not $self->_changelog_is_disabled
!ll&&!rl&&r
308
%dirty and $self->_changelog_is_tracked
!ll&&!rl&&r
308
MC/DC33%1/3
%dirty and $self->_changelog_is_tracked and not $self->_changelog_is_disabled
%dirty $self->_changelog_is_tracked not $self->_changelog_is_disabled
Truth table50%2/4
A and B and not(C)
A= %dirtyB= $self->_changelog_is_trackedC= not $self->_changelog_is_disabled
ABCresult
0XX0
10X0
1100
1111
175 8 for my $col (keys %dirty) {
176 # _column_data_in_storage has the previously stored values
177 $old_values{$col} = exists $self->{_column_data_in_storage}
178 10 ▶ ? $self->{_column_data_in_storage}{$col}
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'_column_data_in_storage'} ? : 10 0 10
179 : undef;
180 }
181 }
182  
183 11 my $result = $self->next::method;
184  
185 11 ▶ if (%dirty && $self->_changelog_is_tracked && !$self->_changelog_is_disabled) {
Branch: 1 branch
BranchTrueFalseTotal
if (%dirty and $self->_changelog_is_tracked and not $self->_changelog_is_disabled) 8 3 11
Condition66%4/6
%dirty and $self->_changelog_is_tracked and not $self->_changelog_is_disabled
!ll&&!rl&&r
308
%dirty and $self->_changelog_is_tracked
!ll&&!rl&&r
308
MC/DC33%1/3
%dirty and $self->_changelog_is_tracked and not $self->_changelog_is_disabled
%dirty $self->_changelog_is_tracked not $self->_changelog_is_disabled
Truth table50%2/4
A and B and not(C)
A= %dirtyB= $self->_changelog_is_trackedC= not $self->_changelog_is_disabled
ABCresult
0XX0
10X0
1100
1111
186 8 my %diffs;
187 8 for my $col (keys %dirty) {
188 10 $diffs{$col} = [ $old_values{$col}, $dirty{$col} ];
189 }
190 8 ▶ $self->_changelog_record('update', \%diffs) if %diffs;
Branch: 1 branch
BranchTrueFalseTotal
if %diffs 8 0 8
191 }
192  
193 11 return $result;
194 }
195  
196  
197 9 ▶sub delete {
Subroutine delete: called CC=5 SCAR=16.3
Pod documented
198 9 my ($self, @args) = @_;
199  
200 9 my %cols;
201 9 ▶ if (ref $self && $self->_changelog_is_tracked && !$self->_changelog_is_disabled) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and $self->_changelog_is_tracked and not $self->_changelog_is_disabled) 8 1 9
Condition50%3/6
ref $self and $self->_changelog_is_tracked and not $self->_changelog_is_disabled
!ll&&!rl&&r
018
ref $self and $self->_changelog_is_tracked
!ll&&!rl&&r
009
MC/DC33%1/3
ref $self and $self->_changelog_is_tracked and not $self->_changelog_is_disabled
ref $self $self->_changelog_is_tracked not $self->_changelog_is_disabled
Truth table50%2/4
A and B and not(C)
A= ref $selfB= $self->_changelog_is_trackedC= not $self->_changelog_is_disabled
ABCresult
0XX0
10X0
1100
1111
202 8 %cols = $self->_changelog_filtered_columns($self->get_columns);
203 }
204  
205 9 my $result = $self->next::method(@args);
206  
207 8 ▶ if (%cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (%cols) 7 1 8
208 7 $self->_changelog_record('delete', \%cols);
209 }
210  
211 8 return $result;
212 }
213  
214  
215 8 ▶sub log_event {
Subroutine log_event: called CC=2 SCAR=6.9
Pod documented
216 8 my ($self, $event_name, $details) = @_;
217 8 ▶ $details //= {};
Condition100%2/2
$details //= {}
l!l
53
MC/DC100%1/1
$details //= {}
$details
Truth table100%2/2
A
A= $details
Aresult
11
00
218 8 $self->_changelog_record($event_name, $details);
219 }
220  
221  
222 5 ▶sub changelog {
Subroutine changelog: called CC=2 SCAR=6.9
Pod documented
223 5 my ($self) = @_;
224 5 my $schema = $self->result_source->schema;
225 5 my $source_name = $self->result_source->source_name;
226 5 my $cl_source_name = $source_name . '_ChangeLog';
227  
228 5 my $rs = $schema->resultset($cl_source_name);
229  
230 # ADR 0045 / karr k325: derived from this row, so the pin of a context-bound
231 # row goes with it (the ->search_rs below passes it on to the clone).
232 5 ▶ $rs->{_async_context} = $self->{_async_context} if $self->{_async_context};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_async_context"} 2 3 5
233  
234 5 return $rs->search_rs({
235 row_id => $self->_changelog_serialize_pk,
236 });
237 }
238  
239 11 1;
240  
241 __END__
« blib/lib/DBIO/Relationship/CascadeActions.pm ↑ Summary blib/lib/DBIO/ChangeLog/Table.pm »