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