9 use DBIx::Class::Optional::Dependencies ();
12 use SQL::Abstract 'is_literal_value';
13 use DBIx::Class::_Util 'is_exception';
15 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_pg')
16 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_pg');
18 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
20 plan skip_all => <<'EOM' unless $dsn && $user;
21 Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test
22 ( NOTE: This test drops and creates tables called 'artist', 'cd',
23 'timestamp_primary_key_test', 'track', 'casecheck', 'array_test' and
24 'sequence_test' as well as following sequences: 'pkid1_seq', 'pkid2_seq' and
25 'nonpkid_seq'. as well as following schemas: 'dbic_t_schema',
26 'dbic_t_schema_2', 'dbic_t_schema_3', 'dbic_t_schema_4', and 'dbic_t_schema_5')
29 ### load any test classes that are defined further down in the file via BEGIN blocks
31 our @test_classes; #< array that will be pushed into by test classes defined in this file
32 require DBICTest::Schema;
33 DBICTest::Schema->load_classes( map {s/.+:://;$_} @test_classes ) if @test_classes;
35 ### pre-connect tests (keep each test separate as to make sure rebless() runs)
37 my $s = DBICTest->connect_schema($dsn, $user, $pass);
39 ok (!$s->storage->_dbh, 'definitely not connected');
41 # Check that datetime_parser returns correctly before we explicitly connect.
44 "Pg parser detection test needs " . DBIx::Class::Optional::Dependencies->req_missing_for ('test_dt_pg'),
46 ) unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_dt_pg');
48 my $store = ref $s->storage;
49 is($store, 'DBIx::Class::Storage::DBI', 'Started with generic storage');
51 my $parser = $s->storage->datetime_parser;
52 is( $parser, 'DateTime::Format::Pg', 'datetime_parser is as expected');
55 ok (!$s->storage->_dbh, 'still not connected');
59 my $s = DBICTest->connect_schema($dsn, $user, $pass);
60 # make sure sqlt_type overrides work (::Storage::DBI::Pg does this)
61 ok (!$s->storage->_dbh, 'definitely not connected');
62 is ($s->storage->sqlt_type, 'PostgreSQL', 'sqlt_type correct pre-connection');
63 ok (!$s->storage->_dbh, 'still not connected');
68 my $schema = DBICTest->connect_schema($dsn, $user, $pass);
69 drop_test_schema($schema);
70 create_test_schema($schema);
72 $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
74 my $it = $schema->resultset('Artist')->search( {},
77 order_by => 'artistid' }
79 is( $it->count, 3, "LIMIT count ok" ); # ask for 3 rows out of 6 artists
80 is( $it->next->name, "Artist 3", "iterator->next ok" );
84 is( $it->next, undef, "next past end of resultset ok" );
86 # Limit with select-lock
88 $schema->txn_do (sub {
90 $schema->resultset('Artist')->find({artistid => 1}, {for => 'update', rows => 1}),
91 'DBICTest::Schema::Artist',
94 } 'Limited FOR UPDATE select works';
97 # check if we indeed do support stuff
98 my $test_server_supports_insert_returning = do {
100 my $si = DBICTest->connect_schema($dsn, $user, $pass)->storage->_server_info;
101 die "Unparseable Pg server version: $si->{dbms_version}\n"
102 unless $si->{normalized_dbms_version};
104 $si->{normalized_dbms_version} < 8.002 ? 0 : 1;
107 DBICTest->connect_schema($dsn, $user, $pass)->storage->_use_insert_returning,
108 $test_server_supports_insert_returning,
109 'insert returning capability guessed correctly'
113 for my $use_insert_returning ($test_server_supports_insert_returning
118 no warnings qw/once redefine/;
119 my $old_connection = DBICTest::Schema->can('connection');
120 local *DBICTest::Schema::connection = subname 'DBICTest::Schema::connection' => sub {
121 my $s = shift->$old_connection(@_);
122 $s->storage->_use_insert_returning ($use_insert_returning);
126 ### test capability override
128 my $s = DBICTest->connect_schema($dsn, $user, $pass);
130 ok (!$s->storage->_dbh, 'definitely not connected');
133 ! ($s->storage->_use_insert_returning xor $use_insert_returning),
134 'insert returning capability set correctly',
136 ok (!$s->storage->_dbh, 'still not connected (capability override works)');
139 ### connect, create postgres-specific test schema
141 $schema = DBICTest->connect_schema($dsn, $user, $pass);
142 $schema->storage->ensure_connected;
144 drop_test_schema($schema);
145 create_test_schema($schema);
149 # run a BIG bunch of tests for last-insert-id / Auto-PK / sequence
151 run_apk_tests($schema); #< older set of auto-pk tests
152 run_extended_apk_tests($schema); #< new extended set of auto-pk tests
156 my $test_type_info = {
158 'data_type' => 'integer',
163 'data_type' => 'character varying',
166 'default_value' => undef,
169 'data_type' => 'integer',
172 'default_value' => 13,
176 'data_type' => 'character',
179 'default_value' => undef,
182 'data_type' => 'integer[]',
185 'default_value' => undef,
189 my $type_info = $schema->storage->columns_info_for('dbic_t_schema.artist');
190 my $artistid_defval = delete $type_info->{artistid}->{default_value};
191 like($artistid_defval,
192 qr/^nextval\('([^\.]*\.){0,1}artist_artistid_seq'::(?:text|regclass)\)/,
193 'columns_info_for - sequence matches Pg get_autoinc_seq expectations');
194 is_deeply($type_info, $test_type_info,
195 'columns_info_for - column data types');
203 package DBICTest::Schema::ArrayTest;
204 push @main::test_classes, __PACKAGE__;
208 use base 'DBICTest::BaseResult';
210 __PACKAGE__->table('dbic_t_schema.array_test');
211 __PACKAGE__->add_columns(qw/id arrayfield/);
212 __PACKAGE__->column_info_from_storage(1);
213 __PACKAGE__->set_primary_key('id');
217 skip "Need DBD::Pg 2.9.2 or newer for array tests", 4 if $DBD::Pg::VERSION < 2.009002;
219 my $arr_rs = $schema->resultset('ArrayTest');
223 arrayfield => [1, 2],
225 } 'inserting arrayref as pg array data';
229 arrayfield => [3, 4],
231 } 'updating arrayref as pg array data';
234 arrayfield => [5, 6],
238 $schema->populate('ArrayTest', [
242 } 'inserting arrayref using void ctx populate';
244 # Search using arrays
247 $arr_rs->search({ arrayfield => { -value => [3,4] } })->first->arrayfield,
249 'Array value matches'
251 } 'searching by arrayref';
255 $arr_rs->search({ arrayfield => { '=' => { -value => [3,4] }} })->first->arrayfield,
257 'Array value matches explicit equal'
259 } 'searching by arrayref (explicit equal sign)';
263 $arr_rs->search({ arrayfield => { '>' => { -value => [3,1] }} })->first->arrayfield,
265 'Array value matches greater than'
267 } 'searching by arrayref (greater than)';
271 $arr_rs->search({ arrayfield => { '>' => { -value => [3,7] }} })->count,
273 'Greater than search found [5,6]',
275 } 'searching by arrayref (greater than)';
280 $arr_rs->find({ arrayfield => { -value => [3,4] } })->arrayfield,
282 'Array value matches implicit equal'
284 } 'find by arrayref';
288 $arr_rs->find({ arrayfield => { '=' => { -value => [3,4] }} })->arrayfield,
290 'Array value matches explicit equal'
292 } 'find by arrayref (equal)';
294 # test inferred condition for creation
297 \[ '= ?' => [arrayfield => [3, 4]] ],
299 local $TODO = 'No introspection of complex literal conditions :('
300 if is_literal_value $cond;
303 my $arr_rs_cond = $arr_rs->search({ arrayfield => $cond });
305 my $row = $arr_rs_cond->create({});
306 is_deeply ($row->arrayfield, [3,4], 'Array value taken from $rs condition');
307 $row->discard_changes;
308 is_deeply ($row->arrayfield, [3,4], 'Array value made it to storage');
312 ########## Case check
315 package DBICTest::Schema::Casecheck;
316 push @main::test_classes, __PACKAGE__;
320 use base 'DBIx::Class::Core';
322 __PACKAGE__->table('dbic_t_schema.casecheck');
323 __PACKAGE__->add_columns(qw/id name NAME uc_name/);
324 __PACKAGE__->column_info_from_storage(1);
325 __PACKAGE__->set_primary_key('id');
328 my $name_info = $schema->source('Casecheck')->column_info( 'name' );
329 is( $name_info->{size}, 1, "Case sensitive matching info for 'name'" );
331 my $NAME_info = $schema->source('Casecheck')->column_info( 'NAME' );
332 is( $NAME_info->{size}, 2, "Case sensitive matching info for 'NAME'" );
334 my $uc_name_info = $schema->source('Casecheck')->column_info( 'uc_name' );
335 is( $uc_name_info->{size}, 3, "Case insensitive matching info for 'uc_name'" );
338 ## Test ResultSet->update
339 my $artist = $schema->resultset('Artist')->first;
340 my $cds = $artist->cds_unordered->search({
341 year => { '!=' => 2010 }
342 }, { prefetch => 'liner_notes' });
343 lives_ok { $cds->update({ year => '2010' }) } 'Update on prefetched rs';
345 ## Test SELECT ... FOR UPDATE
347 skip "Your system does not support unsafe signals (d_sigaction) - unable to run deadlock test", 1
348 unless eval { $Config{d_sigaction} and require POSIX };
350 my ($timed_out, $artist2);
354 # Make sure that an error was raised, and that the update failed
357 ok($timed_out, "update from second schema times out");
358 ok($artist2->is_column_changed('name'), "'name' column is still dirty from second schema");
362 # Make sure that an error was NOT raised, and that the update succeeded
365 ok(! $timed_out, "update from second schema DOES NOT timeout");
366 ok(! $artist2->is_column_changed('name'), "'name' column is NOT dirty from second schema");
370 # create a new schema
371 my $schema2 = DBICTest->connect_schema($dsn, $user, $pass);
372 $schema2->source("Artist")->name("dbic_t_schema.artist");
374 $schema->txn_do( sub {
375 my $rs = $schema->resultset('Artist')->search(
379 $t->{update_lock} ? { for => 'update' } : {}
381 ok ($rs->count, 'Count works');
383 my $artist = $rs->next;
384 is($artist->artistid, 1, "select returns artistid = 1");
389 # can not use %SIG assignment directly - we need sigaction below
390 # localization to a block still works however
393 POSIX::sigaction( POSIX::SIGALRM() => POSIX::SigAction->new(
394 sub { die "DBICTestTimeout" },
398 $artist2 = $schema2->resultset('Artist')->find(1);
399 $artist2->name('fooey');
405 if (is_exception($@)) {
406 $timed_out = $@ =~ /DBICTestTimeout/
416 ######## other older Auto-pk tests
418 $schema->source("SequenceTest")->name("dbic_t_schema.sequence_test");
420 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
421 is($st->pkid1, $_, "Auto-PK for sequence without default: First primary key");
422 is($st->pkid2, $_ + 9, "Auto-PK for sequence without default: Second primary key");
423 is($st->nonpkid, $_ + 19, "Auto-PK for sequence without default: Non-primary key");
425 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
426 is($st->pkid1, 55, "Auto-PK for sequence without default: First primary key set manually");
429 ######## test non-serial auto-pk
431 if ($schema->storage->_use_insert_returning) {
432 $schema->source('TimestampPrimaryKey')->name('dbic_t_schema.timestamp_primary_key_test');
433 my $row = $schema->resultset('TimestampPrimaryKey')->create({});
437 ######## test with_deferred_fk_checks
439 $schema->source('CD')->name('dbic_t_schema.cd');
440 $schema->source('Track')->name('dbic_t_schema.track');
441 lives_and { warning_is {
442 $schema->storage->with_deferred_fk_checks(sub {
443 $schema->resultset('Track')->create({
444 trackid => 999, cd => 999, position => 1, title => 'deferred FK track'
446 $schema->resultset('CD')->create({
447 artist => 1, cdid => 999, year => '2003', title => 'deferred FK cd'
450 } undef } 'with_deferred_fk_checks code survived';
452 is eval { $schema->resultset('Track')->find(999)->title }, 'deferred FK track',
453 'code in with_deferred_fk_checks worked';
456 $schema->resultset('Track')->create({
457 trackid => 1, cd => 9999, position => 1, title => 'Track1'
459 } qr/constraint/i, 'with_deferred_fk_checks is off';
465 return unless $schema;
466 drop_test_schema($schema);
467 eapk_drop_all($schema);
472 ######### SUBROUTINES
474 sub create_test_schema {
476 $schema->storage->dbh_do(sub {
477 my (undef,$dbh) = @_;
479 local $dbh->{Warn} = 0;
481 my $std_artist_table = <<EOS;
483 artistid serial PRIMARY KEY
485 , rank INTEGER NOT NULL DEFAULT '13'
487 , arrayfield INTEGER[]
491 $dbh->do("CREATE SCHEMA dbic_t_schema");
492 $dbh->do("CREATE TABLE dbic_t_schema.artist $std_artist_table");
495 CREATE TABLE dbic_t_schema.timestamp_primary_key_test (
496 id timestamp default current_timestamp
500 CREATE TABLE dbic_t_schema.cd (
501 cdid int PRIMARY KEY,
510 CREATE TABLE dbic_t_schema.track (
512 cd int REFERENCES dbic_t_schema.cd(cdid) DEFERRABLE,
515 last_updated_on date,
521 CREATE TABLE dbic_t_schema.sequence_test (
526 , CONSTRAINT pk PRIMARY KEY(pkid1, pkid2)
529 $dbh->do("CREATE SEQUENCE pkid1_seq START 1 MAXVALUE 999999 MINVALUE 0");
530 $dbh->do("CREATE SEQUENCE pkid2_seq START 10 MAXVALUE 999999 MINVALUE 0");
531 $dbh->do("CREATE SEQUENCE nonpkid_seq START 20 MAXVALUE 999999 MINVALUE 0");
533 CREATE TABLE dbic_t_schema.casecheck (
534 id serial PRIMARY KEY
537 , "UC_NAME" VARCHAR(3)
541 CREATE TABLE dbic_t_schema.array_test (
542 id serial PRIMARY KEY
543 , arrayfield INTEGER[]
546 $dbh->do("CREATE SCHEMA dbic_t_schema_2");
547 $dbh->do("CREATE TABLE dbic_t_schema_2.artist $std_artist_table");
548 $dbh->do("CREATE SCHEMA dbic_t_schema_3");
549 $dbh->do("CREATE TABLE dbic_t_schema_3.artist $std_artist_table");
550 $dbh->do('set search_path=dbic_t_schema,public');
551 $dbh->do("CREATE SCHEMA dbic_t_schema_4");
552 $dbh->do("CREATE SCHEMA dbic_t_schema_5");
554 CREATE TABLE dbic_t_schema_4.artist
556 artistid integer not null default nextval('artist_artistid_seq'::regclass) PRIMARY KEY
558 , rank INTEGER NOT NULL DEFAULT '13'
560 , arrayfield INTEGER[]
563 $dbh->do('set search_path=public,dbic_t_schema,dbic_t_schema_3');
564 $dbh->do('create sequence public.artist_artistid_seq'); #< in the public schema
566 CREATE TABLE dbic_t_schema_5.artist
568 artistid integer not null default nextval('public.artist_artistid_seq'::regclass) PRIMARY KEY
570 , rank INTEGER NOT NULL DEFAULT '13'
572 , arrayfield INTEGER[]
575 $dbh->do('set search_path=dbic_t_schema,public');
581 sub drop_test_schema {
582 my ( $schema, $warn_exceptions ) = @_;
584 $schema->storage->dbh_do(sub {
585 my (undef,$dbh) = @_;
587 local $dbh->{Warn} = 0;
590 'DROP SCHEMA dbic_t_schema_5 CASCADE',
591 'DROP SEQUENCE public.artist_artistid_seq CASCADE',
592 'DROP SCHEMA dbic_t_schema_4 CASCADE',
593 'DROP SCHEMA dbic_t_schema CASCADE',
594 'DROP SEQUENCE pkid1_seq CASCADE',
595 'DROP SEQUENCE pkid2_seq CASCADE',
596 'DROP SEQUENCE nonpkid_seq CASCADE',
597 'DROP SCHEMA dbic_t_schema_2 CASCADE',
598 'DROP SCHEMA dbic_t_schema_3 CASCADE',
600 eval { $dbh->do ($stat) };
601 diag $@ if $@ && $warn_exceptions;
607 ### auto-pk / last_insert_id / sequence discovery
611 # This is in Core now, but it's here just to test that it doesn't break
612 $schema->class('Artist')->load_components('PK::Auto');
613 cmp_ok( $schema->resultset('Artist')->count, '==', 0, 'this should start with an empty artist table');
615 # test that auto-pk also works with the defined search path by
616 # un-schema-qualifying the table name
617 apk_t_set($schema,'artist');
621 $unq_new = $schema->resultset('Artist')->create({ name => 'baz' });
622 } 'insert into unqualified, shadowed table succeeds';
624 is($unq_new && $unq_new->artistid, 1, "and got correct artistid");
626 my @test_schemas = ( [qw| dbic_t_schema_2 1 |],
627 [qw| dbic_t_schema_3 1 |],
628 [qw| dbic_t_schema_4 2 |],
629 [qw| dbic_t_schema_5 1 |],
631 foreach my $t ( @test_schemas ) {
632 my ($sch_name, $start_num) = @$t;
633 #test with dbic_t_schema_2
634 apk_t_set($schema,"$sch_name.artist");
637 $another_new = $schema->resultset('Artist')->create({ name => 'Tollbooth Willy'});
638 is( $another_new->artistid,$start_num, "got correct artistid for $sch_name")
639 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
640 } "$sch_name liid 1 did not die"
641 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
643 $another_new = $schema->resultset('Artist')->create({ name => 'Adam Sandler'});
644 is( $another_new->artistid,$start_num+1, "got correct artistid for $sch_name")
645 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
646 } "$sch_name liid 2 did not die"
647 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
652 apk_t_set($schema,'dbic_t_schema.artist');
653 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
654 is($new->artistid, 4, "Auto-PK worked");
655 $new = $schema->resultset('Artist')->create({ name => 'bar' });
656 is($new->artistid, 5, "Auto-PK worked");
657 } 'old auto-pk tests did not die either';
660 # sets the artist table name and clears sequence name cache
663 $s->source("Artist")->name($n);
664 $s->source('Artist')->column_info('artistid')->{sequence} = undef; #< clear sequence name cache
668 ######## EXTENDED AUTO-PK TESTS
672 package DBICTest::Schema::ExtAPK;
673 push @main::test_classes, __PACKAGE__;
677 use base 'DBIx::Class::Core';
679 __PACKAGE__->table('apk');
681 @eapk_id_columns = qw( id1 id2 id3 id4 );
682 __PACKAGE__->add_columns(
683 map { $_ => { data_type => 'integer', is_auto_increment => 1 } }
687 __PACKAGE__->set_primary_key('id2'); #< note the SECOND column is
692 BEGIN{ @eapk_schemas = map "dbic_apk_$_", 0..5 }
693 my %seqs; #< hash of schema.table.col => currval of its (DBIC) primary key sequence
695 sub run_extended_apk_tests {
698 #save the search path and reset it at the end
699 my $search_path_save = eapk_get_search_path($schema);
701 eapk_drop_all($schema);
704 # make the test schemas and sequences
705 $schema->storage->dbh_do(sub {
706 my ( undef, $dbh ) = @_;
708 $dbh->do("CREATE SCHEMA $_")
711 $dbh->do("CREATE SEQUENCE $eapk_schemas[5].fooseq");
712 $dbh->do("SELECT setval('$eapk_schemas[5].fooseq',400)");
713 $seqs{"$eapk_schemas[1].apk.id2"} = 400;
715 $dbh->do("CREATE SEQUENCE $eapk_schemas[4].fooseq");
716 $dbh->do("SELECT setval('$eapk_schemas[4].fooseq',300)");
717 $seqs{"$eapk_schemas[3].apk.id2"} = 300;
719 $dbh->do("CREATE SEQUENCE $eapk_schemas[3].fooseq");
720 $dbh->do("SELECT setval('$eapk_schemas[3].fooseq',200)");
721 $seqs{"$eapk_schemas[4].apk.id2"} = 200;
723 $dbh->do("SET search_path = ".join ',', reverse @eapk_schemas );
726 # clear our search_path cache
727 $schema->storage->{_pg_search_path} = undef;
729 eapk_create( $schema,
730 with_search_path => [0,1],
732 eapk_create( $schema,
733 with_search_path => [1,0,'public'],
734 nextval => "$eapk_schemas[5].fooseq",
736 eapk_create( $schema,
737 with_search_path => ['public',0,1],
740 eapk_create( $schema,
741 with_search_path => [3,1,0,'public'],
742 nextval => "$eapk_schemas[4].fooseq",
744 eapk_create( $schema,
745 with_search_path => [3,1,0,'public'],
746 nextval => "$eapk_schemas[3].fooseq",
750 eapk_poke( $schema );
751 eapk_poke( $schema, 0 );
752 eapk_poke( $schema, 2 );
753 eapk_poke( $schema, 4 );
754 eapk_poke( $schema, 1 );
755 eapk_poke( $schema, 0 );
756 eapk_poke( $schema, 1 );
757 eapk_poke( $schema );
758 eapk_poke( $schema, 4 );
759 eapk_poke( $schema, 3 );
760 eapk_poke( $schema, 1 );
761 eapk_poke( $schema, 2 );
762 eapk_poke( $schema, 0 );
764 # set our search path back
765 eapk_set_search_path( $schema, @$search_path_save );
768 # do a DBIC create on the apk table in the given schema number (which is an
769 # index of @eapk_schemas)
772 my ($s, $schema_num) = @_;
774 my $schema_name = defined $schema_num
775 ? $eapk_schemas[$schema_num]
778 my $schema_name_actual = $schema_name || eapk_find_visible_schema($s);
780 $s->source('ExtAPK')->name($schema_name ? $schema_name.'.apk' : 'apk');
781 #< clear sequence name cache
782 $s->source('ExtAPK')->column_info($_)->{sequence} = undef
783 for @eapk_id_columns;
785 no warnings 'uninitialized';
788 for my $inc (1,2,3) {
789 $new = $schema->resultset('ExtAPK')->create({ id1 => 1});
790 my $proper_seqval = ++$seqs{"$schema_name_actual.apk.id2"};
791 is( $new->id2, $proper_seqval, "$schema_name_actual.apk.id2 correct inc $inc" )
792 or eapk_seq_diag($s,$schema_name);
793 $new->discard_changes;
795 for my $id ('id3','id4') {
796 my $proper_seqval = ++$seqs{"$schema_name_actual.apk.$id"};
797 is( $new->$id, $proper_seqval, "$schema_name_actual.apk.$id correct inc $inc" )
798 or eapk_seq_diag($s,$schema_name);
801 } "create in schema '$schema_name' lives"
802 or eapk_seq_diag($s,$schema_name);
805 # print diagnostic info on which sequences were found in the ExtAPK
809 my $schema = shift || eapk_find_visible_schema($s);
811 diag "$schema.apk sequences: ",
813 map "$_:".($s->source('ExtAPK')->column_info($_)->{sequence} || '<none>'),
818 # get the postgres search path as an arrayref
819 sub eapk_get_search_path {
821 # cache the search path as ['schema','schema',...] in the storage
824 return $s->storage->dbh_do(sub {
825 my (undef, $dbh) = @_;
827 my ($sp_string) = $dbh->selectrow_array('SHOW search_path');
828 while ( $sp_string =~ s/("[^"]+"|[^,]+),?// ) {
829 unless( defined $1 and length $1 ) {
830 die "search path sanity check failed: '$1'";
832 push @search_path, $1;
837 sub eapk_set_search_path {
839 my $sp = join ',',@sp;
840 $s->storage->dbh_do( sub { $_[1]->do("SET search_path = $sp") } );
843 # create the apk table in the given schema, can set whether the table name is qualified, what the nextval is for the second ID
845 my ($schema, %a) = @_;
847 $schema->storage->dbh_do(sub {
848 my (undef,$dbh) = @_;
851 if ( $a{with_search_path} ) {
852 ($searchpath_save) = $dbh->selectrow_array('SHOW search_path');
854 my $search_path = join ',',map {/\D/ ? $_ : $eapk_schemas[$_]} @{$a{with_search_path}};
856 $dbh->do("SET search_path = $search_path");
859 my $table_name = $a{qualify_table}
860 ? ($eapk_schemas[$a{qualify_table}] || die). ".apk"
862 local $_[1]->{Warn} = 0;
864 my $id_def = $a{nextval}
865 ? "integer not null default nextval('$a{nextval}'::regclass)"
868 CREATE TABLE $table_name (
871 , id3 serial primary key
876 if( $searchpath_save ) {
877 $dbh->do("SET search_path = $searchpath_save");
883 my ( $schema, $warn_exceptions ) = @_;
885 $schema->storage->dbh_do(sub {
886 my (undef,$dbh) = @_;
888 local $dbh->{Warn} = 0;
890 # drop the test schemas
891 for (@eapk_schemas ) {
892 eval{ $dbh->do("DROP SCHEMA $_ CASCADE") };
893 diag $@ if $@ && $warn_exceptions;
900 sub eapk_find_visible_schema {
904 $s->storage->dbh_do(sub {
905 $_[1]->selectrow_array(<<EOS);
907 FROM pg_catalog.pg_namespace n
908 JOIN pg_catalog.pg_class c ON c.relnamespace = n.oid
909 WHERE c.relname = 'apk'
910 AND pg_catalog.pg_table_is_visible(c.oid)