minor changes
[dbsrgits/DBIx-Class.git] / t / 72pg.t
CommitLineData
70350518 1use strict;
4617b792 2use warnings;
70350518 3
4use Test::More;
9ba627e3 5use Test::Exception;
70350518 6use lib qw(t/lib);
7use DBICTest;
8
89add887 9
0567538f 10my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
11
609c5f1b 12plan skip_all => <<EOM unless $dsn && $user;
13Set \$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
881def48 17 schemas: 'dbic_t_schema', 'dbic_t_schema_2', 'dbic_t_schema_3',
18 'dbic_t_schema_4', and 'dbic_t_schema_5'
609c5f1b 19)
20EOM
0567538f 21
52c53388 22### load any test classes that are defined further down in the file via BEGIN blocks
efd38994 23
70a201a5 24our @test_classes; #< array that will be pushed into by test classes defined in this file
25DBICTest::Schema->load_classes( map {s/.+:://;$_} @test_classes ) if @test_classes;
efd38994 26
efd38994 27
52c53388 28### pre-connect tests (keep each test separate as to make sure rebless() runs)
eabde904 29{
70a201a5 30 my $s = DBICTest::Schema->connect($dsn, $user, $pass);
eabde904 31
70a201a5 32 ok (!$s->storage->_dbh, 'definitely not connected');
eabde904 33
70a201a5 34 # Check that datetime_parser returns correctly before we explicitly connect.
465cdd3f 35 SKIP: {
70a201a5 36 eval { require DateTime::Format::Pg };
37 skip "DateTime::Format::Pg required", 2 if $@;
114780ee 38
70a201a5 39 my $store = ref $s->storage;
40 is($store, 'DBIx::Class::Storage::DBI', 'Started with generic storage');
114780ee 41
70a201a5 42 my $parser = $s->storage->datetime_parser;
43 is( $parser, 'DateTime::Format::Pg', 'datetime_parser is as expected');
44 }
114780ee 45
465cdd3f 46 ok (!$s->storage->_dbh, 'still not connected');
47}
48{
49 my $s = DBICTest::Schema->connect($dsn, $user, $pass);
70a201a5 50 # make sure sqlt_type overrides work (::Storage::DBI::Pg does this)
465cdd3f 51 ok (!$s->storage->_dbh, 'definitely not connected');
70a201a5 52 is ($s->storage->sqlt_type, 'PostgreSQL', 'sqlt_type correct pre-connection');
465cdd3f 53 ok (!$s->storage->_dbh, 'still not connected');
bb452615 54}
0567538f 55
70a201a5 56### connect, create postgres-specific test schema
c3af542a 57
70a201a5 58my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
c3af542a 59
52c53388 60drop_test_schema($schema);
2fbf50ff 61create_test_schema($schema);
609c5f1b 62
70a201a5 63### begin main tests
4617b792 64
b6b65a3e 65
372da819 66# run a BIG bunch of tests for last-insert-id / Auto-PK / sequence
67# discovery
68run_apk_tests($schema); #< older set of auto-pk tests
11da4e66 69run_extended_apk_tests($schema); #< new extended set of auto-pk tests
70a201a5 70
52c53388 71
72
73
74
70a201a5 75### type_info tests
76
a953d8d9 77my $test_type_info = {
78 'artistid' => {
103e3e03 79 'data_type' => 'integer',
80 'is_nullable' => 0,
fc22fbac 81 'size' => 4,
a953d8d9 82 },
83 'name' => {
103e3e03 84 'data_type' => 'character varying',
85 'is_nullable' => 1,
ae515736 86 'size' => 100,
fc22fbac 87 'default_value' => undef,
103e3e03 88 },
85df19d7 89 'rank' => {
90 'data_type' => 'integer',
91 'is_nullable' => 0,
92 'size' => 4,
93 'default_value' => 13,
94
95 },
103e3e03 96 'charfield' => {
97 'data_type' => 'character',
a953d8d9 98 'is_nullable' => 1,
fc22fbac 99 'size' => 10,
100 'default_value' => undef,
103e3e03 101 },
9ba627e3 102 'arrayfield' => {
103 'data_type' => 'integer[]',
104 'is_nullable' => 1,
105 'size' => undef,
106 'default_value' => undef,
107 },
a953d8d9 108};
109
881def48 110my $type_info = $schema->storage->columns_info_for('dbic_t_schema.artist');
fc22fbac 111my $artistid_defval = delete $type_info->{artistid}->{default_value};
112like($artistid_defval,
4d272ce5 113 qr/^nextval\('([^\.]*\.){0,1}artist_artistid_seq'::(?:text|regclass)\)/,
fc22fbac 114 'columns_info_for - sequence matches Pg get_autoinc_seq expectations');
115is_deeply($type_info, $test_type_info,
116 'columns_info_for - column data types');
a953d8d9 117
70a201a5 118
119
120
121####### Array tests
122
123BEGIN {
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/);
881def48 132 __PACKAGE__->table('dbic_t_schema.array_test');
70a201a5 133 __PACKAGE__->add_columns(qw/id arrayfield/);
134 __PACKAGE__->column_info_from_storage(1);
135 __PACKAGE__->set_primary_key('id');
136
137}
ac45262b 138SKIP: {
139 skip "Need DBD::Pg 2.9.2 or newer for array tests", 4 if $DBD::Pg::VERSION < 2.009002;
140
9ba627e3 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';
c224117b 152
153 $schema->resultset('ArrayTest')->create({
154 arrayfield => [5, 6],
155 });
156
157 my $count;
158 lives_ok {
159 $count = $schema->resultset('ArrayTest')->search({
aab0d3b7 160 arrayfield => \[ '= ?' => [arrayfield => [3, 4]] ], #Todo anything less ugly than this?
c224117b 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');
9ba627e3 164}
165
166
70a201a5 167
168########## Case check
169
170BEGIN {
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/);
881def48 179 __PACKAGE__->table('dbic_t_schema.casecheck');
52c53388 180 __PACKAGE__->add_columns(qw/id name NAME uc_name/);
70a201a5 181 __PACKAGE__->column_info_from_storage(1);
182 __PACKAGE__->set_primary_key('id');
70a201a5 183}
184
3ff5b740 185my $name_info = $schema->source('Casecheck')->column_info( 'name' );
ae515736 186is( $name_info->{size}, 1, "Case sensitive matching info for 'name'" );
187
3ff5b740 188my $NAME_info = $schema->source('Casecheck')->column_info( 'NAME' );
ae515736 189is( $NAME_info->{size}, 2, "Case sensitive matching info for 'NAME'" );
190
3ff5b740 191my $uc_name_info = $schema->source('Casecheck')->column_info( 'uc_name' );
ae515736 192is( $uc_name_info->{size}, 3, "Case insensitive matching info for 'uc_name'" );
193
70a201a5 194
195
196
197## Test SELECT ... FOR UPDATE
198
95ba7ee4 199SKIP: {
52c53388 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 }
95ba7ee4 206
52c53388 207 my ($timed_out, $artist2);
95ba7ee4 208
52c53388 209 for my $t (
210 {
95ba7ee4 211 # Make sure that an error was raised, and that the update failed
52c53388 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 {
95ba7ee4 232 my $artist = $schema->resultset('Artist')->search(
233 {
234 artistid => 1
235 },
52c53388 236 $t->{update_lock} ? { for => 'update' } : {}
95ba7ee4 237 )->first;
52c53388 238 is($artist->artistid, 1, "select returns artistid = 1");
95ba7ee4 239
52c53388 240 $timed_out = 0;
95ba7ee4 241 eval {
242 my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
243 alarm(2);
52c53388 244 $artist2 = $schema2->resultset('Artist')->find(1);
245 $artist2->name('fooey');
246 $artist2->update;
95ba7ee4 247 alarm(0);
248 };
52c53388 249 $timed_out = $@ =~ /DBICTestTimeout/;
250 });
95ba7ee4 251
52c53388 252 $t->{test_sub}->();
253 }
95ba7ee4 254}
255
70a201a5 256
372da819 257######## other older Auto-pk tests
70a201a5 258
881def48 259$schema->source("SequenceTest")->name("dbic_t_schema.sequence_test");
d093d3eb 260for (1..5) {
39b8d119 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");
d093d3eb 265}
266my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
267is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
268
2fbf50ff 269done_testing;
609c5f1b 270
70a201a5 271exit;
5935c90a 272
273END {
f66c9ba8 274 return unless $schema;
5935c90a 275 drop_test_schema($schema);
6a6dd46a 276 eapk_drop_all( $schema)
5935c90a 277};
70a201a5 278
279
280######### SUBROUTINES
281
282sub create_test_schema {
2fbf50ff 283 my $schema = shift;
284 $schema->storage->dbh_do(sub {
285 my (undef,$dbh) = @_;
70a201a5 286
2fbf50ff 287 local $dbh->{Warn} = 0;
70a201a5 288
2fbf50ff 289 my $std_artist_table = <<EOS;
70a201a5 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)
297EOS
298
881def48 299 $dbh->do("CREATE SCHEMA dbic_t_schema");
300 $dbh->do("CREATE TABLE dbic_t_schema.artist $std_artist_table");
2fbf50ff 301 $dbh->do(<<EOS);
881def48 302CREATE TABLE dbic_t_schema.sequence_test (
70a201a5 303 pkid1 integer
304 , pkid2 integer
305 , nonpkid integer
306 , name VARCHAR(100)
307 , CONSTRAINT pk PRIMARY KEY(pkid1, pkid2)
308)
309EOS
2fbf50ff 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);
881def48 314CREATE TABLE dbic_t_schema.casecheck (
70a201a5 315 id serial PRIMARY KEY
316 , "name" VARCHAR(1)
317 , "NAME" VARCHAR(2)
318 , "UC_NAME" VARCHAR(3)
70a201a5 319)
320EOS
2fbf50ff 321 $dbh->do(<<EOS);
881def48 322CREATE TABLE dbic_t_schema.array_test (
70a201a5 323 id serial PRIMARY KEY
324 , arrayfield INTEGER[]
325)
326EOS
881def48 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");
2fbf50ff 334 $dbh->do(<<EOS);
881def48 335 CREATE TABLE dbic_t_schema_4.artist
70a201a5 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 );
343EOS
881def48 344 $dbh->do('set search_path=public,dbic_t_schema,dbic_t_schema_3');
2fbf50ff 345 $dbh->do('create sequence public.artist_artistid_seq'); #< in the public schema
346 $dbh->do(<<EOS);
881def48 347 CREATE TABLE dbic_t_schema_5.artist
70a201a5 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 );
355EOS
881def48 356 $dbh->do('set search_path=dbic_t_schema,public');
2fbf50ff 357 });
70a201a5 358}
359
360
361
362sub drop_test_schema {
52c53388 363 my ( $schema, $warn_exceptions ) = @_;
2fbf50ff 364
365 $schema->storage->dbh_do(sub {
366 my (undef,$dbh) = @_;
367
368 local $dbh->{Warn} = 0;
369
370 for my $stat (
881def48 371 'DROP SCHEMA dbic_t_schema_5 CASCADE',
2fbf50ff 372 'DROP SEQUENCE public.artist_artistid_seq',
881def48 373 'DROP SCHEMA dbic_t_schema_4 CASCADE',
374 'DROP SCHEMA dbic_t_schema CASCADE',
2fbf50ff 375 'DROP SEQUENCE pkid1_seq',
376 'DROP SEQUENCE pkid2_seq',
377 'DROP SEQUENCE nonpkid_seq',
881def48 378 'DROP SCHEMA dbic_t_schema_2 CASCADE',
379 'DROP SCHEMA dbic_t_schema_3 CASCADE',
2fbf50ff 380 ) {
381 eval { $dbh->do ($stat) };
52c53388 382 diag $@ if $@ && $warn_exceptions;
2fbf50ff 383 }
384 });
3ff5b740 385}
7603d2a5 386
372da819 387
ee9f372c 388### auto-pk / last_insert_id / sequence discovery
389sub 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
ee9f372c 441# sets the artist table name and clears sequence name cache
442sub 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
372da819 448
5935c90a 449######## EXTENDED AUTO-PK TESTS
450
f295a73c 451my @eapk_id_columns;
5935c90a 452BEGIN {
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/);
f295a73c 461 __PACKAGE__->table('apk');
5935c90a 462
f295a73c 463 @eapk_id_columns = qw( id1 id2 id3 id4 );
5935c90a 464 __PACKAGE__->add_columns(
465 map { $_ => { data_type => 'integer', is_auto_increment => 1 } }
f295a73c 466 @eapk_id_columns
5935c90a 467 );
468
f295a73c 469 __PACKAGE__->set_primary_key('id2'); #< note the SECOND column is
470 #the primary key
5935c90a 471}
472
f295a73c 473my @eapk_schemas;
474BEGIN{ @eapk_schemas = map "dbic_apk_$_", 0..5 }
5935c90a 475
372da819 476sub run_extended_apk_tests {
11da4e66 477 my $schema = shift;
372da819 478
f295a73c 479 #save the search path and reset it at the end
49aa7e87 480 my $search_path_save = eapk_get_search_path($schema);
f295a73c 481
52c53388 482 eapk_drop_all($schema);
5935c90a 483
f295a73c 484 # make the test schemas and sequences
11da4e66 485 $schema->storage->dbh_do(sub {
f295a73c 486 my ( undef, $dbh ) = @_;
487
488 $dbh->do("CREATE SCHEMA $_")
489 for @eapk_schemas;
490
491 $dbh->do("CREATE SEQUENCE $eapk_schemas[5].fooseq");
fb7be534 492 $dbh->do("CREATE SEQUENCE $eapk_schemas[4].fooseq");
93a6e8fa 493 $dbh->do("CREATE SEQUENCE $eapk_schemas[3].fooseq");
f295a73c 494
495 $dbh->do("SET search_path = ".join ',', @eapk_schemas );
11da4e66 496 });
372da819 497
f295a73c 498 # clear our search_path cache
499 $schema->storage->{_pg_search_path} = undef;
500
501 eapk_create( $schema,
502 with_search_path => [0,1],
503 );
504 eapk_create( $schema,
505 with_search_path => [1,0,'public'],
506 nextval => "$eapk_schemas[5].fooseq",
507 );
508 eapk_create( $schema,
509 with_search_path => ['public',0,1],
510 qualify_table => 2,
511 );
fb7be534 512 eapk_create( $schema,
513 with_search_path => [3,1,0,'public'],
514 nextval => "$eapk_schemas[4].fooseq",
515 );
93a6e8fa 516 eapk_create( $schema,
517 with_search_path => [3,1,0,'public'],
518 nextval => "$eapk_schemas[3].fooseq",
519 qualify_table => 4,
520 );
372da819 521
f295a73c 522 eapk_poke( $schema, 0 );
eb80e8d5 523 eapk_poke( $schema, 2 );
93a6e8fa 524 eapk_poke( $schema, 4 );
f295a73c 525 eapk_poke( $schema, 1 );
eb80e8d5 526 eapk_poke( $schema, 0 );
f295a73c 527 eapk_poke( $schema, 1 );
93a6e8fa 528 eapk_poke( $schema, 4 );
fb7be534 529 eapk_poke( $schema, 3 );
eb80e8d5 530 eapk_poke( $schema, 1 );
531 eapk_poke( $schema, 2 );
532 eapk_poke( $schema, 0 );
f295a73c 533
534 # set our search path back
535 eapk_set_search_path( $schema, @$search_path_save );
536}
537
538# do a DBIC create on the apk table in the given schema number (which is an
539# index of @eapk_schemas)
540
541my %seqs; #< sanity-check hash of schema.table.col => currval of its sequence
542
543sub eapk_poke {
544 my ($s, $schema_num) = @_;
372da819 545
f295a73c 546 my $schema_name = defined $schema_num
547 ? $eapk_schemas[$schema_num]
548 : '';
549
49aa7e87 550 my $schema_name_actual = $schema_name || eapk_get_search_path($s)->[0];
f295a73c 551
8ce84173 552 $s->source('ExtAPK')->name($schema_name ? $schema_name.'.apk' : 'apk');
f295a73c 553 #< clear sequence name cache
554 $s->source('ExtAPK')->column_info($_)->{sequence} = undef
555 for @eapk_id_columns;
556
557 no warnings 'uninitialized';
558 lives_ok {
559 my $new;
560 for my $inc (1,2,3) {
561 $new = $schema->resultset('ExtAPK')->create({});
691f3663 562 my $proper_seqval = ++$seqs{"$schema_name_actual.apk.id2"};
563 is( $new->id2, $proper_seqval, "$schema_name_actual.apk.id2 correct inc $inc" )
564 or eapk_seq_diag($s,$schema_name);
565 $new->discard_changes;
566 for my $id (grep $_ ne 'id2', @eapk_id_columns) {
f295a73c 567 my $proper_seqval = ++$seqs{"$schema_name_actual.apk.$id"};
93a6e8fa 568 is( $new->$id, $proper_seqval, "$schema_name_actual.apk.$id correct inc $inc" )
fb7be534 569 or eapk_seq_diag($s,$schema_name);
f295a73c 570 }
571 }
572 } "create in schema '$schema_name' lives"
fb7be534 573 or eapk_seq_diag($s,$schema_name);
372da819 574}
575
f295a73c 576# print diagnostic info on which sequences were found in the ExtAPK
577# class
578sub eapk_seq_diag {
579 my $s = shift;
49aa7e87 580 my $schema = shift || eapk_get_search_path($s)->[0];
f295a73c 581
582 diag "$schema.apk sequences: ",
583 join(', ',
584 map "$_:".($s->source('ExtAPK')->column_info($_)->{sequence} || '<none>'),
585 @eapk_id_columns
586 );
587}
588
49aa7e87 589# get the postgres search path as an arrayref
590sub eapk_get_search_path {
591 my ( $s ) = @_;
592 # cache the search path as ['schema','schema',...] in the storage
593 # obj
594
595 return $s->storage->dbh_do(sub {
596 my (undef, $dbh) = @_;
597 my @search_path;
598 my ($sp_string) = $dbh->selectrow_array('SHOW search_path');
599 while ( $sp_string =~ s/("[^"]+"|[^,]+),?// ) {
600 unless( defined $1 and length $1 ) {
601 die "search path sanity check failed: '$1'";
602 }
603 push @search_path, $1;
604 }
605 \@search_path
606 });
607}
f295a73c 608sub eapk_set_search_path {
609 my ($s,@sp) = @_;
610 my $sp = join ',',@sp;
611 $s->storage->dbh_do( sub { $_[1]->do("SET search_path = $sp") } );
612}
613
614# create the apk table in the given schema, can set whether the table name is qualified, what the nextval is for the second ID
5935c90a 615sub eapk_create {
616 my ($schema, %a) = @_;
617
372da819 618 $schema->storage->dbh_do(sub {
5935c90a 619 my (undef,$dbh) = @_;
372da819 620
5935c90a 621 my $searchpath_save;
622 if ( $a{with_search_path} ) {
623 ($searchpath_save) = $dbh->selectrow_array('SHOW search_path');
624
f295a73c 625 my $search_path = join ',',map {/\D/ ? $_ : $eapk_schemas[$_]} @{$a{with_search_path}};
5935c90a 626
627 $dbh->do("SET search_path = $search_path");
628 }
629
f295a73c 630 my $table_name = $a{qualify_table}
631 ? ($eapk_schemas[$a{qualify_table}] || die). ".apk"
632 : 'apk';
e25fe566 633 local $_[1]->{Warn} = 0;
f295a73c 634
635 my $id_def = $a{nextval}
636 ? "integer primary key not null default nextval('$a{nextval}'::regclass)"
637 : 'serial primary key';
5935c90a 638 $dbh->do(<<EOS);
f295a73c 639CREATE TABLE $table_name (
640 id1 serial
641 , id2 $id_def
5935c90a 642 , id3 serial
643 , id4 serial
644)
645EOS
372da819 646
5935c90a 647 if( $searchpath_save ) {
648 $dbh->do("SET search_path = $searchpath_save");
649 }
372da819 650 });
651}
652
5935c90a 653sub eapk_drop_all {
52c53388 654 my ( $schema, $warn_exceptions ) = @_;
372da819 655
656 $schema->storage->dbh_do(sub {
657 my (undef,$dbh) = @_;
658
659 local $dbh->{Warn} = 0;
5935c90a 660
661 # drop the test schemas
f295a73c 662 for (@eapk_schemas ) {
11da4e66 663 eval{ $dbh->do("DROP SCHEMA $_ CASCADE") };
52c53388 664 diag $@ if $@ && $warn_exceptions;
11da4e66 665 }
666
372da819 667
372da819 668 });
669}