| 1 | package UR::Context::Transaction; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 266 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 266 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 265 | require UR; | |||||||||||||||||||||||||||
| 7 | 265 | our $VERSION = "0.47"; # UR $VERSION; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 266 | ▶ | use Carp qw(croak confess shortmess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 266 | ▶ | use constant TRANSACTION_STATE_OPEN => 'open'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 266 | ▶ | use constant TRANSACTION_STATE_COMMITTED => 'committed'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 266 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 265 | our @EXPORT_OK = qw(TRANSACTION_STATE_OPEN TRANSACTION_STATE_COMMITTED); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 265 | UR::Object::Type->define( | |||||||||||||||||||||||||||
| 17 | class_name => __PACKAGE__, | ||||||||||||||||||||||||||||
| 18 | is => ['UR::Context'], | ||||||||||||||||||||||||||||
| 19 | has => [ | ||||||||||||||||||||||||||||
| 20 | begin_point => { is => 'Integer' }, | ||||||||||||||||||||||||||||
| 21 | end_point => { is => 'Integer', is_optional => 1}, # FIXME is this ever used anywhere? | ||||||||||||||||||||||||||||
| 22 | state => { is => 'Text', valid_values => [TRANSACTION_STATE_OPEN, TRANSACTION_STATE_COMMITTED] }, | ||||||||||||||||||||||||||||
| 23 | commit_validator => { default_value => 'changes_can_be_saved', | ||||||||||||||||||||||||||||
| 24 | doc => 'validation function used before commit() can succeed' }, | ||||||||||||||||||||||||||||
| 25 | ], | ||||||||||||||||||||||||||||
| 26 | is_transactional => 1, | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 265 | our $log_all_changes = 0; | |||||||||||||||||||||||||||
| 30 | 265 | our @change_log; | |||||||||||||||||||||||||||
| 31 | 265 | our @open_transaction_stack; | |||||||||||||||||||||||||||
| 32 | 265 | our $last_transaction_id = 0; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | sub delete { | ||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 36 | 0 | $self->rollback; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub begin { | ||||||||||||||||||||||||||
|
Subroutine
begin: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 41 | 0 | my %params = @_; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | delete @params{'begin_point', 'end_point', 'state'}; # These are set within this function | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | my $id = $last_transaction_id++; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | my $begin_point = @change_log; | |||||||||||||||||||||||||||
| 48 | 0 | $log_all_changes = 1; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | my $last_trans = $open_transaction_stack[-1]; | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | if ($last_trans and $last_trans != $UR::Context::current) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$last_trans and $last_trans != $UR::Context::current
MC/DC0%0/2
$last_trans and $last_trans != $UR::Context::current
$last_trans
$last_trans != $UR::Context::current
Truth table0%0/3
A and B
A= $last_transB= $last_trans != $UR::Context::current
| |||||||||||||||||||||||||||||
| 52 | 0 | die "Current transaction does not match the top of the transaction stack!?" | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | $last_trans ||= $UR::Context::current; | ||||||||||||||||||||||||||
|
Condition0%0/3
$last_trans ||= $UR::Context::current
MC/DC0%0/2
$last_trans ||= $UR::Context::current
$last_trans
$UR::Context::current
Truth table0%0/3
A ||= B
A= $last_transB= $UR::Context::current
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | my $self = $class->create( | |||||||||||||||||||||||||||
| 57 | id => $id, | ||||||||||||||||||||||||||||
| 58 | begin_point => $begin_point, | ||||||||||||||||||||||||||||
| 59 | state => TRANSACTION_STATE_OPEN, | ||||||||||||||||||||||||||||
| 60 | parent => $last_trans, | ||||||||||||||||||||||||||||
| 61 | %params, | ||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | unless ($self) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | Carp::confess("Failed to being transaction!"); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | push @open_transaction_stack, $self; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | $UR::Context::current = $self; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | return $self; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | sub log_change { | ||||||||||||||||||||||||||
|
Subroutine
log_change: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 76 | 0 | my $this_class = shift; | |||||||||||||||||||||||||||
| 77 | 0 | my ($object, $class, $id, $aspect, $undo_data) = @_; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | return if $class eq "UR::Change"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # wrappers (create/delete/load/unload/define) signal change also | ||||||||||||||||||||||||||||
| 82 | # and we undo the wrapper, thereby undoing these | ||||||||||||||||||||||||||||
| 83 | # -> ignore any signal from a method which is wrapped by another signalling method which gets undone | ||||||||||||||||||||||||||||
| 84 | 0 | ▶ | return if ( $aspect eq "load" or | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$aspect eq "load" or $aspect eq "load_external"
MC/DC0%0/2
$aspect eq "load" or $aspect eq "load_external"
$aspect eq "load"
$aspect eq "load_external"
Truth table0%0/3
A or B
A= $aspect eq "load"B= $aspect eq "load_external"
| |||||||||||||||||||||||||||||
| 85 | $aspect eq "load_external" | ||||||||||||||||||||||||||||
| 86 | ); | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if (!ref($object) or $class eq "UR::Object::Index") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not ref $object or $class eq "UR::Object::Index"
MC/DC0%0/2
not ref $object or $class eq "UR::Object::Index"
not ref $object
$class eq "UR::Object::Index"
Truth table0%0/3
not(A) or B
A= not ref $objectB= $class eq "UR::Object::Index"
| |||||||||||||||||||||||||||||
| 89 | #print "skipping @_\n"; | ||||||||||||||||||||||||||||
| 90 | 0 | return; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ($aspect eq "delete") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | $undo_data = Data::Dumper::Dumper($object); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | Carp::confess() if ref($class); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | my $change = UR::Change->create( | |||||||||||||||||||||||||||
| 100 | id => scalar(@change_log)+1, | ||||||||||||||||||||||||||||
| 101 | changed_class_name => $class, | ||||||||||||||||||||||||||||
| 102 | changed_id => $id, | ||||||||||||||||||||||||||||
| 103 | changed_aspect => $aspect, | ||||||||||||||||||||||||||||
| 104 | undo_data => $undo_data, | ||||||||||||||||||||||||||||
| 105 | ); | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | unless (ref($change)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | #$DB::single = 1; | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | push @change_log, $change; | |||||||||||||||||||||||||||
| 112 | 0 | return $change; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | sub has_changes { | ||||||||||||||||||||||||||
|
Subroutine
has_changes: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 116 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 117 | 0 | my @changes = $self->get_changes(); | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | return (@changes > 1 ? 1 : ()); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | sub get_changes { | ||||||||||||||||||||||||||
|
Subroutine
get_changes: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 123 | 0 | my $begin_point = $self->begin_point; | |||||||||||||||||||||||||||
| 124 | 0 | ▶ | my $end_point = $self->end_point || $#change_log; | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->end_point || $#UR::Context::Transaction::change_log
MC/DC0%0/2
$self->end_point || $#UR::Context::Transaction::change_log
$self->end_point
$#UR::Context::Transaction::change_log
Truth table0%0/3
A || B
A= $self->end_pointB= $#UR::Context::Transaction::change_log
| |||||||||||||||||||||||||||||
| 125 | 0 | my @changes = @change_log[$begin_point..$end_point]; | |||||||||||||||||||||||||||
| 126 | 0 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 0 | @changes = UR::Change->get(id => \@changes, @_) | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | else { | ||||||||||||||||||||||||||||
| 130 | 0 | return @changes; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub get_change_summary { | ||||||||||||||||||||||||||
|
Subroutine
get_change_summary: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 135 | # TODO: This should compress multiple changes to the same object as much as possible | ||||||||||||||||||||||||||||
| 136 | # Right now, it just omits the creation event for the transaction object itself. | ||||||||||||||||||||||||||||
| 137 | # -> should the creation of the transaction be part of it? | ||||||||||||||||||||||||||||
| 138 | # A: It should really be part of the prior transaction, and after commit/rollback | ||||||||||||||||||||||||||||
| 139 | # the nesting collapses. The @change_log should be _inside the transaction object, | ||||||||||||||||||||||||||||
| 140 | # or the change should contain a transaction id. The list can be destroyed on | ||||||||||||||||||||||||||||
| 141 | # rollback, or summarized on commit. | ||||||||||||||||||||||||||||
| 142 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 143 | my @changes = | ||||||||||||||||||||||||||||
| 144 | 0 | grep { $_->changed_aspect !~ /^(load|define)$/ } | |||||||||||||||||||||||||||
| 145 | $self->get_changes; | ||||||||||||||||||||||||||||
| 146 | 0 | shift @changes; # $self creation event | |||||||||||||||||||||||||||
| 147 | 0 | return @changes; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | sub rollback { | ||||||||||||||||||||||||||
|
Subroutine
rollback: not called CC=17 SCAR=57.2
Pod
documented
| |||||||||||||||||||||||||||||
| 151 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # Support calling as a class method: UR::Context::Transaction->rollback rolls back the current trans | ||||||||||||||||||||||||||||
| 154 | 0 | ▶ | unless (ref($self)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 0 | $self = $open_transaction_stack[-1]; | |||||||||||||||||||||||||||
| 156 | 0 | ▶ | unless ($self) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 0 | Carp::confess("No open transaction!? Cannot rollback."); | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | if ($self->state ne TRANSACTION_STATE_OPEN) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | Carp::confess("Cannot rollback a transaction that is " . $self->state . ".") | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | $self->__signal_change__('prerollback'); | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 0 | my $begin_point = $self->begin_point; | |||||||||||||||||||||||||||
| 168 | 0 | ▶ | unless ($self eq $open_transaction_stack[-1]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | # This is not the top transaction on the stack. | ||||||||||||||||||||||||||||
| 170 | # Rollback internally nested transactions in order from the end. | ||||||||||||||||||||||||||||
| 171 | my @transactions_with_begin_point = | ||||||||||||||||||||||||||||
| 172 | 0 | map { [ $_->begin_point, $_ ] } | |||||||||||||||||||||||||||
| 173 | $self->class->get( | ||||||||||||||||||||||||||||
| 174 | begin_point => { operator => ">", value => $begin_point } | ||||||||||||||||||||||||||||
| 175 | ); | ||||||||||||||||||||||||||||
| 176 | my @later_transactions = | ||||||||||||||||||||||||||||
| 177 | 0 | map { $_->[1] } | |||||||||||||||||||||||||||
| 178 | 0 | sort { $b->[0] <=> $a->[0] } | |||||||||||||||||||||||||||
| 179 | @transactions_with_begin_point; | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 0 | for my $later_transaction (@later_transactions) { | |||||||||||||||||||||||||||
| 182 | 0 | ▶ | if ($later_transaction->isa("UR::DeletedRef")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | #$DB::single = 1; | ||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | 0 | $later_transaction->rollback; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 0 | my $parent = $self->parent; | |||||||||||||||||||||||||||
| 190 | 0 | ▶ | if ($open_transaction_stack[-2] and $open_transaction_stack[-2] != $parent) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$UR::Context::Transaction::open_transaction_stack[-2] and $UR::Context::Transaction::open_transaction_stack[-2] != $parent
MC/DC0%0/2
$UR::Context::Transaction::open_transaction_stack[-2] and $UR::Context::Transaction::open_transaction_stack[-2] != $parent
$UR::Context::Transaction::open_transaction_stack[-2]
$UR::Context::Transaction::open_transaction_stack[-2] != $parent
Truth table0%0/3
A and B
A= $UR::Context::Transaction::open_transaction_stack[-2]B= $UR::Context::Transaction::open_transaction_stack[-2] != $parent
| |||||||||||||||||||||||||||||
| 191 | 0 | die "Parent transaction $parent is not below this one on the stack $open_transaction_stack[-2]?"; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | { | ||||||||||||||||||||||||||||
| 195 | # Reverse each change, starting from the most recent, and | ||||||||||||||||||||||||||||
| 196 | # ending with the creation of the transaction object itself. | ||||||||||||||||||||||||||||
| 197 | 0 | local $log_all_changes = 0; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 0 | $self->__signal_change__('rollback', 1); | |||||||||||||||||||||||||||
| 201 | 0 | my @changes_to_undo = reverse $self->get_changes(); | |||||||||||||||||||||||||||
| 202 | 0 | my $transaction_change = pop @changes_to_undo; | |||||||||||||||||||||||||||
| 203 | 0 | my $transaction = $transaction_change->changed_class_name->get($transaction_change->changed_id); | |||||||||||||||||||||||||||
| 204 | 0 | ▶ | unless ($self == $transaction && $transaction_change->changed_aspect eq 'create') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self == $transaction and $transaction_change->changed_aspect eq "create"
MC/DC0%0/2
$self == $transaction and $transaction_change->changed_aspect eq "create"
$self == $transaction
$transaction_change->changed_aspect eq "create"
Truth table0%0/3
A and B
A= $self == $transactionB= $transaction_change->changed_aspect eq "create"
| |||||||||||||||||||||||||||||
| 205 | 0 | die "First change was not the creation of this transaction!"; | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | 0 | for my $change (@changes_to_undo) { | |||||||||||||||||||||||||||
| 208 | 0 | ▶ | if ($change == $changes_to_undo[0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | # the transaction reverses itself in its own context, | ||||||||||||||||||||||||||||
| 210 | # but the removal of the transaction itself happens in the parent context | ||||||||||||||||||||||||||||
| 211 | 0 | $UR::Context::current = $parent; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 0 | $change->undo; | |||||||||||||||||||||||||||
| 215 | 0 | $change->delete; | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 0 | for my $change (@changes_to_undo) { | |||||||||||||||||||||||||||
| 219 | 0 | ▶ | unless($change->isa('UR::DeletedRef')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | 0 | Carp::confess("Failed to undo a change during transaction rollback."); | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 0 | $transaction_change->undo; | |||||||||||||||||||||||||||
| 225 | 0 | $transaction_change->delete; | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 0 | $#change_log = $begin_point-1; | |||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 0 | ▶ | unless($self->isa("UR::DeletedRef")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | #$DB::single = 1; | ||||||||||||||||||||||||||||
| 232 | 0 | Carp::confess("Failed to remove transaction during rollback."); | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 0 | pop @open_transaction_stack; | |||||||||||||||||||||||||||
| 236 | 0 | ▶ | unless (@open_transaction_stack) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | 0 | $log_all_changes = 0; | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | 0 | $UR::Context::current = $parent; | |||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 0 | return 1; | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 0 | ▶ | sub commit { | ||||||||||||||||||||||||||
|
Subroutine
commit: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||
| 245 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | # Support calling as a class method: UR::Context::Transaction->commit commits the current transaction. | ||||||||||||||||||||||||||||
| 248 | 0 | ▶ | unless (ref($self)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 249 | 0 | $self = $open_transaction_stack[-1]; | |||||||||||||||||||||||||||
| 250 | 0 | ▶ | unless ($self) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | 0 | Carp::confess("No open transaction!? Cannot commit."); | |||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 0 | ▶ | if ($self->state ne TRANSACTION_STATE_OPEN) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 256 | 0 | Carp::confess("Cannot commit a transaction that is " . $self->state . ".") | |||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 0 | ▶ | unless ($open_transaction_stack[-1] == $self) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 260 | # TODO: decide if this should work like rollback, and commit nested transactions automatically | ||||||||||||||||||||||||||||
| 261 | 0 | Carp::confess("Cannot commit a transaction with open sub-transactions!"); | |||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | 0 | $self->__signal_change__('precommit'); | |||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 0 | my $validator = $self->commit_validator; | |||||||||||||||||||||||||||
| 266 | 0 | ▶ | unless ($self->$validator()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | 0 | return; | |||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 0 | $self->state(TRANSACTION_STATE_COMMITTED); | |||||||||||||||||||||||||||
| 271 | 0 | ▶ | if ($self->state eq TRANSACTION_STATE_COMMITTED) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 272 | 0 | $self->__signal_change__('commit',1); | |||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | else { | ||||||||||||||||||||||||||||
| 275 | 0 | $self->__signal_change__('commit',0); | |||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | 0 | pop @open_transaction_stack; | |||||||||||||||||||||||||||
| 278 | 0 | ▶ | unless (@open_transaction_stack) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 279 | 0 | $log_all_changes = 0; | |||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | 0 | $UR::Context::current = $self->parent; | |||||||||||||||||||||||||||
| 283 | 0 | return 1; | |||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | 0 | ▶ | sub changes_can_be_saved { | ||||||||||||||||||||||||||
|
Subroutine
changes_can_be_saved: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 287 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | # This is very similar to behavior in UR::Context::_sync_databases. The only | ||||||||||||||||||||||||||||
| 290 | # reason it isn't re-used from UR::Context is the desire to limit changed | ||||||||||||||||||||||||||||
| 291 | # objects to those changed within the transaction. | ||||||||||||||||||||||||||||
| 292 | # TODO: limit to objects that changed within transaction as to not duplicate | ||||||||||||||||||||||||||||
| 293 | # error checking unnecessarily. | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | my @changed_objects = | ||||||||||||||||||||||||||||
| 296 | 0 | grep { ! $_->isa('UR::DeletedRef') } | |||||||||||||||||||||||||||
| 297 | 0 | map { $_->changed_object() } $self->get_changes(); | |||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | # This is primarily to catch custom validity logic in class overrides. | ||||||||||||||||||||||||||||
| 300 | 0 | my @invalid = grep { $_->__errors__ } @changed_objects; | |||||||||||||||||||||||||||
| 301 | 0 | ▶ | if (@invalid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 302 | 0 | $self->display_invalid_data_for_save(\@invalid); | |||||||||||||||||||||||||||
| 303 | 0 | return; | |||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | 0 | return 1; | |||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 0 | ▶ | sub eval_or_do { | ||||||||||||||||||||||||||
|
Subroutine
eval_or_do: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 310 | 0 | my $is_failure = shift; | |||||||||||||||||||||||||||
| 311 | 0 | my $block = shift; | |||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | 0 | my $class = __PACKAGE__; | |||||||||||||||||||||||||||
| 314 | 0 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 315 | 0 | confess('%s::eval takes one argument', $class); | |||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | 0 | my $tx = $class->begin(); | |||||||||||||||||||||||||||
| 318 | 0 | my $result = CORE::eval { $block->() }; | |||||||||||||||||||||||||||
| 319 | 0 | my $eval_error = $@; | |||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 0 | ▶ | if ($is_failure->($result, $eval_error)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 322 | 0 | $class->debug_message(shortmess('Rolling back transaction')); | |||||||||||||||||||||||||||
| 323 | 0 | ▶ | $class->debug_message($eval_error) if ($eval_error); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | 0 | ▶ | unless($tx->rollback()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | 0 | Carp::croak 'failed to rollback transaction'; | |||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | } else { | ||||||||||||||||||||||||||||
| 328 | 0 | ▶ | unless($tx->commit()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 329 | 0 | Carp::croak 'failed to commit transaction'; | |||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | 0 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 334 | 0 | return ($result, $eval_error); | |||||||||||||||||||||||||||
| 335 | } else { | ||||||||||||||||||||||||||||
| 336 | 0 | return $result; | |||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | # eval function takes a block (&) sort of like CORE::eval | ||||||||||||||||||||||||||||
| 341 | # eval will rollback on a caught die | ||||||||||||||||||||||||||||
| 342 | 0 | ▶ | sub eval(&) { | ||||||||||||||||||||||||||
|
Subroutine
eval: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 343 | 0 | ▶ | my $is_failure = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 344 | 0 | my ($result, $eval_error) = @_; | |||||||||||||||||||||||||||
| 345 | 0 | return $eval_error; | |||||||||||||||||||||||||||
| 346 | 0 | }; | |||||||||||||||||||||||||||
| 347 | 0 | return eval_or_do($is_failure, @_); | |||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | # do function takes a block (&) sort of like CORE::do | ||||||||||||||||||||||||||||
| 351 | # do will rollback on a false result as well as before re-throwing a caught die | ||||||||||||||||||||||||||||
| 352 | 0 | ▶ | sub do(&) { | ||||||||||||||||||||||||||
|
Subroutine
do: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 353 | 0 | ▶ | my $is_failure = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 354 | 0 | my ($result, $eval_error) = @_; | |||||||||||||||||||||||||||
| 355 | 0 | ▶ | return !$result || $eval_error; | ||||||||||||||||||||||||||
|
Condition0%0/3
!$result || $eval_error
MC/DC0%0/2
!$result || $eval_error
!$result
$eval_error
Truth table0%0/3
not(A) || B
A= !$resultB= $eval_error
| |||||||||||||||||||||||||||||
| 356 | 0 | }; | |||||||||||||||||||||||||||
| 357 | 0 | my ($result, $eval_error) = eval_or_do($is_failure, @_); | |||||||||||||||||||||||||||
| 358 | 0 | ▶ | if ($eval_error) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 359 | 0 | croak $eval_error, "\t...propogated"; | |||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | 0 | return $result; | |||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | 265 | 1; | |||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | =pod | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | =head1 NAME | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | UR::Context::Transaction - API for software transactions | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | my $o = Some::Obj->create(foo => 1); | ||||||||||||||||||||||||||||
| 375 | print "o's foo is ",$o->foo,"\n"; # prints 1 | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | my $t = UR::Context::Transaction->begin(); | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | $o->foo(4); | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | print "In transaction, o's foo is ",$o->foo,"\n"; # prints 4 | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | if (&should_we_commit()) { | ||||||||||||||||||||||||||||
| 384 | $t->commit(); | ||||||||||||||||||||||||||||
| 385 | print "Transaction committed, o's foo is ",$o->foo,"\n"; # prints 4 | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | } else { | ||||||||||||||||||||||||||||
| 388 | $t->rollback(); | ||||||||||||||||||||||||||||
| 389 | print "Transaction rollback, o's foo is ",$o->foo,"\n"; # prints 1 | ||||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | UR::Context::Transaction instances represent in-memory transactions as a diff | ||||||||||||||||||||||||||||
| 395 | of the contents of the object cache in the Process context. Transactions are | ||||||||||||||||||||||||||||
| 396 | nestable. Their instances exist in the object cache and are subject to the | ||||||||||||||||||||||||||||
| 397 | same scoping rules as other UR-based objects, meaning that they do not | ||||||||||||||||||||||||||||
| 398 | disappear mearly because the lexical variable they're assigned to goes out of | ||||||||||||||||||||||||||||
| 399 | scope. They must be explicitly disposed of via the commit or rollback methods. | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | =head1 INHERITANCE | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | UR::Context::Transaction is a subclass of UR::Context | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | =over 4 | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | =item begin | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | $t = UR::Context::Transaction->begin(); | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | Creates a new software transaction context to track changes to UR-based | ||||||||||||||||||||||||||||
| 414 | objects. As all activity to objects occurs in some kind of transaction | ||||||||||||||||||||||||||||
| 415 | context, the newly created transaction exists within whatever context was | ||||||||||||||||||||||||||||
| 416 | current before the call to begin(). | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | $t = UR::Context::Transaction->begin(commit_validator => sub { ... }); | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | A validation function may be assigned with the C<commit_validator> property. | ||||||||||||||||||||||||||||
| 421 | When the transaction is committed, this function is called. The commit | ||||||||||||||||||||||||||||
| 422 | proceeds if this function returns a true value. The default function, | ||||||||||||||||||||||||||||
| 423 | C<changes_can_be_saved> requires that all objects changed within the | ||||||||||||||||||||||||||||
| 424 | transaction be valid, ie. that C<$obj->__errors__()> returns an empty list. | ||||||||||||||||||||||||||||
| 425 | The validation function is passed one argument: the transaction object | ||||||||||||||||||||||||||||
| 426 | being committed. | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | =back | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | =head1 METHODS | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | =over 4 | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | =item commit | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | $t->commit(); | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | Causes all objects with changes to save those changes back to the underlying | ||||||||||||||||||||||||||||
| 439 | context. | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | If the validation function (specified with the C<commit_validator> param when | ||||||||||||||||||||||||||||
| 442 | the transaction was created with C<begin()>) returns false, the changes are | ||||||||||||||||||||||||||||
| 443 | not committed to the encompassing context, C<commit()> returns false and this | ||||||||||||||||||||||||||||
| 444 | transaction remains in effect. | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | Returns true if all the transaction's changes are committed to the encompassing | ||||||||||||||||||||||||||||
| 447 | Context. This transaction object then becomes invalid, and its state will be | ||||||||||||||||||||||||||||
| 448 | 'committed'. | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | =item rollback | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | $t->rollback(); | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | Causes all objects with changes to have those changes reverted to their | ||||||||||||||||||||||||||||
| 455 | state when the transaction began. Classes with properties whose meta-property | ||||||||||||||||||||||||||||
| 456 | is_transactional => 0 are not tracked within a transaction and will not be | ||||||||||||||||||||||||||||
| 457 | reverted. | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | After C<rollback()>, this transaction becomes invalid, and the object will become | ||||||||||||||||||||||||||||
| 460 | a L<UR::DeletedRef>. | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | =item delete | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | $t->delete(); | ||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | delete() is a synomym for rollback | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | =item has_changes | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | $bool = $t->has_changes(); | ||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||
| 472 | Returns true if any UR-based objects have changes within the transaction. | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | =item get_changes | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | @changes = $t->get_changes(); | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | Return a list or L<UR::Change> objects representing changes within the transaction. | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | =back | ||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | =head1 CLASS METHODS | ||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | =over 4 | ||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | =item eval | ||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | UR::Context::Transaction::eval BLOCK | ||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | Executes the BLOCK (with no arguments) wrapped by a software transaction and a | ||||||||||||||||||||||||||||
| 491 | CORE::eval. If the BLOCK dies then the exception is caught and the software | ||||||||||||||||||||||||||||
| 492 | transaction is rolled back. | ||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | =item do | ||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | UR::Context::Transaction::do BLOCK | ||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | Executes the BLOCK (with no arguments) wrapped by a software transaction and a | ||||||||||||||||||||||||||||
| 499 | CORE::eval. If the BLOCK returns a true value and does not die then the | ||||||||||||||||||||||||||||
| 500 | software transaction is committed. If the BLOCK returns false or dies then the | ||||||||||||||||||||||||||||
| 501 | software transaction is rolled back. | ||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||
| 503 | If the BLOCK throws an exception, it will be caught, the software transaction | ||||||||||||||||||||||||||||
| 504 | rolled back, and the exception will be re-thrown with die(). | ||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | =back | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | =head1 EXPORTS | ||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | This module can export constants that match the valid values of the C<state> | ||||||||||||||||||||||||||||
| 511 | property: TRANSACTION_STATE_OPEN and TRANSACTION_STATE_COMMITTED | ||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||
| 513 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | L<UR::Context> | ||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | =cut | ||||||||||||||||||||||||||||