fix oracle dep in Makefile.PL
[dbsrgits/DBIx-Class.git] / t / 72pg.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8
9
10 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
11
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'
19 )
20 EOM
21
22 ### load any test classes that are defined further down in the file via BEGIN blocks
23
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;
26
27
28 ###  pre-connect tests (keep each test separate as to make sure rebless() runs)
29 {
30   my $s = DBICTest::Schema->connect($dsn, $user, $pass);
31
32   ok (!$s->storage->_dbh, 'definitely not connected');
33
34   # Check that datetime_parser returns correctly before we explicitly connect.
35   SKIP: {
36       eval { require DateTime::Format::Pg };
37       skip "DateTime::Format::Pg required", 2 if $@;
38
39       my $store = ref $s->storage;
40       is($store, 'DBIx::Class::Storage::DBI', 'Started with generic storage');
41
42       my $parser = $s->storage->datetime_parser;
43       is( $parser, 'DateTime::Format::Pg', 'datetime_parser is as expected');
44   }
45
46   ok (!$s->storage->_dbh, 'still not connected');
47 }
48 {
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');
54 }
55
56 ### connect, create postgres-specific test schema
57
58 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
59
60 drop_test_schema($schema);
61 create_test_schema($schema);
62
63 ### begin main tests
64
65
66 # run a BIG bunch of tests for last-insert-id / Auto-PK / sequence
67 # discovery
68 run_apk_tests($schema); #< older set of auto-pk tests
69 run_extended_apk_tests($schema); #< new extended set of auto-pk tests
70
71
72
73
74
75 ### type_info tests
76
77 my $test_type_info = {
78     'artistid' => {
79         'data_type' => 'integer',
80         'is_nullable' => 0,
81         'size' => 4,
82     },
83     'name' => {
84         'data_type' => 'character varying',
85         'is_nullable' => 1,
86         'size' => 100,
87         'default_value' => undef,
88     },
89     'rank' => {
90         'data_type' => 'integer',
91         'is_nullable' => 0,
92         'size' => 4,
93         'default_value' => 13,
94
95     },
96     'charfield' => {
97         'data_type' => 'character',
98         'is_nullable' => 1,
99         'size' => 10,
100         'default_value' => undef,
101     },
102     'arrayfield' => {
103         'data_type' => 'integer[]',
104         'is_nullable' => 1,
105         'size' => undef,
106         'default_value' => undef,
107     },
108 };
109
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');
117
118
119
120
121 ####### Array tests
122
123 BEGIN {
124   package DBICTest::Schema::ArrayTest;
125   push @main::test_classes, __PACKAGE__;
126
127   use strict;
128   use warnings;
129   use base 'DBIx::Class';
130
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');
136
137 }
138 SKIP: {
139   skip "Need DBD::Pg 2.9.2 or newer for array tests", 4 if $DBD::Pg::VERSION < 2.009002;
140
141   lives_ok {
142     $schema->resultset('ArrayTest')->create({
143       arrayfield => [1, 2],
144     });
145   } 'inserting arrayref as pg array data';
146
147   lives_ok {
148     $schema->resultset('ArrayTest')->update({
149       arrayfield => [3, 4],
150     });
151   } 'updating arrayref as pg array data';
152
153   $schema->resultset('ArrayTest')->create({
154     arrayfield => [5, 6],
155   });
156
157   my $count;
158   lives_ok {
159     $count = $schema->resultset('ArrayTest')->search({
160       arrayfield => \[ '= ?' => [arrayfield => [3, 4]] ],   #Todo anything less ugly than this?
161     })->count;
162   } 'comparing arrayref to pg array data does not blow up';
163   is($count, 1, 'comparing arrayref to pg array data gives correct result');
164 }
165
166
167
168 ########## Case check
169
170 BEGIN {
171   package DBICTest::Schema::Casecheck;
172   push @main::test_classes, __PACKAGE__;
173
174   use strict;
175   use warnings;
176   use base 'DBIx::Class';
177
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');
183 }
184
185 my $name_info = $schema->source('Casecheck')->column_info( 'name' );
186 is( $name_info->{size}, 1, "Case sensitive matching info for 'name'" );
187
188 my $NAME_info = $schema->source('Casecheck')->column_info( 'NAME' );
189 is( $NAME_info->{size}, 2, "Case sensitive matching info for 'NAME'" );
190
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'" );
193
194
195
196
197 ## Test SELECT ... FOR UPDATE
198
199 SKIP: {
200     if(eval "require Sys::SigAction" && !$@) {
201         Sys::SigAction->import( 'set_sig_handler' );
202     }
203     else {
204       skip "Sys::SigAction is not available", 6;
205     }
206
207     my ($timed_out, $artist2);
208
209     for my $t (
210       {
211         # Make sure that an error was raised, and that the update failed
212         update_lock => 1,
213         test_sub => sub {
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");
216         },
217       },
218       {
219         # Make sure that an error was NOT raised, and that the update succeeded
220         update_lock => 0,
221         test_sub => sub {
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");
224         },
225       },
226     ) {
227       # create a new schema
228       my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
229       $schema2->source("Artist")->name("dbic_t_schema.artist");
230
231       $schema->txn_do( sub {
232         my $artist = $schema->resultset('Artist')->search(
233             {
234                 artistid => 1
235             },
236             $t->{update_lock} ? { for => 'update' } : {}
237         )->first;
238         is($artist->artistid, 1, "select returns artistid = 1");
239
240         $timed_out = 0;
241         eval {
242             my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
243             alarm(2);
244             $artist2 = $schema2->resultset('Artist')->find(1);
245             $artist2->name('fooey');
246             $artist2->update;
247             alarm(0);
248         };
249         $timed_out = $@ =~ /DBICTestTimeout/;
250       });
251
252       $t->{test_sub}->();
253     }
254 }
255
256
257 ######## other older Auto-pk tests
258
259 $schema->source("SequenceTest")->name("dbic_t_schema.sequence_test");
260 for (1..5) {
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");
265 }
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");
268
269 done_testing;
270
271 exit;
272
273 END {
274     return unless $schema;
275     drop_test_schema($schema);
276     eapk_drop_all( $schema)
277 };
278
279
280 ######### SUBROUTINES
281
282 sub create_test_schema {
283     my $schema = shift;
284     $schema->storage->dbh_do(sub {
285       my (undef,$dbh) = @_;
286
287       local $dbh->{Warn} = 0;
288
289       my $std_artist_table = <<EOS;
290 (
291   artistid serial PRIMARY KEY
292   , name VARCHAR(100)
293   , rank INTEGER NOT NULL DEFAULT '13'
294   , charfield CHAR(10)
295   , arrayfield INTEGER[]
296 )
297 EOS
298
299       $dbh->do("CREATE SCHEMA dbic_t_schema");
300       $dbh->do("CREATE TABLE dbic_t_schema.artist $std_artist_table");
301       $dbh->do(<<EOS);
302 CREATE TABLE dbic_t_schema.sequence_test (
303     pkid1 integer
304     , pkid2 integer
305     , nonpkid integer
306     , name VARCHAR(100)
307     , CONSTRAINT pk PRIMARY KEY(pkid1, pkid2)
308 )
309 EOS
310       $dbh->do("CREATE SEQUENCE pkid1_seq START 1 MAXVALUE 999999 MINVALUE 0");
311       $dbh->do("CREATE SEQUENCE pkid2_seq START 10 MAXVALUE 999999 MINVALUE 0");
312       $dbh->do("CREATE SEQUENCE nonpkid_seq START 20 MAXVALUE 999999 MINVALUE 0");
313       $dbh->do(<<EOS);
314 CREATE TABLE dbic_t_schema.casecheck (
315     id serial PRIMARY KEY
316     , "name" VARCHAR(1)
317     , "NAME" VARCHAR(2)
318     , "UC_NAME" VARCHAR(3)
319 )
320 EOS
321       $dbh->do(<<EOS);
322 CREATE TABLE dbic_t_schema.array_test (
323     id serial PRIMARY KEY
324     , arrayfield INTEGER[]
325 )
326 EOS
327       $dbh->do("CREATE SCHEMA dbic_t_schema_2");
328       $dbh->do("CREATE TABLE dbic_t_schema_2.artist $std_artist_table");
329       $dbh->do("CREATE SCHEMA dbic_t_schema_3");
330       $dbh->do("CREATE TABLE dbic_t_schema_3.artist $std_artist_table");
331       $dbh->do('set search_path=dbic_t_schema,public');
332       $dbh->do("CREATE SCHEMA dbic_t_schema_4");
333       $dbh->do("CREATE SCHEMA dbic_t_schema_5");
334       $dbh->do(<<EOS);
335  CREATE TABLE dbic_t_schema_4.artist
336  (
337    artistid integer not null default nextval('artist_artistid_seq'::regclass) PRIMARY KEY
338    , name VARCHAR(100)
339    , rank INTEGER NOT NULL DEFAULT '13'
340    , charfield CHAR(10)
341    , arrayfield INTEGER[]
342  );
343 EOS
344       $dbh->do('set search_path=public,dbic_t_schema,dbic_t_schema_3');
345       $dbh->do('create sequence public.artist_artistid_seq'); #< in the public schema
346       $dbh->do(<<EOS);
347  CREATE TABLE dbic_t_schema_5.artist
348  (
349    artistid integer not null default nextval('public.artist_artistid_seq'::regclass) PRIMARY KEY
350    , name VARCHAR(100)
351    , rank INTEGER NOT NULL DEFAULT '13'
352    , charfield CHAR(10)
353    , arrayfield INTEGER[]
354  );
355 EOS
356       $dbh->do('set search_path=dbic_t_schema,public');
357   });
358 }
359
360
361
362 sub drop_test_schema {
363     my ( $schema, $warn_exceptions ) = @_;
364
365     $schema->storage->dbh_do(sub {
366         my (undef,$dbh) = @_;
367
368         local $dbh->{Warn} = 0;
369
370         for my $stat (
371                       'DROP SCHEMA dbic_t_schema_5 CASCADE',
372                       'DROP SEQUENCE public.artist_artistid_seq',
373                       'DROP SCHEMA dbic_t_schema_4 CASCADE',
374                       'DROP SCHEMA dbic_t_schema CASCADE',
375                       'DROP SEQUENCE pkid1_seq',
376                       'DROP SEQUENCE pkid2_seq',
377                       'DROP SEQUENCE nonpkid_seq',
378                       'DROP SCHEMA dbic_t_schema_2 CASCADE',
379                       'DROP SCHEMA dbic_t_schema_3 CASCADE',
380                      ) {
381             eval { $dbh->do ($stat) };
382             diag $@ if $@ && $warn_exceptions;
383         }
384     });
385 }
386
387
388 ###  auto-pk / last_insert_id / sequence discovery
389 sub run_apk_tests {
390     my $schema = shift;
391
392     # This is in Core now, but it's here just to test that it doesn't break
393     $schema->class('Artist')->load_components('PK::Auto');
394     cmp_ok( $schema->resultset('Artist')->count, '==', 0, 'this should start with an empty artist table');
395
396     # test that auto-pk also works with the defined search path by
397     # un-schema-qualifying the table name
398     apk_t_set($schema,'artist');
399
400     my $unq_new;
401     lives_ok {
402         $unq_new = $schema->resultset('Artist')->create({ name => 'baz' });
403     } 'insert into unqualified, shadowed table succeeds';
404
405     is($unq_new && $unq_new->artistid, 1, "and got correct artistid");
406
407     my @test_schemas = ( [qw| dbic_t_schema_2    1  |],
408                          [qw| dbic_t_schema_3    1  |],
409                          [qw| dbic_t_schema_4    2  |],
410                          [qw| dbic_t_schema_5    1  |],
411                        );
412     foreach my $t ( @test_schemas ) {
413         my ($sch_name, $start_num) = @$t;
414         #test with dbic_t_schema_2
415         apk_t_set($schema,"$sch_name.artist");
416         my $another_new;
417         lives_ok {
418             $another_new = $schema->resultset('Artist')->create({ name => 'Tollbooth Willy'});
419             is( $another_new->artistid,$start_num, "got correct artistid for $sch_name")
420                 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
421         } "$sch_name liid 1 did not die"
422             or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
423         lives_ok {
424             $another_new = $schema->resultset('Artist')->create({ name => 'Adam Sandler'});
425             is( $another_new->artistid,$start_num+1, "got correct artistid for $sch_name")
426                 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
427         } "$sch_name liid 2 did not die"
428             or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
429
430     }
431
432     lives_ok {
433         apk_t_set($schema,'dbic_t_schema.artist');
434         my $new = $schema->resultset('Artist')->create({ name => 'foo' });
435         is($new->artistid, 4, "Auto-PK worked");
436         $new = $schema->resultset('Artist')->create({ name => 'bar' });
437         is($new->artistid, 5, "Auto-PK worked");
438     } 'old auto-pk tests did not die either';
439 }
440
441 # sets the artist table name and clears sequence name cache
442 sub apk_t_set {
443     my ( $s, $n ) = @_;
444     $s->source("Artist")->name($n);
445     $s->source('Artist')->column_info('artistid')->{sequence} = undef; #< clear sequence name cache
446 }
447
448
449 ######## EXTENDED AUTO-PK TESTS
450
451 my @eapk_id_columns;
452 BEGIN {
453   package DBICTest::Schema::ExtAPK;
454   push @main::test_classes, __PACKAGE__;
455
456   use strict;
457   use warnings;
458   use base 'DBIx::Class';
459
460   __PACKAGE__->load_components(qw/Core/);
461   __PACKAGE__->table('apk');
462
463   @eapk_id_columns = qw( id1 id2 id3 id4 );
464   __PACKAGE__->add_columns(
465     map { $_ => { data_type => 'integer', is_auto_increment => 1 } }
466        @eapk_id_columns
467   );
468
469   __PACKAGE__->set_primary_key('id2'); #< note the SECOND column is
470                                        #the primary key
471 }
472
473 my @eapk_schemas;
474 BEGIN{ @eapk_schemas = map "dbic_apk_$_", 0..5 }
475 my %seqs; #< hash of schema.table.col => currval of its (DBIC) primary key sequence
476
477 sub run_extended_apk_tests {
478   my $schema = shift;
479
480   #save the search path and reset it at the end
481   my $search_path_save = eapk_get_search_path($schema);
482
483   eapk_drop_all($schema);
484
485   # make the test schemas and sequences
486   $schema->storage->dbh_do(sub {
487     my ( undef, $dbh ) = @_;
488
489     $dbh->do("CREATE SCHEMA $_")
490         for @eapk_schemas;
491
492     $dbh->do("CREATE SEQUENCE $eapk_schemas[5].fooseq");
493     $dbh->do("SELECT setval('$eapk_schemas[5].fooseq',400)");
494     $seqs{"$eapk_schemas[1].apk.id2"} = 400;
495
496     $dbh->do("CREATE SEQUENCE $eapk_schemas[4].fooseq");
497     $dbh->do("SELECT setval('$eapk_schemas[4].fooseq',300)");
498     $seqs{"$eapk_schemas[3].apk.id2"} = 300;
499
500     $dbh->do("CREATE SEQUENCE $eapk_schemas[3].fooseq");
501     $dbh->do("SELECT setval('$eapk_schemas[3].fooseq',200)");
502     $seqs{"$eapk_schemas[4].apk.id2"} = 200;
503
504     $dbh->do("SET search_path = ".join ',', reverse @eapk_schemas );
505   });
506
507   # clear our search_path cache
508   $schema->storage->{_pg_search_path} = undef;
509
510   eapk_create( $schema,
511                with_search_path => [0,1],
512              );
513   eapk_create( $schema,
514                with_search_path => [1,0,'public'],
515                nextval => "$eapk_schemas[5].fooseq",
516              );
517   eapk_create( $schema,
518                with_search_path => ['public',0,1],
519                qualify_table => 2,
520              );
521   eapk_create( $schema,
522                with_search_path => [3,1,0,'public'],
523                nextval => "$eapk_schemas[4].fooseq",
524              );
525   eapk_create( $schema,
526                with_search_path => [3,1,0,'public'],
527                nextval => "$eapk_schemas[3].fooseq",
528                qualify_table => 4,
529              );
530
531   eapk_poke( $schema );
532   eapk_poke( $schema, 0 );
533   eapk_poke( $schema, 2 );
534   eapk_poke( $schema, 4 );
535   eapk_poke( $schema, 1 );
536   eapk_poke( $schema, 0 );
537   eapk_poke( $schema, 1 );
538   eapk_poke( $schema );
539   eapk_poke( $schema, 4 );
540   eapk_poke( $schema, 3 );
541   eapk_poke( $schema, 1 );
542   eapk_poke( $schema, 2 );
543   eapk_poke( $schema, 0 );
544
545   # set our search path back
546   eapk_set_search_path( $schema, @$search_path_save );
547 }
548
549 # do a DBIC create on the apk table in the given schema number (which is an
550 # index of @eapk_schemas)
551
552 sub eapk_poke {
553   my ($s, $schema_num) = @_;
554
555   my $schema_name = defined $schema_num
556       ? $eapk_schemas[$schema_num]
557       : '';
558
559   my $schema_name_actual = $schema_name || eapk_find_visible_schema($s);
560
561   $s->source('ExtAPK')->name($schema_name ? $schema_name.'.apk' : 'apk');
562   #< clear sequence name cache
563   $s->source('ExtAPK')->column_info($_)->{sequence} = undef
564       for @eapk_id_columns;
565
566   no warnings 'uninitialized';
567   lives_ok {
568     my $new;
569     for my $inc (1,2,3) {
570       $new = $schema->resultset('ExtAPK')->create({ id1 => 1});
571       my $proper_seqval = ++$seqs{"$schema_name_actual.apk.id2"};
572       is( $new->id2, $proper_seqval, "$schema_name_actual.apk.id2 correct inc $inc" )
573           or eapk_seq_diag($s,$schema_name);
574       $new->discard_changes;
575       is( $new->id1, 1 );
576       for my $id ('id3','id4') {
577         my $proper_seqval = ++$seqs{"$schema_name_actual.apk.$id"};
578         is( $new->$id, $proper_seqval, "$schema_name_actual.apk.$id correct inc $inc" )
579             or eapk_seq_diag($s,$schema_name);
580       }
581     }
582   } "create in schema '$schema_name' lives"
583       or eapk_seq_diag($s,$schema_name);
584 }
585
586 # print diagnostic info on which sequences were found in the ExtAPK
587 # class
588 sub eapk_seq_diag {
589     my $s = shift;
590     my $schema = shift || eapk_find_visible_schema($s);
591
592     diag "$schema.apk sequences: ",
593         join(', ',
594              map "$_:".($s->source('ExtAPK')->column_info($_)->{sequence} || '<none>'),
595              @eapk_id_columns
596             );
597 }
598
599 # get the postgres search path as an arrayref
600 sub eapk_get_search_path {
601     my ( $s ) = @_;
602     # cache the search path as ['schema','schema',...] in the storage
603     # obj
604
605     return $s->storage->dbh_do(sub {
606         my (undef, $dbh) = @_;
607         my @search_path;
608         my ($sp_string) = $dbh->selectrow_array('SHOW search_path');
609         while ( $sp_string =~ s/("[^"]+"|[^,]+),?// ) {
610             unless( defined $1 and length $1 ) {
611                 die "search path sanity check failed: '$1'";
612             }
613             push @search_path, $1;
614         }
615         \@search_path
616     });
617 }
618 sub eapk_set_search_path {
619     my ($s,@sp) = @_;
620     my $sp = join ',',@sp;
621     $s->storage->dbh_do( sub { $_[1]->do("SET search_path = $sp") } );
622 }
623
624 # create the apk table in the given schema, can set whether the table name is qualified, what the nextval is for the second ID
625 sub eapk_create {
626     my ($schema, %a) = @_;
627
628     $schema->storage->dbh_do(sub {
629         my (undef,$dbh) = @_;
630
631         my $searchpath_save;
632         if ( $a{with_search_path} ) {
633             ($searchpath_save) = $dbh->selectrow_array('SHOW search_path');
634
635             my $search_path = join ',',map {/\D/ ? $_ : $eapk_schemas[$_]} @{$a{with_search_path}};
636
637             $dbh->do("SET search_path = $search_path");
638         }
639
640         my $table_name = $a{qualify_table}
641             ? ($eapk_schemas[$a{qualify_table}] || die). ".apk"
642             : 'apk';
643         local $_[1]->{Warn} = 0;
644
645         my $id_def = $a{nextval}
646             ? "integer not null default nextval('$a{nextval}'::regclass)"
647             : 'serial';
648         $dbh->do(<<EOS);
649 CREATE TABLE $table_name (
650   id1 serial
651   , id2 $id_def
652   , id3 serial primary key
653   , id4 serial
654 )
655 EOS
656
657         if( $searchpath_save ) {
658             $dbh->do("SET search_path = $searchpath_save");
659         }
660     });
661 }
662
663 sub eapk_drop_all {
664     my ( $schema, $warn_exceptions ) = @_;
665
666     $schema->storage->dbh_do(sub {
667         my (undef,$dbh) = @_;
668
669         local $dbh->{Warn} = 0;
670
671         # drop the test schemas
672         for (@eapk_schemas ) {
673             eval{ $dbh->do("DROP SCHEMA $_ CASCADE") };
674             diag $@ if $@ && $warn_exceptions;
675         }
676
677
678     });
679 }
680
681 sub eapk_find_visible_schema {
682     my ($s) = @_;
683
684     my ($schema) =
685         $s->storage->dbh_do(sub {
686             $_[1]->selectrow_array(<<EOS);
687 SELECT n.nspname
688 FROM pg_catalog.pg_namespace n
689 JOIN pg_catalog.pg_class c ON c.relnamespace = n.oid
690 WHERE c.relname = 'apk'
691   AND pg_catalog.pg_table_is_visible(c.oid)
692 EOS
693         });
694     return $schema;
695 }