AuditAny/Collector/AutoDBIC.pm

filter: Statementstmt 91.6% 121 / 132 Branchbran 37.5% 9 / 24 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 94.5% 35 / 37 Podpod 100% 4 / 4 totaltotal 86.1% 174 / 202 CC 15 SCAR 11.0
CC
15
Cov
84%
deploy_schema
12.9
get_context_column_infos
5.0
__ANON__
2.1
CRAP 3.0
« AuditAny/AuditContext.pm ↑ Summary AuditAny/Util/ResultMaker.pm »
1 package DBIx::Class::AuditAny::Collector::AutoDBIC;
2 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: Collector class for recording AuditAny changes in auto-generated DBIC schemas
6  
7 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use MooX::Types::MooseLike::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 extends 'DBIx::Class::AuditAny::Collector::DBIC';
10  
11 =head1 NAME
12  
13 DBIx::Class::AuditAny::Collector::AutoDBIC - Collector class for recording AuditAny
14 changes in auto-generated DBIC schemas
15  
16 =head1 DESCRIPTION
17  
18 This Collector facilitates recording ChangeSets, Changes, and Column Changes within a
19 clean relational structure into an automatically configured and deployed DBIC schema
20 using SQLite database files.
21  
22 This class extends L<DBIx::Class::AuditAny::Collector::DBIC> which provides greater
23 flexibility for configuration, can record to different forms of databases and tables,
24 and so on
25  
26 =head1 ATTRIBUTES
27  
28 Docs regarding the API/purpose of the attributes and methods in this class still TBD...
29  
30 =head2 auto_deploy
31  
32 =head2 change_data_rel
33  
34 =head2 change_source_name
35  
36 =head2 changeset_source_name
37  
38 =head2 column_change_source_name
39  
40 =head2 column_data_rel
41  
42 =head2 deploy_info_source_name
43  
44 =head2 reverse_change_data_rel
45  
46 =head2 reverse_changeset_data_rel
47  
48 =head2 sqlite_db
49  
50 =head1 METHODS
51  
52 =head2 get_context_column_infos
53  
54 =head2 init_schema_namespace
55  
56 =head2 deploy_schema
57  
58 =head2 get_clean_md5
59  
60 =cut
61  
62 9 ▶use DBIx::Class::AuditAny::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 9 ▶use DBIx::Class::AuditAny::Util::SchemaMaker;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 9 ▶use String::CamelCase qw(decamelize);
Subroutine BEGIN: called CC=1 SCAR=0.0
65 9 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
66 9 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 has 'connect', is => 'ro', isa => ArrayRef, lazy => 1, default => sub {
69 9 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.1
70 9 ▶ my $db = $self->sqlite_db or die "no 'connect' or 'sqlite_db' specified.";
Branch: 1 branch
BranchTrueFalseTotal
my $db = $self->sqlite_db or die "no 'connect' or 'sqlite_db' specified." 0 9 9
71 9 return [ "dbi:SQLite:dbname=$db","","", { AutoCommit => 1 } ];
72 9 };
73  
74 0 ▶has 'sqlite_db', is => 'ro', isa => Maybe[Str], default => sub{undef};
Subroutine __ANON__: not called CC=1 SCAR=1.2
75 11 ▶has 'auto_deploy', is => 'ro', isa => Bool, default => sub{1};
Subroutine __ANON__: called CC=1 SCAR=0.0
76  
77 has 'target_schema_namespace', is => 'ro', lazy => 1, default => sub {
78 11 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
79 11 return ref($self->AuditObj->schema) . '::AuditSchema';
80 9 };
81  
82 has '+target_schema', default => sub {
83 11 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.2
84
85 11 my $class = $self->init_schema_namespace;
86 9 my $schema = $class->connect(@{$self->connect});
87 9 ▶ $self->deploy_schema($schema) if ($self->auto_deploy);
Branch: 1 branch
BranchTrueFalseTotal
if $self->auto_deploy 9 0 9
88
89 0 return $schema;
90 9 };
91  
92 has 'target_source', is => 'ro', isa => Str, lazy => 1,
93 0 ▶ default => sub { (shift)->changeset_source_name };
Subroutine __ANON__: not called CC=1 SCAR=1.2
94  
95 11 ▶has 'changeset_source_name', is => 'ro', isa => Str, default => sub{'AuditChangeSet'};
Subroutine __ANON__: called CC=1 SCAR=0.0
96 11 ▶has 'change_source_name', is => 'ro', isa => Str, default => sub{'AuditChange'};
Subroutine __ANON__: called CC=1 SCAR=0.0
97 11 ▶has 'column_change_source_name', is => 'ro', isa => Str, default => sub{'AuditChangeColumn'};
Subroutine __ANON__: called CC=1 SCAR=0.0
98 11 ▶has 'deploy_info_source_name', is => 'ro', isa => Str, default => sub{'DeployInfo'};
Subroutine __ANON__: called CC=1 SCAR=0.0
99  
100 has 'changeset_table_name', is => 'ro', isa => Str, lazy => 1,
101 11 ▶ default => sub { decamelize((shift)->changeset_source_name) };
Subroutine __ANON__: called CC=1 SCAR=0.0
102
103 has 'change_table_name', is => 'ro', isa => Str, lazy => 1,
104 11 ▶ default => sub { decamelize((shift)->change_source_name) };
Subroutine __ANON__: called CC=1 SCAR=0.0
105
106 has 'column_change_table_name', is => 'ro', isa => Str, lazy => 1,
107 11 ▶ default => sub { decamelize((shift)->column_change_source_name) };
Subroutine __ANON__: called CC=1 SCAR=0.0
108  
109 has 'deploy_info_table_name', is => 'ro', isa => Str, lazy => 1,
110 11 ▶ default => sub { decamelize((shift)->deploy_info_source_name) };
Subroutine __ANON__: called CC=1 SCAR=0.0
111  
112 11 ▶has '+change_data_rel', default => sub{'audit_changes'};
Subroutine __ANON__: called CC=1 SCAR=0.0
113 11 ▶has '+column_data_rel', default => sub{'audit_change_columns'};
Subroutine __ANON__: called CC=1 SCAR=0.0
114 11 ▶has 'reverse_change_data_rel', is => 'ro', isa => Str, default => sub{'change'};
Subroutine __ANON__: called CC=1 SCAR=0.0
115 11 ▶has 'reverse_changeset_data_rel', is => 'ro', isa => Str, default => sub{'changeset'};
Subroutine __ANON__: called CC=1 SCAR=0.0
116  
117 has 'changeset_columns', is => 'ro', isa => ArrayRef, lazy => 1,
118 default => sub {
119 11 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
120 return [
121 11 id => {
122 data_type => "integer",
123 extra => { unsigned => 1 },
124 is_auto_increment => 1,
125 is_nullable => 0,
126 },
127 $self->get_context_column_infos(qw(base set))
128 ];
129 9 };
130  
131 has 'change_columns', is => 'ro', isa => ArrayRef, lazy => 1,
132 default => sub {
133 11 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
134 return [
135 11 id => {
136 data_type => "integer",
137 extra => { unsigned => 1 },
138 is_auto_increment => 1,
139 is_nullable => 0,
140 },
141 changeset_id => {
142 data_type => "integer",
143 extra => { unsigned => 1 },
144 is_foreign_key => 1,
145 is_nullable => 0,
146 },
147 $self->get_context_column_infos(qw(source change))
148 ];
149 9 };
150  
151 has 'change_column_columns', is => 'ro', isa => ArrayRef, lazy => 1,
152 default => sub {
153 11 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
154 return [
155 11 id => {
156 data_type => "integer",
157 extra => { unsigned => 1 },
158 is_auto_increment => 1,
159 is_nullable => 0,
160 },
161 change_id => {
162 data_type => "integer",
163 extra => { unsigned => 1 },
164 is_foreign_key => 1,
165 is_nullable => 0,
166 },
167 $self->get_context_column_infos(qw(column))
168 ];
169 9 };
170  
171 # Gets and validates DBIC column configs per supplied datapoint contexts
172 sub get_context_column_infos {
173 33 ▶ my $self = shift;
Subroutine get_context_column_infos: called CC=5 SCAR=16.1
Pod documented
174 33 my @DataPoints = $self->AuditObj->get_context_datapoints(@_);
175 33 ▶ return () unless (scalar @DataPoints > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @DataPoints > 0 4 29 33
176
177 29 my %reserved = map {$_=>1} qw(id changeset_id change_id);
178 29 my %no_accessor = map {$_=>1} qw(new meta);
179
180 29 my @cols = ();
181 29 foreach my $DataPoint (@DataPoints) {
182 120 my $name = $DataPoint->name;
183 120 my $info = $DataPoint->column_info;
184 120 ▶ $reserved{$name} and die "Bad datapoint name '$name' - reserved keyword.";
Branch: 1 branch
BranchTrueFalseTotal
$reserved{$name} and die "Bad datapoint name '${name}' - reserved keyword." 0 120 120
185 120 ▶ $no_accessor{$name} and $info->{accessor} = undef;
Branch: 1 branch
BranchTrueFalseTotal
$no_accessor{$name} and $info->{"accessor"} = undef 6 114 120
186 120 push @cols, ( $name => $info );
187 }
188
189 29 return @cols;
190 }
191  
192  
193 has 'schema_namespace_config', is => 'ro', isa => HashRef, init_arg => undef, lazy => 1,
194 default => sub {
195 11 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
196
197 11 my $ColumnName = $self->AuditObj->get_datapoint_orig('column_name');
198 11 ▶ my $col_context_uniq_const = $ColumnName ?
Branch: 1 branch
BranchTrueFalseTotal
$ColumnName ? : 11 0 11
199 [ add_unique_constraint => ["change_id", ["change_id", $ColumnName->name]] ] : [];
200  
201 11 my $namespace = $self->target_schema_namespace;
202 return {
203 11 schema_namespace => $namespace,
204 results => {
205 $self->deploy_info_source_name => {
206 table_name => $self->deploy_info_table_name,
207 columns => [
208 md5 => {
209 data_type => "char",
210 is_nullable => 0,
211 size => 32
212 },
213 comment => {
214 data_type => "varchar",
215 is_nullable => 0,
216 size => 255
217 },
218 deployed_ddl => {
219 data_type => 'mediumtext',
220 is_nullable => 0
221 },
222 deployed_ts => {
223 data_type => "datetime",
224 datetime_undef_if_invalid => 1,
225 is_nullable => 0
226 },
227 auditany_params => {
228 data_type => 'mediumtext',
229 is_nullable => 0
230 },
231 ],
232 call_class_methods => [
233 set_primary_key => ['md5'],
234 ]
235 },
236 $self->changeset_source_name => {
237 table_name => $self->changeset_table_name,
238 columns => $self->changeset_columns,
239 call_class_methods => [
240 set_primary_key => ['id'],
241 has_many => [
242 $self->change_data_rel,
243 $namespace . '::' . $self->change_source_name,
244 { "foreign.changeset_id" => "self.id" },
245 { cascade_copy => 0, cascade_delete => 0 },
246 ]
247 ]
248 },
249 $self->change_source_name => {
250 table_name => $self->change_table_name,
251 columns => $self->change_columns,
252 call_class_methods => [
253 set_primary_key => ['id'],
254 belongs_to => [
255 $self->reverse_changeset_data_rel,
256 $namespace . '::' . $self->changeset_source_name,
257 { id => "changeset_id" },
258 { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" },
259 ],
260 has_many => [
261 $self->column_data_rel,
262 $namespace . '::' . $self->column_change_source_name,
263 { "foreign.change_id" => "self.id" },
264 { cascade_copy => 0, cascade_delete => 0 },
265 ]
266 ]
267 },
268 $self->column_change_source_name => {
269 table_name => $self->column_change_table_name,
270 columns => $self->change_column_columns,
271 call_class_methods => [
272 set_primary_key => ['id'],
273 @$col_context_uniq_const,
274 #add_unique_constraint => ["change_id", ["change_id", "column_name"]],
275 belongs_to => [
276 $self->reverse_change_data_rel,
277 $namespace . '::' . $self->change_source_name,
278 { id => "change_id" },
279 { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" },
280 ],
281 ]
282 }
283 }
284 };
285 9 };
286  
287 sub init_schema_namespace {
288 11 ▶ my $self = shift;
Subroutine init_schema_namespace: called CC=1 SCAR=0.0
Pod documented
289
290 #scream($self->schema_namespace_config);
291
292 return DBIx::Class::AuditAny::Util::SchemaMaker->initialize(
293 11 %{ $self->schema_namespace_config }
294 );
295 }
296  
297  
298 sub deploy_schema {
299 9 ▶ my $self = shift;
Subroutine deploy_schema: called CC=6 SCAR=25.5
Pod documented
300 9 my $schema = shift;
301
302 9 my $deploy_statements = $schema->deployment_statements;
303 9 my $md5 = $self->get_clean_md5($deploy_statements);
304 9 my $Rs = $schema->resultset($self->deploy_info_source_name);
305 9 my $table = $Rs->result_source->from;
306 9 my $deployRow;
307
308 try {
309 9 ▶ $deployRow = $Rs->find($md5);
Subroutine __ANON__: called CC=1 SCAR=0.0
310 }
311 catch {
312 # Assume exception is due to not being deployed yet and try to deploy:
313 9 ▶ $schema->deploy;
Subroutine __ANON__: called CC=2 SCAR=7.0
314
315 # Save the actual AuditAny params, ->track() or ->new():
316 9 local $Data::Dumper::Maxdepth = 3;
317 9 ▶ my $auditany_params = $self->AuditObj->track_init_args ?
Branch: 1 branch
BranchTrueFalseTotal
$self->AuditObj->track_init_args ? : 9 0 9
318 Data::Dumper->Dump([$self->AuditObj->track_init_args],['*track']) :
319 Data::Dumper->Dump([$self->AuditObj->build_init_args],['*new']);
320
321 9 $Rs->create({
322 md5 => $md5,
323 comment => 'DO NOT REMOVE THIS ROW',
324 deployed_ddl => $deploy_statements,
325 deployed_ts => $self->AuditObj->get_dt,
326 auditany_params => $auditany_params
327 });
328 9 };
329
330 # If we've already been deployed and the ddl checksum matches:
331 0 ▶ return 1 if ($deployRow);
Branch: 1 branch
BranchTrueFalseTotal
if $deployRow 0 0 0
332
333 0 my $count = $Rs->count;
334 0 my $dsn = $self->connect->[0];
335
336 0 ▶ die "Database error; deploy_info table ('$table') exists but is empty in audit database '$dsn'"
Branch: 1 branch
BranchTrueFalseTotal
unless $count > 0 0 0 0
337 unless ($count > 0);
338
339 0 ▶ die "Database error; multiple rows in deploy_info table ('$table') in audit database '$dsn'"
Branch: 1 branch
BranchTrueFalseTotal
if $count > 1 0 0 0
340 if ($count > 1);
341
342 0 ▶ my $exist_md5 = $Rs->first->md5 or die "Database error; found deploy_info row in table '$table' " .
Branch: 1 branch
BranchTrueFalseTotal
my $exist_md5 = $Rs->first->md5 or die "Database error; found deploy_info row in table '${table}' " . "in audit database '${dsn}', but it appears to be corrupt (no md5 checksum)." 0 0 0
343 "in audit database '$dsn', but it appears to be corrupt (no md5 checksum).";
344
345 0 ▶ return 1 if ($md5 eq $exist_md5);
Branch: 1 branch
BranchTrueFalseTotal
if $md5 eq $exist_md5 0 0 0
346
347 0 die "\n\n" . join("\n",
348 " The selected audit database '$dsn' already has a",
349 " deployed Collector::AutoDBIC schema (md5 checksum: $exist_md5) but it does",
350 " not match the current auto-generated schema (md5 checksum: $md5).",
351 " This probably means datapoints or other options have been changed since this AutoDBIC ",
352 " audit database was deployed. If you're not worried about existing audit logs, you can ",
353 " fix this error by simply clearing/deleting the audit database so it can be reinitialized."
354 ) . "\n\n";
355 }
356  
357 # Need to strip out comments and blank lines to make sure the md5s will be consistent
358 sub get_clean_md5 {
359 9 ▶ my $self = shift;
Subroutine get_clean_md5: called CC=2 SCAR=6.9
Pod documented
360 9 my $deploy_statements = shift;
361 9 ▶ my $clean = join("\n", grep { ! /^\-\-/ && ! /^\s*$/ } split(/\r?\n/,$deploy_statements) );
Condition100%3/3
/^\-\-/ || /^\s*$/
l!l&&r!l&&!r
14481330
MC/DC100%2/2
/^\-\-/ || /^\s*$/
/^\-\-/ /^\s*$/
Truth table100%3/3
A || B
A= /^\-\-/B= /^\s*$/
ABresult
1X1
011
000
362 9 return md5_hex($clean);
363 }
364  
365 9 1;
366  
367 __END__
368  
369 =head1 SEE ALSO
370  
371 =over
372  
373 =item *
374  
375 L<DBIx::Class::AuditAny>
376  
377 =item *
378  
379 L<DBIx::Class>
380  
381 =back
382  
383 =head1 SUPPORT
384
385 IRC:
386
387 Join #rapidapp on irc.perl.org.
388  
389 =head1 AUTHOR
390  
391 Henry Van Styn <vanstyn@cpan.org>
392  
393 =head1 COPYRIGHT AND LICENSE
394  
395 This software is copyright (c) 2012-2015 by IntelliTree Solutions llc.
396  
397 This is free software; you can redistribute it and/or modify it under
398 the same terms as the Perl 5 programming language system itself.
399  
400 =cut
« AuditAny/AuditContext.pm ↑ Summary AuditAny/Util/ResultMaker.pm »