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', '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'
22 ### load any test classes that are defined further down in the file
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;
30 my $s = DBICTest::Schema->connect($dsn, $user, $pass);
32 ok (!$s->storage->_dbh, 'definitely not connected');
34 # Check that datetime_parser returns correctly before we explicitly connect.
36 eval { require DateTime::Format::Pg };
37 skip "DateTime::Format::Pg required", 2 if $@;
39 my $store = ref $s->storage;
40 is($store, 'DBIx::Class::Storage::DBI', 'Started with generic storage');
42 my $parser = $s->storage->datetime_parser;
43 is( $parser, 'DateTime::Format::Pg', 'datetime_parser is as expected');
47 # make sure sqlt_type overrides work (::Storage::DBI::Pg does this)
48 is ($s->storage->sqlt_type, 'PostgreSQL', 'sqlt_type correct pre-connection');
51 ### connect, create postgres-specific test schema
53 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
55 drop_test_schema($schema, 'no warn');
56 create_test_schema($schema);
61 # run a BIG bunch of tests for last-insert-id / Auto-PK / sequence
63 run_apk_tests($schema); #< older set of auto-pk tests
64 run_extended_apk_tests($schema); #< new extended set of auto-pk tests
68 my $test_type_info = {
70 'data_type' => 'integer',
75 'data_type' => 'character varying',
78 'default_value' => undef,
81 'data_type' => 'integer',
84 'default_value' => 13,
88 'data_type' => 'character',
91 'default_value' => undef,
94 'data_type' => 'integer[]',
97 'default_value' => undef,
101 my $type_info = $schema->storage->columns_info_for('dbic_t_schema.artist');
102 my $artistid_defval = delete $type_info->{artistid}->{default_value};
103 like($artistid_defval,
104 qr/^nextval\('([^\.]*\.){0,1}artist_artistid_seq'::(?:text|regclass)\)/,
105 'columns_info_for - sequence matches Pg get_autoinc_seq expectations');
106 is_deeply($type_info, $test_type_info,
107 'columns_info_for - column data types');
115 package DBICTest::Schema::ArrayTest;
116 push @main::test_classes, __PACKAGE__;
120 use base 'DBIx::Class';
122 __PACKAGE__->load_components(qw/Core/);
123 __PACKAGE__->table('dbic_t_schema.array_test');
124 __PACKAGE__->add_columns(qw/id arrayfield/);
125 __PACKAGE__->column_info_from_storage(1);
126 __PACKAGE__->set_primary_key('id');
130 skip "Need DBD::Pg 2.9.2 or newer for array tests", 4 if $DBD::Pg::VERSION < 2.009002;
133 $schema->resultset('ArrayTest')->create({
134 arrayfield => [1, 2],
136 } 'inserting arrayref as pg array data';
139 $schema->resultset('ArrayTest')->update({
140 arrayfield => [3, 4],
142 } 'updating arrayref as pg array data';
144 $schema->resultset('ArrayTest')->create({
145 arrayfield => [5, 6],
150 $count = $schema->resultset('ArrayTest')->search({
151 arrayfield => \[ '= ?' => [arrayfield => [3, 4]] ], #Todo anything less ugly than this?
153 } 'comparing arrayref to pg array data does not blow up';
154 is($count, 1, 'comparing arrayref to pg array data gives correct result');
159 ########## Case check
162 package DBICTest::Schema::Casecheck;
163 push @main::test_classes, __PACKAGE__;
167 use base 'DBIx::Class';
169 __PACKAGE__->load_components(qw/Core/);
170 __PACKAGE__->table('dbic_t_schema.casecheck');
171 __PACKAGE__->add_columns(qw/id name NAME uc_name storecolumn/);
172 __PACKAGE__->column_info_from_storage(1);
173 __PACKAGE__->set_primary_key('id');
176 my ($self, $name, $value) = @_;
177 $value = '#'.$value if($name eq "storecolumn");
178 $self->maybe::next::method($name, $value);
182 # store_column is called once for create() for non sequence columns
183 ok(my $storecolumn = $schema->resultset('Casecheck')->create({'storecolumn' => 'a'}));
184 is($storecolumn->storecolumn, '#a'); # was '##a'
186 my $name_info = $schema->source('Casecheck')->column_info( 'name' );
187 is( $name_info->{size}, 1, "Case sensitive matching info for 'name'" );
189 my $NAME_info = $schema->source('Casecheck')->column_info( 'NAME' );
190 is( $NAME_info->{size}, 2, "Case sensitive matching info for 'NAME'" );
192 my $uc_name_info = $schema->source('Casecheck')->column_info( 'uc_name' );
193 is( $uc_name_info->{size}, 3, "Case insensitive matching info for 'uc_name'" );
198 ## Test SELECT ... FOR UPDATE
200 my $HaveSysSigAction = eval "require Sys::SigAction" && !$@;
201 if( $HaveSysSigAction ) {
202 Sys::SigAction->import( 'set_sig_handler' );
205 skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
206 # create a new schema
207 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
208 $schema2->source("Artist")->name("dbic_t_schema.artist");
210 $schema->txn_do( sub {
211 my $artist = $schema->resultset('Artist')->search(
219 is($artist->artistid, 1, "select for update returns artistid = 1");
221 my $artist_from_schema2;
224 my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
226 $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
227 $artist_from_schema2->name('fooey');
228 $artist_from_schema2->update;
232 $error_ok = $e =~ /DBICTestTimeout/;
235 # Make sure that an error was raised, and that the update failed
236 ok($error_ok, "update from second schema times out");
237 ok($artist_from_schema2->is_column_changed('name'), "'name' column is still dirty from second schema");
242 skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
243 # create a new schema
244 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
245 $schema2->source("Artist")->name("dbic_t_schema.artist");
247 $schema->txn_do( sub {
248 my $artist = $schema->resultset('Artist')->search(
253 is($artist->artistid, 1, "select for update returns artistid = 1");
255 my $artist_from_schema2;
258 my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
260 $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
261 $artist_from_schema2->name('fooey');
262 $artist_from_schema2->update;
266 $error_ok = $e =~ /DBICTestTimeout/;
269 # Make sure that an error was NOT raised, and that the update succeeded
270 ok(! $error_ok, "update from second schema DOES NOT timeout");
271 ok(! $artist_from_schema2->is_column_changed('name'), "'name' column is NOT dirty from second schema");
276 ######## other older Auto-pk tests
278 $schema->source("SequenceTest")->name("dbic_t_schema.sequence_test");
280 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
281 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
282 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
283 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
285 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
286 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
293 drop_test_schema($schema);
294 eapk_drop_all( $schema)
298 ######### SUBROUTINES
300 sub create_test_schema {
302 $schema->storage->dbh_do(sub {
303 my (undef,$dbh) = @_;
305 local $dbh->{Warn} = 0;
307 my $std_artist_table = <<EOS;
309 artistid serial PRIMARY KEY
311 , rank INTEGER NOT NULL DEFAULT '13'
313 , arrayfield INTEGER[]
317 $dbh->do("CREATE SCHEMA dbic_t_schema");
318 $dbh->do("CREATE TABLE dbic_t_schema.artist $std_artist_table");
320 CREATE TABLE dbic_t_schema.sequence_test (
325 , CONSTRAINT pk PRIMARY KEY(pkid1, pkid2)
328 $dbh->do("CREATE SEQUENCE pkid1_seq START 1 MAXVALUE 999999 MINVALUE 0");
329 $dbh->do("CREATE SEQUENCE pkid2_seq START 10 MAXVALUE 999999 MINVALUE 0");
330 $dbh->do("CREATE SEQUENCE nonpkid_seq START 20 MAXVALUE 999999 MINVALUE 0");
332 CREATE TABLE dbic_t_schema.casecheck (
333 id serial PRIMARY KEY
336 , "UC_NAME" VARCHAR(3)
337 , "storecolumn" VARCHAR(10)
341 CREATE TABLE dbic_t_schema.array_test (
342 id serial PRIMARY KEY
343 , arrayfield INTEGER[]
346 $dbh->do("CREATE SCHEMA dbic_t_schema_2");
347 $dbh->do("CREATE TABLE dbic_t_schema_2.artist $std_artist_table");
348 $dbh->do("CREATE SCHEMA dbic_t_schema_3");
349 $dbh->do("CREATE TABLE dbic_t_schema_3.artist $std_artist_table");
350 $dbh->do('set search_path=dbic_t_schema,public');
351 $dbh->do("CREATE SCHEMA dbic_t_schema_4");
352 $dbh->do("CREATE SCHEMA dbic_t_schema_5");
354 CREATE TABLE dbic_t_schema_4.artist
356 artistid integer not null default nextval('artist_artistid_seq'::regclass) PRIMARY KEY
358 , rank INTEGER NOT NULL DEFAULT '13'
360 , arrayfield INTEGER[]
363 $dbh->do('set search_path=public,dbic_t_schema,dbic_t_schema_3');
364 $dbh->do('create sequence public.artist_artistid_seq'); #< in the public schema
366 CREATE TABLE dbic_t_schema_5.artist
368 artistid integer not null default nextval('public.artist_artistid_seq'::regclass) PRIMARY KEY
370 , rank INTEGER NOT NULL DEFAULT '13'
372 , arrayfield INTEGER[]
375 $dbh->do('set search_path=dbic_t_schema,public');
381 sub drop_test_schema {
382 my ( $schema, $no_warn ) = @_;
384 $schema->storage->dbh_do(sub {
385 my (undef,$dbh) = @_;
387 local $dbh->{Warn} = 0;
390 'DROP SCHEMA dbic_t_schema_5 CASCADE',
391 'DROP SEQUENCE public.artist_artistid_seq',
392 'DROP SCHEMA dbic_t_schema_4 CASCADE',
393 'DROP SCHEMA dbic_t_schema CASCADE',
394 'DROP SEQUENCE pkid1_seq',
395 'DROP SEQUENCE pkid2_seq',
396 'DROP SEQUENCE nonpkid_seq',
397 'DROP SCHEMA dbic_t_schema_2 CASCADE',
398 'DROP SCHEMA dbic_t_schema_3 CASCADE',
400 eval { $dbh->do ($stat) };
401 diag $@ if $@ && !$no_warn;
407 ### auto-pk / last_insert_id / sequence discovery
411 # This is in Core now, but it's here just to test that it doesn't break
412 $schema->class('Artist')->load_components('PK::Auto');
413 cmp_ok( $schema->resultset('Artist')->count, '==', 0, 'this should start with an empty artist table');
415 # test that auto-pk also works with the defined search path by
416 # un-schema-qualifying the table name
417 apk_t_set($schema,'artist');
421 $unq_new = $schema->resultset('Artist')->create({ name => 'baz' });
422 } 'insert into unqualified, shadowed table succeeds';
424 is($unq_new && $unq_new->artistid, 1, "and got correct artistid");
426 my @test_schemas = ( [qw| dbic_t_schema_2 1 |],
427 [qw| dbic_t_schema_3 1 |],
428 [qw| dbic_t_schema_4 2 |],
429 [qw| dbic_t_schema_5 1 |],
431 foreach my $t ( @test_schemas ) {
432 my ($sch_name, $start_num) = @$t;
433 #test with dbic_t_schema_2
434 apk_t_set($schema,"$sch_name.artist");
437 $another_new = $schema->resultset('Artist')->create({ name => 'Tollbooth Willy'});
438 is( $another_new->artistid,$start_num, "got correct artistid for $sch_name")
439 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
440 } "$sch_name liid 1 did not die"
441 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
443 $another_new = $schema->resultset('Artist')->create({ name => 'Adam Sandler'});
444 is( $another_new->artistid,$start_num+1, "got correct artistid for $sch_name")
445 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
446 } "$sch_name liid 2 did not die"
447 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
452 apk_t_set($schema,'dbic_t_schema.artist');
453 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
454 is($new->artistid, 4, "Auto-PK worked");
455 $new = $schema->resultset('Artist')->create({ name => 'bar' });
456 is($new->artistid, 5, "Auto-PK worked");
457 } 'old auto-pk tests did not die either';
460 # sets the artist table name and clears sequence name cache
463 $s->source("Artist")->name($n);
464 $s->source('Artist')->column_info('artistid')->{sequence} = undef; #< clear sequence name cache
468 ######## EXTENDED AUTO-PK TESTS
471 package DBICTest::Schema::ExtAPK;
472 push @main::test_classes, __PACKAGE__;
476 use base 'DBIx::Class';
478 __PACKAGE__->load_components(qw/Core/);
479 __PACKAGE__->table('apk_t');
481 __PACKAGE__->add_columns(
482 map { $_ => { data_type => 'integer', is_auto_increment => 1 } }
483 qw( id1 id2 id3 id4 )
486 __PACKAGE__->set_primary_key('id1');
490 BEGIN{ @apk_schemas = map "dbic_apk_$_", 0..5 }
492 sub run_extended_apk_tests {
495 eapk_drop_all($schema,'no warn');
497 # make the test schemas
498 $schema->storage->dbh_do(sub {
499 $_[1]->do("CREATE SCHEMA $_")
503 eapk_create($schema, with_search_path => [0,1]);
505 #unqualified table, unqualified
507 $schema->resultset('ExtAPK')->create({});
508 } 'create in first schema does not die';
513 my ($schema, %a) = @_;
515 $schema->storage->dbh_do(sub {
516 my (undef,$dbh) = @_;
519 if ( $a{with_search_path} ) {
520 ($searchpath_save) = $dbh->selectrow_array('SHOW search_path');
522 my $search_path = join ',',@apk_schemas[@{$a{with_search_path}}];
524 $dbh->do("SET search_path = $search_path");
527 my $schema = $a{qualify} ? "$a{qualify}." : '';
530 id1 serial primary key
537 if( $searchpath_save ) {
538 $dbh->do("SET search_path = $searchpath_save");
545 my ( $schema, $no_warn ) = @_;
547 $schema->storage->dbh_do(sub {
548 my (undef,$dbh) = @_;
550 local $dbh->{Warn} = 0;
552 # drop the test schemas
553 for (@apk_schemas ) {
554 eval{ $dbh->do("DROP SCHEMA $_ CASCADE") };
555 diag $@ if $@ && !$no_warn;