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