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: 'testschema', 'anothertestschema', 'yetanothertestschema',
18 'unq_nextval_schema', and 'unq_nextval_schema2'
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);
54 my $dbh = $schema->storage->dbh;
56 drop_test_schema($schema, 'no warn');
57 create_test_schema($schema);
61 ### auto-pk / last_insert_id / sequence discovery
63 $schema->source("Artist")->name("testschema.artist");
65 # This is in Core now, but it's here just to test that it doesn't break
66 $schema->class('Artist')->load_components('PK::Auto');
67 cmp_ok( $schema->resultset('Artist')->count, '==', 0, 'this should start with an empty artist table');
69 # test that auto-pk also works with the defined search path by
70 # un-schema-qualifying the table name
71 my $artist_name_save = $schema->source("Artist")->name;
72 $schema->source("Artist")->name("artist");
76 $unq_new = $schema->resultset('Artist')->create({ name => 'baz' });
77 } 'insert into unqualified, shadowed table succeeds';
79 is($unq_new && $unq_new->artistid, 1, "and got correct artistid");
81 my @test_schemas = ( [qw| anothertestschema 1 |],
82 [qw| yetanothertestschema 1 |],
84 foreach my $t ( @test_schemas ) {
85 my ($sch_name, $start_num) = @$t;
86 #test with anothertestschema
87 $schema->source('Artist')->name("$sch_name.artist");
88 $schema->source('Artist')->column_info('artistid')->{sequence} = undef; #< clear sequence name cache
91 $another_new = $schema->resultset('Artist')->create({ name => 'Tollbooth Willy'});
92 is( $another_new->artistid,$start_num, "got correct artistid for $sch_name")
93 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
94 } "$sch_name liid 1 did not die"
95 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
97 $another_new = $schema->resultset('Artist')->create({ name => 'Adam Sandler'});
98 is( $another_new->artistid,$start_num+1, "got correct artistid for $sch_name")
99 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
100 } "$sch_name liid 2 did not die"
101 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
107 [qw| unq_nextval_schema 2 |],
108 [qw| unq_nextval_schema2 1 |],
111 foreach my $t ( @todo_schemas ) {
112 my ($sch_name, $start_num) = @$t;
114 #test with anothertestschema
115 $schema->source('Artist')->name("$sch_name.artist");
116 $schema->source('Artist')->column_info('artistid')->{sequence} = undef; #< clear sequence name cache
119 $another_new = $schema->resultset('Artist')->create({ name => 'Tollbooth Willy'});
120 is( $another_new->artistid,$start_num, "got correct artistid for $sch_name")
121 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
122 } "$sch_name liid 1 did not die"
123 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
126 $another_new = $schema->resultset('Artist')->create({ name => 'Adam Sandler'});
127 is( $another_new->artistid,$start_num+1, "got correct artistid for $sch_name")
128 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
129 } "$sch_name liid 2 did not die"
130 or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>');
133 $schema->source('Artist')->column_info('artistid')->{sequence} = undef; #< clear sequence name cache
134 $schema->source("Artist")->name($artist_name_save);
138 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
139 is($new->artistid, 4, "Auto-PK worked");
140 $new = $schema->resultset('Artist')->create({ name => 'bar' });
141 is($new->artistid, 5, "Auto-PK worked");
142 } 'old auto-pk tests did not die either';
148 my $test_type_info = {
150 'data_type' => 'integer',
155 'data_type' => 'character varying',
158 'default_value' => undef,
161 'data_type' => 'integer',
164 'default_value' => 13,
168 'data_type' => 'character',
171 'default_value' => undef,
174 'data_type' => 'integer[]',
177 'default_value' => undef,
181 my $type_info = $schema->storage->columns_info_for('testschema.artist');
182 my $artistid_defval = delete $type_info->{artistid}->{default_value};
183 like($artistid_defval,
184 qr/^nextval\('([^\.]*\.){0,1}artist_artistid_seq'::(?:text|regclass)\)/,
185 'columns_info_for - sequence matches Pg get_autoinc_seq expectations');
186 is_deeply($type_info, $test_type_info,
187 'columns_info_for - column data types');
195 package DBICTest::Schema::ArrayTest;
196 push @main::test_classes, __PACKAGE__;
200 use base 'DBIx::Class';
202 __PACKAGE__->load_components(qw/Core/);
203 __PACKAGE__->table('testschema.array_test');
204 __PACKAGE__->add_columns(qw/id arrayfield/);
205 __PACKAGE__->column_info_from_storage(1);
206 __PACKAGE__->set_primary_key('id');
210 skip "Need DBD::Pg 2.9.2 or newer for array tests", 4 if $DBD::Pg::VERSION < 2.009002;
213 $schema->resultset('ArrayTest')->create({
214 arrayfield => [1, 2],
216 } 'inserting arrayref as pg array data';
219 $schema->resultset('ArrayTest')->update({
220 arrayfield => [3, 4],
222 } 'updating arrayref as pg array data';
224 $schema->resultset('ArrayTest')->create({
225 arrayfield => [5, 6],
230 $count = $schema->resultset('ArrayTest')->search({
231 arrayfield => \[ '= ?' => [arrayfield => [3, 4]] ], #Todo anything less ugly than this?
233 } 'comparing arrayref to pg array data does not blow up';
234 is($count, 1, 'comparing arrayref to pg array data gives correct result');
239 ########## Case check
242 package DBICTest::Schema::Casecheck;
243 push @main::test_classes, __PACKAGE__;
247 use base 'DBIx::Class';
249 __PACKAGE__->load_components(qw/Core/);
250 __PACKAGE__->table('testschema.casecheck');
251 __PACKAGE__->add_columns(qw/id name NAME uc_name storecolumn/);
252 __PACKAGE__->column_info_from_storage(1);
253 __PACKAGE__->set_primary_key('id');
256 my ($self, $name, $value) = @_;
257 $value = '#'.$value if($name eq "storecolumn");
258 $self->maybe::next::method($name, $value);
262 # store_column is called once for create() for non sequence columns
263 ok(my $storecolumn = $schema->resultset('Casecheck')->create({'storecolumn' => 'a'}));
264 is($storecolumn->storecolumn, '#a'); # was '##a'
266 my $name_info = $schema->source('Casecheck')->column_info( 'name' );
267 is( $name_info->{size}, 1, "Case sensitive matching info for 'name'" );
269 my $NAME_info = $schema->source('Casecheck')->column_info( 'NAME' );
270 is( $NAME_info->{size}, 2, "Case sensitive matching info for 'NAME'" );
272 my $uc_name_info = $schema->source('Casecheck')->column_info( 'uc_name' );
273 is( $uc_name_info->{size}, 3, "Case insensitive matching info for 'uc_name'" );
278 ## Test SELECT ... FOR UPDATE
280 my $HaveSysSigAction = eval "require Sys::SigAction" && !$@;
281 if( $HaveSysSigAction ) {
282 Sys::SigAction->import( 'set_sig_handler' );
285 skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
286 # create a new schema
287 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
288 $schema2->source("Artist")->name("testschema.artist");
290 $schema->txn_do( sub {
291 my $artist = $schema->resultset('Artist')->search(
299 is($artist->artistid, 1, "select for update returns artistid = 1");
301 my $artist_from_schema2;
304 my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
306 $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
307 $artist_from_schema2->name('fooey');
308 $artist_from_schema2->update;
312 $error_ok = $e =~ /DBICTestTimeout/;
315 # Make sure that an error was raised, and that the update failed
316 ok($error_ok, "update from second schema times out");
317 ok($artist_from_schema2->is_column_changed('name'), "'name' column is still dirty from second schema");
322 skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
323 # create a new schema
324 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
325 $schema2->source("Artist")->name("testschema.artist");
327 $schema->txn_do( sub {
328 my $artist = $schema->resultset('Artist')->search(
333 is($artist->artistid, 1, "select for update returns artistid = 1");
335 my $artist_from_schema2;
338 my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
340 $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
341 $artist_from_schema2->name('fooey');
342 $artist_from_schema2->update;
346 $error_ok = $e =~ /DBICTestTimeout/;
349 # Make sure that an error was NOT raised, and that the update succeeded
350 ok(! $error_ok, "update from second schema DOES NOT timeout");
351 ok(! $artist_from_schema2->is_column_changed('name'), "'name' column is NOT dirty from second schema");
356 ######## other Auto-pk tests
358 $schema->source("SequenceTest")->name("testschema.sequence_test");
360 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
361 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
362 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
363 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
365 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
366 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
371 END { drop_test_schema($schema) }
374 ######### SUBROUTINES
376 sub create_test_schema {
378 $schema->storage->dbh_do(sub {
379 my (undef,$dbh) = @_;
381 local $dbh->{Warn} = 0;
383 my $std_artist_table = <<EOS;
385 artistid serial PRIMARY KEY
387 , rank INTEGER NOT NULL DEFAULT '13'
389 , arrayfield INTEGER[]
393 $dbh->do("CREATE SCHEMA testschema");
394 $dbh->do("CREATE TABLE testschema.artist $std_artist_table");
396 CREATE TABLE testschema.sequence_test (
401 , CONSTRAINT pk PRIMARY KEY(pkid1, pkid2)
404 $dbh->do("CREATE SEQUENCE pkid1_seq START 1 MAXVALUE 999999 MINVALUE 0");
405 $dbh->do("CREATE SEQUENCE pkid2_seq START 10 MAXVALUE 999999 MINVALUE 0");
406 $dbh->do("CREATE SEQUENCE nonpkid_seq START 20 MAXVALUE 999999 MINVALUE 0");
408 CREATE TABLE testschema.casecheck (
409 id serial PRIMARY KEY
412 , "UC_NAME" VARCHAR(3)
413 , "storecolumn" VARCHAR(10)
417 CREATE TABLE testschema.array_test (
418 id serial PRIMARY KEY
419 , arrayfield INTEGER[]
422 $dbh->do("CREATE SCHEMA anothertestschema");
423 $dbh->do("CREATE TABLE anothertestschema.artist $std_artist_table");
424 $dbh->do("CREATE SCHEMA yetanothertestschema");
425 $dbh->do("CREATE TABLE yetanothertestschema.artist $std_artist_table");
426 $dbh->do('set search_path=testschema,public');
427 $dbh->do("CREATE SCHEMA unq_nextval_schema");
428 $dbh->do("CREATE SCHEMA unq_nextval_schema2");
430 CREATE TABLE unq_nextval_schema.artist
432 artistid integer not null default nextval('artist_artistid_seq'::regclass) PRIMARY KEY
434 , rank INTEGER NOT NULL DEFAULT '13'
436 , arrayfield INTEGER[]
439 $dbh->do('set search_path=public,testschema,yetanothertestschema');
440 $dbh->do('create sequence public.artist_artistid_seq'); #< in the public schema
442 CREATE TABLE unq_nextval_schema2.artist
444 artistid integer not null default nextval('public.artist_artistid_seq'::regclass) PRIMARY KEY
446 , rank INTEGER NOT NULL DEFAULT '13'
448 , arrayfield INTEGER[]
451 $dbh->do('set search_path=testschema,public');
457 sub drop_test_schema {
458 my ( $schema, $no_warn ) = @_;
460 $schema->storage->dbh_do(sub {
461 my (undef,$dbh) = @_;
463 local $dbh->{Warn} = 0;
466 'DROP SCHEMA unq_nextval_schema2 CASCADE',
467 'DROP SEQUENCE public.artist_artistid_seq',
468 'DROP SCHEMA unq_nextval_schema CASCADE',
469 'DROP SCHEMA testschema CASCADE',
470 'DROP SEQUENCE pkid1_seq',
471 'DROP SEQUENCE pkid2_seq',
472 'DROP SEQUENCE nonpkid_seq',
473 'DROP SCHEMA anothertestschema CASCADE',
474 'DROP SCHEMA yetanothertestschema CASCADE',
476 eval { $dbh->do ($stat) };
477 diag $@ if $@ && !$no_warn;