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