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