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