(optdeps) One last pass through tests, streamline skip messages
[dbsrgits/DBIx-Class.git] / t / storage / savepoints.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
ddf66ced 3use strict;
4use warnings;
5
6use Test::More;
cf1d16d8 7use Test::Exception;
bbf6a9a5 8use DBIx::Class::_Util qw(modver_gt_or_eq sigwarn_silencer scope_guard);
2cfc22dd 9
c0329273 10
2cfc22dd 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 = $_) {
ddf66ced 40
3cff955a 41 DBIx::Class::Optional::Dependencies->skip_without($env2optdep->{$prefix});
ddf66ced 42
3cff955a 43 my ($dsn, $user, $pass) = map { $ENV{"${prefix}_$_"} } qw/DSN USER PASS/;
cf1d16d8 44
c7e85630 45 $schema = DBICTest::Schema->connect ($dsn,$user,$pass,{ auto_savepoint => 1 });
cf1d16d8 46
47 my $create_sql;
48 $schema->storage->ensure_connected;
49 if ($schema->storage->isa('DBIx::Class::Storage::DBI::Pg')) {
50 $create_sql = "CREATE TABLE artist (artistid serial PRIMARY KEY, name VARCHAR(100), rank INTEGER NOT NULL DEFAULT '13', charfield CHAR(10))";
51 $schema->storage->dbh->do('SET client_min_messages=WARNING');
52 }
53 elsif ($schema->storage->isa('DBIx::Class::Storage::DBI::mysql')) {
54 $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";
55 }
56 else {
57 skip( 'Untested driver ' . $schema->storage, 1 );
58 }
59
60 $schema->storage->dbh_do (sub {
61 $_[1]->do('DROP TABLE IF EXISTS artist');
62 $_[1]->do($create_sql);
63 });
ae1d3ea1 64 }
65 else {
cf1d16d8 66 $prefix = 'SQLite Internal DB';
67 $schema = DBICTest->init_schema( no_populate => 1, auto_savepoint => 1 );
ae1d3ea1 68 }
ddf66ced 69
ae1d3ea1 70 note "Testing $prefix";
ddf66ced 71
b74b15b0 72 # can not use local() due to an unknown number of storages
73 # (think replicated)
74 my $orig_states = { map
75 { $_ => $schema->storage->$_ }
76 qw(debugcb debugobj debug)
77 };
bbf6a9a5 78 my $sg = scope_guard {
b74b15b0 79 $schema->storage->$_ ( $orig_states->{$_} ) for keys %$orig_states;
bbf6a9a5 80 };
b74b15b0 81 $schema->storage->debugobj (my $stats = DBICTest::SVPTracerObj->new);
82 $schema->storage->debug (1);
ddf66ced 83
ae1d3ea1 84 $schema->resultset('Artist')->create({ name => 'foo' });
ddf66ced 85
ae1d3ea1 86 $schema->txn_begin;
ddf66ced 87
ae1d3ea1 88 my $arty = $schema->resultset('Artist')->find(1);
ddf66ced 89
ae1d3ea1 90 my $name = $arty->name;
ddf66ced 91
ae1d3ea1 92 # First off, test a generated savepoint name
93 $schema->svp_begin;
ddf66ced 94
ae1d3ea1 95 cmp_ok($stats->{'SVP_BEGIN'}, '==', 1, 'Statistics svp_begin tickled');
ddf66ced 96
ae1d3ea1 97 $arty->update({ name => 'Jheephizzy' });
ddf66ced 98
ae1d3ea1 99 $arty->discard_changes;
ddf66ced 100
ae1d3ea1 101 cmp_ok($arty->name, 'eq', 'Jheephizzy', 'Name changed');
ddf66ced 102
ae1d3ea1 103 # Rollback the generated name
104 # Active: 0
105 $schema->svp_rollback;
ddf66ced 106
ae1d3ea1 107 cmp_ok($stats->{'SVP_ROLLBACK'}, '==', 1, 'Statistics svp_rollback tickled');
ddf66ced 108
ae1d3ea1 109 $arty->discard_changes;
ddf66ced 110
ae1d3ea1 111 cmp_ok($arty->name, 'eq', $name, 'Name rolled back');
ddf66ced 112
ae1d3ea1 113 $arty->update({ name => 'Jheephizzy'});
ddf66ced 114
ae1d3ea1 115 # Active: 0 1
116 $schema->svp_begin('testing1');
ddf66ced 117
ae1d3ea1 118 $arty->update({ name => 'yourmom' });
ddf66ced 119
ae1d3ea1 120 # Active: 0 1 2
121 $schema->svp_begin('testing2');
ddf66ced 122
ae1d3ea1 123 $arty->update({ name => 'gphat' });
124 $arty->discard_changes;
125 cmp_ok($arty->name, 'eq', 'gphat', 'name changed');
cf1d16d8 126
ae1d3ea1 127 # Active: 0 1 2
128 # Rollback doesn't DESTROY the savepoint, it just rolls back to the value
3334d204 129 # at its conception
ae1d3ea1 130 $schema->svp_rollback('testing2');
131 $arty->discard_changes;
132 cmp_ok($arty->name, 'eq', 'yourmom', 'testing2 reverted');
ddf66ced 133
ae1d3ea1 134 # Active: 0 1 2 3
135 $schema->svp_begin('testing3');
136 $arty->update({ name => 'coryg' });
cf1d16d8 137
ae1d3ea1 138 # Active: 0 1 2 3 4
139 $schema->svp_begin('testing4');
140 $arty->update({ name => 'watson' });
ddf66ced 141
ae1d3ea1 142 # Release 3, which implicitly releases 4
143 # Active: 0 1 2
144 $schema->svp_release('testing3');
cf1d16d8 145
ae1d3ea1 146 $arty->discard_changes;
147 cmp_ok($arty->name, 'eq', 'watson', 'release left data');
cf1d16d8 148
ae1d3ea1 149 # This rolls back savepoint 2
150 # Active: 0 1 2
151 $schema->svp_rollback;
cf1d16d8 152
ae1d3ea1 153 $arty->discard_changes;
154 cmp_ok($arty->name, 'eq', 'yourmom', 'rolled back to 2');
ddf66ced 155
ae1d3ea1 156 # Rollback the original savepoint, taking us back to the beginning, implicitly
157 # rolling back savepoint 1 and 2
158 $schema->svp_rollback('savepoint_0');
159 $arty->discard_changes;
160 cmp_ok($arty->name, 'eq', 'foo', 'rolled back to start');
ddf66ced 161
ae1d3ea1 162 $schema->txn_commit;
163
cf1d16d8 164 is_deeply( $schema->storage->savepoints, [], 'All savepoints forgotten' );
165
ae1d3ea1 166 # And now to see if txn_do will behave correctly
167 $schema->txn_do (sub {
65d35121 168 my $artycp = $arty;
169
ae1d3ea1 170 $schema->txn_do (sub {
65d35121 171 $artycp->name ('Muff');
172 $artycp->update;
ae1d3ea1 173 });
ddf66ced 174
175 eval {
176 $schema->txn_do (sub {
65d35121 177 $artycp->name ('Moff');
178 $artycp->update;
179 $artycp->discard_changes;
180 is($artycp->name,'Moff','Value updated in nested transaction');
ae1d3ea1 181 $schema->storage->dbh->do ("GUARANTEED TO PHAIL");
182 });
ddf66ced 183 };
184
185 ok ($@,'Nested transaction failed (good)');
186
187 $arty->discard_changes;
188
189 is($arty->name,'Muff','auto_savepoint rollback worked');
190
191 $arty->name ('Miff');
192
193 $arty->update;
194 });
195
cf1d16d8 196 is_deeply( $schema->storage->savepoints, [], 'All savepoints forgotten' );
197
ae1d3ea1 198 $arty->discard_changes;
199
200 is($arty->name,'Miff','auto_savepoint worked');
201
202 cmp_ok($stats->{'SVP_BEGIN'},'==',7,'Correct number of savepoints created');
ddf66ced 203
ae1d3ea1 204 cmp_ok($stats->{'SVP_RELEASE'},'==',3,'Correct number of savepoints released');
ddf66ced 205
ae1d3ea1 206 cmp_ok($stats->{'SVP_ROLLBACK'},'==',5,'Correct number of savepoint rollbacks');
ddf66ced 207
cf1d16d8 208### test originally written for SQLite exclusively (git blame -w -C -M)
209 # test two-phase commit and inner transaction rollback from nested transactions
210 my $ars = $schema->resultset('Artist');
211
212 $schema->txn_do(sub {
213 $ars->create({ name => 'in_outer_transaction' });
214 $schema->txn_do(sub {
215 $ars->create({ name => 'in_inner_transaction' });
216 });
217 ok($ars->search({ name => 'in_inner_transaction' })->first,
218 'commit from inner transaction visible in outer transaction');
219 throws_ok {
220 $schema->txn_do(sub {
221 $ars->create({ name => 'in_inner_transaction_rolling_back' });
222 die 'rolling back inner transaction';
223 });
224 } qr/rolling back inner transaction/, 'inner transaction rollback executed';
225 $ars->create({ name => 'in_outer_transaction2' });
226 });
227
228 is_deeply( $schema->storage->savepoints, [], 'All savepoints forgotten' );
229
8b60b921 230SKIP: {
3cff955a 231 skip "FIXME: Reading inexplicably fails on very old replicated DBD::SQLite<1.33", 1 if (
8b60b921 232 $ENV{DBICTEST_VIA_REPLICATED}
233 and
234 $prefix eq 'SQLite Internal DB'
235 and
236 ! modver_gt_or_eq('DBD::SQLite', '1.33')
237 );
238
cf1d16d8 239 ok($ars->search({ name => 'in_outer_transaction' })->first,
240 'commit from outer transaction');
241 ok($ars->search({ name => 'in_outer_transaction2' })->first,
242 'second commit from outer transaction');
243 ok($ars->search({ name => 'in_inner_transaction' })->first,
244 'commit from inner transaction');
245 is $ars->search({ name => 'in_inner_transaction_rolling_back' })->first,
246 undef,
247 'rollback from inner transaction';
8b60b921 248}
cf1d16d8 249
250### cleanupz
b74b15b0 251 $schema->storage->dbh_do(sub { $_[1]->do("DROP TABLE artist") });
ae1d3ea1 252}}
ddf66ced 253
ae1d3ea1 254done_testing;
ddf66ced 255
65d35121 256END {
8b60b921 257 local $SIG{__WARN__} = sigwarn_silencer( qr/Internal transaction state of handle/ )
258 unless modver_gt_or_eq('DBD::SQLite', '1.33');
b74b15b0 259 eval { $schema->storage->dbh_do(sub { $_[1]->do("DROP TABLE artist") }) } if defined $schema;
65d35121 260 undef $schema;
261}