10 package DBICTest::Schema::Casecheck;
14 use base 'DBIx::Class';
16 __PACKAGE__->load_components(qw/Core/);
17 __PACKAGE__->table('testschema.casecheck');
18 __PACKAGE__->add_columns(qw/id name NAME uc_name/);
19 __PACKAGE__->column_info_from_storage(1);
20 __PACKAGE__->set_primary_key('id');
25 package DBICTest::Schema::ArrayTest;
29 use base 'DBIx::Class';
31 __PACKAGE__->load_components(qw/Core/);
32 __PACKAGE__->table('testschema.array_test');
33 __PACKAGE__->add_columns(qw/id arrayfield/);
34 __PACKAGE__->column_info_from_storage(1);
35 __PACKAGE__->set_primary_key('id');
39 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
41 #warn "$dsn $user $pass";
43 plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test '.
44 '(note: This test drops and creates tables called \'artist\', \'casecheck\', \'array_test\' and \'sequence_test\''.
45 ' as well as following sequences: \'pkid1_seq\', \'pkid2_seq\' and \'nonpkid_seq\''.
46 ' as well as following schemas: \'testschema\'!)'
47 unless ($dsn && $user && $pass);
52 DBICTest::Schema->load_classes( 'Casecheck', 'ArrayTest' );
53 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
55 # Check that datetime_parser returns correctly before we explicitly connect.
57 eval { require DateTime::Format::Pg };
58 skip "DateTime::Format::Pg required", 2 if $@;
60 my $store = ref $schema->storage;
61 is($store, 'DBIx::Class::Storage::DBI', 'Started with generic storage');
63 my $parser = $schema->storage->datetime_parser;
64 is( $parser, 'DateTime::Format::Pg', 'datetime_parser is as expected');
67 my $dbh = $schema->storage->dbh;
68 $schema->source("Artist")->name("testschema.artist");
69 $schema->source("SequenceTest")->name("testschema.sequence_test");
71 local $SIG{__WARN__} = sub {};
72 $dbh->do("CREATE SCHEMA testschema;");
73 $dbh->do("CREATE TABLE testschema.artist (artistid serial PRIMARY KEY, name VARCHAR(100), rank INTEGER NOT NULL DEFAULT '13', charfield CHAR(10), arrayfield INTEGER[]);");
74 $dbh->do("CREATE TABLE testschema.sequence_test (pkid1 integer, pkid2 integer, nonpkid integer, name VARCHAR(100), CONSTRAINT pk PRIMARY KEY(pkid1, pkid2));");
75 $dbh->do("CREATE SEQUENCE pkid1_seq START 1 MAXVALUE 999999 MINVALUE 0");
76 $dbh->do("CREATE SEQUENCE pkid2_seq START 10 MAXVALUE 999999 MINVALUE 0");
77 $dbh->do("CREATE SEQUENCE nonpkid_seq START 20 MAXVALUE 999999 MINVALUE 0");
78 ok ( $dbh->do('CREATE TABLE testschema.casecheck (id serial PRIMARY KEY, "name" VARCHAR(1), "NAME" VARCHAR(2), "UC_NAME" VARCHAR(3));'), 'Creation of casecheck table');
79 ok ( $dbh->do('CREATE TABLE testschema.array_test (id serial PRIMARY KEY, arrayfield INTEGER[]);'), 'Creation of array_test table');
82 # This is in Core now, but it's here just to test that it doesn't break
83 $schema->class('Artist')->load_components('PK::Auto');
85 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
87 is($new->artistid, 1, "Auto-PK worked");
89 $new = $schema->resultset('Artist')->create({ name => 'bar' });
91 is($new->artistid, 2, "Auto-PK worked");
93 my $test_type_info = {
95 'data_type' => 'integer',
100 'data_type' => 'character varying',
103 'default_value' => undef,
106 'data_type' => 'integer',
109 'default_value' => 13,
113 'data_type' => 'character',
116 'default_value' => undef,
119 'data_type' => 'integer[]',
122 'default_value' => undef,
127 my $type_info = $schema->storage->columns_info_for('testschema.artist');
128 my $artistid_defval = delete $type_info->{artistid}->{default_value};
129 like($artistid_defval,
130 qr/^nextval\('([^\.]*\.){0,1}artist_artistid_seq'::(?:text|regclass)\)/,
131 'columns_info_for - sequence matches Pg get_autoinc_seq expectations');
132 is_deeply($type_info, $test_type_info,
133 'columns_info_for - column data types');
136 skip "SQL::Abstract < 1.49 does not pass through arrayrefs", 4
137 if $SQL::Abstract::VERSION < 1.49;
140 $schema->resultset('ArrayTest')->create({
141 arrayfield => [1, 2],
143 } 'inserting arrayref as pg array data';
146 $schema->resultset('ArrayTest')->update({
147 arrayfield => [3, 4],
149 } 'updating arrayref as pg array data';
151 $schema->resultset('ArrayTest')->create({
152 arrayfield => [5, 6],
157 $count = $schema->resultset('ArrayTest')->search({
158 arrayfield => \[ '= ?' => [3, 4] ], #TODO anything less ugly than this?
160 } 'comparing arrayref to pg array data does not blow up';
161 is($count, 1, 'comparing arrayref to pg array data gives correct result');
165 my $name_info = $schema->source('Casecheck')->column_info( 'name' );
166 is( $name_info->{size}, 1, "Case sensitive matching info for 'name'" );
168 my $NAME_info = $schema->source('Casecheck')->column_info( 'NAME' );
169 is( $NAME_info->{size}, 2, "Case sensitive matching info for 'NAME'" );
171 my $uc_name_info = $schema->source('Casecheck')->column_info( 'uc_name' );
172 is( $uc_name_info->{size}, 3, "Case insensitive matching info for 'uc_name'" );
174 # Test SELECT ... FOR UPDATE
175 my $HaveSysSigAction = eval "require Sys::SigAction" && !$@;
176 if ($HaveSysSigAction) {
177 Sys::SigAction->import( 'set_sig_handler' );
181 skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
182 # create a new schema
183 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
184 $schema2->source("Artist")->name("testschema.artist");
186 $schema->txn_do( sub {
187 my $artist = $schema->resultset('Artist')->search(
195 is($artist->artistid, 1, "select for update returns artistid = 1");
197 my $artist_from_schema2;
200 my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
202 $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
203 $artist_from_schema2->name('fooey');
204 $artist_from_schema2->update;
208 $error_ok = $e =~ /DBICTestTimeout/;
211 # Make sure that an error was raised, and that the update failed
212 ok($error_ok, "update from second schema times out");
213 ok($artist_from_schema2->is_column_changed('name'), "'name' column is still dirty from second schema");
218 skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
219 # create a new schema
220 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
221 $schema2->source("Artist")->name("testschema.artist");
223 $schema->txn_do( sub {
224 my $artist = $schema->resultset('Artist')->search(
229 is($artist->artistid, 1, "select for update returns artistid = 1");
231 my $artist_from_schema2;
234 my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
236 $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
237 $artist_from_schema2->name('fooey');
238 $artist_from_schema2->update;
242 $error_ok = $e =~ /DBICTestTimeout/;
245 # Make sure that an error was NOT raised, and that the update succeeded
246 ok(! $error_ok, "update from second schema DOES NOT timeout");
247 ok(! $artist_from_schema2->is_column_changed('name'), "'name' column is NOT dirty from second schema");
252 skip "Oracle Auto-PK tests are broken", 16;
254 # test auto increment using sequences WITHOUT triggers
256 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
257 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
258 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
259 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
261 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
262 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
267 $dbh->do("DROP TABLE testschema.artist;");
268 $dbh->do("DROP TABLE testschema.casecheck;");
269 $dbh->do("DROP TABLE testschema.sequence_test;");
270 $dbh->do("DROP TABLE testschema.array_test;");
271 $dbh->do("DROP SEQUENCE pkid1_seq");
272 $dbh->do("DROP SEQUENCE pkid2_seq");
273 $dbh->do("DROP SEQUENCE nonpkid_seq");
274 $dbh->do("DROP SCHEMA testschema;");