6 my ($create_sql, $dsn, $user, $pass);
8 if ($ENV{DBICTEST_PG_DSN}) {
9 ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
11 $create_sql = "CREATE TABLE artist (artistid serial PRIMARY KEY, name VARCHAR(100), rank INTEGER NOT NULL DEFAULT '13', charfield CHAR(10))";
12 } elsif ($ENV{DBICTEST_MYSQL_DSN}) {
13 ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/};
15 $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";
17 plan skip_all => 'Set DBICTEST_(PG|MYSQL)_DSN _USER and _PASS if you want to run savepoint tests';
26 my $schema = DBICTest::Schema->connect ($dsn,$user,$pass,{ auto_savepoint => 1 });
28 my $stats = DBICTest::Stats->new;
30 $schema->storage->debugobj($stats);
32 $schema->storage->debug(1);
35 local $SIG{__WARN__} = sub {};
36 $schema->storage->dbh->do ('DROP TABLE IF EXISTS artist');
37 $schema->storage->dbh->do ($create_sql);
40 $schema->resultset('Artist')->create({ name => 'foo' });
44 my $arty = $schema->resultset('Artist')->find(1);
46 my $name = $arty->name;
48 # First off, test a generated savepoint name
51 cmp_ok($stats->{'SVP_BEGIN'}, '==', 1, 'Statistics svp_begin tickled');
53 $arty->update({ name => 'Jheephizzy' });
55 $arty->discard_changes;
57 cmp_ok($arty->name, 'eq', 'Jheephizzy', 'Name changed');
59 # Rollback the generated name
61 $schema->svp_rollback;
63 cmp_ok($stats->{'SVP_ROLLBACK'}, '==', 1, 'Statistics svp_rollback tickled');
65 $arty->discard_changes;
67 cmp_ok($arty->name, 'eq', $name, 'Name rolled back');
69 $arty->update({ name => 'Jheephizzy'});
72 $schema->svp_begin('testing1');
74 $arty->update({ name => 'yourmom' });
77 $schema->svp_begin('testing2');
79 $arty->update({ name => 'gphat' });
80 $arty->discard_changes;
81 cmp_ok($arty->name, 'eq', 'gphat', 'name changed');
83 # Rollback doesn't DESTROY the savepoint, it just rolls back to the value
85 $schema->svp_rollback('testing2');
86 $arty->discard_changes;
87 cmp_ok($arty->name, 'eq', 'yourmom', 'testing2 reverted');
90 $schema->svp_begin('testing3');
91 $arty->update({ name => 'coryg' });
93 $schema->svp_begin('testing4');
94 $arty->update({ name => 'watson' });
96 # Release 3, which implicitly releases 4
98 $schema->svp_release('testing3');
99 $arty->discard_changes;
100 cmp_ok($arty->name, 'eq', 'watson', 'release left data');
101 # This rolls back savepoint 2
103 $schema->svp_rollback;
104 $arty->discard_changes;
105 cmp_ok($arty->name, 'eq', 'yourmom', 'rolled back to 2');
107 # Rollback the original savepoint, taking us back to the beginning, implicitly
108 # rolling back savepoint 1 and 2
109 $schema->svp_rollback('savepoint_0');
110 $arty->discard_changes;
111 cmp_ok($arty->name, 'eq', 'foo', 'rolled back to start');
115 # And now to see if txn_do will behave correctly
117 $schema->txn_do (sub {
118 $schema->txn_do (sub {
119 $arty->name ('Muff');
125 $schema->txn_do (sub {
126 $arty->name ('Moff');
130 $arty->discard_changes;
132 is($arty->name,'Moff','Value updated in nested transaction');
134 $schema->storage->dbh->do ("GUARANTEED TO PHAIL");
138 ok ($@,'Nested transaction failed (good)');
140 $arty->discard_changes;
142 is($arty->name,'Muff','auto_savepoint rollback worked');
144 $arty->name ('Miff');
149 $arty->discard_changes;
151 is($arty->name,'Miff','auto_savepoint worked');
153 cmp_ok($stats->{'SVP_BEGIN'},'==',7,'Correct number of savepoints created');
155 cmp_ok($stats->{'SVP_RELEASE'},'==',3,'Correct number of savepoints released');
157 cmp_ok($stats->{'SVP_ROLLBACK'},'==',5,'Correct number of savepoint rollbacks');
159 END { $schema->storage->dbh->do ("DROP TABLE artist") if defined $schema }