Avoid infinite loop if save point does not exist
[dbsrgits/DBIx-Class.git] / t / storage / savepoints.t
CommitLineData
ddf66ced 1use strict;
2use warnings;
3
4use Test::More;
cf1d16d8 5use Test::Exception;
7ae29d74 6use DBIx::Class::Optional::Dependencies;
7use DBIx::Class::_Util qw(sigwarn_silencer scope_guard);
8use Scalar::Util 'weaken';
2cfc22dd 9
10use lib qw(t/lib);
11use 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}
ddf66ced 27
ae1d3ea1 28my $env2optdep = {
cf1d16d8 29 DBICTEST_PG => 'test_rdbms_pg',
ae1d3ea1 30 DBICTEST_MYSQL => 'test_rdbms_mysql',
31};
ddf66ced 32
ae1d3ea1 33my $schema;
ddf66ced 34
cf1d16d8 35for ('', keys %$env2optdep) { SKIP: {
ddf66ced 36
cf1d16d8 37 my $prefix;
ddf66ced 38
cf1d16d8 39 if ($prefix = $_) {
40 my ($dsn, $user, $pass) = map { $ENV{"${prefix}_$_"} } qw/DSN USER PASS/;
ddf66ced 41
cf1d16d8 42 skip ("Skipping tests with $prefix: set \$ENV{${prefix}_DSN} _USER and _PASS", 1)
43 unless $dsn;
ddf66ced 44
cf1d16d8 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 });
ae1d3ea1 67 }
68 else {
cf1d16d8 69 $prefix = 'SQLite Internal DB';
70 $schema = DBICTest->init_schema( no_populate => 1, auto_savepoint => 1 );
ae1d3ea1 71 }
ddf66ced 72
ae1d3ea1 73 note "Testing $prefix";
ddf66ced 74
2cfc22dd 75 local $schema->storage->{debugobj} = my $stats = DBICTest::SVPTracerObj->new;
76 local $schema->storage->{debug} = 1;
ddf66ced 77
ae1d3ea1 78 $schema->resultset('Artist')->create({ name => 'foo' });
ddf66ced 79
ae1d3ea1 80 $schema->txn_begin;
ddf66ced 81
ae1d3ea1 82 my $arty = $schema->resultset('Artist')->find(1);
ddf66ced 83
ae1d3ea1 84 my $name = $arty->name;
ddf66ced 85
ae1d3ea1 86 # First off, test a generated savepoint name
87 $schema->svp_begin;
ddf66ced 88
ae1d3ea1 89 cmp_ok($stats->{'SVP_BEGIN'}, '==', 1, 'Statistics svp_begin tickled');
ddf66ced 90
ae1d3ea1 91 $arty->update({ name => 'Jheephizzy' });
ddf66ced 92
ae1d3ea1 93 $arty->discard_changes;
ddf66ced 94
ae1d3ea1 95 cmp_ok($arty->name, 'eq', 'Jheephizzy', 'Name changed');
ddf66ced 96
ae1d3ea1 97 # Rollback the generated name
98 # Active: 0
99 $schema->svp_rollback;
ddf66ced 100
ae1d3ea1 101 cmp_ok($stats->{'SVP_ROLLBACK'}, '==', 1, 'Statistics svp_rollback tickled');
ddf66ced 102
ae1d3ea1 103 $arty->discard_changes;
ddf66ced 104
ae1d3ea1 105 cmp_ok($arty->name, 'eq', $name, 'Name rolled back');
ddf66ced 106
ae1d3ea1 107 $arty->update({ name => 'Jheephizzy'});
ddf66ced 108
ae1d3ea1 109 # Active: 0 1
110 $schema->svp_begin('testing1');
ddf66ced 111
ae1d3ea1 112 $arty->update({ name => 'yourmom' });
ddf66ced 113
ae1d3ea1 114 # Active: 0 1 2
115 $schema->svp_begin('testing2');
ddf66ced 116
ae1d3ea1 117 $arty->update({ name => 'gphat' });
118 $arty->discard_changes;
119 cmp_ok($arty->name, 'eq', 'gphat', 'name changed');
cf1d16d8 120
ae1d3ea1 121 # Active: 0 1 2
122 # Rollback doesn't DESTROY the savepoint, it just rolls back to the value
3334d204 123 # at its conception
ae1d3ea1 124 $schema->svp_rollback('testing2');
125 $arty->discard_changes;
126 cmp_ok($arty->name, 'eq', 'yourmom', 'testing2 reverted');
ddf66ced 127
ae1d3ea1 128 # Active: 0 1 2 3
129 $schema->svp_begin('testing3');
130 $arty->update({ name => 'coryg' });
cf1d16d8 131
ae1d3ea1 132 # Active: 0 1 2 3 4
133 $schema->svp_begin('testing4');
134 $arty->update({ name => 'watson' });
ddf66ced 135
ae1d3ea1 136 # Release 3, which implicitly releases 4
137 # Active: 0 1 2
138 $schema->svp_release('testing3');
cf1d16d8 139
ae1d3ea1 140 $arty->discard_changes;
141 cmp_ok($arty->name, 'eq', 'watson', 'release left data');
cf1d16d8 142
ae1d3ea1 143 # This rolls back savepoint 2
144 # Active: 0 1 2
145 $schema->svp_rollback;
cf1d16d8 146
ae1d3ea1 147 $arty->discard_changes;
148 cmp_ok($arty->name, 'eq', 'yourmom', 'rolled back to 2');
ddf66ced 149
ae1d3ea1 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');
ddf66ced 155
ae1d3ea1 156 $schema->txn_commit;
157
cf1d16d8 158 is_deeply( $schema->storage->savepoints, [], 'All savepoints forgotten' );
159
ae1d3ea1 160 # And now to see if txn_do will behave correctly
161 $schema->txn_do (sub {
65d35121 162 my $artycp = $arty;
163
ae1d3ea1 164 $schema->txn_do (sub {
65d35121 165 $artycp->name ('Muff');
166 $artycp->update;
ae1d3ea1 167 });
ddf66ced 168
169 eval {
170 $schema->txn_do (sub {
65d35121 171 $artycp->name ('Moff');
172 $artycp->update;
173 $artycp->discard_changes;
174 is($artycp->name,'Moff','Value updated in nested transaction');
ae1d3ea1 175 $schema->storage->dbh->do ("GUARANTEED TO PHAIL");
176 });
ddf66ced 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
cf1d16d8 190 is_deeply( $schema->storage->savepoints, [], 'All savepoints forgotten' );
191
ae1d3ea1 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');
ddf66ced 197
ae1d3ea1 198 cmp_ok($stats->{'SVP_RELEASE'},'==',3,'Correct number of savepoints released');
ddf66ced 199
ae1d3ea1 200 cmp_ok($stats->{'SVP_ROLLBACK'},'==',5,'Correct number of savepoint rollbacks');
ddf66ced 201
cf1d16d8 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
c5d7250e 234 # make sure a fresh txn will work after above
235 $schema->storage->txn_do(sub { ok "noop" } );
236
7ae29d74 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
cf1d16d8 248### cleanupz
ae1d3ea1 249 $schema->storage->dbh->do ("DROP TABLE artist");
250}}
ddf66ced 251
ae1d3ea1 252done_testing;
ddf66ced 253
65d35121 254END {
c5d7250e 255 eval { $schema->storage->dbh_do(sub { $_[1]->do("DROP TABLE artist") }) } if defined $schema;
65d35121 256 undef $schema;
257}