10 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
12 plan skip_all => <<EOM unless $dsn && $user;
13 Set \$ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test
14 ( NOTE: This test drops and creates tables called 'artist', 'casecheck',
15 'array_test' and 'sequence_test' as well as following sequences:
16 'pkid1_seq', 'pkid2_seq' and 'nonpkid_seq''. as well as following
17 schemas: 'dbic_t_schema', 'dbic_t_schema_2', 'dbic_t_schema_3',
18 'dbic_t_schema_4', and 'dbic_t_schema_5'
22 ### load any test classes that are defined further down in the file via BEGIN blocks
24 our @test_classes; #< array that will be pushed into by test classes defined in this file
25 DBICTest::Schema->load_classes( map {s/.+:://;$_} @test_classes ) if @test_classes;
28 ### pre-connect tests (keep each test separate as to make sure rebless() runs)
30 my $s = DBICTest::Schema->connect($dsn, $user, $pass);
32 ok (!$s->storage->_dbh, 'definitely not connected');
34 # Check that datetime_parser returns correctly before we explicitly connect.
36 eval { require DateTime::Format::Pg };
37 skip "DateTime::Format::Pg required", 2 if $@;
39 my $store = ref $s->storage;
40 is($store, 'DBIx::Class::Storage::DBI', 'Started with generic storage');
42 my $parser = $s->storage->datetime_parser;
43 is( $parser, 'DateTime::Format::Pg', 'datetime_parser is as expected');
46 ok (!$s->storage->_dbh, 'still not connected');
49 my $s = DBICTest::Schema->connect($dsn, $user, $pass);
50 # make sure sqlt_type overrides work (::Storage::DBI::Pg does this)
51 ok (!$s->storage->_dbh, 'definitely not connected');
52 is ($s->storage->sqlt_type, 'PostgreSQL', 'sqlt_type correct pre-connection');
53 ok (!$s->storage->_dbh, 'still not connected');
56 ### connect, create postgres-specific test schema
58 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
60 drop_test_schema($schema);
61 create_test_schema($schema);
66 # run a BIG bunch of tests for last-insert-id / Auto-PK / sequence
68 run_apk_tests($schema); #< older set of auto-pk tests
69 run_extended_apk_tests($schema); #< new extended set of auto-pk tests
77 my $test_type_info = {
79 'data_type' => 'integer',
84 'data_type' => 'character varying',
87 'default_value' => undef,
90 'data_type' => 'integer',
93 'default_value' => 13,
97 'data_type' => 'character',
100 'default_value' => undef,
103 'data_type' => 'integer[]',
106 'default_value' => undef,
110 my $type_info = $schema->storage->columns_info_for('dbic_t_schema.artist');
111 my $artistid_defval = delete $type_info->{artistid}->{default_value};
112 like($artistid_defval,
113 qr/^nextval\('([^\.]*\.){0,1}artist_artistid_seq'::(?:text|regclass)\)/,
114 'columns_info_for - sequence matches Pg get_autoinc_seq expectations');
115 is_deeply($type_info, $test_type_info,
116 'columns_info_for - column data types');
124 package DBICTest::Schema::ArrayTest;
125 push @main::test_classes, __PACKAGE__;
129 use base 'DBIx::Class';
131 __PACKAGE__->load_components(qw/Core/);
132 __PACKAGE__->table('dbic_t_schema.array_test');
133 __PACKAGE__->add_columns(qw/id arrayfield/);
134 __PACKAGE__->column_info_from_storage(1);
135 __PACKAGE__->set_primary_key('id');
139 skip "Need DBD::Pg 2.9.2 or newer for array tests", 4 if $DBD::Pg::VERSION < 2.009002;
142 $schema->resultset('ArrayTest')->create({
143 arrayfield => [1, 2],
145 } 'inserting arrayref as pg array data';
148 $schema->resultset('ArrayTest')->update({
149 arrayfield => [3, 4],
151 } 'updating arrayref as pg array data';
153 $schema->resultset('ArrayTest')->create({
154 arrayfield => [5, 6],
159 $count = $schema->resultset('ArrayTest')->search({
160 arrayfield => \[ '= ?' => [arrayfield => [3, 4]] ], #Todo anything less ugly than this?
162 } 'comparing arrayref to pg array data does not blow up';
163 is($count, 1, 'comparing arrayref to pg array data gives correct result');
168 ########## Case check
171 package DBICTest::Schema::Casecheck;
172 push @main::test_classes, __PACKAGE__;
176 use base 'DBIx::Class';
178 __PACKAGE__->load_components(qw/Core/);
179 __PACKAGE__->table('dbic_t_schema.casecheck');
180 __PACKAGE__->add_columns(qw/id name NAME uc_name/);
181 __PACKAGE__->column_info_from_storage(1);
182 __PACKAGE__->set_primary_key('id');
185 my $name_info = $schema->source('Casecheck')->column_info( 'name' );
186 is( $name_info->{size}, 1, "Case sensitive matching info for 'name'" );
188 my $NAME_info = $schema->source('Casecheck')->column_info( 'NAME' );
189 is( $NAME_info->{size}, 2, "Case sensitive matching info for 'NAME'" );
191 my $uc_name_info = $schema->source('Casecheck')->column_info( 'uc_name' );
192 is( $uc_name_info->{size}, 3, "Case insensitive matching info for 'uc_name'" );
197 ## Test SELECT ... FOR UPDATE
200 if(eval "require Sys::SigAction" && !$@) {
201 Sys::SigAction->import( 'set_sig_handler' );
204 skip "Sys::SigAction is not available", 6;
207 my ($timed_out, $artist2);
211 # Make sure that an error was raised, and that the update failed
214 ok($timed_out, "update from second schema times out");
215 ok($artist2->is_column_changed('name'), "'name' column is still dirty from second schema");
219 # Make sure that an error was NOT raised, and that the update succeeded
222 ok(! $timed_out, "update from second schema DOES NOT timeout");
223 ok(! $artist2->is_column_changed('name'), "'name' column is NOT dirty from second schema");
227 # create a new schema
228 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
229 $schema2->source("Artist")->name("dbic_t_schema.artist");
231 $schema->txn_do( sub {
232 my $artist = $schema->resultset('Artist')->search(
236 $t->{update_lock} ? { for => 'update' } : {}
238 is($artist->artistid, 1, "select returns artistid = 1");
242 my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
244 $artist2 = $schema2->resultset('Artist')->find(1);
245 $artist2->name('fooey');
249 $timed_out = $@ =~ /DBICTestTimeout/;
257 ######## other older Auto-pk tests
259 $schema->source("SequenceTest")->name("dbic_t_schema.sequence_test");
261 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
262 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
263 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
264 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
266 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
267 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
274 drop_test_schema($schema);
275 eapk_drop_all( $schema)
279 ######### SUBROUTINES
281 sub create_test_schema {
283 $schema->storage->dbh_do(sub {
284 my (undef,$dbh) = @_;
286 local $dbh->{Warn} = 0;
288 my $std_artist_table = <<EOS;
290 artistid serial PRIMARY KEY
292 , rank INTEGER NOT NULL DEFAULT '13'
294 , arrayfield INTEGER[]
298 $dbh->do("CREATE SCHEMA dbic_t_schema");
299 $dbh->do("CREATE TABLE dbic_t_schema.artist $std_artist_table");
301 CREATE TABLE dbic_t_schema.sequence_test (
306 , CONSTRAINT pk PRIMARY KEY(pkid1, pkid2)
309 $dbh->do("CREATE SEQUENCE pkid1_seq START 1 MAXVALUE 999999 MINVALUE 0");
310 $dbh->do("CREATE SEQUENCE pkid2_seq START 10 MAXVALUE 999999 MINVALUE 0");
311 $dbh->do("CREATE SEQUENCE nonpkid_seq START 20 MAXVALUE 999999 MINVALUE 0");
313 CREATE TABLE dbic_t_schema.casecheck (
314 id serial PRIMARY KEY
317 , "UC_NAME" VARCHAR(3)
321 CREATE TABLE dbic_t_schema.array_test (
322 id serial PRIMARY KEY
323 , arrayfield INTEGER[]
326 $dbh->do("CREATE SCHEMA dbic_t_schema_2");
327 $dbh->do("CREATE TABLE dbic_t_schema_2.artist $std_artist_table");
328 $dbh->do("CREATE SCHEMA dbic_t_schema_3");
329 $dbh->do("CREATE TABLE dbic_t_schema_3.artist $std_artist_table");
330 $dbh->do('set search_path=dbic_t_schema,public');
331 $dbh->do("CREATE SCHEMA dbic_t_schema_4");
332 $dbh->do("CREATE SCHEMA dbic_t_schema_5");
334 CREATE TABLE dbic_t_schema_4.artist
336 artistid integer not null default nextval('artist_artistid_seq'::regclass) PRIMARY KEY
338 , rank INTEGER NOT NULL DEFAULT '13'
340 , arrayfield INTEGER[]
343 $dbh->do('set search_path=public,dbic_t_schema,dbic_t_schema_3');
344 $dbh->do('create sequence public.artist_artistid_seq'); #< in the public schema
346 CREATE TABLE dbic_t_schema_5.artist
348 artistid integer not null default nextval('public.artist_artistid_seq'::regclass) PRIMARY KEY
350 , rank INTEGER NOT NULL DEFAULT '13'
352 , arrayfield INTEGER[]
355 $dbh->do('set search_path=dbic_t_schema,public');
361 sub drop_test_schema {
362 my ( $schema, $warn_exceptions ) = @_;
364 $schema->storage->dbh_do(sub {
365 my (undef,$dbh) = @_;
367 local $dbh->{Warn} = 0;
370 'DROP SCHEMA dbic_t_schema_5 CASCADE',
371 'DROP SEQUENCE public.artist_artistid_seq',
372 'DROP SCHEMA dbic_t_schema_4 CASCADE',
373 'DROP SCHEMA dbic_t_schema CASCADE',
374 'DROP SEQUENCE pkid1_seq',
375 'DROP SEQUENCE pkid2_seq',
376 'DROP SEQUENCE nonpkid_seq',
377 'DROP SCHEMA dbic_t_schema_2 CASCADE',
378 'DROP SCHEMA dbic_t_schema_3 CASCADE',
380 eval { $dbh->do ($stat) };
381 diag $@ if $@ && $warn_exceptions;
387 ### auto-pk / last_insert_id / sequence discovery
391 # This is in Core now, but it's here just to test that it doesn't break
392 $schema->class('Artist')->load_components('PK::Auto');
393 cmp_ok( $schema->resultset('Artist')->count, '==', 0, 'this should start with an empty artist table');
395 # test that auto-pk also works with the defined search path by
396 # un-schema-qualifying the table name
397 apk_t_set($schema,'artist');
401 $unq_new = $schema->resultset('Artist')->create({ name => 'baz' });
402 } 'insert into unqualified, shadowed table succeeds';
404 is($unq_new && $unq_new->artistid, 1, "and got correct artistid");
406 my @test_schemas = ( [qw| dbic_t_schema_2 1 |],
407 [qw| dbic_t_schema_3 1 |],
408 [qw| dbic_t_schema_4 2 |],
409 [qw| dbic_t_schema_5 1 |],
411 foreach my $t ( @test_schemas ) {
412 my ($sch_name, $start_num) = @$t;
413 #test with dbic_t_schema_2
414 apk_t_set($schema,"$sch_name.artist");
417 $another_new = $schema->resultset('Artist')->create({ name => 'Tollbooth Willy'});
418 is( $another_new->artistid,$start_num, "got correct artistid for $sch_name")
419 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
420 } "$sch_name liid 1 did not die"
421 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
423 $another_new = $schema->resultset('Artist')->create({ name => 'Adam Sandler'});
424 is( $another_new->artistid,$start_num+1, "got correct artistid for $sch_name")
425 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
426 } "$sch_name liid 2 did not die"
427 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
432 apk_t_set($schema,'dbic_t_schema.artist');
433 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
434 is($new->artistid, 4, "Auto-PK worked");
435 $new = $schema->resultset('Artist')->create({ name => 'bar' });
436 is($new->artistid, 5, "Auto-PK worked");
437 } 'old auto-pk tests did not die either';
440 # sets the artist table name and clears sequence name cache
443 $s->source("Artist")->name($n);
444 $s->source('Artist')->column_info('artistid')->{sequence} = undef; #< clear sequence name cache
448 ######## EXTENDED AUTO-PK TESTS
452 package DBICTest::Schema::ExtAPK;
453 push @main::test_classes, __PACKAGE__;
457 use base 'DBIx::Class';
459 __PACKAGE__->load_components(qw/Core/);
460 __PACKAGE__->table('apk');
462 @eapk_id_columns = qw( id1 id2 id3 id4 );
463 __PACKAGE__->add_columns(
464 map { $_ => { data_type => 'integer', is_auto_increment => 1 } }
468 __PACKAGE__->set_primary_key('id2'); #< note the SECOND column is
473 BEGIN{ @eapk_schemas = map "dbic_apk_$_", 0..5 }
475 sub run_extended_apk_tests {
478 #save the search path and reset it at the end
479 my $search_path_save = eapk_get_search_path($schema);
481 eapk_drop_all($schema);
483 # make the test schemas and sequences
484 $schema->storage->dbh_do(sub {
485 my ( undef, $dbh ) = @_;
487 $dbh->do("CREATE SCHEMA $_")
490 $dbh->do("CREATE SEQUENCE $eapk_schemas[5].fooseq");
491 $dbh->do("CREATE SEQUENCE $eapk_schemas[4].fooseq");
492 $dbh->do("CREATE SEQUENCE $eapk_schemas[3].fooseq");
494 $dbh->do("SET search_path = ".join ',', @eapk_schemas );
497 # clear our search_path cache
498 $schema->storage->{_pg_search_path} = undef;
500 eapk_create( $schema,
501 with_search_path => [0,1],
503 eapk_create( $schema,
504 with_search_path => [1,0,'public'],
505 nextval => "$eapk_schemas[5].fooseq",
507 eapk_create( $schema,
508 with_search_path => ['public',0,1],
511 eapk_create( $schema,
512 with_search_path => [3,1,0,'public'],
513 nextval => "$eapk_schemas[4].fooseq",
515 eapk_create( $schema,
516 with_search_path => [3,1,0,'public'],
517 nextval => "$eapk_schemas[3].fooseq",
521 eapk_poke( $schema, 0 );
522 eapk_poke( $schema, 2 );
523 eapk_poke( $schema, 4 );
524 eapk_poke( $schema, 1 );
525 eapk_poke( $schema, 0 );
526 eapk_poke( $schema, 1 );
527 eapk_poke( $schema, 4 );
528 eapk_poke( $schema, 3 );
529 eapk_poke( $schema, 1 );
530 eapk_poke( $schema, 2 );
531 eapk_poke( $schema, 0 );
533 # set our search path back
534 eapk_set_search_path( $schema, @$search_path_save );
537 # do a DBIC create on the apk table in the given schema number (which is an
538 # index of @eapk_schemas)
540 my %seqs; #< sanity-check hash of schema.table.col => currval of its sequence
543 my ($s, $schema_num) = @_;
545 my $schema_name = defined $schema_num
546 ? $eapk_schemas[$schema_num]
549 my $schema_name_actual = $schema_name || eapk_get_search_path($s)->[0];
551 $s->source('ExtAPK')->name($schema_name ? $schema_name.'.apk' : 'apk');
552 #< clear sequence name cache
553 $s->source('ExtAPK')->column_info($_)->{sequence} = undef
554 for @eapk_id_columns;
556 no warnings 'uninitialized';
559 for my $inc (1,2,3) {
560 $new = $schema->resultset('ExtAPK')->create({});
561 my $proper_seqval = ++$seqs{"$schema_name_actual.apk.id2"};
562 is( $new->id2, $proper_seqval, "$schema_name_actual.apk.id2 correct inc $inc" )
563 or eapk_seq_diag($s,$schema_name);
564 $new->discard_changes;
565 for my $id (grep $_ ne 'id2', @eapk_id_columns) {
566 my $proper_seqval = ++$seqs{"$schema_name_actual.apk.$id"};
567 is( $new->$id, $proper_seqval, "$schema_name_actual.apk.$id correct inc $inc" )
568 or eapk_seq_diag($s,$schema_name);
571 } "create in schema '$schema_name' lives"
572 or eapk_seq_diag($s,$schema_name);
575 # print diagnostic info on which sequences were found in the ExtAPK
579 my $schema = shift || eapk_get_search_path($s)->[0];
581 diag "$schema.apk sequences: ",
583 map "$_:".($s->source('ExtAPK')->column_info($_)->{sequence} || '<none>'),
588 # get the postgres search path as an arrayref
589 sub eapk_get_search_path {
591 # cache the search path as ['schema','schema',...] in the storage
594 return $s->storage->dbh_do(sub {
595 my (undef, $dbh) = @_;
597 my ($sp_string) = $dbh->selectrow_array('SHOW search_path');
598 while ( $sp_string =~ s/("[^"]+"|[^,]+),?// ) {
599 unless( defined $1 and length $1 ) {
600 die "search path sanity check failed: '$1'";
602 push @search_path, $1;
607 sub eapk_set_search_path {
609 my $sp = join ',',@sp;
610 $s->storage->dbh_do( sub { $_[1]->do("SET search_path = $sp") } );
613 # create the apk table in the given schema, can set whether the table name is qualified, what the nextval is for the second ID
615 my ($schema, %a) = @_;
617 $schema->storage->dbh_do(sub {
618 my (undef,$dbh) = @_;
621 if ( $a{with_search_path} ) {
622 ($searchpath_save) = $dbh->selectrow_array('SHOW search_path');
624 my $search_path = join ',',map {/\D/ ? $_ : $eapk_schemas[$_]} @{$a{with_search_path}};
626 $dbh->do("SET search_path = $search_path");
629 my $table_name = $a{qualify_table}
630 ? ($eapk_schemas[$a{qualify_table}] || die). ".apk"
632 local $_[1]->{Warn} = 0;
634 my $id_def = $a{nextval}
635 ? "integer primary key not null default nextval('$a{nextval}'::regclass)"
636 : 'serial primary key';
638 CREATE TABLE $table_name (
646 if( $searchpath_save ) {
647 $dbh->do("SET search_path = $searchpath_save");
653 my ( $schema, $warn_exceptions ) = @_;
655 $schema->storage->dbh_do(sub {
656 my (undef,$dbh) = @_;
658 local $dbh->{Warn} = 0;
660 # drop the test schemas
661 for (@eapk_schemas ) {
662 eval{ $dbh->do("DROP SCHEMA $_ CASCADE") };
663 diag $@ if $@ && $warn_exceptions;