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