10 # Test txn_scope_guard
12 my $schema = DBICTest->init_schema();
14 is($schema->storage->transaction_depth, 0, "Correct transaction depth");
15 my $artist_rs = $schema->resultset('Artist');
19 my $guard = $schema->txn_scope_guard;
22 name => 'Death Cab for Cutie',
27 } qr/No such column made_up_column .*? at .*?\Q$fn\E line \d+/s, "Error propogated okay";
29 ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
31 my $inner_exception = ''; # set in inner() below
34 }, qr/$inner_exception/, "Nested exceptions propogated");
36 ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
40 # this weird assignment is to stop perl <= 5.8.9 leaking $schema on nested sub{}s
43 warnings_exist ( sub {
44 # The 0 arg says don't die, just let the scope guard go out of scope
45 # forcing a txn_rollback to happen
47 }, qr/A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back./, 'Out of scope warning detected');
49 ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
51 }, 'rollback successful withot exception');
54 my ($schema, $fatal) = @_;
56 my $guard = $schema->txn_scope_guard;
57 $schema->resultset('Artist')->create({
58 name => 'Death Cab for Cutie',
60 inner($schema, $fatal);
64 my ($schema, $fatal) = @_;
66 my $inner_guard = $schema->txn_scope_guard;
67 is($schema->storage->transaction_depth, 2, "Correct transaction depth");
69 my $artist = $schema->resultset('Artist')->find({ name => 'Death Cab for Cutie' });
72 $artist->cds->create({
75 $fatal ? ( foo => 'bar' ) : ()
79 # Record what got thrown so we can test it propgates out properly.
80 $inner_exception = $@;
84 # inner guard should commit without consequences
89 # make sure the guard does not eat exceptions
91 my $schema = DBICTest->init_schema;
94 no warnings 'redefine';
96 local *{DBIx::Class::Storage::DBI::txn_rollback} = sub { die 'die die my darling' };
97 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
100 my $guard = $schema->txn_scope_guard;
101 $schema->resultset ('Artist')->create ({ name => 'bohhoo'});
103 # this should freak out the guard rollback
104 # but it won't work because DBD::SQLite is buggy
105 # instead just install a toxic rollback above
106 #$schema->storage->_dbh( $schema->storage->_dbh->clone );
108 die 'Deliberate exception';
110 ? qr/Deliberate exception/s # temporary until we get the generic exception wrapper rolling
111 : qr/Deliberate exception.+Rollback failed/s
114 # just to mask off warning since we could not disconnect above
115 $schema->storage->_dbh->disconnect;
118 # make sure it warns *big* on failed rollbacks
119 # test with and without a poisoned $@
120 for my $poison (0,1) {
122 my $schema = DBICTest->init_schema();
125 no warnings 'redefine';
126 local *{DBIx::Class::Storage::DBI::txn_rollback} = sub { die 'die die my darling' };
127 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
129 #The warn from within a DESTROY callback freaks out Test::Warn, do it old-school
133 my $guard = $schema->txn_scope_guard;
134 $schema->resultset ('Artist')->create ({ name => 'bohhoo'});
136 # this should freak out the guard rollback
137 # but it won't work because DBD::SQLite is buggy
138 # instead just install a toxic rollback above
139 #$schema->storage->_dbh( $schema->storage->_dbh->clone );
142 qr/A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back./,
143 qr/\*+ ROLLBACK FAILED\!\!\! \*+/,
145 'proper warnings generated on out-of-scope+rollback failure'
149 # delete this once the above works properly (same test)
151 qr/A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back./,
152 qr/\*+ ROLLBACK FAILED\!\!\! \*+/,
156 local $SIG{__WARN__} = sub {
157 if (grep {$_[0] =~ $_} (@want)) {
165 eval { die 'GIFT!' if $poison };
166 my $guard = $schema->txn_scope_guard;
167 $schema->resultset ('Artist')->create ({ name => 'bohhoo'});
170 is (@w, 2, 'Both expected warnings found' . ($poison ? ' (after $@ poisoning)' : '') );
172 # just to mask off warning since we could not disconnect above
173 $schema->storage->_dbh->disconnect;