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