Commit | Line | Data |
fbd83464 |
1 | package dbixcsl_common_tests; |
a78e3fed |
2 | |
3 | use strict; |
3980d69c |
4 | use warnings; |
a78e3fed |
5 | |
6 | use Test::More; |
18fca96a |
7 | use DBIx::Class::Schema::Loader; |
390bbe3f |
8 | use Class::Unload; |
9 | use File::Path; |
a78e3fed |
10 | use DBI; |
11 | |
390bbe3f |
12 | my $DUMP_DIR = './t/_common_dump'; |
13 | rmtree $DUMP_DIR; |
14 | |
a78e3fed |
15 | sub new { |
16 | my $class = shift; |
17 | |
18 | my $self; |
19 | |
20 | if( ref($_[0]) eq 'HASH') { |
21 | my $args = shift; |
22 | $self = { (%$args) }; |
23 | } |
24 | else { |
25 | $self = { @_ }; |
26 | } |
27 | |
28 | # Only MySQL uses this |
29 | $self->{innodb} ||= ''; |
0906d55b |
30 | |
31 | # DB2 doesn't support this |
32 | $self->{null} = 'NULL' unless defined $self->{null}; |
af6c2665 |
33 | |
34 | $self->{verbose} = $ENV{TEST_VERBOSE} || 0; |
a78e3fed |
35 | |
df637f3d |
36 | # Optional extra tables and tests |
37 | $self->{extra} ||= {}; |
38 | |
a78e3fed |
39 | return bless $self => $class; |
40 | } |
41 | |
42 | sub skip_tests { |
43 | my ($self, $why) = @_; |
44 | |
45 | plan skip_all => $why; |
46 | } |
47 | |
4350370d |
48 | sub _monikerize { |
49 | my $name = shift; |
50 | return 'LoaderTest2X' if $name =~ /^loader_test2$/i; |
51 | return undef; |
52 | } |
53 | |
a78e3fed |
54 | sub run_tests { |
55 | my $self = shift; |
56 | |
fc97d5be |
57 | plan tests => 3 + 134 + ($self->{extra}->{count} || 0); |
a78e3fed |
58 | |
59 | $self->create(); |
60 | |
7cb9244f |
61 | my @connect_info = ( |
62 | $self->{dsn}, |
63 | $self->{user}, |
64 | $self->{password}, |
65 | $self->{connect_info_opts}, |
66 | ); |
390bbe3f |
67 | |
68 | # First, with in-memory classes |
69 | my $schema_class = $self->setup_schema(@connect_info); |
70 | $self->test_schema($schema_class); |
390bbe3f |
71 | $self->drop_tables; |
390bbe3f |
72 | } |
73 | |
74 | sub setup_schema { |
75 | my $self = shift; |
76 | my @connect_info = @_; |
77 | |
c2849787 |
78 | my $schema_class = 'DBIXCSL_Test::Schema'; |
a78e3fed |
79 | |
af6c2665 |
80 | my $debug = ($self->{verbose} > 1) ? 1 : 0; |
81 | |
af96f52e |
82 | my %loader_opts = ( |
b1e43108 |
83 | constraint => |
84 | qr/^(?:\S+\.)?(?:$self->{vendor}_)?loader_test[0-9]+(?!.*_)/i, |
507034ad |
85 | relationships => 1, |
86 | additional_classes => 'TestAdditional', |
87 | additional_base_classes => 'TestAdditionalBase', |
88 | left_base_classes => [ qw/TestLeftBase/ ], |
9fa99683 |
89 | components => [ qw/TestComponent/ ], |
e61e3fcb |
90 | resultset_components => [ qw/TestRSComponent/ ], |
996be9ee |
91 | inflect_plural => { loader_test4 => 'loader_test4zes' }, |
92 | inflect_singular => { fkid => 'fkid_singular' }, |
4350370d |
93 | moniker_map => \&_monikerize, |
507034ad |
94 | debug => $debug, |
390bbe3f |
95 | dump_directory => $DUMP_DIR, |
af96f52e |
96 | ); |
97 | |
98 | $loader_opts{db_schema} = $self->{db_schema} if $self->{db_schema}; |
af96f52e |
99 | |
996be9ee |
100 | { |
101 | my @loader_warnings; |
102 | local $SIG{__WARN__} = sub { push(@loader_warnings, $_[0]); }; |
103 | eval qq{ |
104 | package $schema_class; |
105 | use base qw/DBIx::Class::Schema::Loader/; |
106 | |
107 | __PACKAGE__->loader_options(\%loader_opts); |
108 | __PACKAGE__->connection(\@connect_info); |
109 | }; |
390bbe3f |
110 | |
996be9ee |
111 | ok(!$@, "Loader initialization") or diag $@; |
2830a493 |
112 | |
113 | my $warn_count = 2; |
114 | $warn_count++ if grep /ResultSetManager/, @loader_warnings; |
115 | |
996be9ee |
116 | if($self->{skip_rels}) { |
58dfed76 |
117 | SKIP: { |
2830a493 |
118 | is(scalar(@loader_warnings), $warn_count, "No loader warnings") |
58dfed76 |
119 | or diag @loader_warnings; |
120 | skip "No missing PK warnings without rels", 1; |
121 | } |
996be9ee |
122 | } |
123 | else { |
2830a493 |
124 | $warn_count++; |
125 | is(scalar(@loader_warnings), $warn_count, "Expected loader warning") |
58dfed76 |
126 | or diag @loader_warnings; |
2830a493 |
127 | is(grep(/loader_test9 has no primary key/, @loader_warnings), 1, |
58dfed76 |
128 | "Missing PK warning"); |
996be9ee |
129 | } |
130 | } |
390bbe3f |
131 | |
132 | return $schema_class; |
133 | } |
134 | |
135 | sub test_schema { |
136 | my $self = shift; |
137 | my $schema_class = shift; |
a4a19f3c |
138 | |
996be9ee |
139 | my $conn = $schema_class->clone; |
59cfa251 |
140 | my $monikers = {}; |
141 | my $classes = {}; |
142 | foreach my $source_name ($schema_class->sources) { |
143 | my $table_name = $schema_class->source($source_name)->from; |
144 | $monikers->{$table_name} = $source_name; |
145 | $classes->{$table_name} = $schema_class . q{::} . $source_name; |
146 | } |
a4a19f3c |
147 | |
22270947 |
148 | my $moniker1 = $monikers->{loader_test1s}; |
149 | my $class1 = $classes->{loader_test1s}; |
3980d69c |
150 | my $rsobj1 = $conn->resultset($moniker1); |
d70c335f |
151 | check_no_duplicate_unique_constraints($class1); |
3980d69c |
152 | |
153 | my $moniker2 = $monikers->{loader_test2}; |
154 | my $class2 = $classes->{loader_test2}; |
155 | my $rsobj2 = $conn->resultset($moniker2); |
d70c335f |
156 | check_no_duplicate_unique_constraints($class2); |
a78e3fed |
157 | |
996be9ee |
158 | my $moniker23 = $monikers->{LOADER_TEST23}; |
159 | my $class23 = $classes->{LOADER_TEST23}; |
160 | my $rsobj23 = $conn->resultset($moniker1); |
161 | |
162 | my $moniker24 = $monikers->{LoAdEr_test24}; |
163 | my $class24 = $classes->{LoAdEr_test24}; |
164 | my $rsobj24 = $conn->resultset($moniker2); |
165 | |
af6c2665 |
166 | isa_ok( $rsobj1, "DBIx::Class::ResultSet" ); |
167 | isa_ok( $rsobj2, "DBIx::Class::ResultSet" ); |
996be9ee |
168 | isa_ok( $rsobj23, "DBIx::Class::ResultSet" ); |
169 | isa_ok( $rsobj24, "DBIx::Class::ResultSet" ); |
170 | |
8ac8926d |
171 | my @columns_lt2 = $class2->columns; |
58dfed76 |
172 | is_deeply( \@columns_lt2, [ qw/id dat dat2/ ], "Column Ordering" ); |
8ac8926d |
173 | |
996be9ee |
174 | my %uniq1 = $class1->unique_constraints; |
175 | my $uniq1_test = 0; |
176 | foreach my $ucname (keys %uniq1) { |
177 | my $cols_arrayref = $uniq1{$ucname}; |
178 | if(@$cols_arrayref == 1 && $cols_arrayref->[0] eq 'dat') { |
179 | $uniq1_test = 1; |
180 | last; |
181 | } |
182 | } |
58dfed76 |
183 | ok($uniq1_test, "Unique constraint"); |
996be9ee |
184 | |
22270947 |
185 | is($moniker1, 'LoaderTest1', 'moniker singularisation'); |
186 | |
996be9ee |
187 | my %uniq2 = $class2->unique_constraints; |
188 | my $uniq2_test = 0; |
189 | foreach my $ucname (keys %uniq2) { |
190 | my $cols_arrayref = $uniq2{$ucname}; |
191 | if(@$cols_arrayref == 2 |
5223f24a |
192 | && $cols_arrayref->[0] eq 'dat2' |
193 | && $cols_arrayref->[1] eq 'dat') { |
996be9ee |
194 | $uniq2_test = 2; |
195 | last; |
196 | } |
197 | } |
58dfed76 |
198 | ok($uniq2_test, "Multi-col unique constraint"); |
a78e3fed |
199 | |
4350370d |
200 | is($moniker2, 'LoaderTest2X', "moniker_map testing"); |
201 | |
e61e3fcb |
202 | SKIP: { |
203 | can_ok( $class1, 'test_additional_base' ) |
204 | or skip "Pre-requisite test failed", 1; |
205 | is( $class1->test_additional_base, "test_additional_base", |
206 | "Additional Base method" ); |
207 | } |
c2849787 |
208 | |
e61e3fcb |
209 | SKIP: { |
210 | can_ok( $class1, 'test_additional_base_override' ) |
211 | or skip "Pre-requisite test failed", 1; |
212 | is( $class1->test_additional_base_override, |
213 | "test_left_base_override", |
214 | "Left Base overrides Additional Base method" ); |
215 | } |
c2849787 |
216 | |
e61e3fcb |
217 | SKIP: { |
218 | can_ok( $class1, 'test_additional_base_additional' ) |
219 | or skip "Pre-requisite test failed", 1; |
220 | is( $class1->test_additional_base_additional, "test_additional", |
221 | "Additional Base can use Additional package method" ); |
222 | } |
c2849787 |
223 | |
e61e3fcb |
224 | SKIP: { |
225 | can_ok( $class1, 'dbix_class_testcomponent' ) |
226 | or skip "Pre-requisite test failed", 1; |
227 | is( $class1->dbix_class_testcomponent, |
228 | 'dbix_class_testcomponent works', |
229 | 'Additional Component' ); |
230 | } |
c2849787 |
231 | |
e61e3fcb |
232 | SKIP: { |
233 | can_ok($rsobj1, 'dbix_class_testrscomponent') |
234 | or skip "Pre-requisite test failed", 1; |
235 | is( $rsobj1->dbix_class_testrscomponent, |
236 | 'dbix_class_testrscomponent works', |
237 | 'ResultSet component' ); |
c2849787 |
238 | } |
507034ad |
239 | |
78b7ccaa |
240 | SKIP: { |
e61e3fcb |
241 | can_ok( $class1, 'loader_test1_classmeth' ) |
242 | or skip "Pre-requisite test failed", 1; |
243 | is( $class1->loader_test1_classmeth, 'all is well', 'Class method' ); |
244 | } |
78b7ccaa |
245 | |
e61e3fcb |
246 | SKIP: { |
247 | can_ok( $rsobj1, 'loader_test1_rsmeth' ) |
248 | or skip "Pre-requisite test failed"; |
249 | is( $rsobj1->loader_test1_rsmeth, 'all is still well', 'Result set method' ); |
78b7ccaa |
250 | } |
e61e3fcb |
251 | |
252 | ok( $class1->column_info('id')->{is_auto_increment}, 'is_auto_incrment detection' ); |
507034ad |
253 | |
af6c2665 |
254 | my $obj = $rsobj1->find(1); |
58dfed76 |
255 | is( $obj->id, 1, "Find got the right row" ); |
256 | is( $obj->dat, "foo", "Column value" ); |
257 | is( $rsobj2->count, 4, "Count" ); |
b71790b1 |
258 | my $saved_id; |
259 | eval { |
260 | my $new_obj1 = $rsobj1->create({ dat => 'newthing' }); |
996be9ee |
261 | $saved_id = $new_obj1->id; |
b71790b1 |
262 | }; |
58dfed76 |
263 | ok(!$@, "Inserting new record using a PK::Auto key didn't die") or diag $@; |
264 | ok($saved_id, "Got PK::Auto-generated id"); |
b71790b1 |
265 | |
266 | my $new_obj1 = $rsobj1->search({ dat => 'newthing' })->first; |
58dfed76 |
267 | ok($new_obj1, "Found newly inserted PK::Auto record"); |
268 | is($new_obj1->id, $saved_id, "Correct PK::Auto-generated id"); |
a78e3fed |
269 | |
a13b2803 |
270 | my ($obj2) = $rsobj2->search({ dat => 'bbb' })->first; |
a78e3fed |
271 | is( $obj2->id, 2 ); |
272 | |
273 | SKIP: { |
58dfed76 |
274 | skip $self->{skip_rels}, 96 if $self->{skip_rels}; |
a78e3fed |
275 | |
3980d69c |
276 | my $moniker3 = $monikers->{loader_test3}; |
277 | my $class3 = $classes->{loader_test3}; |
278 | my $rsobj3 = $conn->resultset($moniker3); |
279 | |
280 | my $moniker4 = $monikers->{loader_test4}; |
281 | my $class4 = $classes->{loader_test4}; |
282 | my $rsobj4 = $conn->resultset($moniker4); |
283 | |
284 | my $moniker5 = $monikers->{loader_test5}; |
285 | my $class5 = $classes->{loader_test5}; |
286 | my $rsobj5 = $conn->resultset($moniker5); |
287 | |
288 | my $moniker6 = $monikers->{loader_test6}; |
289 | my $class6 = $classes->{loader_test6}; |
290 | my $rsobj6 = $conn->resultset($moniker6); |
291 | |
292 | my $moniker7 = $monikers->{loader_test7}; |
293 | my $class7 = $classes->{loader_test7}; |
294 | my $rsobj7 = $conn->resultset($moniker7); |
295 | |
296 | my $moniker8 = $monikers->{loader_test8}; |
297 | my $class8 = $classes->{loader_test8}; |
298 | my $rsobj8 = $conn->resultset($moniker8); |
299 | |
300 | my $moniker9 = $monikers->{loader_test9}; |
301 | my $class9 = $classes->{loader_test9}; |
302 | my $rsobj9 = $conn->resultset($moniker9); |
af6c2665 |
303 | |
996be9ee |
304 | my $moniker16 = $monikers->{loader_test16}; |
305 | my $class16 = $classes->{loader_test16}; |
306 | my $rsobj16 = $conn->resultset($moniker16); |
307 | |
308 | my $moniker17 = $monikers->{loader_test17}; |
309 | my $class17 = $classes->{loader_test17}; |
310 | my $rsobj17 = $conn->resultset($moniker17); |
311 | |
312 | my $moniker18 = $monikers->{loader_test18}; |
313 | my $class18 = $classes->{loader_test18}; |
314 | my $rsobj18 = $conn->resultset($moniker18); |
315 | |
316 | my $moniker19 = $monikers->{loader_test19}; |
317 | my $class19 = $classes->{loader_test19}; |
318 | my $rsobj19 = $conn->resultset($moniker19); |
319 | |
320 | my $moniker20 = $monikers->{loader_test20}; |
321 | my $class20 = $classes->{loader_test20}; |
322 | my $rsobj20 = $conn->resultset($moniker20); |
323 | |
324 | my $moniker21 = $monikers->{loader_test21}; |
325 | my $class21 = $classes->{loader_test21}; |
326 | my $rsobj21 = $conn->resultset($moniker21); |
327 | |
328 | my $moniker22 = $monikers->{loader_test22}; |
329 | my $class22 = $classes->{loader_test22}; |
330 | my $rsobj22 = $conn->resultset($moniker22); |
331 | |
25e3e91d |
332 | my $moniker25 = $monikers->{loader_test25}; |
333 | my $class25 = $classes->{loader_test25}; |
334 | my $rsobj25 = $conn->resultset($moniker25); |
335 | |
336 | my $moniker26 = $monikers->{loader_test26}; |
337 | my $class26 = $classes->{loader_test26}; |
338 | my $rsobj26 = $conn->resultset($moniker26); |
339 | |
26f1c8c9 |
340 | my $moniker27 = $monikers->{loader_test27}; |
341 | my $class27 = $classes->{loader_test27}; |
342 | my $rsobj27 = $conn->resultset($moniker27); |
343 | |
344 | my $moniker28 = $monikers->{loader_test28}; |
345 | my $class28 = $classes->{loader_test28}; |
346 | my $rsobj28 = $conn->resultset($moniker28); |
347 | |
348 | my $moniker29 = $monikers->{loader_test29}; |
349 | my $class29 = $classes->{loader_test29}; |
350 | my $rsobj29 = $conn->resultset($moniker29); |
351 | |
7dba7c70 |
352 | my $moniker31 = $monikers->{loader_test31}; |
353 | my $class31 = $classes->{loader_test31}; |
354 | my $rsobj31 = $conn->resultset($moniker31); |
355 | |
356 | my $moniker32 = $monikers->{loader_test32}; |
357 | my $class32 = $classes->{loader_test32}; |
358 | my $rsobj32 = $conn->resultset($moniker32); |
359 | |
360 | my $moniker33 = $monikers->{loader_test33}; |
361 | my $class33 = $classes->{loader_test33}; |
362 | my $rsobj33 = $conn->resultset($moniker33); |
363 | |
364 | my $moniker34 = $monikers->{loader_test34}; |
365 | my $class34 = $classes->{loader_test34}; |
366 | my $rsobj34 = $conn->resultset($moniker34); |
367 | |
af6c2665 |
368 | isa_ok( $rsobj3, "DBIx::Class::ResultSet" ); |
369 | isa_ok( $rsobj4, "DBIx::Class::ResultSet" ); |
370 | isa_ok( $rsobj5, "DBIx::Class::ResultSet" ); |
371 | isa_ok( $rsobj6, "DBIx::Class::ResultSet" ); |
372 | isa_ok( $rsobj7, "DBIx::Class::ResultSet" ); |
373 | isa_ok( $rsobj8, "DBIx::Class::ResultSet" ); |
374 | isa_ok( $rsobj9, "DBIx::Class::ResultSet" ); |
996be9ee |
375 | isa_ok( $rsobj16, "DBIx::Class::ResultSet" ); |
376 | isa_ok( $rsobj17, "DBIx::Class::ResultSet" ); |
377 | isa_ok( $rsobj18, "DBIx::Class::ResultSet" ); |
378 | isa_ok( $rsobj19, "DBIx::Class::ResultSet" ); |
379 | isa_ok( $rsobj20, "DBIx::Class::ResultSet" ); |
380 | isa_ok( $rsobj21, "DBIx::Class::ResultSet" ); |
381 | isa_ok( $rsobj22, "DBIx::Class::ResultSet" ); |
25e3e91d |
382 | isa_ok( $rsobj25, "DBIx::Class::ResultSet" ); |
383 | isa_ok( $rsobj26, "DBIx::Class::ResultSet" ); |
26f1c8c9 |
384 | isa_ok( $rsobj27, "DBIx::Class::ResultSet" ); |
385 | isa_ok( $rsobj28, "DBIx::Class::ResultSet" ); |
386 | isa_ok( $rsobj29, "DBIx::Class::ResultSet" ); |
7dba7c70 |
387 | isa_ok( $rsobj31, "DBIx::Class::ResultSet" ); |
388 | isa_ok( $rsobj32, "DBIx::Class::ResultSet" ); |
389 | isa_ok( $rsobj33, "DBIx::Class::ResultSet" ); |
390 | isa_ok( $rsobj34, "DBIx::Class::ResultSet" ); |
a78e3fed |
391 | |
392 | # basic rel test |
af6c2665 |
393 | my $obj4 = $rsobj4->find(123); |
996be9ee |
394 | isa_ok( $obj4->fkid_singular, $class3); |
a78e3fed |
395 | |
892b43b6 |
396 | ok($class4->column_info('fkid')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
397 | |
9fa99683 |
398 | my $obj3 = $rsobj3->find(1); |
4350370d |
399 | my $rs_rel4 = $obj3->search_related('loader_test4zes'); |
9fa99683 |
400 | isa_ok( $rs_rel4->first, $class4); |
c5de0b0c |
401 | |
996be9ee |
402 | # find on multi-col pk |
0906d55b |
403 | my $obj5 = |
404 | eval { $rsobj5->find({id1 => 1, iD2 => 1}) } || |
405 | eval { $rsobj5->find({id1 => 1, id2 => 1}) }; |
406 | die $@ if $@; |
407 | |
892b43b6 |
408 | is( $obj5->id2, 1, "Find on multi-col PK" ); |
a78e3fed |
409 | |
4ce22656 |
410 | # mulit-col fk def |
af6c2665 |
411 | my $obj6 = $rsobj6->find(1); |
3980d69c |
412 | isa_ok( $obj6->loader_test2, $class2); |
413 | isa_ok( $obj6->loader_test5, $class5); |
a78e3fed |
414 | |
e7886624 |
415 | ok($class6->column_info('loader_test2_id')->{is_foreign_key}, 'Foreign key detected'); |
892b43b6 |
416 | ok($class6->column_info('id')->{is_foreign_key}, 'Foreign key detected'); |
948d632b |
417 | |
418 | my $id2_info = eval { $class6->column_info('id2') } || |
419 | $class6->column_info('Id2'); |
420 | ok($id2_info->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
421 | |
a78e3fed |
422 | # fk that references a non-pk key (UNIQUE) |
af6c2665 |
423 | my $obj8 = $rsobj8->find(1); |
3980d69c |
424 | isa_ok( $obj8->loader_test7, $class7); |
a78e3fed |
425 | |
892b43b6 |
426 | ok($class8->column_info('loader_test7')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
427 | |
996be9ee |
428 | # test double-fk 17 ->-> 16 |
429 | my $obj17 = $rsobj17->find(33); |
430 | |
431 | my $rs_rel16_one = $obj17->loader16_one; |
432 | isa_ok($rs_rel16_one, $class16); |
892b43b6 |
433 | is($rs_rel16_one->dat, 'y16', "Multiple FKs to same table"); |
996be9ee |
434 | |
892b43b6 |
435 | ok($class17->column_info('loader16_one')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
436 | |
996be9ee |
437 | my $rs_rel16_two = $obj17->loader16_two; |
438 | isa_ok($rs_rel16_two, $class16); |
892b43b6 |
439 | is($rs_rel16_two->dat, 'z16', "Multiple FKs to same table"); |
996be9ee |
440 | |
892b43b6 |
441 | ok($class17->column_info('loader16_two')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
442 | |
996be9ee |
443 | my $obj16 = $rsobj16->find(2); |
444 | my $rs_rel17 = $obj16->search_related('loader_test17_loader16_ones'); |
445 | isa_ok($rs_rel17->first, $class17); |
892b43b6 |
446 | is($rs_rel17->first->id, 3, "search_related with multiple FKs from same table"); |
996be9ee |
447 | |
448 | # XXX test m:m 18 <- 20 -> 19 |
892b43b6 |
449 | ok($class20->column_info('parent')->{is_foreign_key}, 'Foreign key detected'); |
450 | ok($class20->column_info('child')->{is_foreign_key}, 'Foreign key detected'); |
996be9ee |
451 | |
452 | # XXX test double-fk m:m 21 <- 22 -> 21 |
892b43b6 |
453 | ok($class22->column_info('parent')->{is_foreign_key}, 'Foreign key detected'); |
454 | ok($class22->column_info('child')->{is_foreign_key}, 'Foreign key detected'); |
996be9ee |
455 | |
25e3e91d |
456 | # test double multi-col fk 26 -> 25 |
457 | my $obj26 = $rsobj26->find(33); |
458 | |
459 | my $rs_rel25_one = $obj26->loader_test25_id_rel1; |
460 | isa_ok($rs_rel25_one, $class25); |
892b43b6 |
461 | is($rs_rel25_one->dat, 'x25', "Multiple multi-col FKs to same table"); |
25e3e91d |
462 | |
892b43b6 |
463 | ok($class26->column_info('id')->{is_foreign_key}, 'Foreign key detected'); |
464 | ok($class26->column_info('rel1')->{is_foreign_key}, 'Foreign key detected'); |
465 | ok($class26->column_info('rel2')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
466 | |
25e3e91d |
467 | my $rs_rel25_two = $obj26->loader_test25_id_rel2; |
468 | isa_ok($rs_rel25_two, $class25); |
892b43b6 |
469 | is($rs_rel25_two->dat, 'y25', "Multiple multi-col FKs to same table"); |
25e3e91d |
470 | |
471 | my $obj25 = $rsobj25->find(3,42); |
472 | my $rs_rel26 = $obj25->search_related('loader_test26_id_rel1s'); |
473 | isa_ok($rs_rel26->first, $class26); |
892b43b6 |
474 | is($rs_rel26->first->id, 3, "search_related with multiple multi-col FKs from same table"); |
25e3e91d |
475 | |
26f1c8c9 |
476 | # test one-to-one rels |
477 | my $obj27 = $rsobj27->find(1); |
478 | my $obj28 = $obj27->loader_test28; |
479 | isa_ok($obj28, $class28); |
892b43b6 |
480 | is($obj28->get_column('id'), 1, "One-to-one relationship with PRIMARY FK"); |
26f1c8c9 |
481 | |
892b43b6 |
482 | ok($class28->column_info('id')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
483 | |
26f1c8c9 |
484 | my $obj29 = $obj27->loader_test29; |
485 | isa_ok($obj29, $class29); |
892b43b6 |
486 | is($obj29->id, 1, "One-to-one relationship with UNIQUE FK"); |
26f1c8c9 |
487 | |
892b43b6 |
488 | ok($class29->column_info('fk')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
489 | |
26f1c8c9 |
490 | $obj27 = $rsobj27->find(2); |
892b43b6 |
491 | is($obj27->loader_test28, undef, "Undef for missing one-to-one row"); |
492 | is($obj27->loader_test29, undef, "Undef for missing one-to-one row"); |
26f1c8c9 |
493 | |
7dba7c70 |
494 | # test outer join for nullable referring columns: |
495 | SKIP: { |
e7213f4f |
496 | skip "unreliable column info from db driver",11 unless |
7dba7c70 |
497 | ($class32->column_info('rel2')->{is_nullable}); |
498 | |
892b43b6 |
499 | ok($class32->column_info('rel1')->{is_foreign_key}, 'Foreign key detected'); |
500 | ok($class32->column_info('rel2')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
501 | |
7dba7c70 |
502 | my $obj32 = $rsobj32->find(1,{prefetch=>[qw/rel1 rel2/]}); |
503 | my $obj34 = $rsobj34->find( |
504 | 1,{prefetch=>[qw/loader_test33_id_rel1 loader_test33_id_rel2/]} |
505 | ); |
506 | my $skip_outerjoin; |
507 | isa_ok($obj32,$class32) or $skip_outerjoin = 1; |
508 | isa_ok($obj34,$class34) or $skip_outerjoin = 1; |
509 | |
892b43b6 |
510 | ok($class34->column_info('id')->{is_foreign_key}, 'Foreign key detected'); |
511 | ok($class34->column_info('rel1')->{is_foreign_key}, 'Foreign key detected'); |
512 | ok($class34->column_info('rel2')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
513 | |
7dba7c70 |
514 | SKIP: { |
515 | skip "Pre-requisite test failed", 4 if $skip_outerjoin; |
516 | my $rs_rel31_one = $obj32->rel1; |
517 | my $rs_rel31_two = $obj32->rel2; |
892b43b6 |
518 | isa_ok($rs_rel31_one, $class31); |
519 | is($rs_rel31_two, undef); |
7dba7c70 |
520 | |
521 | my $rs_rel33_one = $obj34->loader_test33_id_rel1; |
522 | my $rs_rel33_two = $obj34->loader_test33_id_rel2; |
523 | |
524 | isa_ok($rs_rel33_one,$class33); |
892b43b6 |
525 | is($rs_rel33_two, undef); |
7dba7c70 |
526 | |
527 | } |
528 | } |
529 | |
a78e3fed |
530 | # from Chisel's tests... |
531 | SKIP: { |
532 | if($self->{vendor} =~ /sqlite/i) { |
e7213f4f |
533 | skip 'SQLite cannot do the advanced tests', 10; |
a78e3fed |
534 | } |
535 | |
3980d69c |
536 | my $moniker10 = $monikers->{loader_test10}; |
537 | my $class10 = $classes->{loader_test10}; |
538 | my $rsobj10 = $conn->resultset($moniker10); |
539 | |
540 | my $moniker11 = $monikers->{loader_test11}; |
541 | my $class11 = $classes->{loader_test11}; |
542 | my $rsobj11 = $conn->resultset($moniker11); |
a78e3fed |
543 | |
af6c2665 |
544 | isa_ok( $rsobj10, "DBIx::Class::ResultSet" ); |
545 | isa_ok( $rsobj11, "DBIx::Class::ResultSet" ); |
a78e3fed |
546 | |
892b43b6 |
547 | ok($class10->column_info('loader_test11')->{is_foreign_key}, 'Foreign key detected'); |
548 | ok($class11->column_info('loader_test10')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
549 | |
af6c2665 |
550 | my $obj10 = $rsobj10->create({ subject => 'xyzzy' }); |
a78e3fed |
551 | |
552 | $obj10->update(); |
892b43b6 |
553 | ok( defined $obj10, 'Create row' ); |
a78e3fed |
554 | |
af6c2665 |
555 | my $obj11 = $rsobj11->create({ loader_test10 => $obj10->id() }); |
a78e3fed |
556 | $obj11->update(); |
892b43b6 |
557 | ok( defined $obj11, 'Create related row' ); |
a78e3fed |
558 | |
559 | eval { |
560 | my $obj10_2 = $obj11->loader_test10; |
561 | $obj10_2->loader_test11( $obj11->id11() ); |
562 | $obj10_2->update(); |
563 | }; |
892b43b6 |
564 | ok(!$@, "Setting up circular relationship"); |
a78e3fed |
565 | |
566 | SKIP: { |
892b43b6 |
567 | skip 'Previous eval block failed', 3 if $@; |
a78e3fed |
568 | |
af6c2665 |
569 | my $results = $rsobj10->search({ subject => 'xyzzy' }); |
892b43b6 |
570 | is( $results->count(), 1, 'No duplicate row created' ); |
a78e3fed |
571 | |
572 | my $obj10_3 = $results->first(); |
3980d69c |
573 | isa_ok( $obj10_3, $class10 ); |
a78e3fed |
574 | is( $obj10_3->loader_test11()->id(), $obj11->id(), |
892b43b6 |
575 | 'Circular rel leads back to same row' ); |
a78e3fed |
576 | } |
577 | } |
4ce22656 |
578 | |
579 | SKIP: { |
e7213f4f |
580 | skip 'This vendor cannot do inline relationship definitions', 8 |
4ce22656 |
581 | if $self->{no_inline_rels}; |
582 | |
3980d69c |
583 | my $moniker12 = $monikers->{loader_test12}; |
584 | my $class12 = $classes->{loader_test12}; |
585 | my $rsobj12 = $conn->resultset($moniker12); |
586 | |
587 | my $moniker13 = $monikers->{loader_test13}; |
588 | my $class13 = $classes->{loader_test13}; |
589 | my $rsobj13 = $conn->resultset($moniker13); |
4ce22656 |
590 | |
591 | isa_ok( $rsobj12, "DBIx::Class::ResultSet" ); |
592 | isa_ok( $rsobj13, "DBIx::Class::ResultSet" ); |
593 | |
892b43b6 |
594 | ok($class13->column_info('id')->{is_foreign_key}, 'Foreign key detected'); |
595 | ok($class13->column_info('loader_test12')->{is_foreign_key}, 'Foreign key detected'); |
596 | ok($class13->column_info('dat')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
597 | |
4ce22656 |
598 | my $obj13 = $rsobj13->find(1); |
3980d69c |
599 | isa_ok( $obj13->id, $class12 ); |
600 | isa_ok( $obj13->loader_test12, $class12); |
601 | isa_ok( $obj13->dat, $class12); |
4ce22656 |
602 | } |
52bf3f26 |
603 | |
604 | SKIP: { |
e7213f4f |
605 | skip 'This vendor cannot do out-of-line implicit rel defs', 4 |
52bf3f26 |
606 | if $self->{no_implicit_rels}; |
607 | my $moniker14 = $monikers->{loader_test14}; |
608 | my $class14 = $classes->{loader_test14}; |
609 | my $rsobj14 = $conn->resultset($moniker14); |
610 | |
611 | my $moniker15 = $monikers->{loader_test15}; |
612 | my $class15 = $classes->{loader_test15}; |
613 | my $rsobj15 = $conn->resultset($moniker15); |
614 | |
615 | isa_ok( $rsobj14, "DBIx::Class::ResultSet" ); |
616 | isa_ok( $rsobj15, "DBIx::Class::ResultSet" ); |
617 | |
892b43b6 |
618 | ok($class15->column_info('loader_test14')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
619 | |
52bf3f26 |
620 | my $obj15 = $rsobj15->find(1); |
621 | isa_ok( $obj15->loader_test14, $class14 ); |
622 | } |
3b7f80f9 |
623 | } |
a60b5b8d |
624 | |
3b7f80f9 |
625 | # rescan test |
626 | SKIP: { |
2328d1fc |
627 | skip $self->{skip_rels}, 4 if $self->{skip_rels}; |
390bbe3f |
628 | skip "Can't rescan dumped schema", 4 if $self->{dump}; |
3b7f80f9 |
629 | |
630 | my @statements_rescan = ( |
631 | qq{ |
657d4b58 |
632 | CREATE TABLE loader_test30 ( |
3b7f80f9 |
633 | id INTEGER NOT NULL PRIMARY KEY, |
634 | loader_test2 INTEGER NOT NULL, |
635 | FOREIGN KEY (loader_test2) REFERENCES loader_test2 (id) |
636 | ) $self->{innodb} |
637 | }, |
657d4b58 |
638 | q{ INSERT INTO loader_test30 (id,loader_test2) VALUES(123, 1) }, |
639 | q{ INSERT INTO loader_test30 (id,loader_test2) VALUES(321, 2) }, |
3b7f80f9 |
640 | ); |
641 | |
642 | my $dbh = $self->dbconnect(1); |
643 | $dbh->do($_) for @statements_rescan; |
644 | $dbh->disconnect; |
645 | |
646 | my @new = $conn->rescan; |
892b43b6 |
647 | is_deeply(\@new, [ qw/LoaderTest30/ ], "Rescan"); |
3b7f80f9 |
648 | |
657d4b58 |
649 | my $rsobj30 = $conn->resultset('LoaderTest30'); |
650 | isa_ok($rsobj30, 'DBIx::Class::ResultSet'); |
651 | my $obj30 = $rsobj30->find(123); |
652 | isa_ok( $obj30->loader_test2, $class2); |
e7213f4f |
653 | |
892b43b6 |
654 | ok($rsobj30->result_source->column_info('loader_test2')->{is_foreign_key}, |
655 | 'Foreign key detected'); |
a78e3fed |
656 | } |
df637f3d |
657 | |
658 | $self->{extra}->{run}->($conn, $monikers, $classes) if $self->{extra}->{run}; |
a78e3fed |
659 | } |
660 | |
d70c335f |
661 | sub check_no_duplicate_unique_constraints { |
662 | my ($class) = @_; |
663 | |
664 | # unique_constraints() automatically includes the PK, if any |
665 | my %uc_cols; |
666 | ++$uc_cols{ join ", ", @$_ } |
667 | for values %{ { $class->unique_constraints } }; |
668 | my $dup_uc = grep { $_ > 1 } values %uc_cols; |
669 | |
670 | is($dup_uc, 0, "duplicate unique constraints ($class)") |
671 | or diag "uc_cols: @{[ %uc_cols ]}"; |
672 | } |
673 | |
a78e3fed |
674 | sub dbconnect { |
675 | my ($self, $complain) = @_; |
676 | |
af96f52e |
677 | my $dbh = DBI->connect( |
a78e3fed |
678 | $self->{dsn}, $self->{user}, |
679 | $self->{password}, |
680 | { |
681 | RaiseError => $complain, |
682 | PrintError => $complain, |
683 | AutoCommit => 1, |
684 | } |
685 | ); |
af96f52e |
686 | |
687 | die "Failed to connect to database: $DBI::errstr" if !$dbh; |
688 | |
689 | return $dbh; |
a78e3fed |
690 | } |
691 | |
692 | sub create { |
693 | my $self = shift; |
694 | |
996be9ee |
695 | $self->{_created} = 1; |
696 | |
c1ac681d |
697 | my $make_auto_inc = $self->{auto_inc_cb} || sub {}; |
a78e3fed |
698 | my @statements = ( |
699 | qq{ |
22270947 |
700 | CREATE TABLE loader_test1s ( |
a78e3fed |
701 | id $self->{auto_inc_pk}, |
996be9ee |
702 | dat VARCHAR(32) NOT NULL UNIQUE |
66742793 |
703 | ) $self->{innodb} |
a78e3fed |
704 | }, |
22270947 |
705 | $make_auto_inc->(qw/loader_test1s id/), |
a78e3fed |
706 | |
22270947 |
707 | q{ INSERT INTO loader_test1s (dat) VALUES('foo') }, |
708 | q{ INSERT INTO loader_test1s (dat) VALUES('bar') }, |
709 | q{ INSERT INTO loader_test1s (dat) VALUES('baz') }, |
a78e3fed |
710 | |
711 | qq{ |
712 | CREATE TABLE loader_test2 ( |
713 | id $self->{auto_inc_pk}, |
996be9ee |
714 | dat VARCHAR(32) NOT NULL, |
715 | dat2 VARCHAR(32) NOT NULL, |
5223f24a |
716 | UNIQUE (dat2, dat) |
996be9ee |
717 | ) $self->{innodb} |
718 | }, |
c1ac681d |
719 | $make_auto_inc->(qw/loader_test2 id/), |
996be9ee |
720 | |
721 | q{ INSERT INTO loader_test2 (dat, dat2) VALUES('aaa', 'zzz') }, |
722 | q{ INSERT INTO loader_test2 (dat, dat2) VALUES('bbb', 'yyy') }, |
723 | q{ INSERT INTO loader_test2 (dat, dat2) VALUES('ccc', 'xxx') }, |
724 | q{ INSERT INTO loader_test2 (dat, dat2) VALUES('ddd', 'www') }, |
725 | |
726 | qq{ |
727 | CREATE TABLE LOADER_TEST23 ( |
728 | ID INTEGER NOT NULL PRIMARY KEY, |
729 | DAT VARCHAR(32) NOT NULL UNIQUE |
66742793 |
730 | ) $self->{innodb} |
a78e3fed |
731 | }, |
732 | |
996be9ee |
733 | qq{ |
734 | CREATE TABLE LoAdEr_test24 ( |
735 | iD INTEGER NOT NULL PRIMARY KEY, |
736 | DaT VARCHAR(32) NOT NULL UNIQUE |
737 | ) $self->{innodb} |
738 | }, |
a78e3fed |
739 | ); |
740 | |
741 | my @statements_reltests = ( |
742 | qq{ |
743 | CREATE TABLE loader_test3 ( |
744 | id INTEGER NOT NULL PRIMARY KEY, |
745 | dat VARCHAR(32) |
66742793 |
746 | ) $self->{innodb} |
a78e3fed |
747 | }, |
748 | |
66742793 |
749 | q{ INSERT INTO loader_test3 (id,dat) VALUES(1,'aaa') }, |
750 | q{ INSERT INTO loader_test3 (id,dat) VALUES(2,'bbb') }, |
751 | q{ INSERT INTO loader_test3 (id,dat) VALUES(3,'ccc') }, |
752 | q{ INSERT INTO loader_test3 (id,dat) VALUES(4,'ddd') }, |
a78e3fed |
753 | |
754 | qq{ |
755 | CREATE TABLE loader_test4 ( |
756 | id INTEGER NOT NULL PRIMARY KEY, |
757 | fkid INTEGER NOT NULL, |
758 | dat VARCHAR(32), |
6eecda8a |
759 | FOREIGN KEY( fkid ) REFERENCES loader_test3 (id) |
66742793 |
760 | ) $self->{innodb} |
a78e3fed |
761 | }, |
762 | |
66742793 |
763 | q{ INSERT INTO loader_test4 (id,fkid,dat) VALUES(123,1,'aaa') }, |
764 | q{ INSERT INTO loader_test4 (id,fkid,dat) VALUES(124,2,'bbb') }, |
765 | q{ INSERT INTO loader_test4 (id,fkid,dat) VALUES(125,3,'ccc') }, |
766 | q{ INSERT INTO loader_test4 (id,fkid,dat) VALUES(126,4,'ddd') }, |
a78e3fed |
767 | |
768 | qq{ |
769 | CREATE TABLE loader_test5 ( |
9b046d97 |
770 | id1 INTEGER NOT NULL, |
996be9ee |
771 | iD2 INTEGER NOT NULL, |
9b046d97 |
772 | dat VARCHAR(8), |
c9373b79 |
773 | PRIMARY KEY (id1,iD2) |
66742793 |
774 | ) $self->{innodb} |
a78e3fed |
775 | }, |
776 | |
c9373b79 |
777 | q{ INSERT INTO loader_test5 (id1,iD2,dat) VALUES (1,1,'aaa') }, |
a78e3fed |
778 | |
779 | qq{ |
780 | CREATE TABLE loader_test6 ( |
708c0939 |
781 | id INTEGER NOT NULL PRIMARY KEY, |
996be9ee |
782 | Id2 INTEGER, |
e7886624 |
783 | loader_test2_id INTEGER, |
9b046d97 |
784 | dat VARCHAR(8), |
e7886624 |
785 | FOREIGN KEY (loader_test2_id) REFERENCES loader_test2 (id), |
f8c39323 |
786 | FOREIGN KEY(id,Id2) REFERENCES loader_test5 (id1,iD2) |
66742793 |
787 | ) $self->{innodb} |
a78e3fed |
788 | }, |
789 | |
c9373b79 |
790 | (q{ INSERT INTO loader_test6 (id, Id2,loader_test2_id,dat) } . |
66742793 |
791 | q{ VALUES (1, 1,1,'aaa') }), |
a78e3fed |
792 | |
793 | qq{ |
794 | CREATE TABLE loader_test7 ( |
795 | id INTEGER NOT NULL PRIMARY KEY, |
796 | id2 VARCHAR(8) NOT NULL UNIQUE, |
9b046d97 |
797 | dat VARCHAR(8) |
66742793 |
798 | ) $self->{innodb} |
a78e3fed |
799 | }, |
800 | |
66742793 |
801 | q{ INSERT INTO loader_test7 (id,id2,dat) VALUES (1,'aaa','bbb') }, |
a78e3fed |
802 | |
803 | qq{ |
804 | CREATE TABLE loader_test8 ( |
805 | id INTEGER NOT NULL PRIMARY KEY, |
806 | loader_test7 VARCHAR(8) NOT NULL, |
9b046d97 |
807 | dat VARCHAR(8), |
a78e3fed |
808 | FOREIGN KEY (loader_test7) REFERENCES loader_test7 (id2) |
66742793 |
809 | ) $self->{innodb} |
a78e3fed |
810 | }, |
811 | |
812 | (q{ INSERT INTO loader_test8 (id,loader_test7,dat) } . |
66742793 |
813 | q{ VALUES (1,'aaa','bbb') }), |
a78e3fed |
814 | |
815 | qq{ |
816 | CREATE TABLE loader_test9 ( |
9b046d97 |
817 | loader_test9 VARCHAR(8) NOT NULL |
66742793 |
818 | ) $self->{innodb} |
a78e3fed |
819 | }, |
996be9ee |
820 | |
821 | qq{ |
822 | CREATE TABLE loader_test16 ( |
823 | id INTEGER NOT NULL PRIMARY KEY, |
824 | dat VARCHAR(8) |
825 | ) $self->{innodb} |
826 | }, |
827 | |
828 | qq{ INSERT INTO loader_test16 (id,dat) VALUES (2,'x16') }, |
829 | qq{ INSERT INTO loader_test16 (id,dat) VALUES (4,'y16') }, |
830 | qq{ INSERT INTO loader_test16 (id,dat) VALUES (6,'z16') }, |
831 | |
832 | qq{ |
833 | CREATE TABLE loader_test17 ( |
834 | id INTEGER NOT NULL PRIMARY KEY, |
835 | loader16_one INTEGER, |
836 | loader16_two INTEGER, |
837 | FOREIGN KEY (loader16_one) REFERENCES loader_test16 (id), |
838 | FOREIGN KEY (loader16_two) REFERENCES loader_test16 (id) |
839 | ) $self->{innodb} |
840 | }, |
841 | |
842 | qq{ INSERT INTO loader_test17 (id, loader16_one, loader16_two) VALUES (3, 2, 4) }, |
843 | qq{ INSERT INTO loader_test17 (id, loader16_one, loader16_two) VALUES (33, 4, 6) }, |
844 | |
845 | qq{ |
846 | CREATE TABLE loader_test18 ( |
847 | id INTEGER NOT NULL PRIMARY KEY, |
848 | dat VARCHAR(8) |
849 | ) $self->{innodb} |
850 | }, |
851 | |
852 | qq{ INSERT INTO loader_test18 (id,dat) VALUES (1,'x18') }, |
853 | qq{ INSERT INTO loader_test18 (id,dat) VALUES (2,'y18') }, |
854 | qq{ INSERT INTO loader_test18 (id,dat) VALUES (3,'z18') }, |
855 | |
856 | qq{ |
857 | CREATE TABLE loader_test19 ( |
858 | id INTEGER NOT NULL PRIMARY KEY, |
859 | dat VARCHAR(8) |
860 | ) $self->{innodb} |
861 | }, |
862 | |
863 | qq{ INSERT INTO loader_test19 (id,dat) VALUES (4,'x19') }, |
864 | qq{ INSERT INTO loader_test19 (id,dat) VALUES (5,'y19') }, |
865 | qq{ INSERT INTO loader_test19 (id,dat) VALUES (6,'z19') }, |
866 | |
867 | qq{ |
868 | CREATE TABLE loader_test20 ( |
869 | parent INTEGER NOT NULL, |
870 | child INTEGER NOT NULL, |
871 | PRIMARY KEY (parent, child), |
872 | FOREIGN KEY (parent) REFERENCES loader_test18 (id), |
873 | FOREIGN KEY (child) REFERENCES loader_test19 (id) |
874 | ) $self->{innodb} |
875 | }, |
876 | |
877 | q{ INSERT INTO loader_test20 (parent, child) VALUES (1,4) }, |
878 | q{ INSERT INTO loader_test20 (parent, child) VALUES (2,5) }, |
879 | q{ INSERT INTO loader_test20 (parent, child) VALUES (3,6) }, |
880 | |
881 | qq{ |
882 | CREATE TABLE loader_test21 ( |
883 | id INTEGER NOT NULL PRIMARY KEY, |
884 | dat VARCHAR(8) |
885 | ) $self->{innodb} |
886 | }, |
887 | |
888 | q{ INSERT INTO loader_test21 (id,dat) VALUES (7,'a21')}, |
889 | q{ INSERT INTO loader_test21 (id,dat) VALUES (11,'b21')}, |
890 | q{ INSERT INTO loader_test21 (id,dat) VALUES (13,'c21')}, |
891 | q{ INSERT INTO loader_test21 (id,dat) VALUES (17,'d21')}, |
892 | |
893 | qq{ |
894 | CREATE TABLE loader_test22 ( |
895 | parent INTEGER NOT NULL, |
896 | child INTEGER NOT NULL, |
897 | PRIMARY KEY (parent, child), |
898 | FOREIGN KEY (parent) REFERENCES loader_test21 (id), |
899 | FOREIGN KEY (child) REFERENCES loader_test21 (id) |
900 | ) $self->{innodb} |
901 | }, |
902 | |
903 | q{ INSERT INTO loader_test22 (parent, child) VALUES (7,11)}, |
904 | q{ INSERT INTO loader_test22 (parent, child) VALUES (11,13)}, |
905 | q{ INSERT INTO loader_test22 (parent, child) VALUES (13,17)}, |
25e3e91d |
906 | |
907 | qq{ |
908 | CREATE TABLE loader_test25 ( |
909 | id1 INTEGER NOT NULL, |
910 | id2 INTEGER NOT NULL, |
911 | dat VARCHAR(8), |
912 | PRIMARY KEY (id1,id2) |
913 | ) $self->{innodb} |
914 | }, |
915 | |
916 | q{ INSERT INTO loader_test25 (id1,id2,dat) VALUES (33,5,'x25') }, |
917 | q{ INSERT INTO loader_test25 (id1,id2,dat) VALUES (33,7,'y25') }, |
918 | q{ INSERT INTO loader_test25 (id1,id2,dat) VALUES (3,42,'z25') }, |
919 | |
920 | qq{ |
921 | CREATE TABLE loader_test26 ( |
922 | id INTEGER NOT NULL PRIMARY KEY, |
923 | rel1 INTEGER NOT NULL, |
924 | rel2 INTEGER NOT NULL, |
925 | FOREIGN KEY (id, rel1) REFERENCES loader_test25 (id1, id2), |
926 | FOREIGN KEY (id, rel2) REFERENCES loader_test25 (id1, id2) |
927 | ) $self->{innodb} |
928 | }, |
929 | |
930 | q{ INSERT INTO loader_test26 (id,rel1,rel2) VALUES (33,5,7) }, |
931 | q{ INSERT INTO loader_test26 (id,rel1,rel2) VALUES (3,42,42) }, |
26f1c8c9 |
932 | |
933 | qq{ |
934 | CREATE TABLE loader_test27 ( |
935 | id INTEGER NOT NULL PRIMARY KEY |
936 | ) $self->{innodb} |
937 | }, |
938 | |
939 | q{ INSERT INTO loader_test27 (id) VALUES (1) }, |
940 | q{ INSERT INTO loader_test27 (id) VALUES (2) }, |
941 | |
942 | qq{ |
943 | CREATE TABLE loader_test28 ( |
944 | id INTEGER NOT NULL PRIMARY KEY, |
945 | FOREIGN KEY (id) REFERENCES loader_test27 (id) |
946 | ) $self->{innodb} |
947 | }, |
948 | |
949 | q{ INSERT INTO loader_test28 (id) VALUES (1) }, |
950 | |
951 | qq{ |
952 | CREATE TABLE loader_test29 ( |
953 | id INTEGER NOT NULL PRIMARY KEY, |
92267f92 |
954 | fk INTEGER NOT NULL UNIQUE, |
26f1c8c9 |
955 | FOREIGN KEY (fk) REFERENCES loader_test27 (id) |
956 | ) $self->{innodb} |
957 | }, |
958 | |
959 | q{ INSERT INTO loader_test29 (id,fk) VALUES (1,1) }, |
7dba7c70 |
960 | |
961 | qq{ |
962 | CREATE TABLE loader_test31 ( |
963 | id INTEGER NOT NULL PRIMARY KEY |
964 | ) $self->{innodb} |
965 | }, |
966 | q{ INSERT INTO loader_test31 (id) VALUES (1) }, |
967 | |
968 | qq{ |
969 | CREATE TABLE loader_test32 ( |
970 | id INTEGER NOT NULL PRIMARY KEY, |
971 | rel1 INTEGER NOT NULL, |
0906d55b |
972 | rel2 INTEGER $self->{null}, |
7dba7c70 |
973 | FOREIGN KEY (rel1) REFERENCES loader_test31(id), |
974 | FOREIGN KEY (rel2) REFERENCES loader_test31(id) |
975 | ) $self->{innodb} |
976 | }, |
977 | q{ INSERT INTO loader_test32 (id,rel1) VALUES (1,1) }, |
978 | |
979 | qq{ |
980 | CREATE TABLE loader_test33 ( |
981 | id1 INTEGER NOT NULL, |
982 | id2 INTEGER NOT NULL, |
983 | PRIMARY KEY (id1,id2) |
984 | ) $self->{innodb} |
985 | }, |
986 | q{ INSERT INTO loader_test33 (id1,id2) VALUES (1,2) }, |
987 | |
988 | qq{ |
989 | CREATE TABLE loader_test34 ( |
990 | id INTEGER NOT NULL PRIMARY KEY, |
991 | rel1 INTEGER NOT NULL, |
0906d55b |
992 | rel2 INTEGER $self->{null}, |
7dba7c70 |
993 | FOREIGN KEY (id,rel1) REFERENCES loader_test33(id1,id2), |
994 | FOREIGN KEY (id,rel2) REFERENCES loader_test33(id1,id2) |
995 | ) $self->{innodb} |
996 | }, |
997 | q{ INSERT INTO loader_test34 (id,rel1) VALUES (1,2) }, |
a78e3fed |
998 | ); |
999 | |
1000 | my @statements_advanced = ( |
1001 | qq{ |
1002 | CREATE TABLE loader_test10 ( |
1003 | id10 $self->{auto_inc_pk}, |
1004 | subject VARCHAR(8), |
0906d55b |
1005 | loader_test11 INTEGER $self->{null} |
66742793 |
1006 | ) $self->{innodb} |
a78e3fed |
1007 | }, |
c1ac681d |
1008 | $make_auto_inc->(qw/loader_test10 id10/), |
a78e3fed |
1009 | |
1010 | qq{ |
1011 | CREATE TABLE loader_test11 ( |
1012 | id11 $self->{auto_inc_pk}, |
1013 | message VARCHAR(8) DEFAULT 'foo', |
0906d55b |
1014 | loader_test10 INTEGER $self->{null}, |
a78e3fed |
1015 | FOREIGN KEY (loader_test10) REFERENCES loader_test10 (id10) |
66742793 |
1016 | ) $self->{innodb} |
a78e3fed |
1017 | }, |
c1ac681d |
1018 | $make_auto_inc->(qw/loader_test11 id11/), |
a78e3fed |
1019 | |
1020 | (q{ ALTER TABLE loader_test10 ADD CONSTRAINT } . |
1021 | q{ loader_test11_fk FOREIGN KEY (loader_test11) } . |
66742793 |
1022 | q{ REFERENCES loader_test11 (id11) }), |
a78e3fed |
1023 | ); |
1024 | |
4ce22656 |
1025 | my @statements_inline_rels = ( |
1026 | qq{ |
1027 | CREATE TABLE loader_test12 ( |
1028 | id INTEGER NOT NULL PRIMARY KEY, |
1029 | id2 VARCHAR(8) NOT NULL UNIQUE, |
cfb6add1 |
1030 | dat VARCHAR(8) NOT NULL UNIQUE |
4ce22656 |
1031 | ) $self->{innodb} |
1032 | }, |
1033 | |
1034 | q{ INSERT INTO loader_test12 (id,id2,dat) VALUES (1,'aaa','bbb') }, |
1035 | |
1036 | qq{ |
1037 | CREATE TABLE loader_test13 ( |
1038 | id INTEGER NOT NULL PRIMARY KEY REFERENCES loader_test12, |
1039 | loader_test12 VARCHAR(8) NOT NULL REFERENCES loader_test12 (id2), |
1040 | dat VARCHAR(8) REFERENCES loader_test12 (dat) |
1041 | ) $self->{innodb} |
1042 | }, |
1043 | |
1044 | (q{ INSERT INTO loader_test13 (id,loader_test12,dat) } . |
1045 | q{ VALUES (1,'aaa','bbb') }), |
1046 | ); |
1047 | |
1048 | |
52bf3f26 |
1049 | my @statements_implicit_rels = ( |
1050 | qq{ |
1051 | CREATE TABLE loader_test14 ( |
1052 | id INTEGER NOT NULL PRIMARY KEY, |
1053 | dat VARCHAR(8) |
1054 | ) $self->{innodb} |
1055 | }, |
1056 | |
1057 | q{ INSERT INTO loader_test14 (id,dat) VALUES (123,'aaa') }, |
1058 | |
1059 | qq{ |
1060 | CREATE TABLE loader_test15 ( |
1061 | id INTEGER NOT NULL PRIMARY KEY, |
1062 | loader_test14 INTEGER NOT NULL, |
1063 | FOREIGN KEY (loader_test14) REFERENCES loader_test14 |
1064 | ) $self->{innodb} |
1065 | }, |
1066 | |
1067 | q{ INSERT INTO loader_test15 (id,loader_test14) VALUES (1,123) }, |
a60b5b8d |
1068 | ); |
52bf3f26 |
1069 | |
af96f52e |
1070 | $self->drop_tables; |
1071 | |
a78e3fed |
1072 | my $dbh = $self->dbconnect(1); |
ddf91322 |
1073 | |
1074 | # Silence annoying but harmless postgres "NOTICE: CREATE TABLE..." |
1075 | local $SIG{__WARN__} = sub { |
1076 | my $msg = shift; |
1077 | print STDERR $msg unless $msg =~ m{^NOTICE:\s+CREATE TABLE}; |
1078 | }; |
1079 | |
a78e3fed |
1080 | $dbh->do($_) for (@statements); |
1081 | unless($self->{skip_rels}) { |
af96f52e |
1082 | # hack for now, since DB2 doesn't like inline comments, and we need |
1083 | # to test one for mysql, which works on everyone else... |
1084 | # this all needs to be refactored anyways. |
a78e3fed |
1085 | $dbh->do($_) for (@statements_reltests); |
1086 | unless($self->{vendor} =~ /sqlite/i) { |
1087 | $dbh->do($_) for (@statements_advanced); |
1088 | } |
4ce22656 |
1089 | unless($self->{no_inline_rels}) { |
1090 | $dbh->do($_) for (@statements_inline_rels); |
1091 | } |
52bf3f26 |
1092 | unless($self->{no_implicit_rels}) { |
1093 | $dbh->do($_) for (@statements_implicit_rels); |
1094 | } |
a78e3fed |
1095 | } |
df637f3d |
1096 | |
1097 | $dbh->do($_) for @{ $self->{extra}->{create} || [] }; |
a78e3fed |
1098 | $dbh->disconnect(); |
1099 | } |
1100 | |
af96f52e |
1101 | sub drop_tables { |
a78e3fed |
1102 | my $self = shift; |
1103 | |
a78e3fed |
1104 | my @tables = qw/ |
22270947 |
1105 | loader_test1s |
a78e3fed |
1106 | loader_test2 |
996be9ee |
1107 | LOADER_TEST23 |
1108 | LoAdEr_test24 |
a78e3fed |
1109 | /; |
c1ac681d |
1110 | |
1111 | my @tables_auto_inc = ( |
22270947 |
1112 | [ qw/loader_test1s id/ ], |
c1ac681d |
1113 | [ qw/loader_test2 id/ ], |
1114 | ); |
a78e3fed |
1115 | |
1116 | my @tables_reltests = qw/ |
1117 | loader_test4 |
1118 | loader_test3 |
1119 | loader_test6 |
1120 | loader_test5 |
1121 | loader_test8 |
1122 | loader_test7 |
1123 | loader_test9 |
996be9ee |
1124 | loader_test17 |
1125 | loader_test16 |
1126 | loader_test20 |
1127 | loader_test19 |
1128 | loader_test18 |
1129 | loader_test22 |
1130 | loader_test21 |
25e3e91d |
1131 | loader_test26 |
1132 | loader_test25 |
26f1c8c9 |
1133 | loader_test28 |
1134 | loader_test29 |
1135 | loader_test27 |
7dba7c70 |
1136 | loader_test32 |
1137 | loader_test31 |
1138 | loader_test34 |
1139 | loader_test33 |
a78e3fed |
1140 | /; |
1141 | |
1142 | my @tables_advanced = qw/ |
1143 | loader_test11 |
1144 | loader_test10 |
1145 | /; |
c1ac681d |
1146 | |
1147 | my @tables_advanced_auto_inc = ( |
1148 | [ qw/loader_test10 id10/ ], |
1149 | [ qw/loader_test11 id11/ ], |
1150 | ); |
a78e3fed |
1151 | |
4ce22656 |
1152 | my @tables_inline_rels = qw/ |
1153 | loader_test13 |
1154 | loader_test12 |
1155 | /; |
1156 | |
52bf3f26 |
1157 | my @tables_implicit_rels = qw/ |
1158 | loader_test15 |
1159 | loader_test14 |
1160 | /; |
1161 | |
657d4b58 |
1162 | my @tables_rescan = qw/ loader_test30 /; |
a60b5b8d |
1163 | |
a78e3fed |
1164 | my $drop_fk_mysql = |
c1ac681d |
1165 | q{ALTER TABLE loader_test10 DROP FOREIGN KEY loader_test11_fk}; |
a78e3fed |
1166 | |
1167 | my $drop_fk = |
c1ac681d |
1168 | q{ALTER TABLE loader_test10 DROP CONSTRAINT loader_test11_fk}; |
a78e3fed |
1169 | |
1170 | my $dbh = $self->dbconnect(0); |
1171 | |
df637f3d |
1172 | $dbh->do("DROP TABLE $_") for @{ $self->{extra}->{drop} || [] }; |
1173 | |
c1ac681d |
1174 | my $drop_auto_inc = $self->{auto_inc_drop_cb} || sub {}; |
1175 | |
a78e3fed |
1176 | unless($self->{skip_rels}) { |
1177 | $dbh->do("DROP TABLE $_") for (@tables_reltests); |
1178 | unless($self->{vendor} =~ /sqlite/i) { |
1179 | if($self->{vendor} =~ /mysql/i) { |
1180 | $dbh->do($drop_fk_mysql); |
1181 | } |
1182 | else { |
1183 | $dbh->do($drop_fk); |
1184 | } |
1185 | $dbh->do("DROP TABLE $_") for (@tables_advanced); |
c1ac681d |
1186 | $dbh->do($_) for map { $drop_auto_inc->(@$_) } @tables_advanced_auto_inc; |
a78e3fed |
1187 | } |
4ce22656 |
1188 | unless($self->{no_inline_rels}) { |
1189 | $dbh->do("DROP TABLE $_") for (@tables_inline_rels); |
1190 | } |
52bf3f26 |
1191 | unless($self->{no_implicit_rels}) { |
1192 | $dbh->do("DROP TABLE $_") for (@tables_implicit_rels); |
1193 | } |
a60b5b8d |
1194 | $dbh->do("DROP TABLE $_") for (@tables_rescan); |
1195 | } |
a78e3fed |
1196 | $dbh->do("DROP TABLE $_") for (@tables); |
c1ac681d |
1197 | $dbh->do($_) for map { $drop_auto_inc->(@$_) } @tables_auto_inc; |
af96f52e |
1198 | $dbh->disconnect; |
a78e3fed |
1199 | } |
1200 | |
996be9ee |
1201 | sub DESTROY { |
1202 | my $self = shift; |
565335e6 |
1203 | unless ($ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}) { |
1204 | $self->drop_tables if $self->{_created}; |
1205 | rmtree $DUMP_DIR |
1206 | } |
996be9ee |
1207 | } |
af96f52e |
1208 | |
a78e3fed |
1209 | 1; |