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