Initial commit of auto_savepoint + some fixes
[dbsrgits/DBIx-Class.git] / t / 72pg.t
1 use strict;
2 use warnings;  
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7 use DBICTest::Stats;
8
9 {
10   package DBICTest::Schema::Casecheck;
11
12   use strict;
13   use warnings;
14   use base 'DBIx::Class';
15
16   __PACKAGE__->load_components(qw/Core/);
17   __PACKAGE__->table('casecheck');
18   __PACKAGE__->add_columns(qw/id name NAME uc_name/);
19   __PACKAGE__->column_info_from_storage(1);
20   __PACKAGE__->set_primary_key('id');
21
22 }
23
24 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
25
26 #warn "$dsn $user $pass";
27
28 plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
29  . ' (note: creates and drops tables named artist and casecheck!)' unless ($dsn && $user);
30
31 plan tests => 43;
32
33 DBICTest::Schema->load_classes( 'Casecheck' );
34 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, { auto_savepoint => 1});
35
36 # Check that datetime_parser returns correctly before we explicitly connect.
37 SKIP: {
38     eval { require DateTime::Format::Pg };
39     skip "DateTime::Format::Pg required", 2 if $@;
40
41     my $store = ref $schema->storage;
42     is($store, 'DBIx::Class::Storage::DBI', 'Started with generic storage');
43
44     my $parser = $schema->storage->datetime_parser;
45     is( $parser, 'DateTime::Format::Pg', 'datetime_parser is as expected');
46 }
47
48 my $dbh = $schema->storage->dbh;
49 my $stats = new DBICTest::Stats();
50 $schema->storage->debugobj($stats);
51 $schema->storage->debug(1);
52
53 $schema->source("Artist")->name("testschema.artist");
54 $schema->source("SequenceTest")->name("testschema.sequence_test");
55 $dbh->do("CREATE SCHEMA testschema;");
56 $dbh->do("CREATE TABLE testschema.artist (artistid serial PRIMARY KEY, name VARCHAR(100), charfield CHAR(10));");
57 $dbh->do("CREATE TABLE testschema.sequence_test (pkid1 integer, pkid2 integer, nonpkid integer, name VARCHAR(100), CONSTRAINT pk PRIMARY KEY(pkid1, pkid2));");
58 $dbh->do("CREATE SEQUENCE pkid1_seq START 1 MAXVALUE 999999 MINVALUE 0");
59 $dbh->do("CREATE SEQUENCE pkid2_seq START 10 MAXVALUE 999999 MINVALUE 0");
60 $dbh->do("CREATE SEQUENCE nonpkid_seq START 20 MAXVALUE 999999 MINVALUE 0");
61 ok ( $dbh->do('CREATE TABLE testschema.casecheck (id serial PRIMARY KEY, "name" VARCHAR(1), "NAME" VARCHAR(2), "UC_NAME" VARCHAR(3));'), 'Creation of casecheck table');
62
63 # This is in Core now, but it's here just to test that it doesn't break
64 $schema->class('Artist')->load_components('PK::Auto');
65
66 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
67
68 is($new->artistid, 1, "Auto-PK worked");
69
70 $new = $schema->resultset('Artist')->create({ name => 'bar' });
71
72 is($new->artistid, 2, "Auto-PK worked");
73
74 my $test_type_info = {
75     'artistid' => {
76         'data_type' => 'integer',
77         'is_nullable' => 0,
78         'size' => 4,
79     },
80     'name' => {
81         'data_type' => 'character varying',
82         'is_nullable' => 1,
83         'size' => 100,
84         'default_value' => undef,
85     },
86     'charfield' => {
87         'data_type' => 'character',
88         'is_nullable' => 1,
89         'size' => 10,
90         'default_value' => undef,
91     },
92 };
93
94
95 my $type_info = $schema->storage->columns_info_for('testschema.artist');
96 my $artistid_defval = delete $type_info->{artistid}->{default_value};
97 like($artistid_defval,
98      qr/^nextval\('([^\.]*\.){0,1}artist_artistid_seq'::(?:text|regclass)\)/,
99      'columns_info_for - sequence matches Pg get_autoinc_seq expectations');
100 is_deeply($type_info, $test_type_info,
101           'columns_info_for - column data types');
102
103 my $name_info = $schema->source('Casecheck')->column_info( 'name' );
104 is( $name_info->{size}, 1, "Case sensitive matching info for 'name'" );
105
106 my $NAME_info = $schema->source('Casecheck')->column_info( 'NAME' );
107 is( $NAME_info->{size}, 2, "Case sensitive matching info for 'NAME'" );
108
109 my $uc_name_info = $schema->source('Casecheck')->column_info( 'uc_name' );
110 is( $uc_name_info->{size}, 3, "Case insensitive matching info for 'uc_name'" );
111
112 # Test SELECT ... FOR UPDATE
113 my $HaveSysSigAction = eval "require Sys::SigAction" && !$@;
114 if ($HaveSysSigAction) {
115     Sys::SigAction->import( 'set_sig_handler' );
116 }
117
118 SKIP: {
119     skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
120     # create a new schema
121     my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
122     $schema2->source("Artist")->name("testschema.artist");
123
124     $schema->txn_do( sub {
125         my $artist = $schema->resultset('Artist')->search(
126             {
127                 artistid => 1
128             },
129             {
130                 for => 'update'
131             }
132         )->first;
133         is($artist->artistid, 1, "select for update returns artistid = 1");
134
135         my $artist_from_schema2;
136         my $error_ok = 0;
137         eval {
138             my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
139             alarm(2);
140             $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
141             $artist_from_schema2->name('fooey');
142             $artist_from_schema2->update;
143             alarm(0);
144         };
145         if (my $e = $@) {
146             $error_ok = $e =~ /DBICTestTimeout/;
147         }
148
149         # Make sure that an error was raised, and that the update failed
150         ok($error_ok, "update from second schema times out");
151         ok($artist_from_schema2->is_column_changed('name'), "'name' column is still dirty from second schema");
152     });
153 }
154
155 SKIP: {
156     skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
157     # create a new schema
158     my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
159     $schema2->source("Artist")->name("testschema.artist");
160
161     $schema->txn_do( sub {
162         my $artist = $schema->resultset('Artist')->search(
163             {
164                 artistid => 1
165             },
166         )->first;
167         is($artist->artistid, 1, "select for update returns artistid = 1");
168
169         my $artist_from_schema2;
170         my $error_ok = 0;
171         eval {
172             my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
173             alarm(2);
174             $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
175             $artist_from_schema2->name('fooey');
176             $artist_from_schema2->update;
177             alarm(0);
178         };
179         if (my $e = $@) {
180             $error_ok = $e =~ /DBICTestTimeout/;
181         }
182
183         # Make sure that an error was NOT raised, and that the update succeeded
184         ok(! $error_ok, "update from second schema DOES NOT timeout");
185         ok(! $artist_from_schema2->is_column_changed('name'), "'name' column is NOT dirty from second schema");
186     });
187 }
188
189 SKIP: {
190   skip "Oracle Auto-PK tests are broken", 16;
191   # test auto increment using sequences WITHOUT triggers
192   
193   for (1..5) {
194     my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
195     is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
196     is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
197     is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
198   }
199   my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
200   is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
201 }
202
203 $schema->txn_begin();
204
205 my $arty = $schema->resultset('Artist')->find(1);
206
207 my $name = $arty->name();
208
209 $schema->svp_begin('savepoint1');
210
211 cmp_ok($stats->{'SVP_BEGIN'}, '==', 1, 'Statistics svp_begin tickled');
212
213 $arty->update({ name => 'Jheephizzy' });
214
215 $arty->discard_changes();
216
217 cmp_ok($arty->name(), 'eq', 'Jheephizzy', 'Name changed');
218
219 $schema->svp_rollback('savepoint1');
220
221 cmp_ok($stats->{'SVP_ROLLBACK'}, '==', 1, 'Statistics svp_rollback tickled');
222
223 $arty->discard_changes();
224
225 cmp_ok($arty->name(), 'eq', $name, 'Name rolled back');
226
227 $schema->txn_commit();
228
229 $schema->txn_do (sub {
230     $schema->txn_do (sub {
231         $arty->name ('Muff');
232
233         $arty->update;
234       });
235
236     eval {
237       $schema->txn_do (sub {
238           $arty->name ('Moff');
239
240           $arty->update;
241
242           $arty->discard_changes;
243
244           is($arty->name,'Moff','Value updated in nested transaction');
245
246           $schema->storage->dbh->do ("GUARANTEED TO PHAIL");
247         });
248     };
249
250     ok ($@,'Nested transaction failed (good)');
251
252     $arty->discard_changes;
253
254     is($arty->name,'Muff','auto_savepoint rollback worked');
255
256     $arty->name ('Miff');
257
258     $arty->update;
259   });
260
261 $arty->discard_changes;
262
263 is($arty->name,'Miff','auto_savepoint worked');
264
265 cmp_ok($stats->{'SVP_BEGIN'},'==',3,'Correct number of savepoints created');
266
267 cmp_ok($stats->{'SVP_RELEASE'},'==',2,'Correct number of savepoints released');
268
269 cmp_ok($stats->{'SVP_ROLLBACK'},'==',2,'Correct number of savepoint rollbacks');
270
271 END {
272     if($dbh) {
273         $dbh->do("DROP TABLE testschema.artist;");
274         $dbh->do("DROP TABLE testschema.casecheck;");
275         $dbh->do("DROP TABLE testschema.sequence_test;");
276         $dbh->do("DROP SEQUENCE pkid1_seq");
277         $dbh->do("DROP SEQUENCE pkid2_seq");
278         $dbh->do("DROP SEQUENCE nonpkid_seq");
279         $dbh->do("DROP SCHEMA testschema;");
280     }
281 }
282