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