Commit | Line | Data |
70350518 |
1 | use strict; |
4617b792 |
2 | use warnings; |
70350518 |
3 | |
4 | use Test::More; |
2131aa2e |
5 | use Test::Warn; |
9ba627e3 |
6 | use Test::Exception; |
bbdda281 |
7 | use Sub::Name; |
bb961936 |
8 | use Config; |
199fbc45 |
9 | use DBIx::Class::Optional::Dependencies (); |
70350518 |
10 | use lib qw(t/lib); |
11 | use DBICTest; |
b5ce6748 |
12 | use SQL::Abstract 'is_literal_value'; |
bb961936 |
13 | use DBIx::Class::_Util 'is_exception'; |
70350518 |
14 | |
199fbc45 |
15 | plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_pg') |
16 | unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_pg'); |
89add887 |
17 | |
0567538f |
18 | my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/}; |
19 | |
67b79ae1 |
20 | plan skip_all => <<'EOM' unless $dsn && $user; |
21 | Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test |
bab40dee |
22 | ( NOTE: This test drops and creates tables called 'artist', 'cd', |
23 | 'timestamp_primary_key_test', 'track', 'casecheck', 'array_test' and |
24 | 'sequence_test' as well as following sequences: 'pkid1_seq', 'pkid2_seq' and |
67b79ae1 |
25 | 'nonpkid_seq'. as well as following schemas: 'dbic_t_schema', |
bab40dee |
26 | 'dbic_t_schema_2', 'dbic_t_schema_3', 'dbic_t_schema_4', and 'dbic_t_schema_5') |
609c5f1b |
27 | EOM |
0567538f |
28 | |
52c53388 |
29 | ### load any test classes that are defined further down in the file via BEGIN blocks |
efd38994 |
30 | |
70a201a5 |
31 | our @test_classes; #< array that will be pushed into by test classes defined in this file |
2c2bc4e5 |
32 | require DBICTest::Schema; |
70a201a5 |
33 | DBICTest::Schema->load_classes( map {s/.+:://;$_} @test_classes ) if @test_classes; |
efd38994 |
34 | |
52c53388 |
35 | ### pre-connect tests (keep each test separate as to make sure rebless() runs) |
bab40dee |
36 | { |
2c2bc4e5 |
37 | my $s = DBICTest->connect_schema($dsn, $user, $pass); |
eabde904 |
38 | |
bab40dee |
39 | ok (!$s->storage->_dbh, 'definitely not connected'); |
eabde904 |
40 | |
bab40dee |
41 | # Check that datetime_parser returns correctly before we explicitly connect. |
42 | SKIP: { |
68de9438 |
43 | skip ( |
44 | "Pg parser detection test needs " . DBIx::Class::Optional::Dependencies->req_missing_for ('test_dt_pg'), |
45 | 2 |
46 | ) unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_dt_pg'); |
114780ee |
47 | |
bab40dee |
48 | my $store = ref $s->storage; |
49 | is($store, 'DBIx::Class::Storage::DBI', 'Started with generic storage'); |
114780ee |
50 | |
bab40dee |
51 | my $parser = $s->storage->datetime_parser; |
52 | is( $parser, 'DateTime::Format::Pg', 'datetime_parser is as expected'); |
53 | } |
114780ee |
54 | |
bab40dee |
55 | ok (!$s->storage->_dbh, 'still not connected'); |
56 | } |
bbdda281 |
57 | |
bab40dee |
58 | { |
2c2bc4e5 |
59 | my $s = DBICTest->connect_schema($dsn, $user, $pass); |
bab40dee |
60 | # make sure sqlt_type overrides work (::Storage::DBI::Pg does this) |
61 | ok (!$s->storage->_dbh, 'definitely not connected'); |
62 | is ($s->storage->sqlt_type, 'PostgreSQL', 'sqlt_type correct pre-connection'); |
63 | ok (!$s->storage->_dbh, 'still not connected'); |
64 | } |
0567538f |
65 | |
97277745 |
66 | # test LIMIT support |
67 | { |
2c2bc4e5 |
68 | my $schema = DBICTest->connect_schema($dsn, $user, $pass); |
97277745 |
69 | drop_test_schema($schema); |
70 | create_test_schema($schema); |
71 | for (1..6) { |
72 | $schema->resultset('Artist')->create({ name => 'Artist ' . $_ }); |
73 | } |
74 | my $it = $schema->resultset('Artist')->search( {}, |
75 | { rows => 3, |
76 | offset => 2, |
77 | order_by => 'artistid' } |
78 | ); |
79 | is( $it->count, 3, "LIMIT count ok" ); # ask for 3 rows out of 6 artists |
80 | is( $it->next->name, "Artist 3", "iterator->next ok" ); |
81 | $it->next; |
82 | $it->next; |
83 | $it->next; |
84 | is( $it->next, undef, "next past end of resultset ok" ); |
e5372da4 |
85 | |
86 | # Limit with select-lock |
87 | lives_ok { |
88 | $schema->txn_do (sub { |
89 | isa_ok ( |
90 | $schema->resultset('Artist')->find({artistid => 1}, {for => 'update', rows => 1}), |
91 | 'DBICTest::Schema::Artist', |
92 | ); |
93 | }); |
94 | } 'Limited FOR UPDATE select works'; |
97277745 |
95 | } |
96 | |
bbdda281 |
97 | # check if we indeed do support stuff |
98 | my $test_server_supports_insert_returning = do { |
af1f4f84 |
99 | |
2c2bc4e5 |
100 | my $si = DBICTest->connect_schema($dsn, $user, $pass)->storage->_server_info; |
af1f4f84 |
101 | die "Unparseable Pg server version: $si->{dbms_version}\n" |
102 | unless $si->{normalized_dbms_version}; |
103 | |
104 | $si->{normalized_dbms_version} < 8.002 ? 0 : 1; |
bbdda281 |
105 | }; |
106 | is ( |
2c2bc4e5 |
107 | DBICTest->connect_schema($dsn, $user, $pass)->storage->_use_insert_returning, |
bbdda281 |
108 | $test_server_supports_insert_returning, |
109 | 'insert returning capability guessed correctly' |
110 | ); |
111 | |
112 | my $schema; |
113 | for my $use_insert_returning ($test_server_supports_insert_returning |
114 | ? (0,1) |
115 | : (0) |
116 | ) { |
117 | |
6892eb09 |
118 | no warnings qw/once redefine/; |
119 | my $old_connection = DBICTest::Schema->can('connection'); |
bbdda281 |
120 | local *DBICTest::Schema::connection = subname 'DBICTest::Schema::connection' => sub { |
6892eb09 |
121 | my $s = shift->$old_connection(@_); |
bbdda281 |
122 | $s->storage->_use_insert_returning ($use_insert_returning); |
123 | $s; |
124 | }; |
125 | |
126 | ### test capability override |
127 | { |
2c2bc4e5 |
128 | my $s = DBICTest->connect_schema($dsn, $user, $pass); |
bbdda281 |
129 | |
130 | ok (!$s->storage->_dbh, 'definitely not connected'); |
131 | |
132 | ok ( |
133 | ! ($s->storage->_use_insert_returning xor $use_insert_returning), |
134 | 'insert returning capability set correctly', |
135 | ); |
136 | ok (!$s->storage->_dbh, 'still not connected (capability override works)'); |
137 | } |
138 | |
70a201a5 |
139 | ### connect, create postgres-specific test schema |
c3af542a |
140 | |
2c2bc4e5 |
141 | $schema = DBICTest->connect_schema($dsn, $user, $pass); |
1f5aae08 |
142 | $schema->storage->ensure_connected; |
143 | |
bab40dee |
144 | drop_test_schema($schema); |
145 | create_test_schema($schema); |
609c5f1b |
146 | |
70a201a5 |
147 | ### begin main tests |
4617b792 |
148 | |
372da819 |
149 | # run a BIG bunch of tests for last-insert-id / Auto-PK / sequence |
150 | # discovery |
bab40dee |
151 | run_apk_tests($schema); #< older set of auto-pk tests |
152 | run_extended_apk_tests($schema); #< new extended set of auto-pk tests |
70a201a5 |
153 | |
bab40dee |
154 | ### type_info tests |
52c53388 |
155 | |
bab40dee |
156 | my $test_type_info = { |
157 | 'artistid' => { |
158 | 'data_type' => 'integer', |
159 | 'is_nullable' => 0, |
160 | 'size' => 4, |
161 | }, |
162 | 'name' => { |
163 | 'data_type' => 'character varying', |
164 | 'is_nullable' => 1, |
165 | 'size' => 100, |
166 | 'default_value' => undef, |
167 | }, |
168 | 'rank' => { |
169 | 'data_type' => 'integer', |
170 | 'is_nullable' => 0, |
171 | 'size' => 4, |
172 | 'default_value' => 13, |
52c53388 |
173 | |
bab40dee |
174 | }, |
175 | 'charfield' => { |
176 | 'data_type' => 'character', |
177 | 'is_nullable' => 1, |
178 | 'size' => 10, |
179 | 'default_value' => undef, |
180 | }, |
181 | 'arrayfield' => { |
182 | 'data_type' => 'integer[]', |
183 | 'is_nullable' => 1, |
184 | 'size' => undef, |
185 | 'default_value' => undef, |
186 | }, |
187 | }; |
52c53388 |
188 | |
bab40dee |
189 | my $type_info = $schema->storage->columns_info_for('dbic_t_schema.artist'); |
190 | my $artistid_defval = delete $type_info->{artistid}->{default_value}; |
191 | like($artistid_defval, |
192 | qr/^nextval\('([^\.]*\.){0,1}artist_artistid_seq'::(?:text|regclass)\)/, |
193 | 'columns_info_for - sequence matches Pg get_autoinc_seq expectations'); |
194 | is_deeply($type_info, $test_type_info, |
195 | 'columns_info_for - column data types'); |
52c53388 |
196 | |
70a201a5 |
197 | |
a953d8d9 |
198 | |
a953d8d9 |
199 | |
bab40dee |
200 | ####### Array tests |
70a201a5 |
201 | |
bab40dee |
202 | BEGIN { |
203 | package DBICTest::Schema::ArrayTest; |
204 | push @main::test_classes, __PACKAGE__; |
70a201a5 |
205 | |
bab40dee |
206 | use strict; |
207 | use warnings; |
41519379 |
208 | use base 'DBICTest::BaseResult'; |
70a201a5 |
209 | |
bab40dee |
210 | __PACKAGE__->table('dbic_t_schema.array_test'); |
211 | __PACKAGE__->add_columns(qw/id arrayfield/); |
212 | __PACKAGE__->column_info_from_storage(1); |
213 | __PACKAGE__->set_primary_key('id'); |
70a201a5 |
214 | |
bab40dee |
215 | } |
216 | SKIP: { |
0647e0cc |
217 | skip "Need DBD::Pg 2.9.2 or newer for array tests", 4 if $DBD::Pg::VERSION < 2.009002; |
70a201a5 |
218 | |
41519379 |
219 | my $arr_rs = $schema->resultset('ArrayTest'); |
220 | |
bab40dee |
221 | lives_ok { |
41519379 |
222 | $arr_rs->create({ |
bab40dee |
223 | arrayfield => [1, 2], |
224 | }); |
225 | } 'inserting arrayref as pg array data'; |
70a201a5 |
226 | |
bab40dee |
227 | lives_ok { |
41519379 |
228 | $arr_rs->update({ |
bab40dee |
229 | arrayfield => [3, 4], |
230 | }); |
231 | } 'updating arrayref as pg array data'; |
ac45262b |
232 | |
41519379 |
233 | $arr_rs->create({ |
0647e0cc |
234 | arrayfield => [5, 6], |
9ba627e3 |
235 | }); |
c224117b |
236 | |
f6faeab8 |
237 | lives_ok { |
238 | $schema->populate('ArrayTest', [ |
239 | [ qw/arrayfield/ ], |
240 | [ [0,0] ], |
241 | ]); |
242 | } 'inserting arrayref using void ctx populate'; |
243 | |
41519379 |
244 | # Search using arrays |
245 | lives_ok { |
246 | is_deeply ( |
247 | $arr_rs->search({ arrayfield => { -value => [3,4] } })->first->arrayfield, |
248 | [3,4], |
249 | 'Array value matches' |
250 | ); |
251 | } 'searching by arrayref'; |
a780a0ff |
252 | |
bab40dee |
253 | lives_ok { |
41519379 |
254 | is_deeply ( |
255 | $arr_rs->search({ arrayfield => { '=' => { -value => [3,4] }} })->first->arrayfield, |
49eeb48d |
256 | [3,4], |
41519379 |
257 | 'Array value matches explicit equal' |
258 | ); |
259 | } 'searching by arrayref (explicit equal sign)'; |
a780a0ff |
260 | |
41519379 |
261 | lives_ok { |
262 | is_deeply ( |
263 | $arr_rs->search({ arrayfield => { '>' => { -value => [3,1] }} })->first->arrayfield, |
264 | [3,4], |
265 | 'Array value matches greater than' |
266 | ); |
267 | } 'searching by arrayref (greater than)'; |
268 | |
269 | lives_ok { |
270 | is ( |
271 | $arr_rs->search({ arrayfield => { '>' => { -value => [3,7] }} })->count, |
272 | 1, |
273 | 'Greater than search found [5,6]', |
274 | ); |
275 | } 'searching by arrayref (greater than)'; |
276 | |
277 | # Find using arrays |
278 | lives_ok { |
279 | is_deeply ( |
280 | $arr_rs->find({ arrayfield => { -value => [3,4] } })->arrayfield, |
281 | [3,4], |
282 | 'Array value matches implicit equal' |
283 | ); |
284 | } 'find by arrayref'; |
285 | |
286 | lives_ok { |
287 | is_deeply ( |
288 | $arr_rs->find({ arrayfield => { '=' => { -value => [3,4] }} })->arrayfield, |
289 | [3,4], |
290 | 'Array value matches explicit equal' |
291 | ); |
292 | } 'find by arrayref (equal)'; |
293 | |
294 | # test inferred condition for creation |
4ca1fd6f |
295 | for my $cond ( |
41519379 |
296 | { -value => [3,4] }, |
297 | \[ '= ?' => [arrayfield => [3, 4]] ], |
298 | ) { |
5f35ba0f |
299 | local $TODO = 'No introspection of complex literal conditions :(' |
300 | if is_literal_value $cond; |
301 | |
302 | |
41519379 |
303 | my $arr_rs_cond = $arr_rs->search({ arrayfield => $cond }); |
304 | |
305 | my $row = $arr_rs_cond->create({}); |
a780a0ff |
306 | is_deeply ($row->arrayfield, [3,4], 'Array value taken from $rs condition'); |
307 | $row->discard_changes; |
308 | is_deeply ($row->arrayfield, [3,4], 'Array value made it to storage'); |
309 | } |
bab40dee |
310 | } |
9ba627e3 |
311 | |
70a201a5 |
312 | ########## Case check |
313 | |
bab40dee |
314 | BEGIN { |
315 | package DBICTest::Schema::Casecheck; |
316 | push @main::test_classes, __PACKAGE__; |
70a201a5 |
317 | |
bab40dee |
318 | use strict; |
319 | use warnings; |
320 | use base 'DBIx::Class::Core'; |
70a201a5 |
321 | |
bab40dee |
322 | __PACKAGE__->table('dbic_t_schema.casecheck'); |
323 | __PACKAGE__->add_columns(qw/id name NAME uc_name/); |
324 | __PACKAGE__->column_info_from_storage(1); |
325 | __PACKAGE__->set_primary_key('id'); |
326 | } |
70a201a5 |
327 | |
bab40dee |
328 | my $name_info = $schema->source('Casecheck')->column_info( 'name' ); |
329 | is( $name_info->{size}, 1, "Case sensitive matching info for 'name'" ); |
ae515736 |
330 | |
bab40dee |
331 | my $NAME_info = $schema->source('Casecheck')->column_info( 'NAME' ); |
332 | is( $NAME_info->{size}, 2, "Case sensitive matching info for 'NAME'" ); |
ae515736 |
333 | |
bab40dee |
334 | my $uc_name_info = $schema->source('Casecheck')->column_info( 'uc_name' ); |
335 | is( $uc_name_info->{size}, 3, "Case insensitive matching info for 'uc_name'" ); |
ae515736 |
336 | |
70a201a5 |
337 | |
ecccae8a |
338 | ## Test ResultSet->update |
339 | my $artist = $schema->resultset('Artist')->first; |
340 | my $cds = $artist->cds_unordered->search({ |
341 | year => { '!=' => 2010 } |
342 | }, { prefetch => 'liner_notes' }); |
67b79ae1 |
343 | lives_ok { $cds->update({ year => '2010' }) } 'Update on prefetched rs'; |
70a201a5 |
344 | |
345 | ## Test SELECT ... FOR UPDATE |
bab40dee |
346 | SKIP: { |
bb961936 |
347 | skip "Your system does not support unsafe signals (d_sigaction) - unable to run deadlock test", 1 |
348 | unless eval { $Config{d_sigaction} and require POSIX }; |
bab40dee |
349 | |
350 | my ($timed_out, $artist2); |
351 | |
352 | for my $t ( |
353 | { |
354 | # Make sure that an error was raised, and that the update failed |
355 | update_lock => 1, |
356 | test_sub => sub { |
357 | ok($timed_out, "update from second schema times out"); |
358 | ok($artist2->is_column_changed('name'), "'name' column is still dirty from second schema"); |
359 | }, |
52c53388 |
360 | }, |
bab40dee |
361 | { |
362 | # Make sure that an error was NOT raised, and that the update succeeded |
363 | update_lock => 0, |
364 | test_sub => sub { |
365 | ok(! $timed_out, "update from second schema DOES NOT timeout"); |
366 | ok(! $artist2->is_column_changed('name'), "'name' column is NOT dirty from second schema"); |
367 | }, |
52c53388 |
368 | }, |
bab40dee |
369 | ) { |
370 | # create a new schema |
2c2bc4e5 |
371 | my $schema2 = DBICTest->connect_schema($dsn, $user, $pass); |
bab40dee |
372 | $schema2->source("Artist")->name("dbic_t_schema.artist"); |
373 | |
374 | $schema->txn_do( sub { |
d2408067 |
375 | my $rs = $schema->resultset('Artist')->search( |
bab40dee |
376 | { |
377 | artistid => 1 |
378 | }, |
379 | $t->{update_lock} ? { for => 'update' } : {} |
d2408067 |
380 | ); |
381 | ok ($rs->count, 'Count works'); |
382 | |
383 | my $artist = $rs->next; |
bab40dee |
384 | is($artist->artistid, 1, "select returns artistid = 1"); |
385 | |
386 | $timed_out = 0; |
bb961936 |
387 | |
bab40dee |
388 | eval { |
bb961936 |
389 | # can not use %SIG assignment directly - we need sigaction below |
390 | # localization to a block still works however |
391 | local $SIG{ALRM}; |
392 | |
393 | POSIX::sigaction( POSIX::SIGALRM() => POSIX::SigAction->new( |
394 | sub { die "DBICTestTimeout" }, |
395 | )); |
396 | |
bab40dee |
397 | alarm(2); |
398 | $artist2 = $schema2->resultset('Artist')->find(1); |
399 | $artist2->name('fooey'); |
400 | $artist2->update; |
bab40dee |
401 | }; |
bb961936 |
402 | |
403 | alarm(0); |
404 | |
405 | if (is_exception($@)) { |
406 | $timed_out = $@ =~ /DBICTestTimeout/ |
407 | or die $@; |
408 | } |
bab40dee |
409 | }); |
410 | |
411 | $t->{test_sub}->(); |
412 | } |
413 | } |
95ba7ee4 |
414 | |
70a201a5 |
415 | |
372da819 |
416 | ######## other older Auto-pk tests |
70a201a5 |
417 | |
bab40dee |
418 | $schema->source("SequenceTest")->name("dbic_t_schema.sequence_test"); |
419 | for (1..5) { |
420 | my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' }); |
421 | is($st->pkid1, $_, "Auto-PK for sequence without default: First primary key"); |
422 | is($st->pkid2, $_ + 9, "Auto-PK for sequence without default: Second primary key"); |
423 | is($st->nonpkid, $_ + 19, "Auto-PK for sequence without default: Non-primary key"); |
424 | } |
425 | my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 }); |
426 | is($st->pkid1, 55, "Auto-PK for sequence without default: First primary key set manually"); |
4d4dc518 |
427 | |
428 | |
38d5ea9f |
429 | ######## test non-serial auto-pk |
4d4dc518 |
430 | |
bbdda281 |
431 | if ($schema->storage->_use_insert_returning) { |
bab40dee |
432 | $schema->source('TimestampPrimaryKey')->name('dbic_t_schema.timestamp_primary_key_test'); |
433 | my $row = $schema->resultset('TimestampPrimaryKey')->create({}); |
434 | ok $row->id; |
435 | } |
d093d3eb |
436 | |
be860760 |
437 | ######## test with_deferred_fk_checks |
438 | |
bab40dee |
439 | $schema->source('CD')->name('dbic_t_schema.cd'); |
440 | $schema->source('Track')->name('dbic_t_schema.track'); |
2131aa2e |
441 | lives_and { warning_is { |
bab40dee |
442 | $schema->storage->with_deferred_fk_checks(sub { |
443 | $schema->resultset('Track')->create({ |
444 | trackid => 999, cd => 999, position => 1, title => 'deferred FK track' |
445 | }); |
446 | $schema->resultset('CD')->create({ |
447 | artist => 1, cdid => 999, year => '2003', title => 'deferred FK cd' |
448 | }); |
be860760 |
449 | }); |
2131aa2e |
450 | } undef } 'with_deferred_fk_checks code survived'; |
be860760 |
451 | |
bab40dee |
452 | is eval { $schema->resultset('Track')->find(999)->title }, 'deferred FK track', |
8273e845 |
453 | 'code in with_deferred_fk_checks worked'; |
be860760 |
454 | |
bab40dee |
455 | throws_ok { |
456 | $schema->resultset('Track')->create({ |
457 | trackid => 1, cd => 9999, position => 1, title => 'Track1' |
458 | }); |
459 | } qr/constraint/i, 'with_deferred_fk_checks is off'; |
460 | } |
be860760 |
461 | |
2fbf50ff |
462 | done_testing; |
609c5f1b |
463 | |
5935c90a |
464 | END { |
f66c9ba8 |
465 | return unless $schema; |
5935c90a |
466 | drop_test_schema($schema); |
65d35121 |
467 | eapk_drop_all($schema); |
468 | undef $schema; |
5935c90a |
469 | }; |
70a201a5 |
470 | |
471 | |
472 | ######### SUBROUTINES |
473 | |
474 | sub create_test_schema { |
2fbf50ff |
475 | my $schema = shift; |
476 | $schema->storage->dbh_do(sub { |
477 | my (undef,$dbh) = @_; |
70a201a5 |
478 | |
2fbf50ff |
479 | local $dbh->{Warn} = 0; |
70a201a5 |
480 | |
2fbf50ff |
481 | my $std_artist_table = <<EOS; |
70a201a5 |
482 | ( |
483 | artistid serial PRIMARY KEY |
484 | , name VARCHAR(100) |
485 | , rank INTEGER NOT NULL DEFAULT '13' |
486 | , charfield CHAR(10) |
487 | , arrayfield INTEGER[] |
488 | ) |
489 | EOS |
490 | |
881def48 |
491 | $dbh->do("CREATE SCHEMA dbic_t_schema"); |
492 | $dbh->do("CREATE TABLE dbic_t_schema.artist $std_artist_table"); |
4d4dc518 |
493 | |
494 | $dbh->do(<<EOS); |
495 | CREATE TABLE dbic_t_schema.timestamp_primary_key_test ( |
be860760 |
496 | id timestamp default current_timestamp |
4d4dc518 |
497 | ) |
498 | EOS |
2fbf50ff |
499 | $dbh->do(<<EOS); |
be860760 |
500 | CREATE TABLE dbic_t_schema.cd ( |
501 | cdid int PRIMARY KEY, |
502 | artist int, |
503 | title varchar(255), |
504 | year varchar(4), |
505 | genreid int, |
506 | single_track int |
507 | ) |
508 | EOS |
509 | $dbh->do(<<EOS); |
510 | CREATE TABLE dbic_t_schema.track ( |
511 | trackid int, |
512 | cd int REFERENCES dbic_t_schema.cd(cdid) DEFERRABLE, |
513 | position int, |
514 | title varchar(255), |
515 | last_updated_on date, |
3d98c75e |
516 | last_updated_at date |
be860760 |
517 | ) |
518 | EOS |
519 | |
520 | $dbh->do(<<EOS); |
881def48 |
521 | CREATE TABLE dbic_t_schema.sequence_test ( |
70a201a5 |
522 | pkid1 integer |
523 | , pkid2 integer |
524 | , nonpkid integer |
525 | , name VARCHAR(100) |
526 | , CONSTRAINT pk PRIMARY KEY(pkid1, pkid2) |
527 | ) |
528 | EOS |
2fbf50ff |
529 | $dbh->do("CREATE SEQUENCE pkid1_seq START 1 MAXVALUE 999999 MINVALUE 0"); |
530 | $dbh->do("CREATE SEQUENCE pkid2_seq START 10 MAXVALUE 999999 MINVALUE 0"); |
531 | $dbh->do("CREATE SEQUENCE nonpkid_seq START 20 MAXVALUE 999999 MINVALUE 0"); |
532 | $dbh->do(<<EOS); |
881def48 |
533 | CREATE TABLE dbic_t_schema.casecheck ( |
70a201a5 |
534 | id serial PRIMARY KEY |
535 | , "name" VARCHAR(1) |
536 | , "NAME" VARCHAR(2) |
537 | , "UC_NAME" VARCHAR(3) |
70a201a5 |
538 | ) |
539 | EOS |
2fbf50ff |
540 | $dbh->do(<<EOS); |
881def48 |
541 | CREATE TABLE dbic_t_schema.array_test ( |
70a201a5 |
542 | id serial PRIMARY KEY |
543 | , arrayfield INTEGER[] |
544 | ) |
545 | EOS |
881def48 |
546 | $dbh->do("CREATE SCHEMA dbic_t_schema_2"); |
547 | $dbh->do("CREATE TABLE dbic_t_schema_2.artist $std_artist_table"); |
548 | $dbh->do("CREATE SCHEMA dbic_t_schema_3"); |
549 | $dbh->do("CREATE TABLE dbic_t_schema_3.artist $std_artist_table"); |
550 | $dbh->do('set search_path=dbic_t_schema,public'); |
551 | $dbh->do("CREATE SCHEMA dbic_t_schema_4"); |
552 | $dbh->do("CREATE SCHEMA dbic_t_schema_5"); |
2fbf50ff |
553 | $dbh->do(<<EOS); |
881def48 |
554 | CREATE TABLE dbic_t_schema_4.artist |
70a201a5 |
555 | ( |
556 | artistid integer not null default nextval('artist_artistid_seq'::regclass) PRIMARY KEY |
557 | , name VARCHAR(100) |
558 | , rank INTEGER NOT NULL DEFAULT '13' |
559 | , charfield CHAR(10) |
560 | , arrayfield INTEGER[] |
561 | ); |
562 | EOS |
881def48 |
563 | $dbh->do('set search_path=public,dbic_t_schema,dbic_t_schema_3'); |
2fbf50ff |
564 | $dbh->do('create sequence public.artist_artistid_seq'); #< in the public schema |
565 | $dbh->do(<<EOS); |
881def48 |
566 | CREATE TABLE dbic_t_schema_5.artist |
70a201a5 |
567 | ( |
568 | artistid integer not null default nextval('public.artist_artistid_seq'::regclass) PRIMARY KEY |
569 | , name VARCHAR(100) |
570 | , rank INTEGER NOT NULL DEFAULT '13' |
571 | , charfield CHAR(10) |
572 | , arrayfield INTEGER[] |
573 | ); |
574 | EOS |
881def48 |
575 | $dbh->do('set search_path=dbic_t_schema,public'); |
2fbf50ff |
576 | }); |
70a201a5 |
577 | } |
578 | |
579 | |
580 | |
581 | sub drop_test_schema { |
52c53388 |
582 | my ( $schema, $warn_exceptions ) = @_; |
2fbf50ff |
583 | |
584 | $schema->storage->dbh_do(sub { |
585 | my (undef,$dbh) = @_; |
586 | |
587 | local $dbh->{Warn} = 0; |
588 | |
589 | for my $stat ( |
881def48 |
590 | 'DROP SCHEMA dbic_t_schema_5 CASCADE', |
e78c769d |
591 | 'DROP SEQUENCE public.artist_artistid_seq CASCADE', |
881def48 |
592 | 'DROP SCHEMA dbic_t_schema_4 CASCADE', |
593 | 'DROP SCHEMA dbic_t_schema CASCADE', |
e78c769d |
594 | 'DROP SEQUENCE pkid1_seq CASCADE', |
595 | 'DROP SEQUENCE pkid2_seq CASCADE', |
596 | 'DROP SEQUENCE nonpkid_seq CASCADE', |
881def48 |
597 | 'DROP SCHEMA dbic_t_schema_2 CASCADE', |
598 | 'DROP SCHEMA dbic_t_schema_3 CASCADE', |
2fbf50ff |
599 | ) { |
600 | eval { $dbh->do ($stat) }; |
52c53388 |
601 | diag $@ if $@ && $warn_exceptions; |
2fbf50ff |
602 | } |
603 | }); |
3ff5b740 |
604 | } |
7603d2a5 |
605 | |
372da819 |
606 | |
ee9f372c |
607 | ### auto-pk / last_insert_id / sequence discovery |
608 | sub run_apk_tests { |
609 | my $schema = shift; |
610 | |
611 | # This is in Core now, but it's here just to test that it doesn't break |
612 | $schema->class('Artist')->load_components('PK::Auto'); |
613 | cmp_ok( $schema->resultset('Artist')->count, '==', 0, 'this should start with an empty artist table'); |
614 | |
615 | # test that auto-pk also works with the defined search path by |
616 | # un-schema-qualifying the table name |
617 | apk_t_set($schema,'artist'); |
618 | |
619 | my $unq_new; |
620 | lives_ok { |
621 | $unq_new = $schema->resultset('Artist')->create({ name => 'baz' }); |
622 | } 'insert into unqualified, shadowed table succeeds'; |
623 | |
624 | is($unq_new && $unq_new->artistid, 1, "and got correct artistid"); |
625 | |
626 | my @test_schemas = ( [qw| dbic_t_schema_2 1 |], |
627 | [qw| dbic_t_schema_3 1 |], |
628 | [qw| dbic_t_schema_4 2 |], |
629 | [qw| dbic_t_schema_5 1 |], |
630 | ); |
631 | foreach my $t ( @test_schemas ) { |
632 | my ($sch_name, $start_num) = @$t; |
633 | #test with dbic_t_schema_2 |
634 | apk_t_set($schema,"$sch_name.artist"); |
635 | my $another_new; |
636 | lives_ok { |
637 | $another_new = $schema->resultset('Artist')->create({ name => 'Tollbooth Willy'}); |
638 | is( $another_new->artistid,$start_num, "got correct artistid for $sch_name") |
639 | or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>'); |
640 | } "$sch_name liid 1 did not die" |
641 | or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>'); |
642 | lives_ok { |
643 | $another_new = $schema->resultset('Artist')->create({ name => 'Adam Sandler'}); |
644 | is( $another_new->artistid,$start_num+1, "got correct artistid for $sch_name") |
645 | or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>'); |
646 | } "$sch_name liid 2 did not die" |
647 | or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || '<none>'); |
648 | |
649 | } |
650 | |
651 | lives_ok { |
652 | apk_t_set($schema,'dbic_t_schema.artist'); |
653 | my $new = $schema->resultset('Artist')->create({ name => 'foo' }); |
654 | is($new->artistid, 4, "Auto-PK worked"); |
655 | $new = $schema->resultset('Artist')->create({ name => 'bar' }); |
656 | is($new->artistid, 5, "Auto-PK worked"); |
657 | } 'old auto-pk tests did not die either'; |
658 | } |
659 | |
ee9f372c |
660 | # sets the artist table name and clears sequence name cache |
661 | sub apk_t_set { |
662 | my ( $s, $n ) = @_; |
663 | $s->source("Artist")->name($n); |
664 | $s->source('Artist')->column_info('artistid')->{sequence} = undef; #< clear sequence name cache |
665 | } |
666 | |
372da819 |
667 | |
5935c90a |
668 | ######## EXTENDED AUTO-PK TESTS |
669 | |
f295a73c |
670 | my @eapk_id_columns; |
5935c90a |
671 | BEGIN { |
672 | package DBICTest::Schema::ExtAPK; |
673 | push @main::test_classes, __PACKAGE__; |
674 | |
675 | use strict; |
676 | use warnings; |
d88ecca6 |
677 | use base 'DBIx::Class::Core'; |
5935c90a |
678 | |
f295a73c |
679 | __PACKAGE__->table('apk'); |
5935c90a |
680 | |
f295a73c |
681 | @eapk_id_columns = qw( id1 id2 id3 id4 ); |
5935c90a |
682 | __PACKAGE__->add_columns( |
683 | map { $_ => { data_type => 'integer', is_auto_increment => 1 } } |
f295a73c |
684 | @eapk_id_columns |
5935c90a |
685 | ); |
686 | |
f295a73c |
687 | __PACKAGE__->set_primary_key('id2'); #< note the SECOND column is |
688 | #the primary key |
5935c90a |
689 | } |
690 | |
f295a73c |
691 | my @eapk_schemas; |
692 | BEGIN{ @eapk_schemas = map "dbic_apk_$_", 0..5 } |
ae645c61 |
693 | my %seqs; #< hash of schema.table.col => currval of its (DBIC) primary key sequence |
5935c90a |
694 | |
372da819 |
695 | sub run_extended_apk_tests { |
11da4e66 |
696 | my $schema = shift; |
372da819 |
697 | |
f295a73c |
698 | #save the search path and reset it at the end |
49aa7e87 |
699 | my $search_path_save = eapk_get_search_path($schema); |
f295a73c |
700 | |
52c53388 |
701 | eapk_drop_all($schema); |
bab40dee |
702 | %seqs = (); |
5935c90a |
703 | |
f295a73c |
704 | # make the test schemas and sequences |
11da4e66 |
705 | $schema->storage->dbh_do(sub { |
f295a73c |
706 | my ( undef, $dbh ) = @_; |
707 | |
708 | $dbh->do("CREATE SCHEMA $_") |
709 | for @eapk_schemas; |
710 | |
711 | $dbh->do("CREATE SEQUENCE $eapk_schemas[5].fooseq"); |
ae645c61 |
712 | $dbh->do("SELECT setval('$eapk_schemas[5].fooseq',400)"); |
713 | $seqs{"$eapk_schemas[1].apk.id2"} = 400; |
714 | |
fb7be534 |
715 | $dbh->do("CREATE SEQUENCE $eapk_schemas[4].fooseq"); |
ae645c61 |
716 | $dbh->do("SELECT setval('$eapk_schemas[4].fooseq',300)"); |
717 | $seqs{"$eapk_schemas[3].apk.id2"} = 300; |
718 | |
93a6e8fa |
719 | $dbh->do("CREATE SEQUENCE $eapk_schemas[3].fooseq"); |
ae645c61 |
720 | $dbh->do("SELECT setval('$eapk_schemas[3].fooseq',200)"); |
721 | $seqs{"$eapk_schemas[4].apk.id2"} = 200; |
f295a73c |
722 | |
78c9596c |
723 | $dbh->do("SET search_path = ".join ',', reverse @eapk_schemas ); |
11da4e66 |
724 | }); |
372da819 |
725 | |
f295a73c |
726 | # clear our search_path cache |
727 | $schema->storage->{_pg_search_path} = undef; |
728 | |
729 | eapk_create( $schema, |
730 | with_search_path => [0,1], |
731 | ); |
732 | eapk_create( $schema, |
733 | with_search_path => [1,0,'public'], |
734 | nextval => "$eapk_schemas[5].fooseq", |
735 | ); |
736 | eapk_create( $schema, |
737 | with_search_path => ['public',0,1], |
738 | qualify_table => 2, |
739 | ); |
fb7be534 |
740 | eapk_create( $schema, |
741 | with_search_path => [3,1,0,'public'], |
742 | nextval => "$eapk_schemas[4].fooseq", |
743 | ); |
93a6e8fa |
744 | eapk_create( $schema, |
745 | with_search_path => [3,1,0,'public'], |
746 | nextval => "$eapk_schemas[3].fooseq", |
747 | qualify_table => 4, |
748 | ); |
372da819 |
749 | |
78c9596c |
750 | eapk_poke( $schema ); |
f295a73c |
751 | eapk_poke( $schema, 0 ); |
eb80e8d5 |
752 | eapk_poke( $schema, 2 ); |
93a6e8fa |
753 | eapk_poke( $schema, 4 ); |
f295a73c |
754 | eapk_poke( $schema, 1 ); |
eb80e8d5 |
755 | eapk_poke( $schema, 0 ); |
f295a73c |
756 | eapk_poke( $schema, 1 ); |
78c9596c |
757 | eapk_poke( $schema ); |
93a6e8fa |
758 | eapk_poke( $schema, 4 ); |
fb7be534 |
759 | eapk_poke( $schema, 3 ); |
eb80e8d5 |
760 | eapk_poke( $schema, 1 ); |
761 | eapk_poke( $schema, 2 ); |
762 | eapk_poke( $schema, 0 ); |
f295a73c |
763 | |
764 | # set our search path back |
765 | eapk_set_search_path( $schema, @$search_path_save ); |
766 | } |
767 | |
768 | # do a DBIC create on the apk table in the given schema number (which is an |
769 | # index of @eapk_schemas) |
770 | |
f295a73c |
771 | sub eapk_poke { |
772 | my ($s, $schema_num) = @_; |
372da819 |
773 | |
f295a73c |
774 | my $schema_name = defined $schema_num |
775 | ? $eapk_schemas[$schema_num] |
776 | : ''; |
777 | |
78c9596c |
778 | my $schema_name_actual = $schema_name || eapk_find_visible_schema($s); |
f295a73c |
779 | |
8ce84173 |
780 | $s->source('ExtAPK')->name($schema_name ? $schema_name.'.apk' : 'apk'); |
f295a73c |
781 | #< clear sequence name cache |
782 | $s->source('ExtAPK')->column_info($_)->{sequence} = undef |
783 | for @eapk_id_columns; |
784 | |
785 | no warnings 'uninitialized'; |
786 | lives_ok { |
787 | my $new; |
788 | for my $inc (1,2,3) { |
ae645c61 |
789 | $new = $schema->resultset('ExtAPK')->create({ id1 => 1}); |
691f3663 |
790 | my $proper_seqval = ++$seqs{"$schema_name_actual.apk.id2"}; |
791 | is( $new->id2, $proper_seqval, "$schema_name_actual.apk.id2 correct inc $inc" ) |
792 | or eapk_seq_diag($s,$schema_name); |
793 | $new->discard_changes; |
ae645c61 |
794 | is( $new->id1, 1 ); |
795 | for my $id ('id3','id4') { |
f295a73c |
796 | my $proper_seqval = ++$seqs{"$schema_name_actual.apk.$id"}; |
93a6e8fa |
797 | is( $new->$id, $proper_seqval, "$schema_name_actual.apk.$id correct inc $inc" ) |
fb7be534 |
798 | or eapk_seq_diag($s,$schema_name); |
f295a73c |
799 | } |
800 | } |
801 | } "create in schema '$schema_name' lives" |
fb7be534 |
802 | or eapk_seq_diag($s,$schema_name); |
372da819 |
803 | } |
804 | |
f295a73c |
805 | # print diagnostic info on which sequences were found in the ExtAPK |
806 | # class |
807 | sub eapk_seq_diag { |
808 | my $s = shift; |
78c9596c |
809 | my $schema = shift || eapk_find_visible_schema($s); |
f295a73c |
810 | |
811 | diag "$schema.apk sequences: ", |
812 | join(', ', |
813 | map "$_:".($s->source('ExtAPK')->column_info($_)->{sequence} || '<none>'), |
814 | @eapk_id_columns |
815 | ); |
816 | } |
817 | |
49aa7e87 |
818 | # get the postgres search path as an arrayref |
819 | sub eapk_get_search_path { |
820 | my ( $s ) = @_; |
821 | # cache the search path as ['schema','schema',...] in the storage |
822 | # obj |
823 | |
824 | return $s->storage->dbh_do(sub { |
825 | my (undef, $dbh) = @_; |
826 | my @search_path; |
827 | my ($sp_string) = $dbh->selectrow_array('SHOW search_path'); |
828 | while ( $sp_string =~ s/("[^"]+"|[^,]+),?// ) { |
829 | unless( defined $1 and length $1 ) { |
830 | die "search path sanity check failed: '$1'"; |
831 | } |
832 | push @search_path, $1; |
833 | } |
834 | \@search_path |
835 | }); |
836 | } |
f295a73c |
837 | sub eapk_set_search_path { |
838 | my ($s,@sp) = @_; |
839 | my $sp = join ',',@sp; |
840 | $s->storage->dbh_do( sub { $_[1]->do("SET search_path = $sp") } ); |
841 | } |
842 | |
843 | # 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 |
844 | sub eapk_create { |
845 | my ($schema, %a) = @_; |
846 | |
372da819 |
847 | $schema->storage->dbh_do(sub { |
5935c90a |
848 | my (undef,$dbh) = @_; |
372da819 |
849 | |
5935c90a |
850 | my $searchpath_save; |
851 | if ( $a{with_search_path} ) { |
852 | ($searchpath_save) = $dbh->selectrow_array('SHOW search_path'); |
853 | |
f295a73c |
854 | my $search_path = join ',',map {/\D/ ? $_ : $eapk_schemas[$_]} @{$a{with_search_path}}; |
5935c90a |
855 | |
856 | $dbh->do("SET search_path = $search_path"); |
857 | } |
858 | |
f295a73c |
859 | my $table_name = $a{qualify_table} |
860 | ? ($eapk_schemas[$a{qualify_table}] || die). ".apk" |
861 | : 'apk'; |
e25fe566 |
862 | local $_[1]->{Warn} = 0; |
f295a73c |
863 | |
864 | my $id_def = $a{nextval} |
ae645c61 |
865 | ? "integer not null default nextval('$a{nextval}'::regclass)" |
866 | : 'serial'; |
5935c90a |
867 | $dbh->do(<<EOS); |
f295a73c |
868 | CREATE TABLE $table_name ( |
869 | id1 serial |
870 | , id2 $id_def |
ae645c61 |
871 | , id3 serial primary key |
5935c90a |
872 | , id4 serial |
873 | ) |
874 | EOS |
372da819 |
875 | |
5935c90a |
876 | if( $searchpath_save ) { |
877 | $dbh->do("SET search_path = $searchpath_save"); |
878 | } |
372da819 |
879 | }); |
880 | } |
881 | |
5935c90a |
882 | sub eapk_drop_all { |
52c53388 |
883 | my ( $schema, $warn_exceptions ) = @_; |
372da819 |
884 | |
885 | $schema->storage->dbh_do(sub { |
886 | my (undef,$dbh) = @_; |
887 | |
888 | local $dbh->{Warn} = 0; |
5935c90a |
889 | |
890 | # drop the test schemas |
f295a73c |
891 | for (@eapk_schemas ) { |
11da4e66 |
892 | eval{ $dbh->do("DROP SCHEMA $_ CASCADE") }; |
52c53388 |
893 | diag $@ if $@ && $warn_exceptions; |
11da4e66 |
894 | } |
895 | |
372da819 |
896 | |
372da819 |
897 | }); |
898 | } |
78c9596c |
899 | |
900 | sub eapk_find_visible_schema { |
901 | my ($s) = @_; |
902 | |
903 | my ($schema) = |
904 | $s->storage->dbh_do(sub { |
905 | $_[1]->selectrow_array(<<EOS); |
906 | SELECT n.nspname |
907 | FROM pg_catalog.pg_namespace n |
908 | JOIN pg_catalog.pg_class c ON c.relnamespace = n.oid |
909 | WHERE c.relname = 'apk' |
910 | AND pg_catalog.pg_table_is_visible(c.oid) |
911 | EOS |
912 | }); |
913 | return $schema; |
914 | } |