| 1 | package DBIx::Class::AuditAny::Collector::DBIC; | ||||||||||||
| 2 | 12 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 12 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | # ABSTRACT: Collector class for recording AuditAny changes in DBIC schemas | ||||||||||||
| 6 | |||||||||||||
| 7 | 12 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 12 | ▶ | use MooX::Types::MooseLike::Base qw(:all); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 12 | with 'DBIx::Class::AuditAny::Role::Collector'; | |||||||||||
| 10 | |||||||||||||
| 11 | =head1 NAME | ||||||||||||
| 12 | |||||||||||||
| 13 | DBIx::Class::AuditAny::Collector::DBIC - Collector class for recording AuditAny | ||||||||||||
| 14 | changes in DBIC schemas | ||||||||||||
| 15 | |||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||
| 17 | |||||||||||||
| 18 | This Collector facilitates recording ChangeSets, Changes, and Column Changes within a | ||||||||||||
| 19 | clean relational structure into a DBIC schema. | ||||||||||||
| 20 | |||||||||||||
| 21 | If you don't want to handle the details of configuring this yourself, see | ||||||||||||
| 22 | L<DBIx::Class::AuditAny::Collector::AutoDBIC> which is a subclass of us, but handles | ||||||||||||
| 23 | most of the defaults for you w/o fuss. | ||||||||||||
| 24 | |||||||||||||
| 25 | =head1 ATTRIBUTES | ||||||||||||
| 26 | |||||||||||||
| 27 | Docs regarding the API/purpose of the attributes and methods in this class still TBD... | ||||||||||||
| 28 | |||||||||||||
| 29 | =head2 target_schema | ||||||||||||
| 30 | |||||||||||||
| 31 | =head2 target_source | ||||||||||||
| 32 | |||||||||||||
| 33 | =head2 change_data_rel | ||||||||||||
| 34 | |||||||||||||
| 35 | =head2 column_data_rel | ||||||||||||
| 36 | |||||||||||||
| 37 | =cut | ||||||||||||
| 38 | |||||||||||||
| 39 | 1 | ▶ | has 'target_schema', is => 'ro', isa => Object, lazy => 1, default => sub { (shift)->AuditObj->schema }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 40 | 12 | has 'target_source', is => 'ro', isa => Str, required => 1; | |||||||||||
| 41 | 12 | has 'change_data_rel', is => 'ro', isa => Maybe[Str]; | |||||||||||
| 42 | 12 | has 'column_data_rel', is => 'ro', isa => Maybe[Str]; | |||||||||||
| 43 | |||||||||||||
| 44 | |||||||||||||
| 45 | # the top level source; could be either change or changeset | ||||||||||||
| 46 | has 'targetSource', is => 'ro', isa => Object, | ||||||||||||
| 47 | 15 | ▶ | lazy => 1, init_arg => undef, default => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||
| 48 | 15 | my $self = shift; | |||||||||||
| 49 | 15 | ▶ | my $Source = $self->target_schema->source($self->target_source) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 50 | or die "Bad target_source name '" . $self->target_source . "'"; | ||||||||||||
| 51 | 2 | return $Source; | |||||||||||
| 52 | 12 | }; | |||||||||||
| 53 | |||||||||||||
| 54 | has 'changesetSource', is => 'ro', isa => Maybe[Object], | ||||||||||||
| 55 | 15 | ▶ | lazy => 1, init_arg => undef, default => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||
| 56 | 15 | my $self = shift; | |||||||||||
| 57 | 15 | ▶ | return $self->change_data_rel ? $self->targetSource : undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 58 | 12 | }; | |||||||||||
| 59 | |||||||||||||
| 60 | has 'changeSource', is => 'ro', isa => Object, | ||||||||||||
| 61 | 2 | ▶ | lazy => 1, init_arg => undef, default => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.4
| |||||||||||||
| 62 | 2 | my $self = shift; | |||||||||||
| 63 | 2 | ▶ | my $SetSource = $self->changesetSource or return $self->targetSource; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 64 | 2 | ▶ | my $Source = $SetSource->related_source($self->change_data_rel) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 65 | or die "Bad change_data_rel name '" . $self->change_data_rel . "'"; | ||||||||||||
| 66 | 2 | return $Source; | |||||||||||
| 67 | 12 | }; | |||||||||||
| 68 | |||||||||||||
| 69 | has 'columnSource', is => 'ro', isa => Maybe[Object], | ||||||||||||
| 70 | 2 | ▶ | lazy => 1, init_arg => undef, default => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.4
| |||||||||||||
| 71 | 2 | my $self = shift; | |||||||||||
| 72 | 2 | ▶ | return undef unless ($self->column_data_rel); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 73 | 2 | ▶ | my $Source = $self->changeSource->related_source($self->column_data_rel) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 74 | or die "Bad column_data_rel name '" . $self->column_data_rel . "'"; | ||||||||||||
| 75 | 2 | return $Source; | |||||||||||
| 76 | 12 | }; | |||||||||||
| 77 | |||||||||||||
| 78 | has 'changeset_datapoints', is => 'ro', isa => ArrayRef[Str], | ||||||||||||
| 79 | 15 | ▶ | lazy => 1, default => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||
| 80 | 15 | my $self = shift; | |||||||||||
| 81 | 15 | ▶ | return [] unless ($self->changesetSource); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 82 | 2 | my @DataPoints = $self->AuditObj->get_context_datapoints(qw(base set)); | |||||||||||
| 83 | 2 | my @names = map { $_->name } @DataPoints; | |||||||||||
| 84 | 2 | $self->enforce_source_has_columns($self->changesetSource,@names); | |||||||||||
| 85 | 2 | return \@names; | |||||||||||
| 86 | 12 | }; | |||||||||||
| 87 | |||||||||||||
| 88 | has 'change_datapoints', is => 'ro', isa => ArrayRef[Str], | ||||||||||||
| 89 | 2 | ▶ | lazy => 1, default => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||
| 90 | 2 | my $self = shift; | |||||||||||
| 91 | 2 | my @contexts = qw(source change); | |||||||||||
| 92 | 2 | ▶ | push @contexts,(qw(base set)) unless ($self->changesetSource); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 93 | 2 | my @DataPoints = $self->AuditObj->get_context_datapoints(@contexts); | |||||||||||
| 94 | 2 | my @names = map { $_->name } @DataPoints; | |||||||||||
| 95 | 2 | $self->enforce_source_has_columns($self->changeSource,@names); | |||||||||||
| 96 | 2 | return \@names; | |||||||||||
| 97 | 12 | }; | |||||||||||
| 98 | |||||||||||||
| 99 | has 'column_datapoints', is => 'ro', isa => ArrayRef[Str], | ||||||||||||
| 100 | 2 | ▶ | lazy => 1, default => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||
| 101 | 2 | my $self = shift; | |||||||||||
| 102 | 2 | ▶ | return [] unless ($self->columnSource); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 103 | 2 | my @DataPoints = $self->AuditObj->get_context_datapoints(qw(column)); | |||||||||||
| 104 | 2 | my @names = map { $_->name } @DataPoints; | |||||||||||
| 105 | 2 | $self->enforce_source_has_columns($self->columnSource,@names); | |||||||||||
| 106 | 2 | return \@names; | |||||||||||
| 107 | 12 | }; | |||||||||||
| 108 | |||||||||||||
| 109 | 1 | ▶ | has 'write_sources', is => 'ro', isa => ArrayRef[Str], lazy => 1, default => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.5
| |||||||||||||
| 110 | 1 | my $self = shift; | |||||||||||
| 111 | 1 | my @sources = (); | |||||||||||
| 112 | 1 | ▶ | push @sources, $self->changesetSource->source_name if ($self->changesetSource); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 113 | 1 | ▶ | push @sources, $self->changeSource->source_name if ($self->changeSource); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 114 | 1 | ▶ | push @sources, $self->columnSource->source_name if ($self->columnSource); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 115 | 1 | return \@sources; | |||||||||||
| 116 | 12 | }; | |||||||||||
| 117 | |||||||||||||
| 118 | 2 | ▶ | has '+writes_bound_schema_sources', default => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 119 | 2 | my $self = shift; | |||||||||||
| 120 | 2 | ▶ | return $self->target_schema == $self->AuditObj->schema ? | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 121 | $self->write_sources : []; | ||||||||||||
| 122 | 12 | }; | |||||||||||
| 123 | |||||||||||||
| 124 | 15 | ▶ | sub BUILD { | ||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 125 | 15 | my $self = shift; | |||||||||||
| 126 | |||||||||||||
| 127 | 15 | $self->validate_target_schema; | |||||||||||
| 128 | |||||||||||||
| 129 | } | ||||||||||||
| 130 | |||||||||||||
| 131 | =head1 METHODS | ||||||||||||
| 132 | |||||||||||||
| 133 | =head2 validate_target_schema | ||||||||||||
| 134 | |||||||||||||
| 135 | =cut | ||||||||||||
| 136 | 15 | ▶ | sub validate_target_schema { | ||||||||||
|
Subroutine
validate_target_schema: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 137 | 15 | my $self = shift; | |||||||||||
| 138 | |||||||||||||
| 139 | 15 | $self->changeset_datapoints; | |||||||||||
| 140 | 2 | $self->change_datapoints; | |||||||||||
| 141 | 2 | $self->column_datapoints; | |||||||||||
| 142 | |||||||||||||
| 143 | } | ||||||||||||
| 144 | |||||||||||||
| 145 | =head2 enforce_source_has_columns | ||||||||||||
| 146 | |||||||||||||
| 147 | =cut | ||||||||||||
| 148 | 6 | ▶ | sub enforce_source_has_columns { | ||||||||||
|
Subroutine
enforce_source_has_columns: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||||
| 149 | 6 | my $self = shift; | |||||||||||
| 150 | 6 | my $Source = shift; | |||||||||||
| 151 | 6 | my @columns = @_; | |||||||||||
| 152 | |||||||||||||
| 153 | 6 | my @missing = (); | |||||||||||
| 154 | 6 | ▶ | $Source->has_column($_) or push @missing, $_ for (@columns); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 155 | |||||||||||||
| 156 | 6 | ▶ | return 1 unless (scalar(@missing) > 0); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 157 | |||||||||||||
| 158 | die "Source '" . $Source->source_name . "' missing required columns: " . | ||||||||||||
| 159 | 0 | join(',',map { "'$_'" } @missing); | |||||||||||
| 160 | } | ||||||||||||
| 161 | |||||||||||||
| 162 | =head2 get_add_create_change | ||||||||||||
| 163 | |||||||||||||
| 164 | =cut | ||||||||||||
| 165 | 0 | ▶ | sub get_add_create_change { | ||||||||||
|
Subroutine
get_add_create_change: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 166 | 0 | my $self = shift; | |||||||||||
| 167 | 0 | my $ChangeContext = shift; | |||||||||||
| 168 | |||||||||||||
| 169 | 0 | my $create = $ChangeContext->get_datapoints_data($self->change_datapoints); | |||||||||||
| 170 | |||||||||||||
| 171 | 0 | my $relname = $self->column_data_rel; | |||||||||||
| 172 | 0 | ▶ | if($relname) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 173 | 0 | my @ColChanges = $ChangeContext->all_column_changes; | |||||||||||
| 174 | $create->{$relname} = [ | ||||||||||||
| 175 | 0 | map { $_->get_datapoints_data($self->column_datapoints) } @ColChanges | |||||||||||
| 176 | ]; | ||||||||||||
| 177 | } | ||||||||||||
| 178 | |||||||||||||
| 179 | 0 | return $create; | |||||||||||
| 180 | } | ||||||||||||
| 181 | |||||||||||||
| 182 | =head2 add_change_row | ||||||||||||
| 183 | |||||||||||||
| 184 | =cut | ||||||||||||
| 185 | 0 | ▶ | sub add_change_row { | ||||||||||
|
Subroutine
add_change_row: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 186 | 0 | my $self = shift; | |||||||||||
| 187 | 0 | my $ChangeContext = shift; | |||||||||||
| 188 | 0 | my $create = $self->get_add_create_change($ChangeContext); | |||||||||||
| 189 | 0 | return $self->changeSource->resultset->create($create); | |||||||||||
| 190 | } | ||||||||||||
| 191 | |||||||||||||
| 192 | =head2 add_changeset_row | ||||||||||||
| 193 | |||||||||||||
| 194 | =cut | ||||||||||||
| 195 | 0 | ▶ | sub add_changeset_row { | ||||||||||
|
Subroutine
add_changeset_row: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 196 | 0 | my $self = shift; | |||||||||||
| 197 | 0 | my $ChangeSetContext = shift; | |||||||||||
| 198 | |||||||||||||
| 199 | 0 | my $create = $ChangeSetContext->get_datapoints_data($self->changeset_datapoints); | |||||||||||
| 200 | |||||||||||||
| 201 | 0 | my $relname = $self->change_data_rel; | |||||||||||
| 202 | 0 | ▶ | if($relname) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 203 | 0 | my @Changes = $ChangeSetContext->all_changes; | |||||||||||
| 204 | 0 | $create->{$relname} = [ map { $self->get_add_create_change($_) } @Changes ]; | |||||||||||
| 205 | } | ||||||||||||
| 206 | |||||||||||||
| 207 | 0 | return $self->changesetSource->resultset->create($create); | |||||||||||
| 208 | } | ||||||||||||
| 209 | |||||||||||||
| 210 | |||||||||||||
| 211 | ######### Public API ######### | ||||||||||||
| 212 | |||||||||||||
| 213 | =head2 record_changes | ||||||||||||
| 214 | |||||||||||||
| 215 | =cut | ||||||||||||
| 216 | 0 | ▶ | sub record_changes { | ||||||||||
|
Subroutine
record_changes: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 217 | 0 | my $self = shift; | |||||||||||
| 218 | 0 | my $ChangeSet = shift; | |||||||||||
| 219 | |||||||||||||
| 220 | 0 | ▶ | return $self->add_changeset_row($ChangeSet) if ($self->changesetSource); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 221 | 0 | my @Changes = $ChangeSet->all_changes; | |||||||||||
| 222 | 0 | $self->add_change_row($_) for (@Changes); | |||||||||||
| 223 | |||||||||||||
| 224 | 0 | return 1; | |||||||||||
| 225 | } | ||||||||||||
| 226 | |||||||||||||
| 227 | =head2 has_full_row_stored | ||||||||||||
| 228 | |||||||||||||
| 229 | =cut | ||||||||||||
| 230 | 0 | ▶ | sub has_full_row_stored { | ||||||||||
|
Subroutine
has_full_row_stored: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||
| 231 | 0 | my $self = shift; | |||||||||||
| 232 | 0 | my $Row = shift; | |||||||||||
| 233 | |||||||||||||
| 234 | 0 | ▶ | my $Rs = $self->changeSource->resultset | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 235 | or die "No changeSource in this collector"; | ||||||||||||
| 236 | |||||||||||||
| 237 | 0 | my $source_name = $Row->result_source->source_name; | |||||||||||
| 238 | 0 | ▶ | my $SourceContext = $self->AuditObj->tracked_sources->{$source_name} | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 239 | or die "Source '$source_name' is not being tracked by the Auditor!"; | ||||||||||||
| 240 | |||||||||||||
| 241 | 0 | my $pri_key_value = $SourceContext->get_pri_key_value($Row); | |||||||||||
| 242 | |||||||||||||
| 243 | 0 | ▶ | my $rename = $self->AuditObj->rename_datapoints || {}; | ||||||||||
|
Condition0%0/2
$self->AuditObj->rename_datapoints || {}
MC/DC0%0/1
$self->AuditObj->rename_datapoints || {}
$self->AuditObj->rename_datapoints
Truth table0%0/2
A
A= $self->AuditObj->rename_datapoints
| |||||||||||||
| 244 | |||||||||||||
| 245 | 0 | ▶ | my $pri_key_row = $rename->{pri_key_value} || 'pri_key_value'; | ||||||||||
|
Condition0%0/2
$rename->{'pri_key_value'} || 'pri_key_value'
MC/DC0%0/1
$rename->{'pri_key_value'} || 'pri_key_value'
$rename->{'pri_key_value'}
Truth table0%0/2
A
A= $rename->{'pri_key_value'}
| |||||||||||||
| 246 | 0 | ▶ | my $source = $rename->{source} || 'source'; | ||||||||||
|
Condition0%0/2
$rename->{'source'} || 'source'
MC/DC0%0/1
$rename->{'source'} || 'source'
$rename->{'source'}
Truth table0%0/2
A
A= $rename->{'source'}
| |||||||||||||
| 247 | 0 | ▶ | my $action = $rename->{action} || 'action'; | ||||||||||
|
Condition0%0/2
$rename->{'action'} || 'action'
MC/DC0%0/1
$rename->{'action'} || 'action'
$rename->{'action'}
Truth table0%0/2
A
A= $rename->{'action'}
| |||||||||||||
| 248 | |||||||||||||
| 249 | 0 | $Rs = $Rs->search_rs({ | |||||||||||
| 250 | $pri_key_row => $pri_key_value, | ||||||||||||
| 251 | $source => $source_name, | ||||||||||||
| 252 | $action => [ 'select','insert' ] | ||||||||||||
| 253 | },{ limit => 1 }); | ||||||||||||
| 254 | |||||||||||||
| 255 | 0 | return $Rs->count; | |||||||||||
| 256 | } | ||||||||||||
| 257 | |||||||||||||
| 258 | |||||||||||||
| 259 | 12 | 1; | |||||||||||
| 260 | |||||||||||||
| 261 | |||||||||||||
| 262 | __END__ | ||||||||||||