1 use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_rdbms_pg';
11 use DBIx::Class::Optional::Dependencies ();
14 use SQL::Abstract 'is_literal_value';
15 use DBIx::Class::_Util 'is_exception';
17 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
19 ### load any test classes that are defined further down in the file via BEGIN blocks
20 our @test_classes; #< array that will be pushed into by test classes defined in this file
21 DBICTest::Schema->load_classes( map {s/.+:://;$_} @test_classes ) if @test_classes;
23 ### pre-connect tests (keep each test separate as to make sure rebless() runs)
25 my $s = DBICTest::Schema->connect($dsn, $user, $pass);
26 # make sure sqlt_type overrides work (::Storage::DBI::Pg does this)
27 ok (!$s->storage->_dbh, 'definitely not connected');
28 is ($s->storage->sqlt_type, 'PostgreSQL', 'sqlt_type correct pre-connection');
29 ok (!$s->storage->_dbh, 'still not connected');
34 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
35 drop_test_schema($schema);
36 create_test_schema($schema);
38 $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
40 my $it = $schema->resultset('Artist')->search( {},
43 order_by => 'artistid' }
45 is( $it->count, 3, "LIMIT count ok" ); # ask for 3 rows out of 6 artists
46 is( $it->next->name, "Artist 3", "iterator->next ok" );
50 is( $it->next, undef, "next past end of resultset ok" );
52 # Limit with select-lock
54 $schema->txn_do (sub {
56 $schema->resultset('Artist')->find({artistid => 1}, {for => 'update', rows => 1}),
57 'DBICTest::Schema::Artist',
60 } 'Limited FOR UPDATE select works';
63 # check if we indeed do support stuff
64 my $test_server_supports_insert_returning = do {
66 my $si = DBICTest::Schema->connect($dsn, $user, $pass)->storage->_server_info;
67 die "Unparseable Pg server version: $si->{dbms_version}\n"
68 unless $si->{normalized_dbms_version};
70 $si->{normalized_dbms_version} < 8.002 ? 0 : 1;
73 DBICTest::Schema->connect($dsn, $user, $pass)->storage->_use_insert_returning,
74 $test_server_supports_insert_returning,
75 'insert returning capability guessed correctly'
79 for my $use_insert_returning ($test_server_supports_insert_returning
84 no warnings qw/once redefine/;
85 my $old_connection = DBICTest::Schema->can('connection');
86 local *DBICTest::Schema::connection = subname 'DBICTest::Schema::connection' => sub {
87 my $s = shift->$old_connection(@_);
88 $s->storage->_use_insert_returning ($use_insert_returning);
92 ### test capability override
94 my $s = DBICTest::Schema->connect($dsn, $user, $pass);
96 ok (!$s->storage->_dbh, 'definitely not connected');
99 ! ($s->storage->_use_insert_returning xor $use_insert_returning),
100 'insert returning capability set correctly',
102 ok (!$s->storage->_dbh, 'still not connected (capability override works)');
105 ### connect, create postgres-specific test schema
107 $schema = DBICTest::Schema->connect($dsn, $user, $pass);
108 $schema->storage->ensure_connected;
110 drop_test_schema($schema);
111 create_test_schema($schema);
115 # run a BIG bunch of tests for last-insert-id / Auto-PK / sequence
117 run_apk_tests($schema); #< older set of auto-pk tests
118 run_extended_apk_tests($schema); #< new extended set of auto-pk tests
121 ######## test the pg-specific syntax from https://rt.cpan.org/Ticket/Display.html?id=99503
124 $schema->resultset('Artist')->search({ artistid => { -in => \ '(select 4) union (select 5)' } })->count,
126 'Two expected artists found on subselect union within IN',
132 my $test_type_info = {
134 'data_type' => 'integer',
139 'data_type' => 'character varying',
142 'default_value' => undef,
145 'data_type' => 'integer',
148 'default_value' => 13,
152 'data_type' => 'character',
155 'default_value' => undef,
158 'data_type' => 'integer[]',
161 'default_value' => undef,
165 my $type_info = $schema->storage->columns_info_for('dbic_t_schema.artist');
166 my $artistid_defval = delete $type_info->{artistid}->{default_value};
168 # The curor info is too radically different from what is in the column_info
169 # call - just punt it (DBD::SQLite tests the codepath plenty enough)
170 unless (DBIx::Class::_ENV_::STRESSTEST_COLUMN_INFO_UNAWARE_STORAGE) {
173 qr/^nextval\('([^\.]*\.){0,1}artist_artistid_seq'::(?:text|regclass)\)/,
174 'columns_info_for - sequence matches Pg get_autoinc_seq expectations'
177 is_deeply($type_info, $test_type_info,
178 'columns_info_for - column data types');
184 package DBICTest::Schema::ArrayTest;
185 push @main::test_classes, __PACKAGE__;
189 use base 'DBICTest::BaseResult';
191 __PACKAGE__->table('dbic_t_schema.array_test');
192 __PACKAGE__->add_columns(qw/id arrayfield/);
193 __PACKAGE__->column_info_from_storage(1);
194 __PACKAGE__->set_primary_key('id');
198 skip "Need DBD::Pg 2.9.2 or newer for array tests", 4 if $DBD::Pg::VERSION < 2.009002;
200 my $arr_rs = $schema->resultset('ArrayTest');
204 arrayfield => [1, 2],
206 } 'inserting arrayref as pg array data';
210 arrayfield => [3, 4],
212 } 'updating arrayref as pg array data';
215 arrayfield => [5, 6],
219 $schema->populate('ArrayTest', [
223 } 'inserting arrayref using void ctx populate';
225 # Search using arrays
228 $arr_rs->search({ arrayfield => { -value => [3,4] } })->first->arrayfield,
230 'Array value matches'
232 } 'searching by arrayref';
236 $arr_rs->search({ arrayfield => { '=' => { -value => [3,4] }} })->first->arrayfield,
238 'Array value matches explicit equal'
240 } 'searching by arrayref (explicit equal sign)';
244 $arr_rs->search({ arrayfield => { '>' => { -value => [3,1] }} })->first->arrayfield,
246 'Array value matches greater than'
248 } 'searching by arrayref (greater than)';
252 $arr_rs->search({ arrayfield => { '>' => { -value => [3,7] }} })->count,
254 'Greater than search found [5,6]',
256 } 'searching by arrayref (greater than)';
261 $arr_rs->find({ arrayfield => { -value => [3,4] } })->arrayfield,
263 'Array value matches implicit equal'
265 } 'find by arrayref';
269 $arr_rs->find({ arrayfield => { '=' => { -value => [3,4] }} })->arrayfield,
271 'Array value matches explicit equal'
273 } 'find by arrayref (equal)';
275 # test inferred condition for creation
278 \[ '= ?' => [3, 4] ],
280 local $TODO = 'No introspection of complex literal conditions :('
281 if is_literal_value $cond;
284 my $arr_rs_cond = $arr_rs->search({ arrayfield => $cond });
286 my $row = $arr_rs_cond->create({});
287 is_deeply ($row->arrayfield, [3,4], 'Array value taken from $rs condition');
288 $row->discard_changes;
289 is_deeply ($row->arrayfield, [3,4], 'Array value made it to storage');
293 # exercise the creation-logic even more (akin to t/100populate.t)
294 for my $insert_value (
302 $arr_rs->create({ arrayfield => $insert_value }),
303 $arr_rs->populate([ { arrayfield => $insert_value } ]),
304 $arr_rs->populate([ ['arrayfield'], [ $insert_value ] ]),
307 my $loose_obj = $arr_rs->new({ arrayfield => $insert_value });
309 unless (is_literal_value $insert_value) {
310 is_deeply( $_->arrayfield, $arr, 'array value preserved during set_columns' )
311 for ($loose_obj, @objs)
314 push @objs, $loose_obj->insert;
316 $_->discard_changes for @objs;
317 is_deeply( $_->arrayfield, $arr, 'array value correct after discard_changes' )
320 # insert couple more in void ctx
321 $arr_rs->populate([ { arrayfield => $insert_value } ]);
322 $arr_rs->populate([ ['arrayfield'], [ $insert_value ] ]);
324 # should have a total of 6 now, all pristine
325 my @retrieved_objs = $arr_rs->search({
326 arrayfield => ref $insert_value eq 'ARRAY'
327 ? { -value => $insert_value }
328 : { '=' => $insert_value }
330 is scalar @retrieved_objs, 6, 'Correct count of inserted rows';
331 is_deeply( $_->arrayfield, $arr, 'array value correct after storage retrieval' )
332 for (@retrieved_objs);
336 ########## Case check
339 package DBICTest::Schema::Casecheck;
340 push @main::test_classes, __PACKAGE__;
344 use base 'DBIx::Class::Core';
346 __PACKAGE__->table('dbic_t_schema.casecheck');
347 __PACKAGE__->add_columns(qw/id name NAME uc_name/);
348 __PACKAGE__->column_info_from_storage(1);
349 __PACKAGE__->set_primary_key('id');
352 my $name_info = $schema->source('Casecheck')->column_info( 'name' );
353 is( $name_info->{size}, 1, "Case sensitive matching info for 'name'" );
355 my $NAME_info = $schema->source('Casecheck')->column_info( 'NAME' );
356 is( $NAME_info->{size}, 2, "Case sensitive matching info for 'NAME'" );
358 my $uc_name_info = $schema->source('Casecheck')->column_info( 'uc_name' );
359 is( $uc_name_info->{size}, 3, "Case insensitive matching info for 'uc_name'" );
362 ## Test ResultSet->update
363 my $artist = $schema->resultset('Artist')->first;
364 my $cds = $artist->cds_unordered->search({
365 year => { '!=' => 2010 }
366 }, { prefetch => 'liner_notes' });
367 lives_ok { $cds->update({ year => '2010' }) } 'Update on prefetched rs';
369 ## Test SELECT ... FOR UPDATE
371 skip "Your system does not support unsafe signals (d_sigaction) - unable to run deadlock test", 1
372 unless eval { $Config{d_sigaction} and require POSIX };
374 my ($timed_out, $artist2);
378 # Make sure that an error was raised, and that the update failed
381 ok($timed_out, "update from second schema times out");
382 ok($artist2->is_column_changed('name'), "'name' column is still dirty from second schema");
386 # Make sure that an error was NOT raised, and that the update succeeded
389 ok(! $timed_out, "update from second schema DOES NOT timeout");
390 ok(! $artist2->is_column_changed('name'), "'name' column is NOT dirty from second schema");
394 # create a new schema
395 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
396 $schema2->source("Artist")->name("dbic_t_schema.artist");
398 $schema->txn_do( sub {
399 my $rs = $schema->resultset('Artist')->search(
403 $t->{update_lock} ? { for => 'update' } : {}
405 ok ($rs->count, 'Count works');
407 my $artist = $rs->next;
408 is($artist->artistid, 1, "select returns artistid = 1");
413 # can not use %SIG assignment directly - we need sigaction below
414 # localization to a block still works however
417 POSIX::sigaction( POSIX::SIGALRM() => POSIX::SigAction->new(
418 sub { die "DBICTestTimeout" },
421 $artist2 = $schema2->resultset('Artist')->find(1);
422 $artist2->name('fooey');
424 # FIXME - this needs to go away in lieu of a non-retrying runner
425 # ( i.e. after solving RT#47005 )
426 local *DBIx::Class::Storage::DBI::_ping = sub { 1 }, DBIx::Class::_ENV_::OLD_MRO && Class::C3->reinitialize()
427 if DBIx::Class::_Util::modver_gt_or_eq( 'DBD::Pg' => '3.5.0' );
435 if (is_exception($@)) {
436 $timed_out = $@ =~ /DBICTestTimeout/
446 ######## other older Auto-pk tests
448 $schema->source("SequenceTest")->name("dbic_t_schema.sequence_test");
450 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
451 is($st->pkid1, $_, "Auto-PK for sequence without default: First primary key");
452 is($st->pkid2, $_ + 9, "Auto-PK for sequence without default: Second primary key");
453 is($st->nonpkid, $_ + 19, "Auto-PK for sequence without default: Non-primary key");
455 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
456 is($st->pkid1, 55, "Auto-PK for sequence without default: First primary key set manually");
459 ######## test non-serial auto-pk
461 if ($schema->storage->_use_insert_returning) {
462 $schema->source('TimestampPrimaryKey')->name('dbic_t_schema.timestamp_primary_key_test');
463 my $row = $schema->resultset('TimestampPrimaryKey')->create({});
467 ######## test with_deferred_fk_checks
469 $schema->source('CD')->name('dbic_t_schema.cd');
470 $schema->source('Track')->name('dbic_t_schema.track');
472 $schema->storage->with_deferred_fk_checks(sub {
473 $schema->resultset('Track')->create({
474 trackid => 999, cd => 999, position => 1, title => 'deferred FK track'
476 $schema->resultset('CD')->create({
477 artist => 1, cdid => 999, year => '2003', title => 'deferred FK cd'
480 } 'with_deferred_fk_checks code survived';
482 is eval { $schema->resultset('Track')->find(999)->title }, 'deferred FK track',
483 'code in with_deferred_fk_checks worked';
486 $schema->resultset('Track')->create({
487 trackid => 1, cd => 9999, position => 1, title => 'Track1'
489 } qr/violates foreign key constraint/i, 'with_deferred_fk_checks is off outside of TXN';
491 # rerun the same under with_deferred_fk_checks
492 # it is expected to fail, hence the eval
493 # but it also should not warn
496 $schema->storage->with_deferred_fk_checks(sub {
497 $schema->resultset('Track')->create({
498 trackid => 1, cd => 9999, position => 1, title => 'Track1'
503 like $@, qr/violates foreign key constraint/i,
504 "Still expected exception on deferred failure at commit time";
506 } [], 'No warnings on deferred rollback';
512 return unless $schema;
513 drop_test_schema($schema);
514 eapk_drop_all($schema);
519 ######### SUBROUTINES
521 sub create_test_schema {
523 $schema->storage->dbh_do(sub {
524 my (undef,$dbh) = @_;
526 local $dbh->{Warn} = 0;
528 my $std_artist_table = <<EOS;
530 artistid serial PRIMARY KEY
532 , rank INTEGER NOT NULL DEFAULT '13'
534 , arrayfield INTEGER[]
538 $dbh->do("CREATE SCHEMA dbic_t_schema");
539 $dbh->do("CREATE TABLE dbic_t_schema.artist $std_artist_table");
542 CREATE TABLE dbic_t_schema.timestamp_primary_key_test (
543 id timestamp default current_timestamp
547 CREATE TABLE dbic_t_schema.cd (
548 cdid int PRIMARY KEY,
557 CREATE TABLE dbic_t_schema.track (
559 cd int REFERENCES dbic_t_schema.cd(cdid) DEFERRABLE,
562 last_updated_on date,
568 CREATE TABLE dbic_t_schema.sequence_test (
573 , CONSTRAINT pk PRIMARY KEY(pkid1, pkid2)
576 $dbh->do("CREATE SEQUENCE pkid1_seq START 1 MAXVALUE 999999 MINVALUE 0");
577 $dbh->do("CREATE SEQUENCE pkid2_seq START 10 MAXVALUE 999999 MINVALUE 0");
578 $dbh->do("CREATE SEQUENCE nonpkid_seq START 20 MAXVALUE 999999 MINVALUE 0");
580 CREATE TABLE dbic_t_schema.casecheck (
581 id serial PRIMARY KEY
584 , "UC_NAME" VARCHAR(3)
588 CREATE TABLE dbic_t_schema.array_test (
589 id serial PRIMARY KEY
590 , arrayfield INTEGER[]
593 $dbh->do("CREATE SCHEMA dbic_t_schema_2");
594 $dbh->do("CREATE TABLE dbic_t_schema_2.artist $std_artist_table");
595 $dbh->do("CREATE SCHEMA dbic_t_schema_3");
596 $dbh->do("CREATE TABLE dbic_t_schema_3.artist $std_artist_table");
597 $dbh->do('set search_path=dbic_t_schema,public');
598 $dbh->do("CREATE SCHEMA dbic_t_schema_4");
599 $dbh->do("CREATE SCHEMA dbic_t_schema_5");
601 CREATE TABLE dbic_t_schema_4.artist
603 artistid integer not null default nextval('artist_artistid_seq'::regclass) PRIMARY KEY
605 , rank INTEGER NOT NULL DEFAULT '13'
607 , arrayfield INTEGER[]
610 $dbh->do('set search_path=public,dbic_t_schema,dbic_t_schema_3');
611 $dbh->do('create sequence public.artist_artistid_seq'); #< in the public schema
613 CREATE TABLE dbic_t_schema_5.artist
615 artistid integer not null default nextval('public.artist_artistid_seq'::regclass) PRIMARY KEY
617 , rank INTEGER NOT NULL DEFAULT '13'
619 , arrayfield INTEGER[]
622 $dbh->do('set search_path=dbic_t_schema,public');
628 sub drop_test_schema {
629 my ( $schema, $warn_exceptions ) = @_;
631 $schema->storage->dbh_do(sub {
632 my (undef,$dbh) = @_;
634 local $dbh->{Warn} = 0;
637 'DROP SCHEMA dbic_t_schema_5 CASCADE',
638 'DROP SEQUENCE public.artist_artistid_seq CASCADE',
639 'DROP SCHEMA dbic_t_schema_4 CASCADE',
640 'DROP SCHEMA dbic_t_schema CASCADE',
641 'DROP SEQUENCE pkid1_seq CASCADE',
642 'DROP SEQUENCE pkid2_seq CASCADE',
643 'DROP SEQUENCE nonpkid_seq CASCADE',
644 'DROP SCHEMA dbic_t_schema_2 CASCADE',
645 'DROP SCHEMA dbic_t_schema_3 CASCADE',
647 eval { $dbh->do ($stat) };
648 diag $@ if $@ && $warn_exceptions;
654 ### auto-pk / last_insert_id / sequence discovery
658 # This is in Core now, but it's here just to test that it doesn't break
659 $schema->class('Artist')->load_components('PK::Auto');
660 cmp_ok( $schema->resultset('Artist')->count, '==', 0, 'this should start with an empty artist table');
662 # test that auto-pk also works with the defined search path by
663 # un-schema-qualifying the table name
664 apk_t_set($schema,'artist');
668 $unq_new = $schema->resultset('Artist')->create({ name => 'baz' });
669 } 'insert into unqualified, shadowed table succeeds';
671 is($unq_new && $unq_new->artistid, 1, "and got correct artistid");
673 my @test_schemas = ( [qw| dbic_t_schema_2 1 |],
674 [qw| dbic_t_schema_3 1 |],
675 [qw| dbic_t_schema_4 2 |],
676 [qw| dbic_t_schema_5 1 |],
678 foreach my $t ( @test_schemas ) {
679 my ($sch_name, $start_num) = @$t;
680 #test with dbic_t_schema_2
681 apk_t_set($schema,"$sch_name.artist");
684 $another_new = $schema->resultset('Artist')->create({ name => 'Tollbooth Willy'});
685 is( $another_new->artistid,$start_num, "got correct artistid for $sch_name")
686 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
687 } "$sch_name liid 1 did not die"
688 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
690 $another_new = $schema->resultset('Artist')->create({ name => 'Adam Sandler'});
691 is( $another_new->artistid,$start_num+1, "got correct artistid for $sch_name")
692 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
693 } "$sch_name liid 2 did not die"
694 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
699 apk_t_set($schema,'dbic_t_schema.artist');
700 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
701 is($new->artistid, 4, "Auto-PK worked");
702 $new = $schema->resultset('Artist')->create({ name => 'bar' });
703 is($new->artistid, 5, "Auto-PK worked");
704 } 'old auto-pk tests did not die either';
707 # sets the artist table name and clears sequence name cache
710 $s->source("Artist")->name($n);
711 $s->source('Artist')->column_info('artistid')->{sequence} = undef; #< clear sequence name cache
715 ######## EXTENDED AUTO-PK TESTS
719 package DBICTest::Schema::ExtAPK;
720 push @main::test_classes, __PACKAGE__;
724 use base 'DBIx::Class::Core';
726 __PACKAGE__->table('apk');
728 @eapk_id_columns = qw( id1 id2 id3 id4 );
729 __PACKAGE__->add_columns(
730 map { $_ => { data_type => 'integer', is_auto_increment => 1 } }
734 __PACKAGE__->set_primary_key('id2'); #< note the SECOND column is
739 BEGIN{ @eapk_schemas = map "dbic_apk_$_", 0..5 }
740 my %seqs; #< hash of schema.table.col => currval of its (DBIC) primary key sequence
742 sub run_extended_apk_tests {
745 #save the search path and reset it at the end
746 my $search_path_save = eapk_get_search_path($schema);
748 eapk_drop_all($schema);
751 # make the test schemas and sequences
752 $schema->storage->dbh_do(sub {
753 my ( undef, $dbh ) = @_;
755 $dbh->do("CREATE SCHEMA $_")
758 $dbh->do("CREATE SEQUENCE $eapk_schemas[5].fooseq");
759 $dbh->do("SELECT setval('$eapk_schemas[5].fooseq',400)");
760 $seqs{"$eapk_schemas[1].apk.id2"} = 400;
762 $dbh->do("CREATE SEQUENCE $eapk_schemas[4].fooseq");
763 $dbh->do("SELECT setval('$eapk_schemas[4].fooseq',300)");
764 $seqs{"$eapk_schemas[3].apk.id2"} = 300;
766 $dbh->do("CREATE SEQUENCE $eapk_schemas[3].fooseq");
767 $dbh->do("SELECT setval('$eapk_schemas[3].fooseq',200)");
768 $seqs{"$eapk_schemas[4].apk.id2"} = 200;
770 $dbh->do("SET search_path = ".join ',', reverse @eapk_schemas );
773 # clear our search_path cache
774 $schema->storage->{_pg_search_path} = undef;
776 eapk_create( $schema,
777 with_search_path => [0,1],
779 eapk_create( $schema,
780 with_search_path => [1,0,'public'],
781 nextval => "$eapk_schemas[5].fooseq",
783 eapk_create( $schema,
784 with_search_path => ['public',0,1],
787 eapk_create( $schema,
788 with_search_path => [3,1,0,'public'],
789 nextval => "$eapk_schemas[4].fooseq",
791 eapk_create( $schema,
792 with_search_path => [3,1,0,'public'],
793 nextval => "$eapk_schemas[3].fooseq",
797 eapk_poke( $schema );
798 eapk_poke( $schema, 0 );
799 eapk_poke( $schema, 2 );
800 eapk_poke( $schema, 4 );
801 eapk_poke( $schema, 1 );
802 eapk_poke( $schema, 0 );
803 eapk_poke( $schema, 1 );
804 eapk_poke( $schema );
805 eapk_poke( $schema, 4 );
806 eapk_poke( $schema, 3 );
807 eapk_poke( $schema, 1 );
808 eapk_poke( $schema, 2 );
809 eapk_poke( $schema, 0 );
811 # set our search path back
812 eapk_set_search_path( $schema, @$search_path_save );
815 # do a DBIC create on the apk table in the given schema number (which is an
816 # index of @eapk_schemas)
819 my ($s, $schema_num) = @_;
821 my $schema_name = defined $schema_num
822 ? $eapk_schemas[$schema_num]
825 my $schema_name_actual = $schema_name || eapk_find_visible_schema($s);
827 $s->source('ExtAPK')->name($schema_name ? $schema_name.'.apk' : 'apk');
828 #< clear sequence name cache
829 $s->source('ExtAPK')->column_info($_)->{sequence} = undef
830 for @eapk_id_columns;
832 no warnings 'uninitialized';
835 for my $inc (1,2,3) {
836 $new = $schema->resultset('ExtAPK')->create({ id1 => 1});
837 my $proper_seqval = ++$seqs{"$schema_name_actual.apk.id2"};
838 is( $new->id2, $proper_seqval, "$schema_name_actual.apk.id2 correct inc $inc" )
839 or eapk_seq_diag($s,$schema_name);
840 $new->discard_changes;
842 for my $id ('id3','id4') {
843 my $proper_seqval = ++$seqs{"$schema_name_actual.apk.$id"};
844 is( $new->$id, $proper_seqval, "$schema_name_actual.apk.$id correct inc $inc" )
845 or eapk_seq_diag($s,$schema_name);
848 } "create in schema '$schema_name' lives"
849 or eapk_seq_diag($s,$schema_name);
852 # print diagnostic info on which sequences were found in the ExtAPK
856 my $schema = shift || eapk_find_visible_schema($s);
858 diag "$schema.apk sequences: ",
860 map "$_:".($s->source('ExtAPK')->column_info($_)->{sequence} || '<none>'),
865 # get the postgres search path as an arrayref
866 sub eapk_get_search_path {
868 # cache the search path as ['schema','schema',...] in the storage
871 return $s->storage->dbh_do(sub {
872 my (undef, $dbh) = @_;
874 my ($sp_string) = $dbh->selectrow_array('SHOW search_path');
875 while ( $sp_string =~ s/("[^"]+"|[^,]+),?// ) {
876 unless( defined $1 and length $1 ) {
877 die "search path sanity check failed: '$1'";
879 push @search_path, $1;
884 sub eapk_set_search_path {
886 my $sp = join ',',@sp;
887 $s->storage->dbh_do( sub { $_[1]->do("SET search_path = $sp") } );
890 # create the apk table in the given schema, can set whether the table name is qualified, what the nextval is for the second ID
892 my ($schema, %a) = @_;
894 $schema->storage->dbh_do(sub {
895 my (undef,$dbh) = @_;
898 if ( $a{with_search_path} ) {
899 ($searchpath_save) = $dbh->selectrow_array('SHOW search_path');
901 my $search_path = join ',',map {/\D/ ? $_ : $eapk_schemas[$_]} @{$a{with_search_path}};
903 $dbh->do("SET search_path = $search_path");
906 my $table_name = $a{qualify_table}
907 ? ($eapk_schemas[$a{qualify_table}] || die). ".apk"
909 local $_[1]->{Warn} = 0;
911 my $id_def = $a{nextval}
912 ? "integer not null default nextval('$a{nextval}'::regclass)"
915 CREATE TABLE $table_name (
918 , id3 serial primary key
923 if( $searchpath_save ) {
924 $dbh->do("SET search_path = $searchpath_save");
930 my ( $schema, $warn_exceptions ) = @_;
932 $schema->storage->dbh_do(sub {
933 my (undef,$dbh) = @_;
935 local $dbh->{Warn} = 0;
937 # drop the test schemas
938 for (@eapk_schemas ) {
939 eval{ $dbh->do("DROP SCHEMA $_ CASCADE") };
940 diag $@ if $@ && $warn_exceptions;
947 sub eapk_find_visible_schema {
951 $s->storage->dbh_do(sub {
952 $_[1]->selectrow_array(<<EOS);
954 FROM pg_catalog.pg_namespace n
955 JOIN pg_catalog.pg_class c ON c.relnamespace = n.oid
956 WHERE c.relname = 'apk'
957 AND pg_catalog.pg_table_is_visible(c.oid)