Make pg sequence autodetect deterministic (or throw exceptions). Test needs adjusting
[dbsrgits/DBIx-Class.git] / t / 72pg.t
CommitLineData
70350518 1use strict;
4617b792 2use warnings;
70350518 3
4use Test::More;
9ba627e3 5use Test::Exception;
70350518 6use lib qw(t/lib);
7use DBICTest;
8
89add887 9{
10 package DBICTest::Schema::Casecheck;
11
12 use strict;
13 use warnings;
14 use base 'DBIx::Class';
15
3ff5b740 16 __PACKAGE__->load_components(qw/Core/);
9ba627e3 17 __PACKAGE__->table('testschema.casecheck');
c3af542a 18 __PACKAGE__->add_columns(qw/id name NAME uc_name storecolumn/);
d9916234 19 __PACKAGE__->column_info_from_storage(1);
89add887 20 __PACKAGE__->set_primary_key('id');
21
ece2b6ec 22 sub store_column {
c3af542a 23 my ($self, $name, $value) = @_;
24 $value = '#'.$value if($name eq "storecolumn");
25 $self->maybe::next::method($name, $value);
ece2b6ec 26 }
89add887 27}
28
9ba627e3 29{
30 package DBICTest::Schema::ArrayTest;
31
32 use strict;
33 use warnings;
34 use base 'DBIx::Class';
35
36 __PACKAGE__->load_components(qw/Core/);
37 __PACKAGE__->table('testschema.array_test');
38 __PACKAGE__->add_columns(qw/id arrayfield/);
39 __PACKAGE__->column_info_from_storage(1);
40 __PACKAGE__->set_primary_key('id');
41
42}
43
0567538f 44my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
45
609c5f1b 46plan skip_all => <<EOM unless $dsn && $user;
47Set \$ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test
48( NOTE: This test drops and creates tables called 'artist', 'casecheck',
49 'array_test' and 'sequence_test' as well as following sequences:
50 'pkid1_seq', 'pkid2_seq' and 'nonpkid_seq''. as well as following
51 schemas: 'testschema', 'anothertestschema', 'yetanothertestschema',
52 'unq_nextval_schema', and 'unq_nextval_schema2'
53)
54EOM
0567538f 55
9ba627e3 56DBICTest::Schema->load_classes( 'Casecheck', 'ArrayTest' );
0567538f 57
1ea3792a 58# make sure sqlt_type overrides work (::Storage::DBI::Pg does this)
eabde904 59{
60 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
61
62 ok (!$schema->storage->_dbh, 'definitely not connected');
63 is ($schema->storage->sqlt_type, 'PostgreSQL', 'sqlt_type correct pre-connection');
64}
65
66my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
114780ee 67# Check that datetime_parser returns correctly before we explicitly connect.
68SKIP: {
69 eval { require DateTime::Format::Pg };
70 skip "DateTime::Format::Pg required", 2 if $@;
71
72 my $store = ref $schema->storage;
73 is($store, 'DBIx::Class::Storage::DBI', 'Started with generic storage');
74
75 my $parser = $schema->storage->datetime_parser;
76 is( $parser, 'DateTime::Format::Pg', 'datetime_parser is as expected');
77}
78
3ff5b740 79my $dbh = $schema->storage->dbh;
80$schema->source("Artist")->name("testschema.artist");
39b8d119 81$schema->source("SequenceTest")->name("testschema.sequence_test");
bb452615 82{
83 local $SIG{__WARN__} = sub {};
7603d2a5 84 _cleanup ($dbh);
85
f2b70f86 86 my $artist_table_def = <<EOS;
87(
88 artistid serial PRIMARY KEY
89 , name VARCHAR(100)
90 , rank INTEGER NOT NULL DEFAULT '13'
91 , charfield CHAR(10)
92 , arrayfield INTEGER[]
93)
94EOS
bb452615 95 $dbh->do("CREATE SCHEMA testschema;");
f2b70f86 96 $dbh->do("CREATE TABLE testschema.artist $artist_table_def;");
bb452615 97 $dbh->do("CREATE TABLE testschema.sequence_test (pkid1 integer, pkid2 integer, nonpkid integer, name VARCHAR(100), CONSTRAINT pk PRIMARY KEY(pkid1, pkid2));");
98 $dbh->do("CREATE SEQUENCE pkid1_seq START 1 MAXVALUE 999999 MINVALUE 0");
99 $dbh->do("CREATE SEQUENCE pkid2_seq START 10 MAXVALUE 999999 MINVALUE 0");
100 $dbh->do("CREATE SEQUENCE nonpkid_seq START 20 MAXVALUE 999999 MINVALUE 0");
c3af542a 101 ok ( $dbh->do('CREATE TABLE testschema.casecheck (id serial PRIMARY KEY, "name" VARCHAR(1), "NAME" VARCHAR(2), "UC_NAME" VARCHAR(3), "storecolumn" VARCHAR(10));'), 'Creation of casecheck table');
9ba627e3 102 ok ( $dbh->do('CREATE TABLE testschema.array_test (id serial PRIMARY KEY, arrayfield INTEGER[]);'), 'Creation of array_test table');
f2b70f86 103 $dbh->do("CREATE SCHEMA anothertestschema;");
104 $dbh->do("CREATE TABLE anothertestschema.artist $artist_table_def;");
105 $dbh->do("CREATE SCHEMA yetanothertestschema;");
106 $dbh->do("CREATE TABLE yetanothertestschema.artist $artist_table_def;");
107 $dbh->do('set search_path=testschema,public');
609c5f1b 108 $dbh->do("CREATE SCHEMA unq_nextval_schema;");
109 $dbh->do("CREATE SCHEMA unq_nextval_schema2;");
110 $dbh->do(<<EOS);
111 CREATE TABLE unq_nextval_schema.artist
112 (
113 artistid integer not null default nextval('artist_artistid_seq'::regclass) PRIMARY KEY
114 , name VARCHAR(100)
115 , rank INTEGER NOT NULL DEFAULT '13'
116 , charfield CHAR(10)
117 , arrayfield INTEGER[]
118 );
119EOS
120 $dbh->do('set search_path=public,testschema,yetanothertestschema');
121 $dbh->do('create sequence public.artist_artistid_seq'); #< in the public schema
122 $dbh->do(<<EOS);
123 CREATE TABLE unq_nextval_schema2.artist
124 (
125 artistid integer not null default nextval('public.artist_artistid_seq'::regclass) PRIMARY KEY
126 , name VARCHAR(100)
127 , rank INTEGER NOT NULL DEFAULT '13'
128 , charfield CHAR(10)
129 , arrayfield INTEGER[]
130 );
131EOS
132 $dbh->do('set search_path=testschema,public');
133
bb452615 134}
0567538f 135
c3af542a 136# store_column is called once for create() for non sequence columns
137
138ok(my $storecolumn = $schema->resultset('Casecheck')->create({'storecolumn' => 'a'}));
139
140is($storecolumn->storecolumn, '#a'); # was '##a'
141
3ff5b740 142# This is in Core now, but it's here just to test that it doesn't break
143$schema->class('Artist')->load_components('PK::Auto');
0567538f 144
46bb5b38 145cmp_ok( $schema->resultset('Artist')->count, '==', 0, 'this should start with an empty artist table');
f2b70f86 146
46bb5b38 147{ # test that auto-pk also works with the defined search path by
148 # un-schema-qualifying the table name
f2b70f86 149 my $artist_name_save = $schema->source("Artist")->name;
150 $schema->source("Artist")->name("artist");
151
152 my $unq_new;
153 lives_ok {
154 $unq_new = $schema->resultset('Artist')->create({ name => 'baz' });
155 } 'insert into unqualified, shadowed table succeeds';
156
157 is($unq_new && $unq_new->artistid, 1, "and got correct artistid");
158
609c5f1b 159 my @test_schemas = ( [qw| anothertestschema 1 |],
160 [qw| yetanothertestschema 1 |],
161 );
162 foreach my $t ( @test_schemas ) {
163 my ($sch_name, $start_num) = @$t;
164 #test with anothertestschema
165 $schema->source('Artist')->name("$sch_name.artist");
166 $schema->source('Artist')->column_info('artistid')->{sequence} = undef; #< clear sequence name cache
167 my $another_new;
168 lives_ok {
169 $another_new = $schema->resultset('Artist')->create({ name => 'Tollbooth Willy'});
170 is( $another_new->artistid,$start_num, "got correct artistid for $sch_name")
171 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
172 } "$sch_name liid 1 did not die"
173 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
174 lives_ok {
175 $another_new = $schema->resultset('Artist')->create({ name => 'Adam Sandler'});
176 is( $another_new->artistid,$start_num+1, "got correct artistid for $sch_name")
177 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
178 } "$sch_name liid 2 did not die"
179 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
180
181 }
4617b792 182
4617b792 183
609c5f1b 184 my @todo_schemas = (
185 [qw| unq_nextval_schema 2 |],
186 [qw| unq_nextval_schema2 1 |],
187 );
3bf6e27c 188
c01a6b75 189 foreach my $t ( @todo_schemas ) {
190 my ($sch_name, $start_num) = @$t;
191
192 #test with anothertestschema
193 $schema->source('Artist')->name("$sch_name.artist");
194 $schema->source('Artist')->column_info('artistid')->{sequence} = undef; #< clear sequence name cache
195 my $another_new;
196 lives_ok {
197 $another_new = $schema->resultset('Artist')->create({ name => 'Tollbooth Willy'});
198 is( $another_new->artistid,$start_num, "got correct artistid for $sch_name")
199 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
200 } "$sch_name liid 1 did not die"
201 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
202
203 lives_ok {
204 $another_new = $schema->resultset('Artist')->create({ name => 'Adam Sandler'});
205 is( $another_new->artistid,$start_num+1, "got correct artistid for $sch_name")
206 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
207 } "$sch_name liid 2 did not die"
208 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
3bf6e27c 209 }
609c5f1b 210
211 $schema->source('Artist')->column_info('artistid')->{sequence} = undef; #< clear sequence name cache
f2b70f86 212 $schema->source("Artist")->name($artist_name_save);
213}
214
609c5f1b 215my $new;
216lives_ok {
217 $new = $schema->resultset('Artist')->create({ name => 'foo' });
218 is($new->artistid, 4, "Auto-PK worked");
219 $new = $schema->resultset('Artist')->create({ name => 'bar' });
220 is($new->artistid, 5, "Auto-PK worked");
221} 'old auto-pk tests did not die either';
f2b70f86 222
b6b65a3e 223
a953d8d9 224my $test_type_info = {
225 'artistid' => {
103e3e03 226 'data_type' => 'integer',
227 'is_nullable' => 0,
fc22fbac 228 'size' => 4,
a953d8d9 229 },
230 'name' => {
103e3e03 231 'data_type' => 'character varying',
232 'is_nullable' => 1,
ae515736 233 'size' => 100,
fc22fbac 234 'default_value' => undef,
103e3e03 235 },
85df19d7 236 'rank' => {
237 'data_type' => 'integer',
238 'is_nullable' => 0,
239 'size' => 4,
240 'default_value' => 13,
241
242 },
103e3e03 243 'charfield' => {
244 'data_type' => 'character',
a953d8d9 245 'is_nullable' => 1,
fc22fbac 246 'size' => 10,
247 'default_value' => undef,
103e3e03 248 },
9ba627e3 249 'arrayfield' => {
250 'data_type' => 'integer[]',
251 'is_nullable' => 1,
252 'size' => undef,
253 'default_value' => undef,
254 },
a953d8d9 255};
256
fc22fbac 257
3ff5b740 258my $type_info = $schema->storage->columns_info_for('testschema.artist');
fc22fbac 259my $artistid_defval = delete $type_info->{artistid}->{default_value};
260like($artistid_defval,
4d272ce5 261 qr/^nextval\('([^\.]*\.){0,1}artist_artistid_seq'::(?:text|regclass)\)/,
fc22fbac 262 'columns_info_for - sequence matches Pg get_autoinc_seq expectations');
263is_deeply($type_info, $test_type_info,
264 'columns_info_for - column data types');
a953d8d9 265
ac45262b 266SKIP: {
267 skip "Need DBD::Pg 2.9.2 or newer for array tests", 4 if $DBD::Pg::VERSION < 2.009002;
268
9ba627e3 269 lives_ok {
270 $schema->resultset('ArrayTest')->create({
271 arrayfield => [1, 2],
272 });
273 } 'inserting arrayref as pg array data';
274
275 lives_ok {
276 $schema->resultset('ArrayTest')->update({
277 arrayfield => [3, 4],
278 });
279 } 'updating arrayref as pg array data';
c224117b 280
281 $schema->resultset('ArrayTest')->create({
282 arrayfield => [5, 6],
283 });
284
285 my $count;
286 lives_ok {
287 $count = $schema->resultset('ArrayTest')->search({
aab0d3b7 288 arrayfield => \[ '= ?' => [arrayfield => [3, 4]] ], #Todo anything less ugly than this?
c224117b 289 })->count;
290 } 'comparing arrayref to pg array data does not blow up';
291 is($count, 1, 'comparing arrayref to pg array data gives correct result');
9ba627e3 292}
293
294
3ff5b740 295my $name_info = $schema->source('Casecheck')->column_info( 'name' );
ae515736 296is( $name_info->{size}, 1, "Case sensitive matching info for 'name'" );
297
3ff5b740 298my $NAME_info = $schema->source('Casecheck')->column_info( 'NAME' );
ae515736 299is( $NAME_info->{size}, 2, "Case sensitive matching info for 'NAME'" );
300
3ff5b740 301my $uc_name_info = $schema->source('Casecheck')->column_info( 'uc_name' );
ae515736 302is( $uc_name_info->{size}, 3, "Case insensitive matching info for 'uc_name'" );
303
95ba7ee4 304# Test SELECT ... FOR UPDATE
305my $HaveSysSigAction = eval "require Sys::SigAction" && !$@;
306if ($HaveSysSigAction) {
307 Sys::SigAction->import( 'set_sig_handler' );
308}
309
310SKIP: {
311 skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
312 # create a new schema
313 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
314 $schema2->source("Artist")->name("testschema.artist");
315
316 $schema->txn_do( sub {
317 my $artist = $schema->resultset('Artist')->search(
318 {
319 artistid => 1
320 },
321 {
322 for => 'update'
323 }
324 )->first;
325 is($artist->artistid, 1, "select for update returns artistid = 1");
326
327 my $artist_from_schema2;
328 my $error_ok = 0;
329 eval {
330 my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
331 alarm(2);
332 $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
333 $artist_from_schema2->name('fooey');
334 $artist_from_schema2->update;
335 alarm(0);
336 };
337 if (my $e = $@) {
338 $error_ok = $e =~ /DBICTestTimeout/;
339 }
340
341 # Make sure that an error was raised, and that the update failed
342 ok($error_ok, "update from second schema times out");
343 ok($artist_from_schema2->is_column_changed('name'), "'name' column is still dirty from second schema");
344 });
345}
346
347SKIP: {
348 skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
349 # create a new schema
350 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
351 $schema2->source("Artist")->name("testschema.artist");
352
353 $schema->txn_do( sub {
354 my $artist = $schema->resultset('Artist')->search(
355 {
356 artistid => 1
357 },
358 )->first;
359 is($artist->artistid, 1, "select for update returns artistid = 1");
360
361 my $artist_from_schema2;
362 my $error_ok = 0;
363 eval {
364 my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
365 alarm(2);
366 $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
367 $artist_from_schema2->name('fooey');
368 $artist_from_schema2->update;
369 alarm(0);
370 };
371 if (my $e = $@) {
372 $error_ok = $e =~ /DBICTestTimeout/;
373 }
374
375 # Make sure that an error was NOT raised, and that the update succeeded
376 ok(! $error_ok, "update from second schema DOES NOT timeout");
377 ok(! $artist_from_schema2->is_column_changed('name'), "'name' column is NOT dirty from second schema");
378 });
379}
380
d093d3eb 381for (1..5) {
39b8d119 382 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
383 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
384 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
385 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
d093d3eb 386}
387my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
388is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
389
609c5f1b 390#_cleanup ($dbh);
391
392done_testing;
393
394
7603d2a5 395sub _cleanup {
396 my $dbh = shift or return;
609c5f1b 397 $dbh->ping or return;
7603d2a5 398
399 for my $stat (
609c5f1b 400 'DROP TABLE unq_nextval_schema2.artist',
401 'DROP SCHEMA unq_nextval_schema2',
402 'DROP SEQUENCE public.artist_artistid_seq',
403 'DROP TABLE unq_nextval_schema.artist',
404 'DROP SCHEMA unq_nextval_schema',
7603d2a5 405 'DROP TABLE testschema.artist',
406 'DROP TABLE testschema.casecheck',
407 'DROP TABLE testschema.sequence_test',
408 'DROP TABLE testschema.array_test',
409 'DROP SEQUENCE pkid1_seq',
410 'DROP SEQUENCE pkid2_seq',
411 'DROP SEQUENCE nonpkid_seq',
412 'DROP SCHEMA testschema',
f2b70f86 413 'DROP TABLE anothertestschema.artist',
414 'DROP SCHEMA anothertestschema',
415 'DROP TABLE yetanothertestschema.artist',
416 'DROP SCHEMA yetanothertestschema',
7603d2a5 417 ) {
418 eval { $dbh->do ($stat) };
609c5f1b 419 diag $@ if $@;
7603d2a5 420 }
3ff5b740 421}
7603d2a5 422
423END { _cleanup($dbh) }