Avoid infinite loop if save point does not exist
[dbsrgits/DBIx-Class.git] / t / storage / savepoints.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use DBIx::Class::Optional::Dependencies;
7 use DBIx::Class::_Util qw(sigwarn_silencer scope_guard);
8 use Scalar::Util 'weaken';
9
10 use lib qw(t/lib);
11 use DBICTest;
12
13 {
14   package # moar hide
15     DBICTest::SVPTracerObj;
16
17   use base 'DBIx::Class::Storage::Statistics';
18
19   sub query_start { 'do notning'}
20   sub callback { 'dummy '}
21
22   for my $svpcall (map { "svp_$_" } qw(begin rollback release)) {
23     no strict 'refs';
24     *$svpcall = sub { $_[0]{uc $svpcall}++ };
25   }
26 }
27
28 my $env2optdep = {
29   DBICTEST_PG => 'test_rdbms_pg',
30   DBICTEST_MYSQL => 'test_rdbms_mysql',
31 };
32
33 my $schema;
34
35 for ('', keys %$env2optdep) { SKIP: {
36
37   my $prefix;
38
39   if ($prefix = $_) {
40     my ($dsn, $user, $pass) = map { $ENV{"${prefix}_$_"} } qw/DSN USER PASS/;
41
42     skip ("Skipping tests with $prefix: set \$ENV{${prefix}_DSN} _USER and _PASS", 1)
43       unless $dsn;
44
45     skip ("Testing with ${prefix}_DSN needs " . DBIx::Class::Optional::Dependencies->req_missing_for( $env2optdep->{$prefix} ), 1)
46       unless  DBIx::Class::Optional::Dependencies->req_ok_for($env2optdep->{$prefix});
47
48     $schema = DBICTest::Schema->connect ($dsn,$user,$pass,{ auto_savepoint => 1 });
49
50     my $create_sql;
51     $schema->storage->ensure_connected;
52     if ($schema->storage->isa('DBIx::Class::Storage::DBI::Pg')) {
53       $create_sql = "CREATE TABLE artist (artistid serial PRIMARY KEY, name VARCHAR(100), rank INTEGER NOT NULL DEFAULT '13', charfield CHAR(10))";
54       $schema->storage->dbh->do('SET client_min_messages=WARNING');
55     }
56     elsif ($schema->storage->isa('DBIx::Class::Storage::DBI::mysql')) {
57       $create_sql = "CREATE TABLE artist (artistid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), rank INTEGER NOT NULL DEFAULT '13', charfield CHAR(10)) ENGINE=InnoDB";
58     }
59     else {
60       skip( 'Untested driver ' . $schema->storage, 1 );
61     }
62
63     $schema->storage->dbh_do (sub {
64       $_[1]->do('DROP TABLE IF EXISTS artist');
65       $_[1]->do($create_sql);
66     });
67   }
68   else {
69     $prefix = 'SQLite Internal DB';
70     $schema = DBICTest->init_schema( no_populate => 1, auto_savepoint => 1 );
71   }
72
73   note "Testing $prefix";
74
75   local $schema->storage->{debugobj} = my $stats = DBICTest::SVPTracerObj->new;
76   local $schema->storage->{debug} = 1;
77
78   $schema->resultset('Artist')->create({ name => 'foo' });
79
80   $schema->txn_begin;
81
82   my $arty = $schema->resultset('Artist')->find(1);
83
84   my $name = $arty->name;
85
86   # First off, test a generated savepoint name
87   $schema->svp_begin;
88
89   cmp_ok($stats->{'SVP_BEGIN'}, '==', 1, 'Statistics svp_begin tickled');
90
91   $arty->update({ name => 'Jheephizzy' });
92
93   $arty->discard_changes;
94
95   cmp_ok($arty->name, 'eq', 'Jheephizzy', 'Name changed');
96
97   # Rollback the generated name
98   # Active: 0
99   $schema->svp_rollback;
100
101   cmp_ok($stats->{'SVP_ROLLBACK'}, '==', 1, 'Statistics svp_rollback tickled');
102
103   $arty->discard_changes;
104
105   cmp_ok($arty->name, 'eq', $name, 'Name rolled back');
106
107   $arty->update({ name => 'Jheephizzy'});
108
109   # Active: 0 1
110   $schema->svp_begin('testing1');
111
112   $arty->update({ name => 'yourmom' });
113
114   # Active: 0 1 2
115   $schema->svp_begin('testing2');
116
117   $arty->update({ name => 'gphat' });
118   $arty->discard_changes;
119   cmp_ok($arty->name, 'eq', 'gphat', 'name changed');
120
121   # Active: 0 1 2
122   # Rollback doesn't DESTROY the savepoint, it just rolls back to the value
123   # at its conception
124   $schema->svp_rollback('testing2');
125   $arty->discard_changes;
126   cmp_ok($arty->name, 'eq', 'yourmom', 'testing2 reverted');
127
128   # Active: 0 1 2 3
129   $schema->svp_begin('testing3');
130   $arty->update({ name => 'coryg' });
131
132   # Active: 0 1 2 3 4
133   $schema->svp_begin('testing4');
134   $arty->update({ name => 'watson' });
135
136   # Release 3, which implicitly releases 4
137   # Active: 0 1 2
138   $schema->svp_release('testing3');
139
140   $arty->discard_changes;
141   cmp_ok($arty->name, 'eq', 'watson', 'release left data');
142
143   # This rolls back savepoint 2
144   # Active: 0 1 2
145   $schema->svp_rollback;
146
147   $arty->discard_changes;
148   cmp_ok($arty->name, 'eq', 'yourmom', 'rolled back to 2');
149
150   # Rollback the original savepoint, taking us back to the beginning, implicitly
151   # rolling back savepoint 1 and 2
152   $schema->svp_rollback('savepoint_0');
153   $arty->discard_changes;
154   cmp_ok($arty->name, 'eq', 'foo', 'rolled back to start');
155
156   $schema->txn_commit;
157
158   is_deeply( $schema->storage->savepoints, [], 'All savepoints forgotten' );
159
160   # And now to see if txn_do will behave correctly
161   $schema->txn_do (sub {
162     my $artycp = $arty;
163
164     $schema->txn_do (sub {
165       $artycp->name ('Muff');
166       $artycp->update;
167     });
168
169     eval {
170       $schema->txn_do (sub {
171         $artycp->name ('Moff');
172         $artycp->update;
173         $artycp->discard_changes;
174         is($artycp->name,'Moff','Value updated in nested transaction');
175         $schema->storage->dbh->do ("GUARANTEED TO PHAIL");
176       });
177     };
178
179     ok ($@,'Nested transaction failed (good)');
180
181     $arty->discard_changes;
182
183     is($arty->name,'Muff','auto_savepoint rollback worked');
184
185     $arty->name ('Miff');
186
187     $arty->update;
188   });
189
190   is_deeply( $schema->storage->savepoints, [], 'All savepoints forgotten' );
191
192   $arty->discard_changes;
193
194   is($arty->name,'Miff','auto_savepoint worked');
195
196   cmp_ok($stats->{'SVP_BEGIN'},'==',7,'Correct number of savepoints created');
197
198   cmp_ok($stats->{'SVP_RELEASE'},'==',3,'Correct number of savepoints released');
199
200   cmp_ok($stats->{'SVP_ROLLBACK'},'==',5,'Correct number of savepoint rollbacks');
201
202 ### test originally written for SQLite exclusively (git blame -w -C -M)
203   # test two-phase commit and inner transaction rollback from nested transactions
204   my $ars = $schema->resultset('Artist');
205
206   $schema->txn_do(sub {
207     $ars->create({ name => 'in_outer_transaction' });
208     $schema->txn_do(sub {
209       $ars->create({ name => 'in_inner_transaction' });
210     });
211     ok($ars->search({ name => 'in_inner_transaction' })->first,
212       'commit from inner transaction visible in outer transaction');
213     throws_ok {
214       $schema->txn_do(sub {
215         $ars->create({ name => 'in_inner_transaction_rolling_back' });
216         die 'rolling back inner transaction';
217       });
218     } qr/rolling back inner transaction/, 'inner transaction rollback executed';
219     $ars->create({ name => 'in_outer_transaction2' });
220   });
221
222   is_deeply( $schema->storage->savepoints, [], 'All savepoints forgotten' );
223
224   ok($ars->search({ name => 'in_outer_transaction' })->first,
225     'commit from outer transaction');
226   ok($ars->search({ name => 'in_outer_transaction2' })->first,
227     'second commit from outer transaction');
228   ok($ars->search({ name => 'in_inner_transaction' })->first,
229     'commit from inner transaction');
230   is $ars->search({ name => 'in_inner_transaction_rolling_back' })->first,
231     undef,
232     'rollback from inner transaction';
233
234   # make sure a fresh txn will work after above
235   $schema->storage->txn_do(sub { ok "noop" } );
236
237 ### Make sure non-existend savepoint release doesn't infloop itself
238   {
239     weaken( my $s = $schema );
240
241     throws_ok {
242       $s->storage->txn_do(sub { $s->svp_release('wibble') })
243     } qr/Savepoint 'wibble' does not exist/,
244       "Calling svp_release on a non-existant savepoint throws expected error"
245     ;
246   }
247
248 ### cleanupz
249   $schema->storage->dbh->do ("DROP TABLE artist");
250 }}
251
252 done_testing;
253
254 END {
255   eval { $schema->storage->dbh_do(sub { $_[1]->do("DROP TABLE artist") }) } if defined $schema;
256   undef $schema;
257 }