6 use DBIx::Class::Optional::Dependencies ();
9 DBICTEST_PG => 'test_rdbms_pg',
10 DBICTEST_MYSQL => 'test_rdbms_mysql',
19 for ('', keys %$env2optdep) { SKIP: {
24 my ($dsn, $user, $pass) = map { $ENV{"${prefix}_$_"} } qw/DSN USER PASS/;
26 skip ("Skipping tests with $prefix: set \$ENV{${prefix}_DSN} _USER and _PASS", 1)
29 skip ("Testing with ${prefix}_DSN needs " . DBIx::Class::Optional::Dependencies->req_missing_for( $env2optdep->{$prefix} ), 1)
30 unless DBIx::Class::Optional::Dependencies->req_ok_for($env2optdep->{$prefix});
32 $schema = DBICTest::Schema->connect ($dsn,$user,$pass,{ auto_savepoint => 1 });
35 $schema->storage->ensure_connected;
36 if ($schema->storage->isa('DBIx::Class::Storage::DBI::Pg')) {
37 $create_sql = "CREATE TABLE artist (artistid serial PRIMARY KEY, name VARCHAR(100), rank INTEGER NOT NULL DEFAULT '13', charfield CHAR(10))";
38 $schema->storage->dbh->do('SET client_min_messages=WARNING');
40 elsif ($schema->storage->isa('DBIx::Class::Storage::DBI::mysql')) {
41 $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";
44 skip( 'Untested driver ' . $schema->storage, 1 );
47 $schema->storage->dbh_do (sub {
48 $_[1]->do('DROP TABLE IF EXISTS artist');
49 $_[1]->do($create_sql);
53 $prefix = 'SQLite Internal DB';
54 $schema = DBICTest->init_schema( no_populate => 1, auto_savepoint => 1 );
57 note "Testing $prefix";
59 my $stats = DBICTest::Stats->new;
60 $schema->storage->debugobj($stats);
61 $schema->storage->debug(1);
63 $schema->resultset('Artist')->create({ name => 'foo' });
67 my $arty = $schema->resultset('Artist')->find(1);
69 my $name = $arty->name;
71 # First off, test a generated savepoint name
74 cmp_ok($stats->{'SVP_BEGIN'}, '==', 1, 'Statistics svp_begin tickled');
76 $arty->update({ name => 'Jheephizzy' });
78 $arty->discard_changes;
80 cmp_ok($arty->name, 'eq', 'Jheephizzy', 'Name changed');
82 # Rollback the generated name
84 $schema->svp_rollback;
86 cmp_ok($stats->{'SVP_ROLLBACK'}, '==', 1, 'Statistics svp_rollback tickled');
88 $arty->discard_changes;
90 cmp_ok($arty->name, 'eq', $name, 'Name rolled back');
92 $arty->update({ name => 'Jheephizzy'});
95 $schema->svp_begin('testing1');
97 $arty->update({ name => 'yourmom' });
100 $schema->svp_begin('testing2');
102 $arty->update({ name => 'gphat' });
103 $arty->discard_changes;
104 cmp_ok($arty->name, 'eq', 'gphat', 'name changed');
107 # Rollback doesn't DESTROY the savepoint, it just rolls back to the value
109 $schema->svp_rollback('testing2');
110 $arty->discard_changes;
111 cmp_ok($arty->name, 'eq', 'yourmom', 'testing2 reverted');
114 $schema->svp_begin('testing3');
115 $arty->update({ name => 'coryg' });
118 $schema->svp_begin('testing4');
119 $arty->update({ name => 'watson' });
121 # Release 3, which implicitly releases 4
123 $schema->svp_release('testing3');
125 $arty->discard_changes;
126 cmp_ok($arty->name, 'eq', 'watson', 'release left data');
128 # This rolls back savepoint 2
130 $schema->svp_rollback;
132 $arty->discard_changes;
133 cmp_ok($arty->name, 'eq', 'yourmom', 'rolled back to 2');
135 # Rollback the original savepoint, taking us back to the beginning, implicitly
136 # rolling back savepoint 1 and 2
137 $schema->svp_rollback('savepoint_0');
138 $arty->discard_changes;
139 cmp_ok($arty->name, 'eq', 'foo', 'rolled back to start');
143 is_deeply( $schema->storage->savepoints, [], 'All savepoints forgotten' );
145 # And now to see if txn_do will behave correctly
146 $schema->txn_do (sub {
149 $schema->txn_do (sub {
150 $artycp->name ('Muff');
155 $schema->txn_do (sub {
156 $artycp->name ('Moff');
158 $artycp->discard_changes;
159 is($artycp->name,'Moff','Value updated in nested transaction');
160 $schema->storage->dbh->do ("GUARANTEED TO PHAIL");
164 ok ($@,'Nested transaction failed (good)');
166 $arty->discard_changes;
168 is($arty->name,'Muff','auto_savepoint rollback worked');
170 $arty->name ('Miff');
175 is_deeply( $schema->storage->savepoints, [], 'All savepoints forgotten' );
177 $arty->discard_changes;
179 is($arty->name,'Miff','auto_savepoint worked');
181 cmp_ok($stats->{'SVP_BEGIN'},'==',7,'Correct number of savepoints created');
183 cmp_ok($stats->{'SVP_RELEASE'},'==',3,'Correct number of savepoints released');
185 cmp_ok($stats->{'SVP_ROLLBACK'},'==',5,'Correct number of savepoint rollbacks');
187 ### test originally written for SQLite exclusively (git blame -w -C -M)
188 # test two-phase commit and inner transaction rollback from nested transactions
189 my $ars = $schema->resultset('Artist');
191 $schema->txn_do(sub {
192 $ars->create({ name => 'in_outer_transaction' });
193 $schema->txn_do(sub {
194 $ars->create({ name => 'in_inner_transaction' });
196 ok($ars->search({ name => 'in_inner_transaction' })->first,
197 'commit from inner transaction visible in outer transaction');
199 $schema->txn_do(sub {
200 $ars->create({ name => 'in_inner_transaction_rolling_back' });
201 die 'rolling back inner transaction';
203 } qr/rolling back inner transaction/, 'inner transaction rollback executed';
204 $ars->create({ name => 'in_outer_transaction2' });
207 is_deeply( $schema->storage->savepoints, [], 'All savepoints forgotten' );
209 ok($ars->search({ name => 'in_outer_transaction' })->first,
210 'commit from outer transaction');
211 ok($ars->search({ name => 'in_outer_transaction2' })->first,
212 'second commit from outer transaction');
213 ok($ars->search({ name => 'in_inner_transaction' })->first,
214 'commit from inner transaction');
215 is $ars->search({ name => 'in_inner_transaction_rolling_back' })->first,
217 'rollback from inner transaction';
220 $schema->storage->dbh->do ("DROP TABLE artist");
226 eval { $schema->storage->dbh->do ("DROP TABLE artist") } if defined $schema;