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', 'cd',
15 'timestamp_primary_key_test', 'track', 'casecheck', 'array_test' and
16 'sequence_test' as well as following sequences: 'pkid1_seq', 'pkid2_seq' and
17 'nonpkid_seq''. as well as following schemas: 'dbic_t_schema',
18 'dbic_t_schema_2', 'dbic_t_schema_3', 'dbic_t_schema_4', and 'dbic_t_schema_5')
21 ### load any test classes that are defined further down in the file via BEGIN blocks
23 our @test_classes; #< array that will be pushed into by test classes defined in this file
24 DBICTest::Schema->load_classes( map {s/.+:://;$_} @test_classes ) if @test_classes;
28 for my $use_insert_returning (0..1) {
29 no warnings qw/redefine once/;
30 require DBIx::Class::Storage::DBI::Pg;
31 local *DBIx::Class::Storage::DBI::Pg::can_insert_returning = sub {
35 ### pre-connect tests (keep each test separate as to make sure rebless() runs)
37 my $s = DBICTest::Schema->connect($dsn, $user, $pass);
39 ok (!$s->storage->_dbh, 'definitely not connected');
41 # Check that datetime_parser returns correctly before we explicitly connect.
43 eval { require DateTime::Format::Pg };
44 skip "DateTime::Format::Pg required", 2 if $@;
46 my $store = ref $s->storage;
47 is($store, 'DBIx::Class::Storage::DBI', 'Started with generic storage');
49 my $parser = $s->storage->datetime_parser;
50 is( $parser, 'DateTime::Format::Pg', 'datetime_parser is as expected');
53 ok (!$s->storage->_dbh, 'still not connected');
56 my $s = DBICTest::Schema->connect($dsn, $user, $pass);
57 # make sure sqlt_type overrides work (::Storage::DBI::Pg does this)
58 ok (!$s->storage->_dbh, 'definitely not connected');
59 is ($s->storage->sqlt_type, 'PostgreSQL', 'sqlt_type correct pre-connection');
60 ok (!$s->storage->_dbh, 'still not connected');
63 ### connect, create postgres-specific test schema
65 $schema = DBICTest::Schema->connect($dsn, $user, $pass);
67 drop_test_schema($schema);
68 create_test_schema($schema);
72 # run a BIG bunch of tests for last-insert-id / Auto-PK / sequence
74 run_apk_tests($schema); #< older set of auto-pk tests
75 run_extended_apk_tests($schema); #< new extended set of auto-pk tests
79 my $test_type_info = {
81 'data_type' => 'integer',
86 'data_type' => 'character varying',
89 'default_value' => undef,
92 'data_type' => 'integer',
95 'default_value' => 13,
99 'data_type' => 'character',
102 'default_value' => undef,
105 'data_type' => 'integer[]',
108 'default_value' => undef,
112 my $type_info = $schema->storage->columns_info_for('dbic_t_schema.artist');
113 my $artistid_defval = delete $type_info->{artistid}->{default_value};
114 like($artistid_defval,
115 qr/^nextval\('([^\.]*\.){0,1}artist_artistid_seq'::(?:text|regclass)\)/,
116 'columns_info_for - sequence matches Pg get_autoinc_seq expectations');
117 is_deeply($type_info, $test_type_info,
118 'columns_info_for - column data types');
126 package DBICTest::Schema::ArrayTest;
127 push @main::test_classes, __PACKAGE__;
131 use base 'DBIx::Class::Core';
133 __PACKAGE__->table('dbic_t_schema.array_test');
134 __PACKAGE__->add_columns(qw/id arrayfield/);
135 __PACKAGE__->column_info_from_storage(1);
136 __PACKAGE__->set_primary_key('id');
140 skip "Need DBD::Pg 2.9.2 or newer for array tests", 4 if $DBD::Pg::VERSION < 2.009002;
143 $schema->resultset('ArrayTest')->create({
144 arrayfield => [1, 2],
146 } 'inserting arrayref as pg array data';
149 $schema->resultset('ArrayTest')->update({
150 arrayfield => [3, 4],
152 } 'updating arrayref as pg array data';
154 $schema->resultset('ArrayTest')->create({
155 arrayfield => [5, 6],
160 $count = $schema->resultset('ArrayTest')->search({
161 arrayfield => \[ '= ?' => [arrayfield => [3, 4]] ], #Todo anything less ugly than this?
163 } 'comparing arrayref to pg array data does not blow up';
164 is($count, 1, 'comparing arrayref to pg array data gives correct result');
169 ########## Case check
172 package DBICTest::Schema::Casecheck;
173 push @main::test_classes, __PACKAGE__;
177 use base 'DBIx::Class::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, $_, "Auto-PK for sequence without default: First primary key");
263 is($st->pkid2, $_ + 9, "Auto-PK for sequence without default: Second primary key");
264 is($st->nonpkid, $_ + 19, "Auto-PK for sequence without default: Non-primary key");
266 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
267 is($st->pkid1, 55, "Auto-PK for sequence without default: First primary key set manually");
270 ######## test non-serial auto-pk
272 if ($schema->storage->can_insert_returning) {
273 $schema->source('TimestampPrimaryKey')->name('dbic_t_schema.timestamp_primary_key_test');
274 my $row = $schema->resultset('TimestampPrimaryKey')->create({});
278 ######## test with_deferred_fk_checks
280 $schema->source('CD')->name('dbic_t_schema.cd');
281 $schema->source('Track')->name('dbic_t_schema.track');
283 $schema->storage->with_deferred_fk_checks(sub {
284 $schema->resultset('Track')->create({
285 trackid => 999, cd => 999, position => 1, title => 'deferred FK track'
287 $schema->resultset('CD')->create({
288 artist => 1, cdid => 999, year => '2003', title => 'deferred FK cd'
291 } 'with_deferred_fk_checks code survived';
293 is eval { $schema->resultset('Track')->find(999)->title }, 'deferred FK track',
294 'code in with_deferred_fk_checks worked';
297 $schema->resultset('Track')->create({
298 trackid => 1, cd => 9999, position => 1, title => 'Track1'
300 } qr/constraint/i, 'with_deferred_fk_checks is off';
306 return unless $schema;
307 drop_test_schema($schema);
308 eapk_drop_all( $schema)
312 ######### SUBROUTINES
314 sub create_test_schema {
316 $schema->storage->dbh_do(sub {
317 my (undef,$dbh) = @_;
319 local $dbh->{Warn} = 0;
321 my $std_artist_table = <<EOS;
323 artistid serial PRIMARY KEY
325 , rank INTEGER NOT NULL DEFAULT '13'
327 , arrayfield INTEGER[]
331 $dbh->do("CREATE SCHEMA dbic_t_schema");
332 $dbh->do("CREATE TABLE dbic_t_schema.artist $std_artist_table");
335 CREATE TABLE dbic_t_schema.timestamp_primary_key_test (
336 id timestamp default current_timestamp
340 CREATE TABLE dbic_t_schema.cd (
341 cdid int PRIMARY KEY,
350 CREATE TABLE dbic_t_schema.track (
352 cd int REFERENCES dbic_t_schema.cd(cdid) DEFERRABLE,
355 last_updated_on date,
356 last_updated_at date,
362 CREATE TABLE dbic_t_schema.sequence_test (
367 , CONSTRAINT pk PRIMARY KEY(pkid1, pkid2)
370 $dbh->do("CREATE SEQUENCE pkid1_seq START 1 MAXVALUE 999999 MINVALUE 0");
371 $dbh->do("CREATE SEQUENCE pkid2_seq START 10 MAXVALUE 999999 MINVALUE 0");
372 $dbh->do("CREATE SEQUENCE nonpkid_seq START 20 MAXVALUE 999999 MINVALUE 0");
374 CREATE TABLE dbic_t_schema.casecheck (
375 id serial PRIMARY KEY
378 , "UC_NAME" VARCHAR(3)
382 CREATE TABLE dbic_t_schema.array_test (
383 id serial PRIMARY KEY
384 , arrayfield INTEGER[]
387 $dbh->do("CREATE SCHEMA dbic_t_schema_2");
388 $dbh->do("CREATE TABLE dbic_t_schema_2.artist $std_artist_table");
389 $dbh->do("CREATE SCHEMA dbic_t_schema_3");
390 $dbh->do("CREATE TABLE dbic_t_schema_3.artist $std_artist_table");
391 $dbh->do('set search_path=dbic_t_schema,public');
392 $dbh->do("CREATE SCHEMA dbic_t_schema_4");
393 $dbh->do("CREATE SCHEMA dbic_t_schema_5");
395 CREATE TABLE dbic_t_schema_4.artist
397 artistid integer not null default nextval('artist_artistid_seq'::regclass) PRIMARY KEY
399 , rank INTEGER NOT NULL DEFAULT '13'
401 , arrayfield INTEGER[]
404 $dbh->do('set search_path=public,dbic_t_schema,dbic_t_schema_3');
405 $dbh->do('create sequence public.artist_artistid_seq'); #< in the public schema
407 CREATE TABLE dbic_t_schema_5.artist
409 artistid integer not null default nextval('public.artist_artistid_seq'::regclass) PRIMARY KEY
411 , rank INTEGER NOT NULL DEFAULT '13'
413 , arrayfield INTEGER[]
416 $dbh->do('set search_path=dbic_t_schema,public');
422 sub drop_test_schema {
423 my ( $schema, $warn_exceptions ) = @_;
425 $schema->storage->dbh_do(sub {
426 my (undef,$dbh) = @_;
428 local $dbh->{Warn} = 0;
431 'DROP SCHEMA dbic_t_schema_5 CASCADE',
432 'DROP SEQUENCE public.artist_artistid_seq',
433 'DROP SCHEMA dbic_t_schema_4 CASCADE',
434 'DROP SCHEMA dbic_t_schema CASCADE',
435 'DROP SEQUENCE pkid1_seq',
436 'DROP SEQUENCE pkid2_seq',
437 'DROP SEQUENCE nonpkid_seq',
438 'DROP SCHEMA dbic_t_schema_2 CASCADE',
439 'DROP SCHEMA dbic_t_schema_3 CASCADE',
441 eval { $dbh->do ($stat) };
442 diag $@ if $@ && $warn_exceptions;
448 ### auto-pk / last_insert_id / sequence discovery
452 # This is in Core now, but it's here just to test that it doesn't break
453 $schema->class('Artist')->load_components('PK::Auto');
454 cmp_ok( $schema->resultset('Artist')->count, '==', 0, 'this should start with an empty artist table');
456 # test that auto-pk also works with the defined search path by
457 # un-schema-qualifying the table name
458 apk_t_set($schema,'artist');
462 $unq_new = $schema->resultset('Artist')->create({ name => 'baz' });
463 } 'insert into unqualified, shadowed table succeeds';
465 is($unq_new && $unq_new->artistid, 1, "and got correct artistid");
467 my @test_schemas = ( [qw| dbic_t_schema_2 1 |],
468 [qw| dbic_t_schema_3 1 |],
469 [qw| dbic_t_schema_4 2 |],
470 [qw| dbic_t_schema_5 1 |],
472 foreach my $t ( @test_schemas ) {
473 my ($sch_name, $start_num) = @$t;
474 #test with dbic_t_schema_2
475 apk_t_set($schema,"$sch_name.artist");
478 $another_new = $schema->resultset('Artist')->create({ name => 'Tollbooth Willy'});
479 is( $another_new->artistid,$start_num, "got correct artistid for $sch_name")
480 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
481 } "$sch_name liid 1 did not die"
482 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
484 $another_new = $schema->resultset('Artist')->create({ name => 'Adam Sandler'});
485 is( $another_new->artistid,$start_num+1, "got correct artistid for $sch_name")
486 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
487 } "$sch_name liid 2 did not die"
488 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
493 apk_t_set($schema,'dbic_t_schema.artist');
494 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
495 is($new->artistid, 4, "Auto-PK worked");
496 $new = $schema->resultset('Artist')->create({ name => 'bar' });
497 is($new->artistid, 5, "Auto-PK worked");
498 } 'old auto-pk tests did not die either';
501 # sets the artist table name and clears sequence name cache
504 $s->source("Artist")->name($n);
505 $s->source('Artist')->column_info('artistid')->{sequence} = undef; #< clear sequence name cache
509 ######## EXTENDED AUTO-PK TESTS
513 package DBICTest::Schema::ExtAPK;
514 push @main::test_classes, __PACKAGE__;
518 use base 'DBIx::Class::Core';
520 __PACKAGE__->table('apk');
522 @eapk_id_columns = qw( id1 id2 id3 id4 );
523 __PACKAGE__->add_columns(
524 map { $_ => { data_type => 'integer', is_auto_increment => 1 } }
528 __PACKAGE__->set_primary_key('id2'); #< note the SECOND column is
533 BEGIN{ @eapk_schemas = map "dbic_apk_$_", 0..5 }
534 my %seqs; #< hash of schema.table.col => currval of its (DBIC) primary key sequence
536 sub run_extended_apk_tests {
539 #save the search path and reset it at the end
540 my $search_path_save = eapk_get_search_path($schema);
542 eapk_drop_all($schema);
545 # make the test schemas and sequences
546 $schema->storage->dbh_do(sub {
547 my ( undef, $dbh ) = @_;
549 $dbh->do("CREATE SCHEMA $_")
552 $dbh->do("CREATE SEQUENCE $eapk_schemas[5].fooseq");
553 $dbh->do("SELECT setval('$eapk_schemas[5].fooseq',400)");
554 $seqs{"$eapk_schemas[1].apk.id2"} = 400;
556 $dbh->do("CREATE SEQUENCE $eapk_schemas[4].fooseq");
557 $dbh->do("SELECT setval('$eapk_schemas[4].fooseq',300)");
558 $seqs{"$eapk_schemas[3].apk.id2"} = 300;
560 $dbh->do("CREATE SEQUENCE $eapk_schemas[3].fooseq");
561 $dbh->do("SELECT setval('$eapk_schemas[3].fooseq',200)");
562 $seqs{"$eapk_schemas[4].apk.id2"} = 200;
564 $dbh->do("SET search_path = ".join ',', reverse @eapk_schemas );
567 # clear our search_path cache
568 $schema->storage->{_pg_search_path} = undef;
570 eapk_create( $schema,
571 with_search_path => [0,1],
573 eapk_create( $schema,
574 with_search_path => [1,0,'public'],
575 nextval => "$eapk_schemas[5].fooseq",
577 eapk_create( $schema,
578 with_search_path => ['public',0,1],
581 eapk_create( $schema,
582 with_search_path => [3,1,0,'public'],
583 nextval => "$eapk_schemas[4].fooseq",
585 eapk_create( $schema,
586 with_search_path => [3,1,0,'public'],
587 nextval => "$eapk_schemas[3].fooseq",
591 eapk_poke( $schema );
592 eapk_poke( $schema, 0 );
593 eapk_poke( $schema, 2 );
594 eapk_poke( $schema, 4 );
595 eapk_poke( $schema, 1 );
596 eapk_poke( $schema, 0 );
597 eapk_poke( $schema, 1 );
598 eapk_poke( $schema );
599 eapk_poke( $schema, 4 );
600 eapk_poke( $schema, 3 );
601 eapk_poke( $schema, 1 );
602 eapk_poke( $schema, 2 );
603 eapk_poke( $schema, 0 );
605 # set our search path back
606 eapk_set_search_path( $schema, @$search_path_save );
609 # do a DBIC create on the apk table in the given schema number (which is an
610 # index of @eapk_schemas)
613 my ($s, $schema_num) = @_;
615 my $schema_name = defined $schema_num
616 ? $eapk_schemas[$schema_num]
619 my $schema_name_actual = $schema_name || eapk_find_visible_schema($s);
621 $s->source('ExtAPK')->name($schema_name ? $schema_name.'.apk' : 'apk');
622 #< clear sequence name cache
623 $s->source('ExtAPK')->column_info($_)->{sequence} = undef
624 for @eapk_id_columns;
626 no warnings 'uninitialized';
629 for my $inc (1,2,3) {
630 $new = $schema->resultset('ExtAPK')->create({ id1 => 1});
631 my $proper_seqval = ++$seqs{"$schema_name_actual.apk.id2"};
632 is( $new->id2, $proper_seqval, "$schema_name_actual.apk.id2 correct inc $inc" )
633 or eapk_seq_diag($s,$schema_name);
634 $new->discard_changes;
636 for my $id ('id3','id4') {
637 my $proper_seqval = ++$seqs{"$schema_name_actual.apk.$id"};
638 is( $new->$id, $proper_seqval, "$schema_name_actual.apk.$id correct inc $inc" )
639 or eapk_seq_diag($s,$schema_name);
642 } "create in schema '$schema_name' lives"
643 or eapk_seq_diag($s,$schema_name);
646 # print diagnostic info on which sequences were found in the ExtAPK
650 my $schema = shift || eapk_find_visible_schema($s);
652 diag "$schema.apk sequences: ",
654 map "$_:".($s->source('ExtAPK')->column_info($_)->{sequence} || '<none>'),
659 # get the postgres search path as an arrayref
660 sub eapk_get_search_path {
662 # cache the search path as ['schema','schema',...] in the storage
665 return $s->storage->dbh_do(sub {
666 my (undef, $dbh) = @_;
668 my ($sp_string) = $dbh->selectrow_array('SHOW search_path');
669 while ( $sp_string =~ s/("[^"]+"|[^,]+),?// ) {
670 unless( defined $1 and length $1 ) {
671 die "search path sanity check failed: '$1'";
673 push @search_path, $1;
678 sub eapk_set_search_path {
680 my $sp = join ',',@sp;
681 $s->storage->dbh_do( sub { $_[1]->do("SET search_path = $sp") } );
684 # create the apk table in the given schema, can set whether the table name is qualified, what the nextval is for the second ID
686 my ($schema, %a) = @_;
688 $schema->storage->dbh_do(sub {
689 my (undef,$dbh) = @_;
692 if ( $a{with_search_path} ) {
693 ($searchpath_save) = $dbh->selectrow_array('SHOW search_path');
695 my $search_path = join ',',map {/\D/ ? $_ : $eapk_schemas[$_]} @{$a{with_search_path}};
697 $dbh->do("SET search_path = $search_path");
700 my $table_name = $a{qualify_table}
701 ? ($eapk_schemas[$a{qualify_table}] || die). ".apk"
703 local $_[1]->{Warn} = 0;
705 my $id_def = $a{nextval}
706 ? "integer not null default nextval('$a{nextval}'::regclass)"
709 CREATE TABLE $table_name (
712 , id3 serial primary key
717 if( $searchpath_save ) {
718 $dbh->do("SET search_path = $searchpath_save");
724 my ( $schema, $warn_exceptions ) = @_;
726 $schema->storage->dbh_do(sub {
727 my (undef,$dbh) = @_;
729 local $dbh->{Warn} = 0;
731 # drop the test schemas
732 for (@eapk_schemas ) {
733 eval{ $dbh->do("DROP SCHEMA $_ CASCADE") };
734 diag $@ if $@ && $warn_exceptions;
741 sub eapk_find_visible_schema {
745 $s->storage->dbh_do(sub {
746 $_[1]->selectrow_array(<<EOS);
748 FROM pg_catalog.pg_namespace n
749 JOIN pg_catalog.pg_class c ON c.relnamespace = n.oid
750 WHERE c.relname = 'apk'
751 AND pg_catalog.pg_table_is_visible(c.oid)