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 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶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 11 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use MooX::Types::MooseLike::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 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 11 ▶use DBIx::Class::AuditAny::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 11 ▶use DBIx::Class::AuditAny::Util::SchemaMaker;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 11 ▶use String::CamelCase qw(decamelize);
Subroutine BEGIN: called CC=1 SCAR=0.0
65 11 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
66 11 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 11 ▶has 'connect', is => 'ro', isa => ArrayRef, lazy => 1, default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
69 11 my $self = shift;
70 11 ▶ 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." 11 0 11
71 11 return [ "dbi:SQLite:dbname=$db","","", { AutoCommit => 1 } ];
72 11 };
73  
74 0 ▶has 'sqlite_db', is => 'ro', isa => Maybe[Str], default => sub{undef};
Subroutine __ANON__: not called CC=1 SCAR=1.2
75 13 ▶has 'auto_deploy', is => 'ro', isa => Bool, default => sub{1};
Subroutine __ANON__: called CC=1 SCAR=0.0
76  
77 13 ▶has 'target_schema_namespace', is => 'ro', lazy => 1, default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
78 13 my $self = shift;
79 13 return ref($self->AuditObj->schema) . '::AuditSchema';
80 11 };
81  
82 13 ▶has '+target_schema', default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
83 13 my $self = shift;
84
85 13 my $class = $self->init_schema_namespace;
86 11 my $schema = $class->connect(@{$self->connect});
87 11 ▶ $self->deploy_schema($schema) if ($self->auto_deploy);
Branch: 1 branch
BranchTrueFalseTotal
if $self->auto_deploy 11 0 11
88
89 0 return $schema;
90 11 };
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 13 ▶has 'changeset_source_name', is => 'ro', isa => Str, default => sub{'AuditChangeSet'};
Subroutine __ANON__: called CC=1 SCAR=0.0
96 13 ▶has 'change_source_name', is => 'ro', isa => Str, default => sub{'AuditChange'};
Subroutine __ANON__: called CC=1 SCAR=0.0
97 13 ▶has 'column_change_source_name', is => 'ro', isa => Str, default => sub{'AuditChangeColumn'};
Subroutine __ANON__: called CC=1 SCAR=0.0
98 13 ▶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 13 ▶ 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 13 ▶ 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 13 ▶ 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 13 ▶ default => sub { decamelize((shift)->deploy_info_source_name) };
Subroutine __ANON__: called CC=1 SCAR=0.0
111  
112 13 ▶has '+change_data_rel', default => sub{'audit_changes'};
Subroutine __ANON__: called CC=1 SCAR=0.0
113 13 ▶has '+column_data_rel', default => sub{'audit_change_columns'};
Subroutine __ANON__: called CC=1 SCAR=0.0
114 13 ▶has 'reverse_change_data_rel', is => 'ro', isa => Str, default => sub{'change'};
Subroutine __ANON__: called CC=1 SCAR=0.0
115 13 ▶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 13 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
119 13 my $self = shift;
120 return [
121 13 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 11 };
130  
131 has 'change_columns', is => 'ro', isa => ArrayRef, lazy => 1,
132 13 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
133 13 my $self = shift;
134 return [
135 13 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 11 };
150  
151 has 'change_column_columns', is => 'ro', isa => ArrayRef, lazy => 1,
152 13 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
153 13 my $self = shift;
154 return [
155 13 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 11 };
170  
171 # Gets and validates DBIC column configs per supplied datapoint contexts
172 39 ▶sub get_context_column_infos {
Subroutine get_context_column_infos: called CC=5 SCAR=16.1
Pod documented
173 39 my $self = shift;
174 39 my @DataPoints = $self->AuditObj->get_context_datapoints(@_);
175 39 ▶ return () unless (scalar @DataPoints > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @DataPoints > 0 4 35 39
176
177 35 my %reserved = map {$_=>1} qw(id changeset_id change_id);
178 35 my %no_accessor = map {$_=>1} qw(new meta);
179
180 35 my @cols = ();
181 35 foreach my $DataPoint (@DataPoints) {
182 142 my $name = $DataPoint->name;
183 142 my $info = $DataPoint->column_info;
184 142 ▶ $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 142 142
185 142 ▶ $no_accessor{$name} and $info->{accessor} = undef;
Branch: 1 branch
BranchTrueFalseTotal
$no_accessor{$name} and $info->{"accessor"} = undef 8 134 142
186 142 push @cols, ( $name => $info );
187 }
188
189 35 return @cols;
190 }
191  
192  
193 has 'schema_namespace_config', is => 'ro', isa => HashRef, init_arg => undef, lazy => 1,
194 13 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
195 13 my $self = shift;
196
197 13 my $ColumnName = $self->AuditObj->get_datapoint_orig('column_name');
198 13 ▶ my $col_context_uniq_const = $ColumnName ?
Branch: 1 branch
BranchTrueFalseTotal
$ColumnName ? : 13 0 13
199 [ add_unique_constraint => ["change_id", ["change_id", $ColumnName->name]] ] : [];
200  
201 13 my $namespace = $self->target_schema_namespace;
202 return {
203 13 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 11 };
286  
287 13 ▶sub init_schema_namespace {
Subroutine init_schema_namespace: called CC=1 SCAR=0.0
Pod documented
288 13 my $self = shift;
289
290 #scream($self->schema_namespace_config);
291
292 return DBIx::Class::AuditAny::Util::SchemaMaker->initialize(
293 13 %{ $self->schema_namespace_config }
294 );
295 }
296  
297  
298 11 ▶sub deploy_schema {
Subroutine deploy_schema: called CC=6 SCAR=25.5
Pod documented
299 11 my $self = shift;
300 11 my $schema = shift;
301
302 11 my $deploy_statements = $schema->deployment_statements;
303 11 my $md5 = $self->get_clean_md5($deploy_statements);
304 11 my $Rs = $schema->resultset($self->deploy_info_source_name);
305 11 my $table = $Rs->result_source->from;
306 11 my $deployRow;
307
308 11 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
309 11 $deployRow = $Rs->find($md5);
310 }
311 11 ▶ catch {
Subroutine __ANON__: called CC=2 SCAR=7.0
312 # Assume exception is due to not being deployed yet and try to deploy:
313 11 $schema->deploy;
314
315 # Save the actual AuditAny params, ->track() or ->new():
316 11 local $Data::Dumper::Maxdepth = 3;
317 11 ▶ my $auditany_params = $self->AuditObj->track_init_args ?
Branch: 1 branch
BranchTrueFalseTotal
$self->AuditObj->track_init_args ? : 11 0 11
318 Data::Dumper->Dump([$self->AuditObj->track_init_args],['*track']) :
319 Data::Dumper->Dump([$self->AuditObj->build_init_args],['*new']);
320
321 11 $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 11 };
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 11 ▶sub get_clean_md5 {
Subroutine get_clean_md5: called CC=2 SCAR=6.9
Pod documented
359 11 my $self = shift;
360 11 my $deploy_statements = shift;
361 11 ▶ my $clean = join("\n", grep { ! /^\-\-/ && ! /^\s*$/ } split(/\r?\n/,$deploy_statements) );
Condition100%3/3
/^\-\-/ || /^\s*$/
l!l&&r!l&&!r
17699404
MC/DC100%2/2
/^\-\-/ || /^\s*$/
/^\-\-/ /^\s*$/
Truth table100%3/3
A || B
A= /^\-\-/B= /^\s*$/
ABresult
1X1
011
000
362 11 return md5_hex($clean);
363 }
364  
365 11 1;
366  
367 __END__
« AuditAny/AuditContext.pm ↑ Summary AuditAny/Util/ResultMaker.pm »