PgLog.pm

filter: Statementstmt 3.4% 3 / 86 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 8 Podpod 100% 3 / 3 totaltotal 4.1% 6 / 146 CC 23 SCAR 35.8
CC
23
Cov
2%
_store_changes
72.0
update
42.0
_modify_pg_log_value
30.0
CRAP 35.9
« Schema/PgLog/Structure.pm ↑ Summary Schema/PgLog.pm »
1 package DBIx::Class::PgLog;
2  
3 =head1 NAME
4  
5 DBIx::Class::PgLog - Postgres simple activity loging for DBIx::Class
6  
7 The PgLog schema consists of 2 tables LogSet and Log, Log table extensively makes use of the power of Postgres to store the Columns, OldValues and NewValues in an Column Array format to avoid the relational database structure which imporves the performance of write and read from PgLog.
8  
9 =head1 VERSION
10  
11 Version 0.03
12  
13 =cut
14  
15 3 our $VERSION = '0.03';
16  
17 =head1 SYNOPSIS
18  
19 DBIx::Class::PgLog is meant for Logging changes made to specific tables in your database.
20  
21 Any insert/update/delete that requires auditing must be wrapped in a L<"txn_do"|DBIx::Class::Schema/"txn_do"> statement.
22  
23 Transactions are saved as LogSets. Each LogSet can have many Log's with TableAction as INSERT/UPDATE/DELETE.
24  
25 =head1 DESCRIPTION
26  
27 Enable the PgLog schema component in your L<DBIx::Class::Schema> class file:
28  
29 package My::Schema;
30 use base qw/DBIx::Class::Schema/;
31  
32 __PACKAGE__->load_components(qw/Schema::PgLog/);
33  
34 Enable the PgLog component in your the individual L<DBIx::Class> table class files that you want to enable logging on:
35  
36 package My::Schema::Result::Table
37 use base qw/DBIx::Class::Core/;
38  
39 __PACKAGE__->load_components(qw/PgLog/);
40  
41 If you want to use methods created by L<DBIx::Class::Relationship::Base>, like "add_to_$rel" or "set_$rel",
42 if you are planing to use L<DBIx::Class::ResultSet/delete> or L<DBIx::Class::ResultSet/update> or if you use
43 modules which make use of these methods (like L<HTML::FormHandler> or L<DBIx::Class::ResultSet::RecursiveUpdate>,
44 load the PgLog-component in your ResultSet classes:
45  
46 package My::Schema::ResultSet::Table;
47  
48 use base 'DBIx::Class::ResultSet';
49  
50 __PACKAGE__->load_components('ResultSet::PgLog');
51  
52 1;
53  
54 In your application wrap any insert/update/delete in a transaction to have pg log activated:
55  
56 * Mandatorily Pass an extra hashref to the txn_do method to indicate a UserId and optional Description for the transaction:
57  
58 $my_schema->txn_do(
59 sub {
60 $my_row->update({ ... });
61 },
62 {
63 UserId => 'User_id',
64 Description => 'description of transaction' # optional
65 }
66 );
67  
68 =head1 DBIx::Class OVERRIDDEN METHODS
69
70 =head2 insert
71
72 =head2 update
73
74 =head2 delete
75  
76 =cut
77  
78 # local $DBIx::Class::AuditLog::enabled = 0;
79 # can be set to temporarily disable audit logging
80 3 our $enabled = 1;
81  
82 0 ▶sub insert {
Subroutine insert: not called CC=3 SCAR=24.8
Pod documented
83 0 my $self = shift;
84  
85 0 ▶ return $self->next::method(@_) if !$enabled || $self->in_storage;
Branch: 1 branch
BranchTrueFalseTotal
if not $enabled or $self->in_storage 0 0 0
Condition0%0/3
not $enabled or $self->in_storage
l!l&&r!l&&!r
000
MC/DC0%0/2
not $enabled or $self->in_storage
not $enabled $self->in_storage
Truth table0%0/3
not(A) or B
A= not $enabledB= $self->in_storage
ABresult
1X1
011
000
86  
87 0 my $result = $self->next::method(@_);
88  
89 0 my $action = "INSERT";
90  
91 0 my %column_data = $result->get_columns;
92 0 $self->_store_changes( $action, $result, {}, \%column_data );
93  
94 0 return $result;
95 }
96  
97 0 ▶sub update {
Subroutine update: not called CC=6 SCAR=37.4
Pod documented
98 0 my $self = shift;
99  
100 0 ▶ return $self->next::method(@_) if !$enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $enabled 0 0 0
101  
102 0 my $stored_row = $self->get_from_storage;
103 0 my %new_data = $self->get_columns;
104 0 ▶ my @changed_columns = keys %{ $_[0] || {} };
Condition0%0/2
$_[0] || {}
l!l
00
MC/DC0%0/1
$_[0] || {}
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
105  
106 0 my $result = $self->next::method(@_);
107  
108 0 ▶ return unless $stored_row; # update on deleted row - nothing to log
Branch: 1 branch
BranchTrueFalseTotal
unless $stored_row 0 0 0
109  
110 0 my %old_data = $stored_row->get_columns;
111  
112 0 ▶ if (@changed_columns) {
Branch: 1 branch
BranchTrueFalseTotal
if (@changed_columns) 0 0 0
113 0 @new_data{@changed_columns} = map $self->get_column($_),
114 @changed_columns;
115 }
116  
117 0 ▶ if ( keys %new_data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %new_data) 0 0 0
118 0 my $action = "UPDATE";
119 0 $self->_store_changes( $action, $result, \%old_data, \%new_data );
120 }
121  
122 0 return $result;
123 }
124  
125 0 ▶sub delete {
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
126 0 my $self = shift;
127  
128 0 ▶ return $self->next::method(@_) if !$enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $enabled 0 0 0
129  
130 0 my $stored_row = $self->get_from_storage;
131  
132 0 my $result = $self->next::method(@_);
133  
134 0 my $action = "DELETE";
135 0 my %old_data = $stored_row->get_columns;
136 0 $self->_store_changes( $action, $result, \%old_data, {} );
137  
138 0 return $result;
139 }
140  
141 =head1 HELPER METHODS
142  
143 =head2 _pg_log_schema
144  
145 Returns PgLog schema from storage
146  
147 my $pl_schema = $schema->pg_log_schema;
148  
149 =cut
150  
151 0 ▶sub _pg_log_schema {
Subroutine _pg_log_schema: not called CC=1 SCAR=6.9
152 0 my $self = shift;
153 0 return $self->result_source->schema->pg_log_schema;
154 }
155  
156 =head2 _store_changes
157
158 Store the column data that has changed
159
160 Requires:
161 action: the action object that has associated changes
162 old_values: the old values are being replaced
163 new_values: the new values that are replacing the old
164 table: dbic object of the audit_log_table object
165  
166 =cut
167  
168 0 ▶sub _store_changes {
Subroutine _store_changes: not called CC=8 SCAR=42.8
169 0 my $self = shift;
170 0 my $action = shift;
171 0 my $row = shift;
172 0 my $old_values = shift;
173 0 my $new_values = shift;
174  
175 0 my $table = $row->result_source_instance->name;
176 0 my $log_data = {};
177  
178 0 ▶ foreach my $column (
Branch: 1 branch
BranchTrueFalseTotal
keys %{$new_values;} ? : 0 0 0
179 0 keys %{$new_values} ? keys %{$new_values} : keys %{$old_values} )
180 {
181 0 ▶ if ( $self->_do_pg_log($column) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_do_pg_log($column)) 0 0 0
182 0 push(@{$log_data->{Columns}}, $column);
183 0 ▶ if(ref($old_values->{$column}) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $old_values->{$column} eq "ARRAY") 0 0 0
184 0 $old_values->{$column} = "{".join(",", @{$old_values->{$column}})."}";
185 }
186 0 ▶ if(ref($new_values->{$column}) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $new_values->{$column} eq "ARRAY") 0 0 0
187 0 $new_values->{$column} = "{".join(",", @{$new_values->{$column}})."}";
188 }
189 0 ▶ if ( $self->_do_modify_pg_log_value($column) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_do_modify_pg_log_value($column)) { } 0 0 0
190 0 push(@{$log_data->{NewValues}}, $self->_modify_pg_log_value( $column, $new_values->{$column} ));
191 0 push(@{$log_data->{OldValues}}, $self->_modify_pg_log_value( $column, $old_values->{$column} ));
192 } else {
193 0 push(@{$log_data->{NewValues}}, $new_values->{$column});
194 0 push(@{$log_data->{OldValues}}, $old_values->{$column});
195 }
196  
197 }
198  
199 }
200
201 0 $log_data->{Table} = $table;
202 0 ▶ $log_data->{TableId} = $row->can(Id)?$row->Id:$row->id;
Branch: 1 branch
BranchTrueFalseTotal
$row->can('Id') ? : 0 0 0
203 0 $log_data->{TableAction} = $action;
204  
205 0 $self->_pg_log_schema->pg_log_create_log($log_data);
206  
207 }
208  
209 =head2 _do_pg_log
210
211 Returns 1 or 0 if the column should be audited or not.
212
213 Requires:
214 column: the name of the column/field to check
215  
216 =cut
217  
218 0 ▶sub _do_pg_log {
Subroutine _do_pg_log: not called CC=3 SCAR=24.8
219 0 my $self = shift;
220 0 my $column = shift;
221  
222 0 my $info = $self->column_info($column);
223 return defined $info->{pg_log_column}
224 0 ▶ && $info->{pg_log_column} == 0 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $info->{'pg_log_column'} && $info->{'pg_log_column'} == 0 ? : 0 0 0
Condition0%0/3
defined $info->{"pg_log_column"} and $info->{"pg_log_column"} == 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $info->{"pg_log_column"} and $info->{"pg_log_column"} == 0
defined $info->{"pg_log_column"} $info->{"pg_log_column"} == 0
Truth table0%0/3
A and B
A= defined $info->{"pg_log_column"}B= $info->{"pg_log_column"} == 0
ABresult
0X0
100
111
225 }
226  
227 =head2 _do_modify_pg_log_value
228  
229 Returns 1 or 0 if the columns value should be modified before audit.
230
231 Requires:
232 column: the name of the column/field to check
233  
234 =cut
235  
236 0 ▶sub _do_modify_pg_log_value {
Subroutine _do_modify_pg_log_value: not called CC=2 SCAR=17.9
237 0 my $self = shift;
238 0 my $column = shift;
239  
240 0 my $info = $self->column_info($column);
241  
242 0 ▶ return $info->{modify_pg_log_value} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$info->{'modify_pg_log_value'} ? : 0 0 0
243 }
244  
245 =head2 _modify_pg_log_value
246  
247 Modifies the colums audit-value. Dies if no modify-method could be found.
248
249 Returns:
250 the modified value
251
252 Requires:
253 column: the name of the column/field to check
254 value: the original value
255  
256 =cut
257  
258 0 ▶sub _modify_pg_log_value {
Subroutine _modify_pg_log_value: not called CC=5 SCAR=34.0
259 0 my $self = shift;
260 0 my $column = shift;
261 0 my $value = shift;
262  
263 0 my $info = $self->column_info($column);
264 0 my $meth = $info->{modify_pg_log_value};
265 0 ▶ return $value
Branch: 1 branch
BranchTrueFalseTotal
unless defined $meth 0 0 0
266 unless defined $meth;
267  
268 0 ▶ return &$meth( $self, $value )
Branch: 1 branch
BranchTrueFalseTotal
if ref $meth eq "CODE" 0 0 0
269 if ref($meth) eq 'CODE';
270  
271 0 ▶ $meth = "modify_pg_log_$column"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($meth) 0 0 0
272 unless $self->can($meth);
273  
274 0 ▶ return $self->$meth($value)
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($meth) 0 0 0
275 if $self->can($meth);
276  
277 0 die "unable to find modify_pg_log_method ($meth) for $column in $self";
278  
279 }
280  
281 =head1 ADDITIONAL DBIC COLUMN ATTRIBUTES
282  
283 Individual columns can have additional attributes added to change the Audit Log functionality.
284  
285 =head2 pg_log_column
286  
287 On an individual column basis you can disable auditing by setting 'pg_log_column' to 0:
288  
289 __PACKAGE__->add_columns(
290 "admin_id",
291 { data_type => "integer", is_auto_increment => 1, is_nullable => 0, pg_log_column => 0 },
292 "admin_name",
293 { data_type => "varchar", is_nullable => 0, size => 20 },
294 "admin_pasword",
295 { data_type => "varchar", is_nullable => 0, size => 20 },
296 );
297  
298 If you are using a DBIx::Class generated schema, and don't want to modify the column defintions directly, you can add the following to the editable portion of the Result Class file:
299  
300 __PACKAGE__->add_columns(
301 "+admin_id",
302 { pg_log_column => 0, }
303 );
304  
305 =head2 modify_pg_log_value
306  
307 It is possible to modify the values stored by DBIC::PgLog on a per-column basis
308 by setting the 'modify_pg_log_value' attibute to either a CodeRef, a method
309 name or any true value. The configured code will be run as an object method of
310 the current DBIC::Result object, and expects the original value as parameter.
311  
312 If 'modify_pg_log_value' is set to a true value which is NOT a method in the
313 current objects class, PgLog will look for a method called
314 'modify_pg_log_$colname', where $colname is the name of the corresponding column.
315  
316 Note: PgLog will simply die if it can not find the modification method while
317 'modify_pg_log_value' is true.
318  
319 The following examples have the same result:
320  
321 passing a coderef:
322  
323 __PACKAGE__->add_columns(
324 "+name",
325 { modify_pg_log_value => sub{
326 my ($self, $value) = @_;
327 $value =~ tr/A-Z/a-z/;
328 return $value;
329 }, }
330 );
331  
332 passing a method name:
333  
334 __PACKAGE__->add_columns(
335 "+name",
336 { modify_pg_log_value => 'to_lowercase'},
337 );
338  
339 sub to_lowercase{
340 my ($self, $value) = @_;
341 $value =~ tr/A-Z/a-z/;
342 return $value;
343 }
344  
345 passing a true value which is NOT a method name:
346  
347 __PACKAGE__->add_columns(
348 "+name",
349 { modify_pg_log_value => 1},
350 );
351  
352 sub modify_pg_log_name{
353 my ($self, $value) = @_;
354 $value =~ tr/A-Z/a-z/;
355 return $value;
356 }
357  
358 =head1 DEPLOYMENT
359  
360 To deploy an PgLog schema, load your main schema, and then run the deploy command on the pg_log_schema:
361  
362 my $schema = PgLogTestPg::Schema->connect( "DBI:Pg:dbname=pg_log_test",
363 "sheeju", "sheeju", { RaiseError => 1, PrintError => 1, 'quote_char' => '"', 'quote_field_names' => '0', 'name_sep' => '.' } ) || die("cant connect");;
364
365 $schema->pg_log_schema->deploy();
366  
367 The db user that is deploying the schema must have the correct create table permissions.
368  
369 Note: this should only be run once.
370  
371 =head1 METHODS
372  
373 =head2 pg_log_schema
374  
375 =over 4
376  
377 =item Returns: DBIC schema
378  
379 =back
380  
381 The PgLog schema can be accessed from your main schema by calling the pg_log_schema method.
382  
383 my $pl_schema = $schema->pg_log_schema;
384  
385 =head1 SEE ALSO
386  
387 =over 4
388  
389 =item * L<DBIx::Class>
390  
391 =item * L<DBIx::Class::Journal>
392  
393 =item * L<DBIx::Class::AuditLog>
394  
395 =back
396  
397 =head1 ACKNOWLEDGEMENTS
398  
399 Development time supported by Exceleron L<www.exceleron.com|http://www.exceleron.com>.
400  
401 Many ideas and code borrowed from L<DBIx::Class::AuditLog>.
402  
403 =head1 AUTHOR
404  
405 Sheeju Alex, C<< <sheeju at exceleron.com> >>
406  
407 =head1 BUGS
408  
409 https://github.com/sheeju/DBIx-Class-PgLog/issues
410  
411 =head1 SUPPORT
412  
413 You can find documentation for this module with the perldoc command.
414  
415 perldoc DBIx::Class::PgLog
416  
417  
418 You can also look for information at:
419  
420 =over 4
421  
422 =item * RT: CPAN's request tracker (report bugs here)
423  
424 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class-PgLog>
425  
426 =item * AnnoCPAN: Annotated CPAN documentation
427  
428 L<http://annocpan.org/dist/DBIx-Class-PgLog>
429  
430 =item * CPAN Ratings
431  
432 L<http://cpanratings.perl.org/d/DBIx-Class-PgLog>
433  
434 =item * Search CPAN
435  
436 L<http://search.cpan.org/dist/DBIx-Class-PgLog/>
437  
438 =back
439  
440 =head1 LICENSE AND COPYRIGHT
441  
442 Copyright 2013 Sheeju Alex.
443  
444 This program is free software; you can redistribute it and/or modify it
445 under the terms of the the Artistic License (2.0). You may obtain a
446 copy of the full license at:
447  
448 L<http://www.perlfoundation.org/artistic_license_2_0>
449  
450 Any use, modification, and distribution of the Standard or Modified
451 Versions is governed by this Artistic License. By using, modifying or
452 distributing the Package, you accept this license. Do not use, modify,
453 or distribute the Package, if you do not accept this license.
454  
455 If your Modified Version has been derived from a Modified Version made
456 by someone other than you, you are nevertheless required to ensure that
457 your Modified Version complies with the requirements of this license.
458  
459 This license does not grant you the right to use any trademark, service
460 mark, tradename, or logo of the Copyright Holder.
461  
462 This license includes the non-exclusive, worldwide, free-of-charge
463 patent license to make, have made, use, offer to sell, sell, import and
464 otherwise transfer the Package with respect to any patent claims
465 licensable by the Copyright Holder that are necessarily infringed by the
466 Package. If you institute patent litigation (including a cross-claim or
467 counterclaim) against any party alleging that the Package constitutes
468 direct or contributory patent infringement, then this Artistic License
469 to you shall terminate on the date that such litigation is filed.
470  
471 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
472 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
473 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
474 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
475 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
476 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
477 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
478 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
479  
480 =cut
481  
482 3 1; # End of DBIx::Class::PgLog
« Schema/PgLog/Structure.pm ↑ Summary Schema/PgLog.pm »