Make sure sqlt_type gets called after determining driver
[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{
10 package DBICTest::Schema::Casecheck;
11
12 use strict;
13 use warnings;
14 use base 'DBIx::Class';
15
3ff5b740 16 __PACKAGE__->load_components(qw/Core/);
9ba627e3 17 __PACKAGE__->table('testschema.casecheck');
c3af542a 18 __PACKAGE__->add_columns(qw/id name NAME uc_name storecolumn/);
d9916234 19 __PACKAGE__->column_info_from_storage(1);
89add887 20 __PACKAGE__->set_primary_key('id');
21
ece2b6ec 22 sub store_column {
c3af542a 23 my ($self, $name, $value) = @_;
24 $value = '#'.$value if($name eq "storecolumn");
25 $self->maybe::next::method($name, $value);
ece2b6ec 26 }
89add887 27}
28
9ba627e3 29{
30 package DBICTest::Schema::ArrayTest;
31
32 use strict;
33 use warnings;
34 use base 'DBIx::Class';
35
36 __PACKAGE__->load_components(qw/Core/);
37 __PACKAGE__->table('testschema.array_test');
38 __PACKAGE__->add_columns(qw/id arrayfield/);
39 __PACKAGE__->column_info_from_storage(1);
40 __PACKAGE__->set_primary_key('id');
41
42}
43
0567538f 44my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
45
9ba627e3 46plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test '.
47 '(note: This test drops and creates tables called \'artist\', \'casecheck\', \'array_test\' and \'sequence_test\''.
48 ' as well as following sequences: \'pkid1_seq\', \'pkid2_seq\' and \'nonpkid_seq\''.
f2b70f86 49 ' as well as following schemas: \'testschema\',\'anothertestschema\'!)'
64d5427c 50 unless ($dsn && $user);
0567538f 51
9ba627e3 52DBICTest::Schema->load_classes( 'Casecheck', 'ArrayTest' );
0567538f 53
eabde904 54{
55 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
56
57 ok (!$schema->storage->_dbh, 'definitely not connected');
58 is ($schema->storage->sqlt_type, 'PostgreSQL', 'sqlt_type correct pre-connection');
59}
60
61my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
114780ee 62# Check that datetime_parser returns correctly before we explicitly connect.
63SKIP: {
64 eval { require DateTime::Format::Pg };
65 skip "DateTime::Format::Pg required", 2 if $@;
66
67 my $store = ref $schema->storage;
68 is($store, 'DBIx::Class::Storage::DBI', 'Started with generic storage');
69
70 my $parser = $schema->storage->datetime_parser;
71 is( $parser, 'DateTime::Format::Pg', 'datetime_parser is as expected');
72}
73
3ff5b740 74my $dbh = $schema->storage->dbh;
75$schema->source("Artist")->name("testschema.artist");
39b8d119 76$schema->source("SequenceTest")->name("testschema.sequence_test");
bb452615 77{
78 local $SIG{__WARN__} = sub {};
7603d2a5 79 _cleanup ($dbh);
80
f2b70f86 81 my $artist_table_def = <<EOS;
82(
83 artistid serial PRIMARY KEY
84 , name VARCHAR(100)
85 , rank INTEGER NOT NULL DEFAULT '13'
86 , charfield CHAR(10)
87 , arrayfield INTEGER[]
88)
89EOS
bb452615 90 $dbh->do("CREATE SCHEMA testschema;");
f2b70f86 91 $dbh->do("CREATE TABLE testschema.artist $artist_table_def;");
bb452615 92 $dbh->do("CREATE TABLE testschema.sequence_test (pkid1 integer, pkid2 integer, nonpkid integer, name VARCHAR(100), CONSTRAINT pk PRIMARY KEY(pkid1, pkid2));");
93 $dbh->do("CREATE SEQUENCE pkid1_seq START 1 MAXVALUE 999999 MINVALUE 0");
94 $dbh->do("CREATE SEQUENCE pkid2_seq START 10 MAXVALUE 999999 MINVALUE 0");
95 $dbh->do("CREATE SEQUENCE nonpkid_seq START 20 MAXVALUE 999999 MINVALUE 0");
c3af542a 96 ok ( $dbh->do('CREATE TABLE testschema.casecheck (id serial PRIMARY KEY, "name" VARCHAR(1), "NAME" VARCHAR(2), "UC_NAME" VARCHAR(3), "storecolumn" VARCHAR(10));'), 'Creation of casecheck table');
9ba627e3 97 ok ( $dbh->do('CREATE TABLE testschema.array_test (id serial PRIMARY KEY, arrayfield INTEGER[]);'), 'Creation of array_test table');
f2b70f86 98 $dbh->do("CREATE SCHEMA anothertestschema;");
99 $dbh->do("CREATE TABLE anothertestschema.artist $artist_table_def;");
100 $dbh->do("CREATE SCHEMA yetanothertestschema;");
101 $dbh->do("CREATE TABLE yetanothertestschema.artist $artist_table_def;");
102 $dbh->do('set search_path=testschema,public');
bb452615 103}
0567538f 104
c3af542a 105# store_column is called once for create() for non sequence columns
106
107ok(my $storecolumn = $schema->resultset('Casecheck')->create({'storecolumn' => 'a'}));
108
109is($storecolumn->storecolumn, '#a'); # was '##a'
110
111
3ff5b740 112# This is in Core now, but it's here just to test that it doesn't break
113$schema->class('Artist')->load_components('PK::Auto');
0567538f 114
46bb5b38 115cmp_ok( $schema->resultset('Artist')->count, '==', 0, 'this should start with an empty artist table');
f2b70f86 116
46bb5b38 117{ # test that auto-pk also works with the defined search path by
118 # un-schema-qualifying the table name
f2b70f86 119 my $artist_name_save = $schema->source("Artist")->name;
120 $schema->source("Artist")->name("artist");
121
122 my $unq_new;
123 lives_ok {
124 $unq_new = $schema->resultset('Artist')->create({ name => 'baz' });
125 } 'insert into unqualified, shadowed table succeeds';
126
127 is($unq_new && $unq_new->artistid, 1, "and got correct artistid");
128
4617b792 129 #test with anothertestschema
130 $schema->source('Artist')->name('anothertestschema.artist');
131 my $another_new = $schema->resultset('Artist')->create({ name => 'ribasushi'});
132 is( $another_new->artistid,1, 'got correct artistid for yetanotherschema');
133
134 #test with yetanothertestschema
135 $schema->source('Artist')->name('yetanothertestschema.artist');
136 my $yetanother_new = $schema->resultset('Artist')->create({ name => 'ribasushi'});
137 is( $yetanother_new->artistid,1, 'got correct artistid for yetanotherschema');
138 is( $yetanother_new->artistid,1, 'got correct artistid for yetanotherschema');
139
f2b70f86 140 $schema->source("Artist")->name($artist_name_save);
141}
142
3ff5b740 143my $new = $schema->resultset('Artist')->create({ name => 'foo' });
0567538f 144
f2b70f86 145is($new->artistid, 2, "Auto-PK worked");
b6b65a3e 146
3ff5b740 147$new = $schema->resultset('Artist')->create({ name => 'bar' });
b6b65a3e 148
f2b70f86 149is($new->artistid, 3, "Auto-PK worked");
150
b6b65a3e 151
a953d8d9 152my $test_type_info = {
153 'artistid' => {
103e3e03 154 'data_type' => 'integer',
155 'is_nullable' => 0,
fc22fbac 156 'size' => 4,
a953d8d9 157 },
158 'name' => {
103e3e03 159 'data_type' => 'character varying',
160 'is_nullable' => 1,
ae515736 161 'size' => 100,
fc22fbac 162 'default_value' => undef,
103e3e03 163 },
85df19d7 164 'rank' => {
165 'data_type' => 'integer',
166 'is_nullable' => 0,
167 'size' => 4,
168 'default_value' => 13,
169
170 },
103e3e03 171 'charfield' => {
172 'data_type' => 'character',
a953d8d9 173 'is_nullable' => 1,
fc22fbac 174 'size' => 10,
175 'default_value' => undef,
103e3e03 176 },
9ba627e3 177 'arrayfield' => {
178 'data_type' => 'integer[]',
179 'is_nullable' => 1,
180 'size' => undef,
181 'default_value' => undef,
182 },
a953d8d9 183};
184
fc22fbac 185
3ff5b740 186my $type_info = $schema->storage->columns_info_for('testschema.artist');
fc22fbac 187my $artistid_defval = delete $type_info->{artistid}->{default_value};
188like($artistid_defval,
4d272ce5 189 qr/^nextval\('([^\.]*\.){0,1}artist_artistid_seq'::(?:text|regclass)\)/,
fc22fbac 190 'columns_info_for - sequence matches Pg get_autoinc_seq expectations');
191is_deeply($type_info, $test_type_info,
192 'columns_info_for - column data types');
a953d8d9 193
ac45262b 194SKIP: {
195 skip "Need DBD::Pg 2.9.2 or newer for array tests", 4 if $DBD::Pg::VERSION < 2.009002;
196
9ba627e3 197 lives_ok {
198 $schema->resultset('ArrayTest')->create({
199 arrayfield => [1, 2],
200 });
201 } 'inserting arrayref as pg array data';
202
203 lives_ok {
204 $schema->resultset('ArrayTest')->update({
205 arrayfield => [3, 4],
206 });
207 } 'updating arrayref as pg array data';
c224117b 208
209 $schema->resultset('ArrayTest')->create({
210 arrayfield => [5, 6],
211 });
212
213 my $count;
214 lives_ok {
215 $count = $schema->resultset('ArrayTest')->search({
aab0d3b7 216 arrayfield => \[ '= ?' => [arrayfield => [3, 4]] ], #Todo anything less ugly than this?
c224117b 217 })->count;
218 } 'comparing arrayref to pg array data does not blow up';
219 is($count, 1, 'comparing arrayref to pg array data gives correct result');
9ba627e3 220}
221
222
3ff5b740 223my $name_info = $schema->source('Casecheck')->column_info( 'name' );
ae515736 224is( $name_info->{size}, 1, "Case sensitive matching info for 'name'" );
225
3ff5b740 226my $NAME_info = $schema->source('Casecheck')->column_info( 'NAME' );
ae515736 227is( $NAME_info->{size}, 2, "Case sensitive matching info for 'NAME'" );
228
3ff5b740 229my $uc_name_info = $schema->source('Casecheck')->column_info( 'uc_name' );
ae515736 230is( $uc_name_info->{size}, 3, "Case insensitive matching info for 'uc_name'" );
231
95ba7ee4 232# Test SELECT ... FOR UPDATE
233my $HaveSysSigAction = eval "require Sys::SigAction" && !$@;
234if ($HaveSysSigAction) {
235 Sys::SigAction->import( 'set_sig_handler' );
236}
237
238SKIP: {
239 skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
240 # create a new schema
241 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
242 $schema2->source("Artist")->name("testschema.artist");
243
244 $schema->txn_do( sub {
245 my $artist = $schema->resultset('Artist')->search(
246 {
247 artistid => 1
248 },
249 {
250 for => 'update'
251 }
252 )->first;
253 is($artist->artistid, 1, "select for update returns artistid = 1");
254
255 my $artist_from_schema2;
256 my $error_ok = 0;
257 eval {
258 my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
259 alarm(2);
260 $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
261 $artist_from_schema2->name('fooey');
262 $artist_from_schema2->update;
263 alarm(0);
264 };
265 if (my $e = $@) {
266 $error_ok = $e =~ /DBICTestTimeout/;
267 }
268
269 # Make sure that an error was raised, and that the update failed
270 ok($error_ok, "update from second schema times out");
271 ok($artist_from_schema2->is_column_changed('name'), "'name' column is still dirty from second schema");
272 });
273}
274
275SKIP: {
276 skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
277 # create a new schema
278 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
279 $schema2->source("Artist")->name("testschema.artist");
280
281 $schema->txn_do( sub {
282 my $artist = $schema->resultset('Artist')->search(
283 {
284 artistid => 1
285 },
286 )->first;
287 is($artist->artistid, 1, "select for update returns artistid = 1");
288
289 my $artist_from_schema2;
290 my $error_ok = 0;
291 eval {
292 my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
293 alarm(2);
294 $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
295 $artist_from_schema2->name('fooey');
296 $artist_from_schema2->update;
297 alarm(0);
298 };
299 if (my $e = $@) {
300 $error_ok = $e =~ /DBICTestTimeout/;
301 }
302
303 # Make sure that an error was NOT raised, and that the update succeeded
304 ok(! $error_ok, "update from second schema DOES NOT timeout");
305 ok(! $artist_from_schema2->is_column_changed('name'), "'name' column is NOT dirty from second schema");
306 });
307}
308
d093d3eb 309for (1..5) {
39b8d119 310 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
311 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
312 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
313 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
d093d3eb 314}
315my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
316is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
317
7603d2a5 318sub _cleanup {
319 my $dbh = shift or return;
320
321 for my $stat (
322 'DROP TABLE testschema.artist',
323 'DROP TABLE testschema.casecheck',
324 'DROP TABLE testschema.sequence_test',
325 'DROP TABLE testschema.array_test',
326 'DROP SEQUENCE pkid1_seq',
327 'DROP SEQUENCE pkid2_seq',
328 'DROP SEQUENCE nonpkid_seq',
329 'DROP SCHEMA testschema',
f2b70f86 330 'DROP TABLE anothertestschema.artist',
331 'DROP SCHEMA anothertestschema',
332 'DROP TABLE yetanothertestschema.artist',
333 'DROP SCHEMA yetanothertestschema',
7603d2a5 334 ) {
335 eval { $dbh->do ($stat) };
336 }
3ff5b740 337}
7603d2a5 338
7ff926e6 339done_testing;
340
7603d2a5 341END { _cleanup($dbh) }