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