9 my ($create_sql, $dsn, $user, $pass);
11 if ($ENV{DBICTEST_PG_DSN}) {
12 ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
14 $create_sql = "CREATE TABLE artist (artistid serial PRIMARY KEY, name VARCHAR(100), rank INTEGER NOT NULL DEFAULT '13', charfield CHAR(10))";
15 } elsif ($ENV{DBICTEST_MYSQL_DSN}) {
16 ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/};
18 $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";
20 plan skip_all => 'Set DBICTEST_(PG|MYSQL)_DSN _USER and _PASS if you want to run savepoint tests';
25 my $schema = DBICTest::Schema->connect ($dsn,$user,$pass,{ auto_savepoint => 1 });
27 my $stats = DBICTest::Stats->new;
29 $schema->storage->debugobj($stats);
31 $schema->storage->debug(1);
34 local $SIG{__WARN__} = sub {};
35 $schema->storage->dbh->do ('DROP TABLE IF EXISTS artist');
36 $schema->storage->dbh->do ($create_sql);
39 $schema->resultset('Artist')->create({ name => 'foo' });
43 my $arty = $schema->resultset('Artist')->find(1);
45 my $name = $arty->name;
47 # First off, test a generated savepoint name
50 cmp_ok($stats->{'SVP_BEGIN'}, '==', 1, 'Statistics svp_begin tickled');
52 $arty->update({ name => 'Jheephizzy' });
54 $arty->discard_changes;
56 cmp_ok($arty->name, 'eq', 'Jheephizzy', 'Name changed');
58 # Rollback the generated name
60 $schema->svp_rollback;
62 cmp_ok($stats->{'SVP_ROLLBACK'}, '==', 1, 'Statistics svp_rollback tickled');
64 $arty->discard_changes;
66 cmp_ok($arty->name, 'eq', $name, 'Name rolled back');
68 $arty->update({ name => 'Jheephizzy'});
71 $schema->svp_begin('testing1');
73 $arty->update({ name => 'yourmom' });
76 $schema->svp_begin('testing2');
78 $arty->update({ name => 'gphat' });
79 $arty->discard_changes;
80 cmp_ok($arty->name, 'eq', 'gphat', 'name changed');
82 # Rollback doesn't DESTROY the savepoint, it just rolls back to the value
84 $schema->svp_rollback('testing2');
85 $arty->discard_changes;
86 cmp_ok($arty->name, 'eq', 'yourmom', 'testing2 reverted');
89 $schema->svp_begin('testing3');
90 $arty->update({ name => 'coryg' });
92 $schema->svp_begin('testing4');
93 $arty->update({ name => 'watson' });
95 # Release 3, which implicitly releases 4
97 $schema->svp_release('testing3');
98 $arty->discard_changes;
99 cmp_ok($arty->name, 'eq', 'watson', 'release left data');
100 # This rolls back savepoint 2
102 $schema->svp_rollback;
103 $arty->discard_changes;
104 cmp_ok($arty->name, 'eq', 'yourmom', 'rolled back to 2');
106 # Rollback the original savepoint, taking us back to the beginning, implicitly
107 # rolling back savepoint 1 and 2
108 $schema->svp_rollback('savepoint_0');
109 $arty->discard_changes;
110 cmp_ok($arty->name, 'eq', 'foo', 'rolled back to start');
114 # And now to see if txn_do will behave correctly
116 $schema->txn_do (sub {
117 $schema->txn_do (sub {
118 $arty->name ('Muff');
124 $schema->txn_do (sub {
125 $arty->name ('Moff');
129 $arty->discard_changes;
131 is($arty->name,'Moff','Value updated in nested transaction');
133 $schema->storage->dbh->do ("GUARANTEED TO PHAIL");
137 ok ($@,'Nested transaction failed (good)');
139 $arty->discard_changes;
141 is($arty->name,'Muff','auto_savepoint rollback worked');
143 $arty->name ('Miff');
148 $arty->discard_changes;
150 is($arty->name,'Miff','auto_savepoint worked');
152 cmp_ok($stats->{'SVP_BEGIN'},'==',7,'Correct number of savepoints created');
154 cmp_ok($stats->{'SVP_RELEASE'},'==',3,'Correct number of savepoints released');
156 cmp_ok($stats->{'SVP_ROLLBACK'},'==',5,'Correct number of savepoint rollbacks');
158 END { $schema->storage->dbh->do ("DROP TABLE artist") if defined $schema }