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