blib/lib/DBIO/Relationship/CascadeActions.pm

filter: Statementstmt 96.0% 48 / 50 Branchbran 83.3% 10 / 12 Conditioncond 50.0% 3 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 88.6% 70 / 79 CC 12 SCAR 17.2
CC
12
Cov
90%
update
8.2
delete
5.0
BEGIN
1.0
CRAP 5.6
« blib/lib/DBIO/Deploy/Base.pm ↑ Summary blib/lib/DBIO/ChangeLog.pm »
1 package DBIO::Relationship::CascadeActions;
2 # ABSTRACT: Cascade delete and update actions across relationships
3  
4 378 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 378 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 378 our $VERSION = '0.900007';
8  
9 378 ▶use DBIO::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 378 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 378 our %_pod_inherit_config =
13 (
14 class_map => { 'DBIO::Relationship::CascadeActions' => 'DBIO::Relationship' }
15 );
16  
17  
18 133 ▶sub delete {
Subroutine delete: called CC=5 SCAR=16.2
Pod documented
19 133 my ($self, @rest) = @_;
20 133 ▶ return $self->next::method(@rest) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 2 131 133
21 # I'm just ignoring this for class deletes because hell, the db should
22 # be handling this anyway. Assuming we have joins we probably actually
23 # *could* do them, but I'd rather not.
24  
25 131 my $source = $self->result_source;
26 131 my %rels = map { $_ => $source->relationship_info($_) } $source->relationships;
27 131 my @cascade = grep { $rels{$_}{attrs}{cascade_delete} } keys %rels;
28  
29 131 ▶ if (@cascade) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cascade) 111 20 131
30 111 my $guard = $source->schema->txn_scope_guard;
31  
32 111 my $ret = $self->next::method(@rest);
33  
34 111 foreach my $rel (@cascade) {
35 537 ▶ if( my $rel_rs = eval{ $self->search_related($rel) } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rel_rs = eval { do { $self->search_related($rel) } }) { } 537 0 537
36 537 $rel_rs->delete_all;
37 } else {
38 0 carp "Skipping cascade delete on relationship '$rel' - related resultsource '$rels{$rel}{class}' is not registered with this schema";
39 0 next;
40 }
41 }
42  
43 111 $guard->commit;
44 111 return $ret;
45 }
46  
47 20 $self->next::method(@rest);
48 }
49  
50  
51 93 ▶sub update {
Subroutine update: called CC=8 SCAR=21.0
Pod documented
52 93 my ($self, @rest) = @_;
53 93 ▶ return $self->next::method(@rest) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 93 93
54 # Because update cascades on a class *really* don't make sense!
55  
56 93 my $source = $self->result_source;
57 93 my %rels = map { $_ => $source->relationship_info($_) } $source->relationships;
58 93 my @cascade = grep { $rels{$_}{attrs}{cascade_update} } keys %rels;
59  
60 93 ▶ if (@cascade) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cascade) 31 62 93
61 31 my $guard = $source->schema->txn_scope_guard;
62  
63 31 my $ret = $self->next::method(@rest);
64  
65 20 foreach my $rel (@cascade) {
66 next if (
67 $rels{$rel}{attrs}{accessor}
68 &&
69 $rels{$rel}{attrs}{accessor} eq 'single'
70 &&
71 60 ▶ !exists($self->{_relationship_data}{$rel})
Branch: 1 branch
BranchTrueFalseTotal
if $rels{$rel}{"attrs"}{"accessor"} and $rels{$rel}{"attrs"}{"accessor"} eq "single" and not exists $self->{"_relationship_data"}{$rel} 56 4 60
Condition50%3/6
$rels{$rel}{"attrs"}{"accessor"} and $rels{$rel}{"attrs"}{"accessor"} eq "single" and not exists $self->{"_relationship_data"}{$rel}
!ll&&!rl&&r
0456
$rels{$rel}{"attrs"}{"accessor"} and $rels{$rel}{"attrs"}{"accessor"} eq "single"
!ll&&!rl&&r
0060
MC/DC33%1/3
$rels{$rel}{"attrs"}{"accessor"} and $rels{$rel}{"attrs"}{"accessor"} eq "single" and not exists $self->{"_relationship_data"}{$rel}
$rels{$rel}{"attrs"}{"accessor"} $rels{$rel}{"attrs"}{"accessor"} eq "single" not exists $self->{"_relationship_data"}{$rel}
Truth table50%2/4
A and B and not(C)
A= $rels{$rel}{"attrs"}{"accessor"}B= $rels{$rel}{"attrs"}{"accessor"} eq "single"C= not exists $self->{"_relationship_data"}{$rel}
ABCresult
0XX0
10X0
1100
1111
72 );
73 4 $_->update for grep defined, $self->$rel;
74 }
75  
76 20 $guard->commit;
77 20 return $ret;
78 }
79  
80 62 $self->next::method(@rest);
81 }
82  
83 378 1;
84  
85 __END__
« blib/lib/DBIO/Deploy/Base.pm ↑ Summary blib/lib/DBIO/ChangeLog.pm »