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 | |
aa63b6ff |
59 | plan tests => 140 + ($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'); |
aa63b6ff |
576 | is($class11->column_info('message')->{default_value},'foo', 'Default value check'); |
e7213f4f |
577 | |
7fc16540 |
578 | my $obj10 = $rsobj10->create({ subject => 'xyzzy' }); |
a78e3fed |
579 | |
7fc16540 |
580 | $obj10->update(); |
581 | ok( defined $obj10, 'Create row' ); |
a78e3fed |
582 | |
7fc16540 |
583 | my $obj11 = $rsobj11->create({ loader_test10 => $obj10->id() }); |
584 | $obj11->update(); |
585 | ok( defined $obj11, 'Create related row' ); |
a78e3fed |
586 | |
7fc16540 |
587 | eval { |
588 | my $obj10_2 = $obj11->loader_test10; |
589 | $obj10_2->loader_test11( $obj11->id11() ); |
590 | $obj10_2->update(); |
591 | }; |
592 | ok(!$@, "Setting up circular relationship"); |
a78e3fed |
593 | |
7fc16540 |
594 | SKIP: { |
595 | skip 'Previous eval block failed', 3 if $@; |
596 | |
597 | my $results = $rsobj10->search({ subject => 'xyzzy' }); |
598 | is( $results->count(), 1, 'No duplicate row created' ); |
a78e3fed |
599 | |
7fc16540 |
600 | my $obj10_3 = $results->first(); |
601 | isa_ok( $obj10_3, $class10 ); |
602 | is( $obj10_3->loader_test11()->id(), $obj11->id(), |
603 | 'Circular rel leads back to same row' ); |
a78e3fed |
604 | } |
4ce22656 |
605 | |
606 | SKIP: { |
e7213f4f |
607 | skip 'This vendor cannot do inline relationship definitions', 8 |
4ce22656 |
608 | if $self->{no_inline_rels}; |
609 | |
3980d69c |
610 | my $moniker12 = $monikers->{loader_test12}; |
611 | my $class12 = $classes->{loader_test12}; |
612 | my $rsobj12 = $conn->resultset($moniker12); |
613 | |
614 | my $moniker13 = $monikers->{loader_test13}; |
615 | my $class13 = $classes->{loader_test13}; |
616 | my $rsobj13 = $conn->resultset($moniker13); |
4ce22656 |
617 | |
618 | isa_ok( $rsobj12, "DBIx::Class::ResultSet" ); |
619 | isa_ok( $rsobj13, "DBIx::Class::ResultSet" ); |
620 | |
892b43b6 |
621 | ok($class13->column_info('id')->{is_foreign_key}, 'Foreign key detected'); |
622 | ok($class13->column_info('loader_test12')->{is_foreign_key}, 'Foreign key detected'); |
623 | ok($class13->column_info('dat')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
624 | |
4ce22656 |
625 | my $obj13 = $rsobj13->find(1); |
3980d69c |
626 | isa_ok( $obj13->id, $class12 ); |
627 | isa_ok( $obj13->loader_test12, $class12); |
628 | isa_ok( $obj13->dat, $class12); |
4ce22656 |
629 | } |
52bf3f26 |
630 | |
631 | SKIP: { |
e7213f4f |
632 | skip 'This vendor cannot do out-of-line implicit rel defs', 4 |
52bf3f26 |
633 | if $self->{no_implicit_rels}; |
634 | my $moniker14 = $monikers->{loader_test14}; |
635 | my $class14 = $classes->{loader_test14}; |
636 | my $rsobj14 = $conn->resultset($moniker14); |
637 | |
638 | my $moniker15 = $monikers->{loader_test15}; |
639 | my $class15 = $classes->{loader_test15}; |
640 | my $rsobj15 = $conn->resultset($moniker15); |
641 | |
642 | isa_ok( $rsobj14, "DBIx::Class::ResultSet" ); |
643 | isa_ok( $rsobj15, "DBIx::Class::ResultSet" ); |
644 | |
892b43b6 |
645 | ok($class15->column_info('loader_test14')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
646 | |
52bf3f26 |
647 | my $obj15 = $rsobj15->find(1); |
648 | isa_ok( $obj15->loader_test14, $class14 ); |
649 | } |
3b7f80f9 |
650 | } |
a60b5b8d |
651 | |
4daef04f |
652 | # rescan and norewrite test |
3b7f80f9 |
653 | SKIP: { |
3b7f80f9 |
654 | my @statements_rescan = ( |
655 | qq{ |
657d4b58 |
656 | CREATE TABLE loader_test30 ( |
3b7f80f9 |
657 | id INTEGER NOT NULL PRIMARY KEY, |
658 | loader_test2 INTEGER NOT NULL, |
659 | FOREIGN KEY (loader_test2) REFERENCES loader_test2 (id) |
660 | ) $self->{innodb} |
661 | }, |
657d4b58 |
662 | q{ INSERT INTO loader_test30 (id,loader_test2) VALUES(123, 1) }, |
663 | q{ INSERT INTO loader_test30 (id,loader_test2) VALUES(321, 2) }, |
3b7f80f9 |
664 | ); |
665 | |
4daef04f |
666 | # get md5 |
667 | my $digest = Digest::MD5->new; |
668 | |
669 | my $find_cb = sub { |
670 | return if -d; |
671 | return if $_ eq 'LoaderTest30.pm'; |
672 | |
673 | open my $fh, '<', $_ or die "Could not open $_ for reading: $!"; |
674 | binmode $fh; |
675 | $digest->addfile($fh); |
676 | }; |
677 | |
678 | find $find_cb, $DUMP_DIR; |
679 | |
680 | my $before_digest = $digest->digest; |
681 | |
3b7f80f9 |
682 | my $dbh = $self->dbconnect(1); |
fdd8ff16 |
683 | |
684 | { |
685 | # Silence annoying but harmless postgres "NOTICE: CREATE TABLE..." |
686 | local $SIG{__WARN__} = sub { |
687 | my $msg = shift; |
688 | print STDERR $msg unless $msg =~ m{^NOTICE:\s+CREATE TABLE}; |
689 | }; |
690 | |
691 | $dbh->do($_) for @statements_rescan; |
692 | } |
693 | |
3b7f80f9 |
694 | $dbh->disconnect; |
695 | |
4daef04f |
696 | sleep 1; |
697 | |
698 | my @new = do { |
699 | # kill the 'Dumping manual schema' warnings |
700 | local $SIG{__WARN__} = sub {}; |
701 | $conn->rescan; |
702 | }; |
892b43b6 |
703 | is_deeply(\@new, [ qw/LoaderTest30/ ], "Rescan"); |
3b7f80f9 |
704 | |
4daef04f |
705 | $digest = Digest::MD5->new; |
706 | find $find_cb, $DUMP_DIR; |
707 | my $after_digest = $digest->digest; |
708 | |
709 | is $before_digest, $after_digest, |
710 | 'dumped files are not rewritten when there is no modification'; |
711 | |
657d4b58 |
712 | my $rsobj30 = $conn->resultset('LoaderTest30'); |
713 | isa_ok($rsobj30, 'DBIx::Class::ResultSet'); |
4daef04f |
714 | |
715 | skip 'no rels', 2 if $self->{skip_rels}; |
716 | |
657d4b58 |
717 | my $obj30 = $rsobj30->find(123); |
718 | isa_ok( $obj30->loader_test2, $class2); |
e7213f4f |
719 | |
892b43b6 |
720 | ok($rsobj30->result_source->column_info('loader_test2')->{is_foreign_key}, |
721 | 'Foreign key detected'); |
a78e3fed |
722 | } |
df637f3d |
723 | |
724 | $self->{extra}->{run}->($conn, $monikers, $classes) if $self->{extra}->{run}; |
a78e3fed |
725 | } |
726 | |
d70c335f |
727 | sub check_no_duplicate_unique_constraints { |
728 | my ($class) = @_; |
729 | |
730 | # unique_constraints() automatically includes the PK, if any |
731 | my %uc_cols; |
732 | ++$uc_cols{ join ", ", @$_ } |
733 | for values %{ { $class->unique_constraints } }; |
734 | my $dup_uc = grep { $_ > 1 } values %uc_cols; |
735 | |
736 | is($dup_uc, 0, "duplicate unique constraints ($class)") |
737 | or diag "uc_cols: @{[ %uc_cols ]}"; |
738 | } |
739 | |
a78e3fed |
740 | sub dbconnect { |
741 | my ($self, $complain) = @_; |
742 | |
af96f52e |
743 | my $dbh = DBI->connect( |
a78e3fed |
744 | $self->{dsn}, $self->{user}, |
745 | $self->{password}, |
746 | { |
747 | RaiseError => $complain, |
748 | PrintError => $complain, |
749 | AutoCommit => 1, |
750 | } |
751 | ); |
af96f52e |
752 | |
753 | die "Failed to connect to database: $DBI::errstr" if !$dbh; |
754 | |
755 | return $dbh; |
a78e3fed |
756 | } |
757 | |
758 | sub create { |
759 | my $self = shift; |
760 | |
996be9ee |
761 | $self->{_created} = 1; |
762 | |
c1ac681d |
763 | my $make_auto_inc = $self->{auto_inc_cb} || sub {}; |
fdd8ff16 |
764 | @statements = ( |
a78e3fed |
765 | qq{ |
22270947 |
766 | CREATE TABLE loader_test1s ( |
a78e3fed |
767 | id $self->{auto_inc_pk}, |
996be9ee |
768 | dat VARCHAR(32) NOT NULL UNIQUE |
66742793 |
769 | ) $self->{innodb} |
a78e3fed |
770 | }, |
22270947 |
771 | $make_auto_inc->(qw/loader_test1s id/), |
a78e3fed |
772 | |
22270947 |
773 | q{ INSERT INTO loader_test1s (dat) VALUES('foo') }, |
774 | q{ INSERT INTO loader_test1s (dat) VALUES('bar') }, |
775 | q{ INSERT INTO loader_test1s (dat) VALUES('baz') }, |
a78e3fed |
776 | |
777 | qq{ |
778 | CREATE TABLE loader_test2 ( |
779 | id $self->{auto_inc_pk}, |
996be9ee |
780 | dat VARCHAR(32) NOT NULL, |
781 | dat2 VARCHAR(32) NOT NULL, |
5223f24a |
782 | UNIQUE (dat2, dat) |
996be9ee |
783 | ) $self->{innodb} |
784 | }, |
c1ac681d |
785 | $make_auto_inc->(qw/loader_test2 id/), |
996be9ee |
786 | |
787 | q{ INSERT INTO loader_test2 (dat, dat2) VALUES('aaa', 'zzz') }, |
788 | q{ INSERT INTO loader_test2 (dat, dat2) VALUES('bbb', 'yyy') }, |
789 | q{ INSERT INTO loader_test2 (dat, dat2) VALUES('ccc', 'xxx') }, |
790 | q{ INSERT INTO loader_test2 (dat, dat2) VALUES('ddd', 'www') }, |
791 | |
792 | qq{ |
793 | CREATE TABLE LOADER_TEST23 ( |
794 | ID INTEGER NOT NULL PRIMARY KEY, |
795 | DAT VARCHAR(32) NOT NULL UNIQUE |
66742793 |
796 | ) $self->{innodb} |
a78e3fed |
797 | }, |
798 | |
996be9ee |
799 | qq{ |
800 | CREATE TABLE LoAdEr_test24 ( |
801 | iD INTEGER NOT NULL PRIMARY KEY, |
802 | DaT VARCHAR(32) NOT NULL UNIQUE |
803 | ) $self->{innodb} |
804 | }, |
a78e3fed |
805 | ); |
806 | |
fdd8ff16 |
807 | @statements_reltests = ( |
a78e3fed |
808 | qq{ |
809 | CREATE TABLE loader_test3 ( |
810 | id INTEGER NOT NULL PRIMARY KEY, |
811 | dat VARCHAR(32) |
66742793 |
812 | ) $self->{innodb} |
a78e3fed |
813 | }, |
814 | |
66742793 |
815 | q{ INSERT INTO loader_test3 (id,dat) VALUES(1,'aaa') }, |
816 | q{ INSERT INTO loader_test3 (id,dat) VALUES(2,'bbb') }, |
817 | q{ INSERT INTO loader_test3 (id,dat) VALUES(3,'ccc') }, |
818 | q{ INSERT INTO loader_test3 (id,dat) VALUES(4,'ddd') }, |
a78e3fed |
819 | |
820 | qq{ |
821 | CREATE TABLE loader_test4 ( |
822 | id INTEGER NOT NULL PRIMARY KEY, |
823 | fkid INTEGER NOT NULL, |
824 | dat VARCHAR(32), |
6eecda8a |
825 | FOREIGN KEY( fkid ) REFERENCES loader_test3 (id) |
66742793 |
826 | ) $self->{innodb} |
a78e3fed |
827 | }, |
828 | |
66742793 |
829 | q{ INSERT INTO loader_test4 (id,fkid,dat) VALUES(123,1,'aaa') }, |
830 | q{ INSERT INTO loader_test4 (id,fkid,dat) VALUES(124,2,'bbb') }, |
831 | q{ INSERT INTO loader_test4 (id,fkid,dat) VALUES(125,3,'ccc') }, |
832 | q{ INSERT INTO loader_test4 (id,fkid,dat) VALUES(126,4,'ddd') }, |
a78e3fed |
833 | |
834 | qq{ |
835 | CREATE TABLE loader_test5 ( |
9b046d97 |
836 | id1 INTEGER NOT NULL, |
996be9ee |
837 | iD2 INTEGER NOT NULL, |
9b046d97 |
838 | dat VARCHAR(8), |
c9373b79 |
839 | PRIMARY KEY (id1,iD2) |
66742793 |
840 | ) $self->{innodb} |
a78e3fed |
841 | }, |
842 | |
c9373b79 |
843 | q{ INSERT INTO loader_test5 (id1,iD2,dat) VALUES (1,1,'aaa') }, |
a78e3fed |
844 | |
845 | qq{ |
846 | CREATE TABLE loader_test6 ( |
708c0939 |
847 | id INTEGER NOT NULL PRIMARY KEY, |
996be9ee |
848 | Id2 INTEGER, |
e7886624 |
849 | loader_test2_id INTEGER, |
9b046d97 |
850 | dat VARCHAR(8), |
e7886624 |
851 | FOREIGN KEY (loader_test2_id) REFERENCES loader_test2 (id), |
f8c39323 |
852 | FOREIGN KEY(id,Id2) REFERENCES loader_test5 (id1,iD2) |
66742793 |
853 | ) $self->{innodb} |
a78e3fed |
854 | }, |
855 | |
c9373b79 |
856 | (q{ INSERT INTO loader_test6 (id, Id2,loader_test2_id,dat) } . |
66742793 |
857 | q{ VALUES (1, 1,1,'aaa') }), |
a78e3fed |
858 | |
859 | qq{ |
860 | CREATE TABLE loader_test7 ( |
861 | id INTEGER NOT NULL PRIMARY KEY, |
862 | id2 VARCHAR(8) NOT NULL UNIQUE, |
9b046d97 |
863 | dat VARCHAR(8) |
66742793 |
864 | ) $self->{innodb} |
a78e3fed |
865 | }, |
866 | |
66742793 |
867 | q{ INSERT INTO loader_test7 (id,id2,dat) VALUES (1,'aaa','bbb') }, |
a78e3fed |
868 | |
869 | qq{ |
870 | CREATE TABLE loader_test8 ( |
871 | id INTEGER NOT NULL PRIMARY KEY, |
872 | loader_test7 VARCHAR(8) NOT NULL, |
9b046d97 |
873 | dat VARCHAR(8), |
a78e3fed |
874 | FOREIGN KEY (loader_test7) REFERENCES loader_test7 (id2) |
66742793 |
875 | ) $self->{innodb} |
a78e3fed |
876 | }, |
877 | |
878 | (q{ INSERT INTO loader_test8 (id,loader_test7,dat) } . |
66742793 |
879 | q{ VALUES (1,'aaa','bbb') }), |
a78e3fed |
880 | |
881 | qq{ |
882 | CREATE TABLE loader_test9 ( |
9b046d97 |
883 | loader_test9 VARCHAR(8) NOT NULL |
66742793 |
884 | ) $self->{innodb} |
a78e3fed |
885 | }, |
996be9ee |
886 | |
887 | qq{ |
888 | CREATE TABLE loader_test16 ( |
889 | id INTEGER NOT NULL PRIMARY KEY, |
890 | dat VARCHAR(8) |
891 | ) $self->{innodb} |
892 | }, |
893 | |
894 | qq{ INSERT INTO loader_test16 (id,dat) VALUES (2,'x16') }, |
895 | qq{ INSERT INTO loader_test16 (id,dat) VALUES (4,'y16') }, |
896 | qq{ INSERT INTO loader_test16 (id,dat) VALUES (6,'z16') }, |
897 | |
898 | qq{ |
899 | CREATE TABLE loader_test17 ( |
900 | id INTEGER NOT NULL PRIMARY KEY, |
901 | loader16_one INTEGER, |
902 | loader16_two INTEGER, |
903 | FOREIGN KEY (loader16_one) REFERENCES loader_test16 (id), |
904 | FOREIGN KEY (loader16_two) REFERENCES loader_test16 (id) |
905 | ) $self->{innodb} |
906 | }, |
907 | |
908 | qq{ INSERT INTO loader_test17 (id, loader16_one, loader16_two) VALUES (3, 2, 4) }, |
909 | qq{ INSERT INTO loader_test17 (id, loader16_one, loader16_two) VALUES (33, 4, 6) }, |
910 | |
911 | qq{ |
912 | CREATE TABLE loader_test18 ( |
913 | id INTEGER NOT NULL PRIMARY KEY, |
914 | dat VARCHAR(8) |
915 | ) $self->{innodb} |
916 | }, |
917 | |
918 | qq{ INSERT INTO loader_test18 (id,dat) VALUES (1,'x18') }, |
919 | qq{ INSERT INTO loader_test18 (id,dat) VALUES (2,'y18') }, |
920 | qq{ INSERT INTO loader_test18 (id,dat) VALUES (3,'z18') }, |
921 | |
922 | qq{ |
923 | CREATE TABLE loader_test19 ( |
924 | id INTEGER NOT NULL PRIMARY KEY, |
925 | dat VARCHAR(8) |
926 | ) $self->{innodb} |
927 | }, |
928 | |
929 | qq{ INSERT INTO loader_test19 (id,dat) VALUES (4,'x19') }, |
930 | qq{ INSERT INTO loader_test19 (id,dat) VALUES (5,'y19') }, |
931 | qq{ INSERT INTO loader_test19 (id,dat) VALUES (6,'z19') }, |
932 | |
933 | qq{ |
934 | CREATE TABLE loader_test20 ( |
935 | parent INTEGER NOT NULL, |
936 | child INTEGER NOT NULL, |
937 | PRIMARY KEY (parent, child), |
938 | FOREIGN KEY (parent) REFERENCES loader_test18 (id), |
939 | FOREIGN KEY (child) REFERENCES loader_test19 (id) |
940 | ) $self->{innodb} |
941 | }, |
942 | |
943 | q{ INSERT INTO loader_test20 (parent, child) VALUES (1,4) }, |
944 | q{ INSERT INTO loader_test20 (parent, child) VALUES (2,5) }, |
945 | q{ INSERT INTO loader_test20 (parent, child) VALUES (3,6) }, |
946 | |
947 | qq{ |
948 | CREATE TABLE loader_test21 ( |
949 | id INTEGER NOT NULL PRIMARY KEY, |
950 | dat VARCHAR(8) |
951 | ) $self->{innodb} |
952 | }, |
953 | |
954 | q{ INSERT INTO loader_test21 (id,dat) VALUES (7,'a21')}, |
955 | q{ INSERT INTO loader_test21 (id,dat) VALUES (11,'b21')}, |
956 | q{ INSERT INTO loader_test21 (id,dat) VALUES (13,'c21')}, |
957 | q{ INSERT INTO loader_test21 (id,dat) VALUES (17,'d21')}, |
958 | |
959 | qq{ |
960 | CREATE TABLE loader_test22 ( |
961 | parent INTEGER NOT NULL, |
962 | child INTEGER NOT NULL, |
963 | PRIMARY KEY (parent, child), |
964 | FOREIGN KEY (parent) REFERENCES loader_test21 (id), |
965 | FOREIGN KEY (child) REFERENCES loader_test21 (id) |
966 | ) $self->{innodb} |
967 | }, |
968 | |
969 | q{ INSERT INTO loader_test22 (parent, child) VALUES (7,11)}, |
970 | q{ INSERT INTO loader_test22 (parent, child) VALUES (11,13)}, |
971 | q{ INSERT INTO loader_test22 (parent, child) VALUES (13,17)}, |
25e3e91d |
972 | |
973 | qq{ |
974 | CREATE TABLE loader_test25 ( |
975 | id1 INTEGER NOT NULL, |
976 | id2 INTEGER NOT NULL, |
977 | dat VARCHAR(8), |
978 | PRIMARY KEY (id1,id2) |
979 | ) $self->{innodb} |
980 | }, |
981 | |
982 | q{ INSERT INTO loader_test25 (id1,id2,dat) VALUES (33,5,'x25') }, |
983 | q{ INSERT INTO loader_test25 (id1,id2,dat) VALUES (33,7,'y25') }, |
984 | q{ INSERT INTO loader_test25 (id1,id2,dat) VALUES (3,42,'z25') }, |
985 | |
986 | qq{ |
987 | CREATE TABLE loader_test26 ( |
988 | id INTEGER NOT NULL PRIMARY KEY, |
989 | rel1 INTEGER NOT NULL, |
990 | rel2 INTEGER NOT NULL, |
991 | FOREIGN KEY (id, rel1) REFERENCES loader_test25 (id1, id2), |
992 | FOREIGN KEY (id, rel2) REFERENCES loader_test25 (id1, id2) |
993 | ) $self->{innodb} |
994 | }, |
995 | |
996 | q{ INSERT INTO loader_test26 (id,rel1,rel2) VALUES (33,5,7) }, |
997 | q{ INSERT INTO loader_test26 (id,rel1,rel2) VALUES (3,42,42) }, |
26f1c8c9 |
998 | |
999 | qq{ |
1000 | CREATE TABLE loader_test27 ( |
1001 | id INTEGER NOT NULL PRIMARY KEY |
1002 | ) $self->{innodb} |
1003 | }, |
1004 | |
1005 | q{ INSERT INTO loader_test27 (id) VALUES (1) }, |
1006 | q{ INSERT INTO loader_test27 (id) VALUES (2) }, |
1007 | |
1008 | qq{ |
1009 | CREATE TABLE loader_test28 ( |
1010 | id INTEGER NOT NULL PRIMARY KEY, |
1011 | FOREIGN KEY (id) REFERENCES loader_test27 (id) |
1012 | ) $self->{innodb} |
1013 | }, |
1014 | |
1015 | q{ INSERT INTO loader_test28 (id) VALUES (1) }, |
1016 | |
1017 | qq{ |
1018 | CREATE TABLE loader_test29 ( |
1019 | id INTEGER NOT NULL PRIMARY KEY, |
92267f92 |
1020 | fk INTEGER NOT NULL UNIQUE, |
26f1c8c9 |
1021 | FOREIGN KEY (fk) REFERENCES loader_test27 (id) |
1022 | ) $self->{innodb} |
1023 | }, |
1024 | |
1025 | q{ INSERT INTO loader_test29 (id,fk) VALUES (1,1) }, |
7dba7c70 |
1026 | |
1027 | qq{ |
1028 | CREATE TABLE loader_test31 ( |
1029 | id INTEGER NOT NULL PRIMARY KEY |
1030 | ) $self->{innodb} |
1031 | }, |
1032 | q{ INSERT INTO loader_test31 (id) VALUES (1) }, |
1033 | |
1034 | qq{ |
1035 | CREATE TABLE loader_test32 ( |
1036 | id INTEGER NOT NULL PRIMARY KEY, |
1037 | rel1 INTEGER NOT NULL, |
0906d55b |
1038 | rel2 INTEGER $self->{null}, |
7dba7c70 |
1039 | FOREIGN KEY (rel1) REFERENCES loader_test31(id), |
1040 | FOREIGN KEY (rel2) REFERENCES loader_test31(id) |
1041 | ) $self->{innodb} |
1042 | }, |
1043 | q{ INSERT INTO loader_test32 (id,rel1) VALUES (1,1) }, |
1044 | |
1045 | qq{ |
1046 | CREATE TABLE loader_test33 ( |
1047 | id1 INTEGER NOT NULL, |
1048 | id2 INTEGER NOT NULL, |
1049 | PRIMARY KEY (id1,id2) |
1050 | ) $self->{innodb} |
1051 | }, |
1052 | q{ INSERT INTO loader_test33 (id1,id2) VALUES (1,2) }, |
1053 | |
1054 | qq{ |
1055 | CREATE TABLE loader_test34 ( |
1056 | id INTEGER NOT NULL PRIMARY KEY, |
1057 | rel1 INTEGER NOT NULL, |
0906d55b |
1058 | rel2 INTEGER $self->{null}, |
7dba7c70 |
1059 | FOREIGN KEY (id,rel1) REFERENCES loader_test33(id1,id2), |
1060 | FOREIGN KEY (id,rel2) REFERENCES loader_test33(id1,id2) |
1061 | ) $self->{innodb} |
1062 | }, |
1063 | q{ INSERT INTO loader_test34 (id,rel1) VALUES (1,2) }, |
a78e3fed |
1064 | ); |
1065 | |
fdd8ff16 |
1066 | @statements_advanced = ( |
a78e3fed |
1067 | qq{ |
1068 | CREATE TABLE loader_test10 ( |
1069 | id10 $self->{auto_inc_pk}, |
1070 | subject VARCHAR(8), |
0906d55b |
1071 | loader_test11 INTEGER $self->{null} |
66742793 |
1072 | ) $self->{innodb} |
a78e3fed |
1073 | }, |
c1ac681d |
1074 | $make_auto_inc->(qw/loader_test10 id10/), |
a78e3fed |
1075 | |
1076 | qq{ |
1077 | CREATE TABLE loader_test11 ( |
1078 | id11 $self->{auto_inc_pk}, |
1079 | message VARCHAR(8) DEFAULT 'foo', |
0906d55b |
1080 | loader_test10 INTEGER $self->{null}, |
a78e3fed |
1081 | FOREIGN KEY (loader_test10) REFERENCES loader_test10 (id10) |
66742793 |
1082 | ) $self->{innodb} |
a78e3fed |
1083 | }, |
c1ac681d |
1084 | $make_auto_inc->(qw/loader_test11 id11/), |
a78e3fed |
1085 | |
1086 | (q{ ALTER TABLE loader_test10 ADD CONSTRAINT } . |
1087 | q{ loader_test11_fk FOREIGN KEY (loader_test11) } . |
66742793 |
1088 | q{ REFERENCES loader_test11 (id11) }), |
a78e3fed |
1089 | ); |
1090 | |
fdd8ff16 |
1091 | @statements_advanced_sqlite = ( |
7fc16540 |
1092 | qq{ |
1093 | CREATE TABLE loader_test10 ( |
1094 | id10 $self->{auto_inc_pk}, |
1095 | subject VARCHAR(8) |
1096 | ) $self->{innodb} |
1097 | }, |
1098 | $make_auto_inc->(qw/loader_test10 id10/), |
1099 | |
1100 | qq{ |
1101 | CREATE TABLE loader_test11 ( |
1102 | id11 $self->{auto_inc_pk}, |
1103 | message VARCHAR(8) DEFAULT 'foo', |
1104 | loader_test10 INTEGER $self->{null}, |
1105 | FOREIGN KEY (loader_test10) REFERENCES loader_test10 (id10) |
1106 | ) $self->{innodb} |
1107 | }, |
1108 | $make_auto_inc->(qw/loader_test11 id11/), |
1109 | |
1110 | (q{ ALTER TABLE loader_test10 ADD COLUMN } . |
1111 | q{ loader_test11 INTEGER REFERENCES loader_test11 (id11) }), |
1112 | ); |
1113 | |
fdd8ff16 |
1114 | @statements_inline_rels = ( |
4ce22656 |
1115 | qq{ |
1116 | CREATE TABLE loader_test12 ( |
1117 | id INTEGER NOT NULL PRIMARY KEY, |
1118 | id2 VARCHAR(8) NOT NULL UNIQUE, |
cfb6add1 |
1119 | dat VARCHAR(8) NOT NULL UNIQUE |
4ce22656 |
1120 | ) $self->{innodb} |
1121 | }, |
1122 | |
1123 | q{ INSERT INTO loader_test12 (id,id2,dat) VALUES (1,'aaa','bbb') }, |
1124 | |
1125 | qq{ |
1126 | CREATE TABLE loader_test13 ( |
1127 | id INTEGER NOT NULL PRIMARY KEY REFERENCES loader_test12, |
1128 | loader_test12 VARCHAR(8) NOT NULL REFERENCES loader_test12 (id2), |
1129 | dat VARCHAR(8) REFERENCES loader_test12 (dat) |
1130 | ) $self->{innodb} |
1131 | }, |
1132 | |
1133 | (q{ INSERT INTO loader_test13 (id,loader_test12,dat) } . |
1134 | q{ VALUES (1,'aaa','bbb') }), |
1135 | ); |
1136 | |
1137 | |
fdd8ff16 |
1138 | @statements_implicit_rels = ( |
52bf3f26 |
1139 | qq{ |
1140 | CREATE TABLE loader_test14 ( |
1141 | id INTEGER NOT NULL PRIMARY KEY, |
1142 | dat VARCHAR(8) |
1143 | ) $self->{innodb} |
1144 | }, |
1145 | |
1146 | q{ INSERT INTO loader_test14 (id,dat) VALUES (123,'aaa') }, |
1147 | |
1148 | qq{ |
1149 | CREATE TABLE loader_test15 ( |
1150 | id INTEGER NOT NULL PRIMARY KEY, |
1151 | loader_test14 INTEGER NOT NULL, |
1152 | FOREIGN KEY (loader_test14) REFERENCES loader_test14 |
1153 | ) $self->{innodb} |
1154 | }, |
1155 | |
1156 | q{ INSERT INTO loader_test15 (id,loader_test14) VALUES (1,123) }, |
a60b5b8d |
1157 | ); |
52bf3f26 |
1158 | |
af96f52e |
1159 | $self->drop_tables; |
1160 | |
a78e3fed |
1161 | my $dbh = $self->dbconnect(1); |
ddf91322 |
1162 | |
1163 | # Silence annoying but harmless postgres "NOTICE: CREATE TABLE..." |
1164 | local $SIG{__WARN__} = sub { |
1165 | my $msg = shift; |
1166 | print STDERR $msg unless $msg =~ m{^NOTICE:\s+CREATE TABLE}; |
1167 | }; |
1168 | |
a78e3fed |
1169 | $dbh->do($_) for (@statements); |
1170 | unless($self->{skip_rels}) { |
af96f52e |
1171 | # hack for now, since DB2 doesn't like inline comments, and we need |
1172 | # to test one for mysql, which works on everyone else... |
1173 | # this all needs to be refactored anyways. |
a78e3fed |
1174 | $dbh->do($_) for (@statements_reltests); |
7fc16540 |
1175 | if($self->{vendor} =~ /sqlite/i) { |
1176 | $dbh->do($_) for (@statements_advanced_sqlite); |
1177 | } |
1178 | else { |
a78e3fed |
1179 | $dbh->do($_) for (@statements_advanced); |
1180 | } |
4ce22656 |
1181 | unless($self->{no_inline_rels}) { |
1182 | $dbh->do($_) for (@statements_inline_rels); |
1183 | } |
52bf3f26 |
1184 | unless($self->{no_implicit_rels}) { |
1185 | $dbh->do($_) for (@statements_implicit_rels); |
1186 | } |
a78e3fed |
1187 | } |
df637f3d |
1188 | |
1189 | $dbh->do($_) for @{ $self->{extra}->{create} || [] }; |
a78e3fed |
1190 | $dbh->disconnect(); |
1191 | } |
1192 | |
af96f52e |
1193 | sub drop_tables { |
a78e3fed |
1194 | my $self = shift; |
1195 | |
a78e3fed |
1196 | my @tables = qw/ |
22270947 |
1197 | loader_test1s |
a78e3fed |
1198 | loader_test2 |
996be9ee |
1199 | LOADER_TEST23 |
1200 | LoAdEr_test24 |
a78e3fed |
1201 | /; |
c1ac681d |
1202 | |
1203 | my @tables_auto_inc = ( |
22270947 |
1204 | [ qw/loader_test1s id/ ], |
c1ac681d |
1205 | [ qw/loader_test2 id/ ], |
1206 | ); |
a78e3fed |
1207 | |
1208 | my @tables_reltests = qw/ |
1209 | loader_test4 |
1210 | loader_test3 |
1211 | loader_test6 |
1212 | loader_test5 |
1213 | loader_test8 |
1214 | loader_test7 |
1215 | loader_test9 |
996be9ee |
1216 | loader_test17 |
1217 | loader_test16 |
1218 | loader_test20 |
1219 | loader_test19 |
1220 | loader_test18 |
1221 | loader_test22 |
1222 | loader_test21 |
25e3e91d |
1223 | loader_test26 |
1224 | loader_test25 |
26f1c8c9 |
1225 | loader_test28 |
1226 | loader_test29 |
1227 | loader_test27 |
7dba7c70 |
1228 | loader_test32 |
1229 | loader_test31 |
1230 | loader_test34 |
1231 | loader_test33 |
a78e3fed |
1232 | /; |
1233 | |
1234 | my @tables_advanced = qw/ |
1235 | loader_test11 |
1236 | loader_test10 |
1237 | /; |
c1ac681d |
1238 | |
1239 | my @tables_advanced_auto_inc = ( |
1240 | [ qw/loader_test10 id10/ ], |
1241 | [ qw/loader_test11 id11/ ], |
1242 | ); |
a78e3fed |
1243 | |
4ce22656 |
1244 | my @tables_inline_rels = qw/ |
1245 | loader_test13 |
1246 | loader_test12 |
1247 | /; |
1248 | |
52bf3f26 |
1249 | my @tables_implicit_rels = qw/ |
1250 | loader_test15 |
1251 | loader_test14 |
1252 | /; |
1253 | |
657d4b58 |
1254 | my @tables_rescan = qw/ loader_test30 /; |
a60b5b8d |
1255 | |
a78e3fed |
1256 | my $drop_fk_mysql = |
c1ac681d |
1257 | q{ALTER TABLE loader_test10 DROP FOREIGN KEY loader_test11_fk}; |
a78e3fed |
1258 | |
1259 | my $drop_fk = |
c1ac681d |
1260 | q{ALTER TABLE loader_test10 DROP CONSTRAINT loader_test11_fk}; |
a78e3fed |
1261 | |
1262 | my $dbh = $self->dbconnect(0); |
1263 | |
df637f3d |
1264 | $dbh->do("DROP TABLE $_") for @{ $self->{extra}->{drop} || [] }; |
1265 | |
c1ac681d |
1266 | my $drop_auto_inc = $self->{auto_inc_drop_cb} || sub {}; |
1267 | |
a78e3fed |
1268 | unless($self->{skip_rels}) { |
1269 | $dbh->do("DROP TABLE $_") for (@tables_reltests); |
7fc16540 |
1270 | if($self->{vendor} =~ /mysql/i) { |
1271 | $dbh->do($drop_fk_mysql); |
1272 | } |
1273 | else { |
1274 | $dbh->do($drop_fk); |
a78e3fed |
1275 | } |
7fc16540 |
1276 | $dbh->do("DROP TABLE $_") for (@tables_advanced); |
1277 | $dbh->do($_) for map { $drop_auto_inc->(@$_) } @tables_advanced_auto_inc; |
1278 | |
4ce22656 |
1279 | unless($self->{no_inline_rels}) { |
1280 | $dbh->do("DROP TABLE $_") for (@tables_inline_rels); |
1281 | } |
52bf3f26 |
1282 | unless($self->{no_implicit_rels}) { |
1283 | $dbh->do("DROP TABLE $_") for (@tables_implicit_rels); |
1284 | } |
a60b5b8d |
1285 | $dbh->do("DROP TABLE $_") for (@tables_rescan); |
1286 | } |
a78e3fed |
1287 | $dbh->do("DROP TABLE $_") for (@tables); |
c1ac681d |
1288 | $dbh->do($_) for map { $drop_auto_inc->(@$_) } @tables_auto_inc; |
af96f52e |
1289 | $dbh->disconnect; |
a78e3fed |
1290 | } |
1291 | |
996be9ee |
1292 | sub DESTROY { |
1293 | my $self = shift; |
565335e6 |
1294 | unless ($ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}) { |
1295 | $self->drop_tables if $self->{_created}; |
1296 | rmtree $DUMP_DIR |
1297 | } |
996be9ee |
1298 | } |
af96f52e |
1299 | |
a78e3fed |
1300 | 1; |