DBIx/Class/Storage/TxnScopeGuard.pm

filter: Statementstmt 98.2% 55 / 56 Branchbran 84.6% 22 / 26 Conditioncond 60.0% 9 / 15 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 86.6% 104 / 120 CC 19 SCAR 20.5
CC
19
Cov
89%
DESTROY
14.8
new
4.0
commit
2.0
CRAP 7.7
« DBIx/Class/Storage/DBI/Cursor.pm ↑ Summary DBIx/Class/Storage/DBI/Sybase/Microsoft_SQL_Server.pm »
1 package DBIx::Class::Storage::TxnScopeGuard;
2  
3 267 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 267 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 267 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 267 ▶use Scalar::Util qw(weaken blessed refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 267 ▶use DBIx::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 267 ▶use DBIx::Class::_Util qw(is_exception detected_reinvoked_destructor);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 267 ▶use DBIx::Class::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 267 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 8911 ▶sub new {
Subroutine new: called CC=4 SCAR=14.0
Pod documented
13 8911 my ($class, $storage) = @_;
14  
15 8911 my $guard = {
16 inactivated => 0,
17 storage => $storage,
18 };
19  
20 # we are starting with an already set $@ - in order for things to work we need to
21 # be able to recognize it upon destruction - store its weakref
22 # recording it before doing the txn_begin stuff
23 #
24 # FIXME FRAGILE - any eval that fails but *does not* rethrow between here
25 # and the unwind will trample over $@ and invalidate the entire mechanism
26 # There got to be a saner way of doing this...
27 8911 ▶ if (is_exception $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_exception($@)) 591 8320 8911
28 weaken(
29 591 ▶ $guard->{existing_exception_ref} = (ref($@) eq '') ? \$@ : $@
Branch: 1 branch
BranchTrueFalseTotal
ref $@ eq '' ? : 2 589 591
30 );
31 }
32  
33 8911 $storage->txn_begin;
34  
35 8911 weaken( $guard->{dbh} = $storage->_dbh );
36  
37 8911 ▶ bless $guard, ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
089110
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
38  
39 8911 $guard;
40 }
41  
42 8778 ▶sub commit {
Subroutine commit: called CC=2 SCAR=7.0
Pod documented
43 8778 my $self = shift;
44  
45 $self->{storage}->throw_exception("Refusing to execute multiple commits on scope guard $self")
46 8778 ▶ if $self->{inactivated};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"inactivated"} 0 8778 8778
47  
48 # FIXME - this assumption may be premature: a commit may fail and a rollback
49 # *still* be necessary. Currently I am not aware of such scenarious, but I
50 # also know the deferred constraint handling is *severely* undertested.
51 # Making the change of "fire txn and never come back to this" in order to
52 # address RT#107159, but this *MUST* be reevaluated later.
53 8778 $self->{inactivated} = 1;
54 8778 $self->{storage}->txn_commit;
55 }
56  
57 8912 ▶sub DESTROY {
Subroutine DESTROY: called CC=14 SCAR=26.9
58 8912 ▶ return if &detected_reinvoked_destructor;
Branch: 1 branch
BranchTrueFalseTotal
if &detected_reinvoked_destructor 1 8911 8912
59  
60 8911 my $self = shift;
61  
62 8911 ▶ return if $self->{inactivated};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"inactivated"} 8778 133 8911
63  
64 # if our dbh is not ours anymore, the $dbh weakref will go undef
65 133 $self->{storage}->_verify_pid unless DBIx::Class::_ENV_::BROKEN_FORK;
66 133 ▶ return unless $self->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dbh"} 10 123 133
67  
68 my $exception = $@ if (
69 is_exception $@
70 and
71 (
72 ! defined $self->{existing_exception_ref}
73 or
74 refaddr( ref($@) eq '' ? \$@ : $@ ) != refaddr($self->{existing_exception_ref})
75 123 ▶ )
Branch: 2 branches
BranchTrueFalseTotal
if &is_exception($@) and !defined($self->{"existing_exception_ref"}) || builtin::refaddr(ref $@ eq "" ? \$@ : $@) != builtin::refaddr($self->{"existing_exception_ref"}) 119 4 123
ref $@ eq '' ? : 0 0 0
Condition50%3/6
&is_exception($@) and !defined($self->{"existing_exception_ref"}) || builtin::refaddr(ref $@ eq "" ? \$@ : $@) != builtin::refaddr($self->{"existing_exception_ref"})
!ll&&!rl&&r
40119
not defined $self->{"existing_exception_ref"} or builtin::refaddr(ref $@ eq "" ? \$@ : $@) != builtin::refaddr($self->{"existing_exception_ref"})
l!l&&r!l&&!r
11900
MC/DC33%1/3
&is_exception($@) and !defined($self->{"existing_exception_ref"}) || builtin::refaddr(ref $@ eq "" ? \$@ : $@) != builtin::refaddr($self->{"existing_exception_ref"})
&is_exception($@) not defined $self->{"existing_exception_ref"} builtin::refaddr(ref $@ eq "" ? \$@ : $@) != builtin::refaddr($self->{"existing_exception_ref"})
Truth table50%2/4
A and not(B) or C
A= &is_exception($@)B= not defined $self->{"existing_exception_ref"}C= builtin::refaddr(ref $@ eq "" ? \$@ : $@) != builtin::refaddr($self->{"existing_exception_ref"})
ABCresult
0XX0
1000
11X1
1011
76 );
77  
78 {
79 123 local $@;
80  
81 123 ▶ carp 'A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back.'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $exception 4 119 123
82 unless defined $exception;
83  
84 123 my $rollback_exception;
85 # do minimal connectivity check due to weird shit like
86 # https://rt.cpan.org/Public/Bug/Display.html?id=62370
87 123 ▶ try { $self->{storage}->_seems_connected && $self->{storage}->txn_rollback }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$self->{"storage"}->_seems_connected and $self->{"storage"}->txn_rollback 122 1 123
88 123 ▶ catch { $rollback_exception = shift };
Subroutine __ANON__: called CC=1 SCAR=0.0
89  
90 123 ▶ if ( $rollback_exception and (
Branch: 1 branch
BranchTrueFalseTotal
if ($rollback_exception and not defined builtin::blessed($rollback_exception) && $rollback_exception->isa("DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION")) 5 118 123
Condition83%5/6
$rollback_exception and not defined builtin::blessed($rollback_exception) && $rollback_exception->isa("DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION")
!ll&&!rl&&r
11715
defined builtin::blessed($rollback_exception) && $rollback_exception->isa('DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION')
!ll&&!rl&&r
501
MC/DC66%2/3
$rollback_exception and not defined builtin::blessed($rollback_exception) && $rollback_exception->isa("DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION")
$rollback_exception defined builtin::blessed($rollback_exception) $rollback_exception->isa('DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION')
Truth table75%3/4
A and not(B && C)
A= $rollback_exceptionB= defined builtin::blessed($rollback_exception)C= $rollback_exception->isa('DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION')
ABCresult
0XX0
1110
10X1
1101
91 ! defined blessed $rollback_exception
92 or
93 ! $rollback_exception->isa('DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION')
94 ) ) {
95 # append our text - THIS IS A TEMPORARY FIXUP!
96 # a real stackable exception object is in the works
97 5 ▶ if (ref $exception eq 'DBIx::Class::Exception') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $exception eq 'DBIx::Class::Exception') { } 0 5 5
elsif ($exception) { } 3 2 5
98 0 $exception->{msg} = "Transaction aborted: $exception->{msg} "
99 ."Rollback failed: ${rollback_exception}";
100 }
101 elsif ($exception) {
102 3 $exception = "Transaction aborted: ${exception} "
103 ."Rollback failed: ${rollback_exception}";
104 }
105 else {
106 2 carp (join ' ',
107 "********************* ROLLBACK FAILED!!! ********************",
108 "\nA rollback operation failed after the guard went out of scope.",
109 'This is potentially a disastrous situation, check your data for',
110 "consistency: $rollback_exception"
111 );
112 }
113 }
114 }
115  
116 123 $@ = $exception;
117 }
118  
119 267 1;
120  
121 __END__
« DBIx/Class/Storage/DBI/Cursor.pm ↑ Summary DBIx/Class/Storage/DBI/Sybase/Microsoft_SQL_Server.pm »