Commit | Line | Data |
fbd83464 |
1 | package dbixcsl_common_tests; |
a78e3fed |
2 | |
3 | use strict; |
3980d69c |
4 | use warnings; |
a78e3fed |
5 | |
6 | use Test::More; |
0c1d5b47 |
7 | use Test::Exception; |
18fca96a |
8 | use DBIx::Class::Schema::Loader; |
390bbe3f |
9 | use Class::Unload; |
10 | use File::Path; |
a78e3fed |
11 | use DBI; |
4daef04f |
12 | use Digest::MD5; |
13 | use File::Find 'find'; |
3a89a69f |
14 | use Class::Unload (); |
15efd63a |
15 | use DBIx::Class::Schema::Loader::Utils 'dumper_squashed'; |
26334ec1 |
16 | use List::MoreUtils 'apply'; |
ef8e9c69 |
17 | use DBIx::Class::Schema::Loader::Optional::Dependencies (); |
18 | use namespace::clean; |
a78e3fed |
19 | |
c213fd3d |
20 | use dbixcsl_test_dir qw/$tdir/; |
21 | |
22 | my $DUMP_DIR = "$tdir/common_dump"; |
390bbe3f |
23 | rmtree $DUMP_DIR; |
24 | |
a78e3fed |
25 | sub new { |
26 | my $class = shift; |
27 | |
28 | my $self; |
29 | |
30 | if( ref($_[0]) eq 'HASH') { |
31 | my $args = shift; |
32 | $self = { (%$args) }; |
33 | } |
34 | else { |
35 | $self = { @_ }; |
36 | } |
37 | |
38 | # Only MySQL uses this |
39 | $self->{innodb} ||= ''; |
0906d55b |
40 | |
ffb03c96 |
41 | # DB2 and Firebird don't support 'field type NULL' |
0906d55b |
42 | $self->{null} = 'NULL' unless defined $self->{null}; |
af6c2665 |
43 | |
44 | $self->{verbose} = $ENV{TEST_VERBOSE} || 0; |
a78e3fed |
45 | |
df637f3d |
46 | # Optional extra tables and tests |
47 | $self->{extra} ||= {}; |
48 | |
8dcf4292 |
49 | $self->{basic_date_datatype} ||= 'DATE'; |
4cbddf8d |
50 | |
e35e5a64 |
51 | # Not all DBS do SQL-standard CURRENT_TIMESTAMP |
6e566cc4 |
52 | $self->{default_function} ||= "current_timestamp"; |
53 | $self->{default_function_def} ||= "timestamp default $self->{default_function}"; |
e35e5a64 |
54 | |
9065de34 |
55 | $self = bless $self, $class; |
56 | |
57 | $self->setup_data_type_tests; |
58 | |
59 | return $self; |
a78e3fed |
60 | } |
61 | |
62 | sub skip_tests { |
63 | my ($self, $why) = @_; |
64 | |
65 | plan skip_all => $why; |
66 | } |
67 | |
4350370d |
68 | sub _monikerize { |
69 | my $name = shift; |
70 | return 'LoaderTest2X' if $name =~ /^loader_test2$/i; |
71 | return undef; |
72 | } |
73 | |
a78e3fed |
74 | sub run_tests { |
75 | my $self = shift; |
76 | |
3a89a69f |
77 | my @connect_info; |
a78e3fed |
78 | |
3a89a69f |
79 | if ($self->{dsn}) { |
80 | push @connect_info, [ @{$self}{qw/dsn user password connect_info_opts/ } ]; |
81 | } |
82 | else { |
83 | foreach my $info (@{ $self->{connect_info} || [] }) { |
84 | push @connect_info, [ @{$info}{qw/dsn user password connect_info_opts/ } ]; |
85 | } |
86 | } |
00805149 |
87 | |
88 | if ($ENV{SCHEMA_LOADER_TESTS_EXTRA_ONLY}) { |
89 | $self->run_only_extra_tests(\@connect_info); |
90 | return; |
91 | } |
a78e3fed |
92 | |
00805149 |
93 | my $extra_count = $self->{extra}{count} || 0; |
94 | |
6947b5d6 |
95 | plan tests => @connect_info * (183 + $extra_count + ($self->{data_type_tests}{test_count} || 0)); |
3a89a69f |
96 | |
97 | foreach my $info_idx (0..$#connect_info) { |
98 | my $info = $connect_info[$info_idx]; |
390bbe3f |
99 | |
3a89a69f |
100 | @{$self}{qw/dsn user password connect_info_opts/} = @$info; |
101 | |
102 | $self->create(); |
103 | |
00805149 |
104 | my $schema_class = $self->setup_schema($info); |
3a89a69f |
105 | $self->test_schema($schema_class); |
106 | |
107 | rmtree $DUMP_DIR |
108 | unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP} && $info_idx == $#connect_info; |
109 | } |
390bbe3f |
110 | } |
111 | |
00805149 |
112 | sub run_only_extra_tests { |
113 | my ($self, $connect_info) = @_; |
114 | |
1805763b |
115 | plan tests => @$connect_info * (4 + ($self->{extra}{count} || 0) + ($self->{data_type_tests}{test_count} || 0)); |
00805149 |
116 | |
33aa3462 |
117 | rmtree $DUMP_DIR; |
118 | |
04dcecc8 |
119 | foreach my $info_idx (0..$#$connect_info) { |
120 | my $info = $connect_info->[$info_idx]; |
121 | |
00805149 |
122 | @{$self}{qw/dsn user password connect_info_opts/} = @$info; |
123 | |
cf0ba25b |
124 | $self->drop_extra_tables_only; |
125 | |
126 | my $dbh = $self->dbconnect(1); |
c38ec663 |
127 | $dbh->do($_) for @{ $self->{extra}{create} || [] }; |
128 | $dbh->do($_) for @{ $self->{data_type_tests}{ddl} || []}; |
00805149 |
129 | $self->{_created} = 1; |
130 | |
cf0ba25b |
131 | my $file_count = grep /CREATE (?:TABLE|VIEW)/i, @{ $self->{extra}{create} || [] }; |
132 | $file_count++; # schema |
dd87d4c4 |
133 | $file_count++ for @{ $self->{data_type_tests}{table_names} || [] }; |
00805149 |
134 | |
cf0ba25b |
135 | my $schema_class = $self->setup_schema($info, $file_count); |
00805149 |
136 | my ($monikers, $classes) = $self->monikers_and_classes($schema_class); |
137 | my $conn = $schema_class->clone; |
138 | |
1805763b |
139 | $self->test_data_types($conn); |
00805149 |
140 | $self->{extra}{run}->($conn, $monikers, $classes) if $self->{extra}{run}; |
04dcecc8 |
141 | |
142 | if (not ($ENV{SCHEMA_LOADER_TESTS_NOCLEANUP} && $info_idx == $#$connect_info)) { |
cf0ba25b |
143 | $self->drop_extra_tables_only; |
04dcecc8 |
144 | rmtree $DUMP_DIR; |
145 | } |
00805149 |
146 | } |
147 | } |
148 | |
cf0ba25b |
149 | sub drop_extra_tables_only { |
150 | my $self = shift; |
151 | |
152 | my $dbh = $self->dbconnect(0); |
7b868481 |
153 | |
c38ec663 |
154 | $dbh->do($_) for @{ $self->{extra}{pre_drop_ddl} || [] }; |
cf0ba25b |
155 | $dbh->do("DROP TABLE $_") for @{ $self->{extra}{drop} || [] }; |
156 | |
3b29d0ac |
157 | foreach my $data_type_table (@{ $self->{data_type_tests}{table_names} || [] }) { |
cf0ba25b |
158 | $dbh->do("DROP TABLE $data_type_table"); |
159 | } |
160 | } |
161 | |
fdd8ff16 |
162 | # defined in sub create |
163 | my (@statements, @statements_reltests, @statements_advanced, |
164 | @statements_advanced_sqlite, @statements_inline_rels, |
165 | @statements_implicit_rels); |
166 | |
390bbe3f |
167 | sub setup_schema { |
00805149 |
168 | my ($self, $connect_info, $expected_count) = @_; |
390bbe3f |
169 | |
c2849787 |
170 | my $schema_class = 'DBIXCSL_Test::Schema'; |
a78e3fed |
171 | |
af6c2665 |
172 | my $debug = ($self->{verbose} > 1) ? 1 : 0; |
173 | |
ab0262bd |
174 | if ( |
175 | $ENV{SCHEMA_LOADER_TESTS_USE_MOOSE} |
176 | && |
177 | ! DBIx::Class::Schema::Loader::Optional::Dependencies->req_ok_for('use_moose') |
178 | ) { |
179 | die sprintf ("Missing dependencies for SCHEMA_LOADER_TESTS_USE_MOOSE: %s\n", |
180 | DBIx::Class::Schema::Loader::Optional::Dependencies->req_missing_for('use_moose') |
181 | ); |
182 | } |
6c2b21a5 |
183 | |
af96f52e |
184 | my %loader_opts = ( |
b1e43108 |
185 | constraint => |
ab0262bd |
186 | qr/^(?:\S+\.)?(?:(?:$self->{vendor}|extra)_?)?loader_?test[0-9]+(?!.*_)/i, |
507034ad |
187 | relationships => 1, |
188 | additional_classes => 'TestAdditional', |
189 | additional_base_classes => 'TestAdditionalBase', |
190 | left_base_classes => [ qw/TestLeftBase/ ], |
9fa99683 |
191 | components => [ qw/TestComponent/ ], |
e61e3fcb |
192 | resultset_components => [ qw/TestRSComponent/ ], |
996be9ee |
193 | inflect_plural => { loader_test4 => 'loader_test4zes' }, |
194 | inflect_singular => { fkid => 'fkid_singular' }, |
4350370d |
195 | moniker_map => \&_monikerize, |
a51d3566 |
196 | custom_column_info => \&_custom_column_info, |
507034ad |
197 | debug => $debug, |
f22644d7 |
198 | use_namespaces => 0, |
390bbe3f |
199 | dump_directory => $DUMP_DIR, |
42e785fa |
200 | datetime_timezone => 'Europe/Berlin', |
18e84656 |
201 | datetime_locale => 'de_DE', |
ab0262bd |
202 | use_moose => $ENV{SCHEMA_LOADER_TESTS_USE_MOOSE}, |
18e84656 |
203 | %{ $self->{loader_options} || {} }, |
af96f52e |
204 | ); |
205 | |
206 | $loader_opts{db_schema} = $self->{db_schema} if $self->{db_schema}; |
af96f52e |
207 | |
3a89a69f |
208 | Class::Unload->unload($schema_class); |
209 | |
ffb03c96 |
210 | my $file_count; |
996be9ee |
211 | { |
26da4cc3 |
212 | my @loader_warnings; |
c38ec663 |
213 | local $SIG{__WARN__} = sub { push(@loader_warnings, @_); }; |
26da4cc3 |
214 | eval qq{ |
215 | package $schema_class; |
216 | use base qw/DBIx::Class::Schema::Loader/; |
217 | |
218 | __PACKAGE__->loader_options(\%loader_opts); |
00805149 |
219 | __PACKAGE__->connection(\@\$connect_info); |
26da4cc3 |
220 | }; |
221 | |
222 | ok(!$@, "Loader initialization") or diag $@; |
acfcc1fb |
223 | |
26da4cc3 |
224 | find sub { return if -d; $file_count++ }, $DUMP_DIR; |
00805149 |
225 | |
226 | my $standard_sources = not defined $expected_count; |
227 | |
228 | if ($standard_sources) { |
dd87d4c4 |
229 | $expected_count = 36; |
230 | $expected_count++ for @{ $self->{data_type_tests}{table_names} || [] }; |
00805149 |
231 | |
232 | $expected_count += grep /CREATE (?:TABLE|VIEW)/i, |
233 | @{ $self->{extra}{create} || [] }; |
234 | |
235 | $expected_count -= grep /CREATE TABLE/, @statements_inline_rels |
236 | if $self->{skip_rels} || $self->{no_inline_rels}; |
237 | |
238 | $expected_count -= grep /CREATE TABLE/, @statements_implicit_rels |
239 | if $self->{skip_rels} || $self->{no_implicit_rels}; |
240 | |
241 | $expected_count -= grep /CREATE TABLE/, ($self->{vendor} =~ /sqlite/ ? @statements_advanced_sqlite : @statements_advanced), @statements_reltests |
242 | if $self->{skip_rels}; |
243 | } |
26da4cc3 |
244 | |
245 | is $file_count, $expected_count, 'correct number of files generated'; |
246 | |
247 | my $warn_count = 2; |
248 | $warn_count++ if grep /ResultSetManager/, @loader_warnings; |
249 | |
250 | $warn_count++ for grep /^Bad table or view/, @loader_warnings; |
251 | |
252 | $warn_count++ for grep /renaming \S+ relation/, @loader_warnings; |
253 | |
26da4cc3 |
254 | $warn_count++ for grep /\b(?!loader_test9)\w+ has no primary key/i, @loader_warnings; |
d073740e |
255 | |
b511f36e |
256 | $warn_count++ for grep { my $w = $_; grep $w =~ $_, @{ $self->{warnings} || [] } } @loader_warnings; |
257 | |
00805149 |
258 | if ($standard_sources) { |
259 | if($self->{skip_rels}) { |
260 | SKIP: { |
261 | is(scalar(@loader_warnings), $warn_count, "No loader warnings") |
262 | or diag @loader_warnings; |
263 | skip "No missing PK warnings without rels", 1; |
264 | } |
265 | } |
266 | else { |
267 | $warn_count++; |
268 | is(scalar(@loader_warnings), $warn_count, "Expected loader warning") |
58dfed76 |
269 | or diag @loader_warnings; |
00805149 |
270 | is(grep(/loader_test9 has no primary key/i, @loader_warnings), 1, |
271 | "Missing PK warning"); |
58dfed76 |
272 | } |
996be9ee |
273 | } |
274 | else { |
00805149 |
275 | SKIP: { |
cf0ba25b |
276 | is scalar(@loader_warnings), $warn_count, 'Correct number of warnings' |
277 | or diag @loader_warnings; |
00805149 |
278 | skip "not testing standard sources", 1; |
279 | } |
996be9ee |
280 | } |
281 | } |
ffb03c96 |
282 | |
283 | exit if $file_count != $expected_count; |
284 | |
390bbe3f |
285 | return $schema_class; |
286 | } |
287 | |
288 | sub test_schema { |
289 | my $self = shift; |
290 | my $schema_class = shift; |
a4a19f3c |
291 | |
996be9ee |
292 | my $conn = $schema_class->clone; |
243c6ebc |
293 | |
294 | ($self->{before_tests_run} || sub {})->($conn); |
295 | |
00805149 |
296 | my ($monikers, $classes) = $self->monikers_and_classes($schema_class); |
a4a19f3c |
297 | |
22270947 |
298 | my $moniker1 = $monikers->{loader_test1s}; |
299 | my $class1 = $classes->{loader_test1s}; |
3980d69c |
300 | my $rsobj1 = $conn->resultset($moniker1); |
d70c335f |
301 | check_no_duplicate_unique_constraints($class1); |
3980d69c |
302 | |
303 | my $moniker2 = $monikers->{loader_test2}; |
304 | my $class2 = $classes->{loader_test2}; |
305 | my $rsobj2 = $conn->resultset($moniker2); |
d70c335f |
306 | check_no_duplicate_unique_constraints($class2); |
a78e3fed |
307 | |
a1cec524 |
308 | my $moniker23 = $monikers->{LOADER_test23} || $monikers->{loader_test23}; |
309 | my $class23 = $classes->{LOADER_test23} || $classes->{loader_test23}; |
996be9ee |
310 | my $rsobj23 = $conn->resultset($moniker1); |
311 | |
4cbddf8d |
312 | my $moniker24 = $monikers->{LoAdEr_test24} || $monikers->{loader_test24}; |
313 | my $class24 = $classes->{LoAdEr_test24} || $classes->{loader_test24}; |
996be9ee |
314 | my $rsobj24 = $conn->resultset($moniker2); |
315 | |
e35e5a64 |
316 | my $moniker35 = $monikers->{loader_test35}; |
317 | my $class35 = $classes->{loader_test35}; |
318 | my $rsobj35 = $conn->resultset($moniker35); |
319 | |
af6c2665 |
320 | isa_ok( $rsobj1, "DBIx::Class::ResultSet" ); |
321 | isa_ok( $rsobj2, "DBIx::Class::ResultSet" ); |
996be9ee |
322 | isa_ok( $rsobj23, "DBIx::Class::ResultSet" ); |
323 | isa_ok( $rsobj24, "DBIx::Class::ResultSet" ); |
e35e5a64 |
324 | isa_ok( $rsobj35, "DBIx::Class::ResultSet" ); |
996be9ee |
325 | |
8ac8926d |
326 | my @columns_lt2 = $class2->columns; |
6947b5d6 |
327 | is_deeply( \@columns_lt2, [ qw/id dat dat2 set_primary_key can dbix_class_testcomponent meta/ ], "Column Ordering" ); |
328 | |
329 | is $class2->column_info('can')->{accessor}, undef, |
330 | 'accessor for column name that conflicts with a UNIVERSAL method removed'; |
9fdf3d5b |
331 | |
1e473081 |
332 | is $class2->column_info('set_primary_key')->{accessor}, undef, |
333 | 'accessor for column name that conflicts with a result base class method removed'; |
9fdf3d5b |
334 | |
1e473081 |
335 | is $class2->column_info('dbix_class_testcomponent')->{accessor}, undef, |
336 | 'accessor for column name that conflicts with a component class method removed'; |
8ac8926d |
337 | |
6c2b21a5 |
338 | is $class2->column_info('meta')->{accessor}, undef, |
339 | 'accessor for column name that conflicts with Moose removed'; |
340 | |
996be9ee |
341 | my %uniq1 = $class1->unique_constraints; |
342 | my $uniq1_test = 0; |
343 | foreach my $ucname (keys %uniq1) { |
344 | my $cols_arrayref = $uniq1{$ucname}; |
18e84656 |
345 | if(@$cols_arrayref == 1 && $cols_arrayref->[0] eq 'dat') { |
996be9ee |
346 | $uniq1_test = 1; |
347 | last; |
348 | } |
349 | } |
58dfed76 |
350 | ok($uniq1_test, "Unique constraint"); |
996be9ee |
351 | |
22270947 |
352 | is($moniker1, 'LoaderTest1', 'moniker singularisation'); |
353 | |
996be9ee |
354 | my %uniq2 = $class2->unique_constraints; |
355 | my $uniq2_test = 0; |
356 | foreach my $ucname (keys %uniq2) { |
357 | my $cols_arrayref = $uniq2{$ucname}; |
358 | if(@$cols_arrayref == 2 |
18e84656 |
359 | && $cols_arrayref->[0] eq 'dat2' |
360 | && $cols_arrayref->[1] eq 'dat') { |
996be9ee |
361 | $uniq2_test = 2; |
362 | last; |
363 | } |
364 | } |
58dfed76 |
365 | ok($uniq2_test, "Multi-col unique constraint"); |
a78e3fed |
366 | |
4350370d |
367 | is($moniker2, 'LoaderTest2X', "moniker_map testing"); |
368 | |
e61e3fcb |
369 | SKIP: { |
370 | can_ok( $class1, 'test_additional_base' ) |
371 | or skip "Pre-requisite test failed", 1; |
372 | is( $class1->test_additional_base, "test_additional_base", |
373 | "Additional Base method" ); |
374 | } |
c2849787 |
375 | |
e61e3fcb |
376 | SKIP: { |
377 | can_ok( $class1, 'test_additional_base_override' ) |
378 | or skip "Pre-requisite test failed", 1; |
379 | is( $class1->test_additional_base_override, |
380 | "test_left_base_override", |
381 | "Left Base overrides Additional Base method" ); |
382 | } |
c2849787 |
383 | |
e61e3fcb |
384 | SKIP: { |
385 | can_ok( $class1, 'test_additional_base_additional' ) |
386 | or skip "Pre-requisite test failed", 1; |
387 | is( $class1->test_additional_base_additional, "test_additional", |
388 | "Additional Base can use Additional package method" ); |
389 | } |
c2849787 |
390 | |
e61e3fcb |
391 | SKIP: { |
392 | can_ok( $class1, 'dbix_class_testcomponent' ) |
393 | or skip "Pre-requisite test failed", 1; |
394 | is( $class1->dbix_class_testcomponent, |
395 | 'dbix_class_testcomponent works', |
396 | 'Additional Component' ); |
397 | } |
c2849787 |
398 | |
e61e3fcb |
399 | SKIP: { |
400 | can_ok($rsobj1, 'dbix_class_testrscomponent') |
401 | or skip "Pre-requisite test failed", 1; |
402 | is( $rsobj1->dbix_class_testrscomponent, |
403 | 'dbix_class_testrscomponent works', |
404 | 'ResultSet component' ); |
c2849787 |
405 | } |
507034ad |
406 | |
78b7ccaa |
407 | SKIP: { |
e61e3fcb |
408 | can_ok( $class1, 'loader_test1_classmeth' ) |
409 | or skip "Pre-requisite test failed", 1; |
410 | is( $class1->loader_test1_classmeth, 'all is well', 'Class method' ); |
411 | } |
78b7ccaa |
412 | |
e61e3fcb |
413 | SKIP: { |
414 | can_ok( $rsobj1, 'loader_test1_rsmeth' ) |
415 | or skip "Pre-requisite test failed"; |
416 | is( $rsobj1->loader_test1_rsmeth, 'all is still well', 'Result set method' ); |
78b7ccaa |
417 | } |
18e84656 |
418 | |
419 | ok( $class1->column_info('id')->{is_auto_increment}, 'is_auto_increment detection' ); |
507034ad |
420 | |
af6c2665 |
421 | my $obj = $rsobj1->find(1); |
58dfed76 |
422 | is( $obj->id, 1, "Find got the right row" ); |
423 | is( $obj->dat, "foo", "Column value" ); |
424 | is( $rsobj2->count, 4, "Count" ); |
b71790b1 |
425 | my $saved_id; |
426 | eval { |
18e84656 |
427 | my $new_obj1 = $rsobj1->create({ dat => 'newthing' }); |
996be9ee |
428 | $saved_id = $new_obj1->id; |
b71790b1 |
429 | }; |
58dfed76 |
430 | ok(!$@, "Inserting new record using a PK::Auto key didn't die") or diag $@; |
431 | ok($saved_id, "Got PK::Auto-generated id"); |
b71790b1 |
432 | |
18e84656 |
433 | my $new_obj1 = $rsobj1->search({ dat => 'newthing' })->first; |
58dfed76 |
434 | ok($new_obj1, "Found newly inserted PK::Auto record"); |
435 | is($new_obj1->id, $saved_id, "Correct PK::Auto-generated id"); |
a78e3fed |
436 | |
18e84656 |
437 | my ($obj2) = $rsobj2->search({ dat => 'bbb' })->first; |
a78e3fed |
438 | is( $obj2->id, 2 ); |
439 | |
e35e5a64 |
440 | is( |
18e84656 |
441 | $class35->column_info('a_varchar')->{default_value}, 'foo', |
e35e5a64 |
442 | 'constant character default', |
443 | ); |
444 | |
445 | is( |
18e84656 |
446 | $class35->column_info('an_int')->{default_value}, 42, |
e35e5a64 |
447 | 'constant integer default', |
448 | ); |
449 | |
1b3e8f7a |
450 | is( |
451 | $class35->column_info('a_negative_int')->{default_value}, -42, |
452 | 'constant negative integer default', |
453 | ); |
454 | |
a60e0f45 |
455 | cmp_ok( |
456 | $class35->column_info('a_double')->{default_value}, '==', 10.555, |
e35e5a64 |
457 | 'constant numeric default', |
458 | ); |
459 | |
1b3e8f7a |
460 | cmp_ok( |
461 | $class35->column_info('a_negative_double')->{default_value}, '==', -10.555, |
462 | 'constant negative numeric default', |
463 | ); |
464 | |
18e84656 |
465 | my $function_default = $class35->column_info('a_function')->{default_value}; |
e35e5a64 |
466 | |
467 | isa_ok( $function_default, 'SCALAR', 'default_value for function default' ); |
468 | is_deeply( |
469 | $function_default, \$self->{default_function}, |
470 | 'default_value for function default is correct' |
471 | ); |
472 | |
a78e3fed |
473 | SKIP: { |
ee07e280 |
474 | skip $self->{skip_rels}, 116 if $self->{skip_rels}; |
a78e3fed |
475 | |
3980d69c |
476 | my $moniker3 = $monikers->{loader_test3}; |
477 | my $class3 = $classes->{loader_test3}; |
478 | my $rsobj3 = $conn->resultset($moniker3); |
479 | |
480 | my $moniker4 = $monikers->{loader_test4}; |
481 | my $class4 = $classes->{loader_test4}; |
482 | my $rsobj4 = $conn->resultset($moniker4); |
483 | |
484 | my $moniker5 = $monikers->{loader_test5}; |
485 | my $class5 = $classes->{loader_test5}; |
486 | my $rsobj5 = $conn->resultset($moniker5); |
487 | |
488 | my $moniker6 = $monikers->{loader_test6}; |
489 | my $class6 = $classes->{loader_test6}; |
490 | my $rsobj6 = $conn->resultset($moniker6); |
491 | |
492 | my $moniker7 = $monikers->{loader_test7}; |
493 | my $class7 = $classes->{loader_test7}; |
494 | my $rsobj7 = $conn->resultset($moniker7); |
495 | |
496 | my $moniker8 = $monikers->{loader_test8}; |
497 | my $class8 = $classes->{loader_test8}; |
498 | my $rsobj8 = $conn->resultset($moniker8); |
499 | |
500 | my $moniker9 = $monikers->{loader_test9}; |
501 | my $class9 = $classes->{loader_test9}; |
502 | my $rsobj9 = $conn->resultset($moniker9); |
af6c2665 |
503 | |
996be9ee |
504 | my $moniker16 = $monikers->{loader_test16}; |
505 | my $class16 = $classes->{loader_test16}; |
506 | my $rsobj16 = $conn->resultset($moniker16); |
507 | |
508 | my $moniker17 = $monikers->{loader_test17}; |
509 | my $class17 = $classes->{loader_test17}; |
510 | my $rsobj17 = $conn->resultset($moniker17); |
511 | |
512 | my $moniker18 = $monikers->{loader_test18}; |
513 | my $class18 = $classes->{loader_test18}; |
514 | my $rsobj18 = $conn->resultset($moniker18); |
515 | |
516 | my $moniker19 = $monikers->{loader_test19}; |
517 | my $class19 = $classes->{loader_test19}; |
518 | my $rsobj19 = $conn->resultset($moniker19); |
519 | |
520 | my $moniker20 = $monikers->{loader_test20}; |
521 | my $class20 = $classes->{loader_test20}; |
522 | my $rsobj20 = $conn->resultset($moniker20); |
523 | |
524 | my $moniker21 = $monikers->{loader_test21}; |
525 | my $class21 = $classes->{loader_test21}; |
526 | my $rsobj21 = $conn->resultset($moniker21); |
527 | |
528 | my $moniker22 = $monikers->{loader_test22}; |
529 | my $class22 = $classes->{loader_test22}; |
530 | my $rsobj22 = $conn->resultset($moniker22); |
531 | |
25e3e91d |
532 | my $moniker25 = $monikers->{loader_test25}; |
533 | my $class25 = $classes->{loader_test25}; |
534 | my $rsobj25 = $conn->resultset($moniker25); |
535 | |
536 | my $moniker26 = $monikers->{loader_test26}; |
537 | my $class26 = $classes->{loader_test26}; |
538 | my $rsobj26 = $conn->resultset($moniker26); |
539 | |
26f1c8c9 |
540 | my $moniker27 = $monikers->{loader_test27}; |
541 | my $class27 = $classes->{loader_test27}; |
542 | my $rsobj27 = $conn->resultset($moniker27); |
543 | |
544 | my $moniker28 = $monikers->{loader_test28}; |
545 | my $class28 = $classes->{loader_test28}; |
546 | my $rsobj28 = $conn->resultset($moniker28); |
547 | |
548 | my $moniker29 = $monikers->{loader_test29}; |
549 | my $class29 = $classes->{loader_test29}; |
550 | my $rsobj29 = $conn->resultset($moniker29); |
551 | |
7dba7c70 |
552 | my $moniker31 = $monikers->{loader_test31}; |
553 | my $class31 = $classes->{loader_test31}; |
554 | my $rsobj31 = $conn->resultset($moniker31); |
555 | |
556 | my $moniker32 = $monikers->{loader_test32}; |
557 | my $class32 = $classes->{loader_test32}; |
558 | my $rsobj32 = $conn->resultset($moniker32); |
559 | |
560 | my $moniker33 = $monikers->{loader_test33}; |
561 | my $class33 = $classes->{loader_test33}; |
562 | my $rsobj33 = $conn->resultset($moniker33); |
563 | |
564 | my $moniker34 = $monikers->{loader_test34}; |
565 | my $class34 = $classes->{loader_test34}; |
566 | my $rsobj34 = $conn->resultset($moniker34); |
567 | |
42e785fa |
568 | my $moniker36 = $monikers->{loader_test36}; |
569 | my $class36 = $classes->{loader_test36}; |
570 | my $rsobj36 = $conn->resultset($moniker36); |
571 | |
af6c2665 |
572 | isa_ok( $rsobj3, "DBIx::Class::ResultSet" ); |
573 | isa_ok( $rsobj4, "DBIx::Class::ResultSet" ); |
574 | isa_ok( $rsobj5, "DBIx::Class::ResultSet" ); |
575 | isa_ok( $rsobj6, "DBIx::Class::ResultSet" ); |
576 | isa_ok( $rsobj7, "DBIx::Class::ResultSet" ); |
577 | isa_ok( $rsobj8, "DBIx::Class::ResultSet" ); |
578 | isa_ok( $rsobj9, "DBIx::Class::ResultSet" ); |
996be9ee |
579 | isa_ok( $rsobj16, "DBIx::Class::ResultSet" ); |
580 | isa_ok( $rsobj17, "DBIx::Class::ResultSet" ); |
581 | isa_ok( $rsobj18, "DBIx::Class::ResultSet" ); |
582 | isa_ok( $rsobj19, "DBIx::Class::ResultSet" ); |
583 | isa_ok( $rsobj20, "DBIx::Class::ResultSet" ); |
584 | isa_ok( $rsobj21, "DBIx::Class::ResultSet" ); |
585 | isa_ok( $rsobj22, "DBIx::Class::ResultSet" ); |
25e3e91d |
586 | isa_ok( $rsobj25, "DBIx::Class::ResultSet" ); |
587 | isa_ok( $rsobj26, "DBIx::Class::ResultSet" ); |
26f1c8c9 |
588 | isa_ok( $rsobj27, "DBIx::Class::ResultSet" ); |
589 | isa_ok( $rsobj28, "DBIx::Class::ResultSet" ); |
590 | isa_ok( $rsobj29, "DBIx::Class::ResultSet" ); |
7dba7c70 |
591 | isa_ok( $rsobj31, "DBIx::Class::ResultSet" ); |
592 | isa_ok( $rsobj32, "DBIx::Class::ResultSet" ); |
593 | isa_ok( $rsobj33, "DBIx::Class::ResultSet" ); |
594 | isa_ok( $rsobj34, "DBIx::Class::ResultSet" ); |
42e785fa |
595 | isa_ok( $rsobj36, "DBIx::Class::ResultSet" ); |
a78e3fed |
596 | |
597 | # basic rel test |
af6c2665 |
598 | my $obj4 = $rsobj4->find(123); |
996be9ee |
599 | isa_ok( $obj4->fkid_singular, $class3); |
a78e3fed |
600 | |
18e84656 |
601 | ok($class4->column_info('fkid')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
602 | |
9fa99683 |
603 | my $obj3 = $rsobj3->find(1); |
4350370d |
604 | my $rs_rel4 = $obj3->search_related('loader_test4zes'); |
9fa99683 |
605 | isa_ok( $rs_rel4->first, $class4); |
c5de0b0c |
606 | |
c496748b |
607 | # check rel naming with prepositions |
2d945eb3 |
608 | ok ($rsobj4->result_source->has_relationship('loader_test5s_to'), |
c496748b |
609 | "rel with preposition 'to' pluralized correctly"); |
610 | |
2d945eb3 |
611 | ok ($rsobj4->result_source->has_relationship('loader_test5s_from'), |
c496748b |
612 | "rel with preposition 'from' pluralized correctly"); |
613 | |
53ef681d |
614 | # check default relationship attributes |
615 | is $rsobj3->result_source->relationship_info('loader_test4zes')->{attrs}{cascade_delete}, 0, |
616 | 'cascade_delete => 0 on has_many by default'; |
617 | |
618 | is $rsobj3->result_source->relationship_info('loader_test4zes')->{attrs}{cascade_copy}, 0, |
619 | 'cascade_copy => 0 on has_many by default'; |
620 | |
621 | ok ((not exists $rsobj3->result_source->relationship_info('loader_test4zes')->{attrs}{on_delete}), |
622 | 'has_many does not have on_delete'); |
623 | |
624 | ok ((not exists $rsobj3->result_source->relationship_info('loader_test4zes')->{attrs}{on_update}), |
625 | 'has_many does not have on_update'); |
626 | |
ee07e280 |
627 | ok ((not exists $rsobj3->result_source->relationship_info('loader_test4zes')->{attrs}{is_deferrable}), |
628 | 'has_many does not have is_deferrable'); |
629 | |
53ef681d |
630 | is $rsobj4->result_source->relationship_info('fkid_singular')->{attrs}{on_delete}, 'CASCADE', |
631 | "on_delete => 'CASCADE' on belongs_to by default"; |
632 | |
633 | is $rsobj4->result_source->relationship_info('fkid_singular')->{attrs}{on_update}, 'CASCADE', |
634 | "on_update => 'CASCADE' on belongs_to by default"; |
635 | |
aa0867ee |
636 | is $rsobj4->result_source->relationship_info('fkid_singular')->{attrs}{is_deferrable}, 1, |
637 | "is_deferrable => 1 on belongs_to by default"; |
ee07e280 |
638 | |
53ef681d |
639 | ok ((not exists $rsobj4->result_source->relationship_info('fkid_singular')->{attrs}{cascade_delete}), |
640 | 'belongs_to does not have cascade_delete'); |
641 | |
642 | ok ((not exists $rsobj4->result_source->relationship_info('fkid_singular')->{attrs}{cascade_copy}), |
643 | 'belongs_to does not have cascade_copy'); |
644 | |
645 | is $rsobj27->result_source->relationship_info('loader_test28')->{attrs}{cascade_delete}, 0, |
646 | 'cascade_delete => 0 on might_have by default'; |
647 | |
648 | is $rsobj27->result_source->relationship_info('loader_test28')->{attrs}{cascade_copy}, 0, |
649 | 'cascade_copy => 0 on might_have by default'; |
650 | |
651 | ok ((not exists $rsobj27->result_source->relationship_info('loader_test28')->{attrs}{on_delete}), |
652 | 'might_have does not have on_delete'); |
653 | |
654 | ok ((not exists $rsobj27->result_source->relationship_info('loader_test28')->{attrs}{on_update}), |
655 | 'might_have does not have on_update'); |
656 | |
ee07e280 |
657 | ok ((not exists $rsobj27->result_source->relationship_info('loader_test28')->{attrs}{is_deferrable}), |
658 | 'might_have does not have is_deferrable'); |
659 | |
996be9ee |
660 | # find on multi-col pk |
cc4f11a2 |
661 | if ($conn->_loader->preserve_case) { |
662 | my $obj5 = $rsobj5->find({id1 => 1, iD2 => 1}); |
663 | is $obj5->i_d2, 1, 'Find on multi-col PK'; |
664 | } |
665 | else { |
666 | my $obj5 = $rsobj5->find({id1 => 1, id2 => 1}); |
667 | is $obj5->id2, 1, 'Find on multi-col PK'; |
668 | } |
a78e3fed |
669 | |
4ce22656 |
670 | # mulit-col fk def |
af6c2665 |
671 | my $obj6 = $rsobj6->find(1); |
3980d69c |
672 | isa_ok( $obj6->loader_test2, $class2); |
673 | isa_ok( $obj6->loader_test5, $class5); |
a78e3fed |
674 | |
18e84656 |
675 | ok($class6->column_info('loader_test2_id')->{is_foreign_key}, 'Foreign key detected'); |
676 | ok($class6->column_info('id')->{is_foreign_key}, 'Foreign key detected'); |
948d632b |
677 | |
18e84656 |
678 | my $id2_info = eval { $class6->column_info('id2') } || |
679 | $class6->column_info('Id2'); |
948d632b |
680 | ok($id2_info->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
681 | |
a78e3fed |
682 | # fk that references a non-pk key (UNIQUE) |
af6c2665 |
683 | my $obj8 = $rsobj8->find(1); |
3980d69c |
684 | isa_ok( $obj8->loader_test7, $class7); |
a78e3fed |
685 | |
18e84656 |
686 | ok($class8->column_info('loader_test7')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
687 | |
996be9ee |
688 | # test double-fk 17 ->-> 16 |
689 | my $obj17 = $rsobj17->find(33); |
690 | |
691 | my $rs_rel16_one = $obj17->loader16_one; |
692 | isa_ok($rs_rel16_one, $class16); |
892b43b6 |
693 | is($rs_rel16_one->dat, 'y16', "Multiple FKs to same table"); |
996be9ee |
694 | |
18e84656 |
695 | ok($class17->column_info('loader16_one')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
696 | |
996be9ee |
697 | my $rs_rel16_two = $obj17->loader16_two; |
698 | isa_ok($rs_rel16_two, $class16); |
892b43b6 |
699 | is($rs_rel16_two->dat, 'z16', "Multiple FKs to same table"); |
996be9ee |
700 | |
18e84656 |
701 | ok($class17->column_info('loader16_two')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
702 | |
996be9ee |
703 | my $obj16 = $rsobj16->find(2); |
704 | my $rs_rel17 = $obj16->search_related('loader_test17_loader16_ones'); |
705 | isa_ok($rs_rel17->first, $class17); |
892b43b6 |
706 | is($rs_rel17->first->id, 3, "search_related with multiple FKs from same table"); |
996be9ee |
707 | |
708 | # XXX test m:m 18 <- 20 -> 19 |
18e84656 |
709 | ok($class20->column_info('parent')->{is_foreign_key}, 'Foreign key detected'); |
710 | ok($class20->column_info('child')->{is_foreign_key}, 'Foreign key detected'); |
996be9ee |
711 | |
712 | # XXX test double-fk m:m 21 <- 22 -> 21 |
18e84656 |
713 | ok($class22->column_info('parent')->{is_foreign_key}, 'Foreign key detected'); |
714 | ok($class22->column_info('child')->{is_foreign_key}, 'Foreign key detected'); |
996be9ee |
715 | |
25e3e91d |
716 | # test double multi-col fk 26 -> 25 |
717 | my $obj26 = $rsobj26->find(33); |
718 | |
719 | my $rs_rel25_one = $obj26->loader_test25_id_rel1; |
720 | isa_ok($rs_rel25_one, $class25); |
892b43b6 |
721 | is($rs_rel25_one->dat, 'x25', "Multiple multi-col FKs to same table"); |
25e3e91d |
722 | |
18e84656 |
723 | ok($class26->column_info('id')->{is_foreign_key}, 'Foreign key detected'); |
724 | ok($class26->column_info('rel1')->{is_foreign_key}, 'Foreign key detected'); |
725 | ok($class26->column_info('rel2')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
726 | |
25e3e91d |
727 | my $rs_rel25_two = $obj26->loader_test25_id_rel2; |
728 | isa_ok($rs_rel25_two, $class25); |
892b43b6 |
729 | is($rs_rel25_two->dat, 'y25', "Multiple multi-col FKs to same table"); |
25e3e91d |
730 | |
731 | my $obj25 = $rsobj25->find(3,42); |
732 | my $rs_rel26 = $obj25->search_related('loader_test26_id_rel1s'); |
733 | isa_ok($rs_rel26->first, $class26); |
892b43b6 |
734 | is($rs_rel26->first->id, 3, "search_related with multiple multi-col FKs from same table"); |
25e3e91d |
735 | |
26f1c8c9 |
736 | # test one-to-one rels |
737 | my $obj27 = $rsobj27->find(1); |
738 | my $obj28 = $obj27->loader_test28; |
739 | isa_ok($obj28, $class28); |
18e84656 |
740 | is($obj28->get_column('id'), 1, "One-to-one relationship with PRIMARY FK"); |
26f1c8c9 |
741 | |
18e84656 |
742 | ok($class28->column_info('id')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
743 | |
26f1c8c9 |
744 | my $obj29 = $obj27->loader_test29; |
745 | isa_ok($obj29, $class29); |
892b43b6 |
746 | is($obj29->id, 1, "One-to-one relationship with UNIQUE FK"); |
26f1c8c9 |
747 | |
18e84656 |
748 | ok($class29->column_info('fk')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
749 | |
26f1c8c9 |
750 | $obj27 = $rsobj27->find(2); |
892b43b6 |
751 | is($obj27->loader_test28, undef, "Undef for missing one-to-one row"); |
752 | is($obj27->loader_test29, undef, "Undef for missing one-to-one row"); |
26f1c8c9 |
753 | |
7dba7c70 |
754 | # test outer join for nullable referring columns: |
18e84656 |
755 | is $class32->column_info('rel2')->{is_nullable}, 1, |
0fa48bf5 |
756 | 'is_nullable detection'; |
757 | |
18e84656 |
758 | ok($class32->column_info('rel1')->{is_foreign_key}, 'Foreign key detected'); |
759 | ok($class32->column_info('rel2')->{is_foreign_key}, 'Foreign key detected'); |
0fa48bf5 |
760 | |
761 | my $obj32 = $rsobj32->find(1,{prefetch=>[qw/rel1 rel2/]}); |
762 | my $obj34 = $rsobj34->find( |
763 | 1,{prefetch=>[qw/loader_test33_id_rel1 loader_test33_id_rel2/]} |
764 | ); |
765 | isa_ok($obj32,$class32); |
766 | isa_ok($obj34,$class34); |
767 | |
18e84656 |
768 | ok($class34->column_info('id')->{is_foreign_key}, 'Foreign key detected'); |
769 | ok($class34->column_info('rel1')->{is_foreign_key}, 'Foreign key detected'); |
770 | ok($class34->column_info('rel2')->{is_foreign_key}, 'Foreign key detected'); |
0fa48bf5 |
771 | |
772 | my $rs_rel31_one = $obj32->rel1; |
773 | my $rs_rel31_two = $obj32->rel2; |
774 | isa_ok($rs_rel31_one, $class31); |
775 | is($rs_rel31_two, undef); |
776 | |
777 | my $rs_rel33_one = $obj34->loader_test33_id_rel1; |
778 | my $rs_rel33_two = $obj34->loader_test33_id_rel2; |
779 | |
780 | isa_ok($rs_rel33_one,$class33); |
781 | is($rs_rel33_two, undef); |
7dba7c70 |
782 | |
a78e3fed |
783 | # from Chisel's tests... |
7fc16540 |
784 | my $moniker10 = $monikers->{loader_test10}; |
785 | my $class10 = $classes->{loader_test10}; |
786 | my $rsobj10 = $conn->resultset($moniker10); |
3980d69c |
787 | |
7fc16540 |
788 | my $moniker11 = $monikers->{loader_test11}; |
789 | my $class11 = $classes->{loader_test11}; |
790 | my $rsobj11 = $conn->resultset($moniker11); |
a78e3fed |
791 | |
a51d3566 |
792 | isa_ok( $rsobj10, "DBIx::Class::ResultSet" ); |
7fc16540 |
793 | isa_ok( $rsobj11, "DBIx::Class::ResultSet" ); |
a78e3fed |
794 | |
18e84656 |
795 | ok($class10->column_info('loader_test11')->{is_foreign_key}, 'Foreign key detected'); |
796 | ok($class11->column_info('loader_test10')->{is_foreign_key}, 'Foreign key detected'); |
d67d058e |
797 | |
18e84656 |
798 | my $obj10 = $rsobj10->create({ subject => 'xyzzy' }); |
a78e3fed |
799 | |
7fc16540 |
800 | $obj10->update(); |
801 | ok( defined $obj10, 'Create row' ); |
a78e3fed |
802 | |
18e84656 |
803 | my $obj11 = $rsobj11->create({ loader_test10 => $obj10->id() }); |
7fc16540 |
804 | $obj11->update(); |
805 | ok( defined $obj11, 'Create related row' ); |
a78e3fed |
806 | |
7fc16540 |
807 | eval { |
808 | my $obj10_2 = $obj11->loader_test10; |
18e84656 |
809 | $obj10_2->update({ loader_test11 => $obj11->id11 }); |
7fc16540 |
810 | }; |
243c6ebc |
811 | diag $@ if $@; |
7fc16540 |
812 | ok(!$@, "Setting up circular relationship"); |
a78e3fed |
813 | |
7fc16540 |
814 | SKIP: { |
815 | skip 'Previous eval block failed', 3 if $@; |
816 | |
18e84656 |
817 | my $results = $rsobj10->search({ subject => 'xyzzy' }); |
7fc16540 |
818 | is( $results->count(), 1, 'No duplicate row created' ); |
a78e3fed |
819 | |
7fc16540 |
820 | my $obj10_3 = $results->first(); |
821 | isa_ok( $obj10_3, $class10 ); |
822 | is( $obj10_3->loader_test11()->id(), $obj11->id(), |
823 | 'Circular rel leads back to same row' ); |
a78e3fed |
824 | } |
4ce22656 |
825 | |
826 | SKIP: { |
e42ec4ef |
827 | skip 'This vendor cannot do inline relationship definitions', 9 |
4ce22656 |
828 | if $self->{no_inline_rels}; |
829 | |
3980d69c |
830 | my $moniker12 = $monikers->{loader_test12}; |
831 | my $class12 = $classes->{loader_test12}; |
832 | my $rsobj12 = $conn->resultset($moniker12); |
833 | |
834 | my $moniker13 = $monikers->{loader_test13}; |
835 | my $class13 = $classes->{loader_test13}; |
836 | my $rsobj13 = $conn->resultset($moniker13); |
4ce22656 |
837 | |
838 | isa_ok( $rsobj12, "DBIx::Class::ResultSet" ); |
839 | isa_ok( $rsobj13, "DBIx::Class::ResultSet" ); |
840 | |
18e84656 |
841 | ok($class13->column_info('id')->{is_foreign_key}, 'Foreign key detected'); |
842 | ok($class13->column_info('loader_test12')->{is_foreign_key}, 'Foreign key detected'); |
843 | ok($class13->column_info('dat')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
844 | |
4ce22656 |
845 | my $obj13 = $rsobj13->find(1); |
3980d69c |
846 | isa_ok( $obj13->id, $class12 ); |
847 | isa_ok( $obj13->loader_test12, $class12); |
848 | isa_ok( $obj13->dat, $class12); |
d13aecb1 |
849 | |
850 | my $obj12 = $rsobj12->find(1); |
851 | isa_ok( $obj12->loader_test13, $class13 ); |
4ce22656 |
852 | } |
52bf3f26 |
853 | |
854 | SKIP: { |
e7213f4f |
855 | skip 'This vendor cannot do out-of-line implicit rel defs', 4 |
52bf3f26 |
856 | if $self->{no_implicit_rels}; |
857 | my $moniker14 = $monikers->{loader_test14}; |
858 | my $class14 = $classes->{loader_test14}; |
859 | my $rsobj14 = $conn->resultset($moniker14); |
860 | |
861 | my $moniker15 = $monikers->{loader_test15}; |
862 | my $class15 = $classes->{loader_test15}; |
863 | my $rsobj15 = $conn->resultset($moniker15); |
864 | |
865 | isa_ok( $rsobj14, "DBIx::Class::ResultSet" ); |
866 | isa_ok( $rsobj15, "DBIx::Class::ResultSet" ); |
867 | |
18e84656 |
868 | ok($class15->column_info('loader_test14')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
869 | |
52bf3f26 |
870 | my $obj15 = $rsobj15->find(1); |
871 | isa_ok( $obj15->loader_test14, $class14 ); |
872 | } |
3b7f80f9 |
873 | } |
a60b5b8d |
874 | |
c5df7397 |
875 | # test custom_column_info and datetime_timezone/datetime_locale |
876 | { |
877 | my $class35 = $classes->{loader_test35}; |
878 | my $class36 = $classes->{loader_test36}; |
879 | |
18e84656 |
880 | ok($class35->column_info('an_int')->{is_numeric}, 'custom_column_info'); |
c5df7397 |
881 | |
18e84656 |
882 | is($class36->column_info('a_date')->{locale},'de_DE','datetime_locale'); |
883 | is($class36->column_info('a_date')->{timezone},'Europe/Berlin','datetime_timezone'); |
c5df7397 |
884 | |
18e84656 |
885 | ok($class36->column_info('b_char_as_data')->{inflate_datetime},'custom_column_info'); |
886 | is($class36->column_info('b_char_as_data')->{locale},'de_DE','datetime_locale'); |
887 | is($class36->column_info('b_char_as_data')->{timezone},'Europe/Berlin','datetime_timezone'); |
c5df7397 |
888 | |
18e84656 |
889 | ok($class36->column_info('c_char_as_data')->{inflate_date},'custom_column_info'); |
890 | is($class36->column_info('c_char_as_data')->{locale},'de_DE','datetime_locale'); |
891 | is($class36->column_info('c_char_as_data')->{timezone},'Europe/Berlin','datetime_timezone'); |
c5df7397 |
892 | } |
893 | |
4daef04f |
894 | # rescan and norewrite test |
0c1d5b47 |
895 | { |
3b7f80f9 |
896 | my @statements_rescan = ( |
897 | qq{ |
657d4b58 |
898 | CREATE TABLE loader_test30 ( |
3b7f80f9 |
899 | id INTEGER NOT NULL PRIMARY KEY, |
900 | loader_test2 INTEGER NOT NULL, |
901 | FOREIGN KEY (loader_test2) REFERENCES loader_test2 (id) |
902 | ) $self->{innodb} |
903 | }, |
657d4b58 |
904 | q{ INSERT INTO loader_test30 (id,loader_test2) VALUES(123, 1) }, |
905 | q{ INSERT INTO loader_test30 (id,loader_test2) VALUES(321, 2) }, |
3b7f80f9 |
906 | ); |
907 | |
4daef04f |
908 | # get md5 |
909 | my $digest = Digest::MD5->new; |
910 | |
911 | my $find_cb = sub { |
912 | return if -d; |
b511f36e |
913 | return if /^(?:LoaderTest30|LoaderTest1|LoaderTest2X)\.pm\z/; |
4daef04f |
914 | |
915 | open my $fh, '<', $_ or die "Could not open $_ for reading: $!"; |
916 | binmode $fh; |
917 | $digest->addfile($fh); |
918 | }; |
919 | |
920 | find $find_cb, $DUMP_DIR; |
921 | |
b511f36e |
922 | # system "rm -f /tmp/before_rescan/* /tmp/after_rescan/*"; |
c213fd3d |
923 | # system "cp $tdir/common_dump/DBIXCSL_Test/Schema/*.pm /tmp/before_rescan"; |
b511f36e |
924 | |
fe8ba914 |
925 | my $before_digest = $digest->b64digest; |
4daef04f |
926 | |
bc5afe55 |
927 | $conn->storage->disconnect; # needed for Firebird and Informix |
3b7f80f9 |
928 | my $dbh = $self->dbconnect(1); |
c38ec663 |
929 | $dbh->do($_) for @statements_rescan; |
3b7f80f9 |
930 | $dbh->disconnect; |
931 | |
4daef04f |
932 | sleep 1; |
933 | |
934 | my @new = do { |
c38ec663 |
935 | local $SIG{__WARN__} = sub { warn @_ |
936 | unless $_[0] =~ /(?i:loader_test)\d+ has no primary key|^Dumping manual schema|^Schema dump completed/ |
937 | }; |
4daef04f |
938 | $conn->rescan; |
939 | }; |
892b43b6 |
940 | is_deeply(\@new, [ qw/LoaderTest30/ ], "Rescan"); |
3b7f80f9 |
941 | |
b511f36e |
942 | # system "cp t/_common_dump/DBIXCSL_Test/Schema/*.pm /tmp/after_rescan"; |
943 | |
4daef04f |
944 | $digest = Digest::MD5->new; |
945 | find $find_cb, $DUMP_DIR; |
fe8ba914 |
946 | my $after_digest = $digest->b64digest; |
4daef04f |
947 | |
948 | is $before_digest, $after_digest, |
949 | 'dumped files are not rewritten when there is no modification'; |
950 | |
657d4b58 |
951 | my $rsobj30 = $conn->resultset('LoaderTest30'); |
952 | isa_ok($rsobj30, 'DBIx::Class::ResultSet'); |
4daef04f |
953 | |
0c1d5b47 |
954 | SKIP: { |
955 | skip 'no rels', 2 if $self->{skip_rels}; |
956 | |
957 | my $obj30 = $rsobj30->find(123); |
958 | isa_ok( $obj30->loader_test2, $class2); |
959 | |
960 | ok($rsobj30->result_source->column_info('loader_test2')->{is_foreign_key}, |
961 | 'Foreign key detected'); |
962 | } |
963 | |
964 | $conn->storage->disconnect; # for Firebird |
965 | $conn->storage->dbh->do("DROP TABLE loader_test30"); |
4daef04f |
966 | |
0c1d5b47 |
967 | @new = do { |
c38ec663 |
968 | local $SIG{__WARN__} = sub { warn @_ |
969 | unless $_[0] =~ /(?i:loader_test)\d+ has no primary key|^Dumping manual schema|^Schema dump completed/ |
970 | }; |
0c1d5b47 |
971 | $conn->rescan; |
972 | }; |
973 | is_deeply(\@new, [], 'no new tables on rescan'); |
e7213f4f |
974 | |
0c1d5b47 |
975 | throws_ok { $conn->resultset('LoaderTest30') } |
976 | qr/Can't find source/, |
977 | 'source unregistered for dropped table after rescan'; |
a78e3fed |
978 | } |
df637f3d |
979 | |
1805763b |
980 | $self->test_data_types($conn); |
981 | |
982 | # run extra tests |
983 | $self->{extra}{run}->($conn, $monikers, $classes) if $self->{extra}{run}; |
984 | |
b511f36e |
985 | $self->test_preserve_case($conn); |
986 | |
1805763b |
987 | $self->drop_tables unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}; |
988 | |
989 | $conn->storage->disconnect; |
990 | } |
991 | |
992 | sub test_data_types { |
993 | my ($self, $conn) = @_; |
994 | |
26da4cc3 |
995 | if ($self->{data_type_tests}{test_count}) { |
996 | my $data_type_tests = $self->{data_type_tests}; |
9065de34 |
997 | |
3b29d0ac |
998 | foreach my $moniker (@{ $data_type_tests->{table_monikers} }) { |
999 | my $columns = $data_type_tests->{columns}{$moniker}; |
9065de34 |
1000 | |
3b29d0ac |
1001 | my $rsrc = $conn->resultset($moniker)->result_source; |
df956aad |
1002 | |
3b29d0ac |
1003 | while (my ($col_name, $expected_info) = each %$columns) { |
1004 | my %info = %{ $rsrc->column_info($col_name) }; |
1005 | delete @info{qw/is_nullable timezone locale sequence/}; |
df956aad |
1006 | |
15efd63a |
1007 | my $text_col_def = dumper_squashed \%info; |
1008 | |
1009 | my $text_expected_info = dumper_squashed $expected_info; |
df956aad |
1010 | |
3b29d0ac |
1011 | is_deeply \%info, $expected_info, |
1012 | "test column $col_name has definition: $text_col_def expecting: $text_expected_info"; |
1013 | } |
9065de34 |
1014 | } |
1015 | } |
00805149 |
1016 | } |
1017 | |
b511f36e |
1018 | sub test_preserve_case { |
1019 | my ($self, $conn) = @_; |
1020 | |
1021 | my ($oqt, $cqt) = $self->get_oqt_cqt(always => 1); # open quote, close quote |
1022 | |
c38ec663 |
1023 | my $dbh = $self->dbconnect; |
b511f36e |
1024 | |
c38ec663 |
1025 | $dbh->do($_) for ( |
b511f36e |
1026 | qq| |
1027 | CREATE TABLE ${oqt}LoaderTest40${cqt} ( |
1028 | ${oqt}Id${cqt} INTEGER NOT NULL PRIMARY KEY, |
1029 | ${oqt}Foo3Bar${cqt} VARCHAR(100) NOT NULL |
1030 | ) $self->{innodb} |
1031 | |, |
1032 | qq| |
1033 | CREATE TABLE ${oqt}LoaderTest41${cqt} ( |
1034 | ${oqt}Id${cqt} INTEGER NOT NULL PRIMARY KEY, |
1035 | ${oqt}LoaderTest40Id${cqt} INTEGER, |
1036 | FOREIGN KEY (${oqt}LoaderTest40Id${cqt}) REFERENCES ${oqt}LoaderTest40${cqt} (${oqt}Id${cqt}) |
1037 | ) $self->{innodb} |
1038 | |, |
1039 | qq| INSERT INTO ${oqt}LoaderTest40${cqt} VALUES (1, 'foo') |, |
1040 | qq| INSERT INTO ${oqt}LoaderTest41${cqt} VALUES (1, 1) |, |
c38ec663 |
1041 | ); |
b511f36e |
1042 | $conn->storage->disconnect; |
1043 | |
1044 | local $conn->_loader->{preserve_case} = 1; |
1045 | $conn->_loader->_setup; |
1046 | |
c38ec663 |
1047 | |
b511f36e |
1048 | { |
c38ec663 |
1049 | local $SIG{__WARN__} = sub { warn @_ |
1050 | unless $_[0] =~ /(?i:loader_test)\d+ has no primary key|^Dumping manual schema|^Schema dump completed/ |
1051 | }; |
b511f36e |
1052 | $conn->rescan; |
c38ec663 |
1053 | }; |
b511f36e |
1054 | |
1055 | if (not $self->{skip_rels}) { |
1056 | is $conn->resultset('LoaderTest41')->find(1)->loader_test40->foo3_bar, 'foo', |
1057 | 'rel and accessor for mixed-case column name in mixed case table'; |
1058 | } |
1059 | else { |
1060 | is $conn->resultset('LoaderTest40')->find(1)->foo3_bar, 'foo', |
1061 | 'accessor for mixed-case column name in mixed case table'; |
1062 | } |
1063 | } |
1064 | |
00805149 |
1065 | sub monikers_and_classes { |
1066 | my ($self, $schema_class) = @_; |
1067 | my ($monikers, $classes); |
1068 | |
1069 | foreach my $source_name ($schema_class->sources) { |
1070 | my $table_name = $schema_class->source($source_name)->from; |
1071 | |
1072 | $table_name = $$table_name if ref $table_name; |
1073 | |
1074 | $monikers->{$table_name} = $source_name; |
1075 | $classes->{$table_name} = $schema_class . q{::} . $source_name; |
1076 | |
1077 | # some DBs (Firebird) uppercase everything |
1078 | $monikers->{lc $table_name} = $source_name; |
1079 | $classes->{lc $table_name} = $schema_class . q{::} . $source_name; |
1080 | } |
1081 | |
1082 | return ($monikers, $classes); |
a78e3fed |
1083 | } |
1084 | |
d70c335f |
1085 | sub check_no_duplicate_unique_constraints { |
1086 | my ($class) = @_; |
1087 | |
1088 | # unique_constraints() automatically includes the PK, if any |
1089 | my %uc_cols; |
1090 | ++$uc_cols{ join ", ", @$_ } |
1091 | for values %{ { $class->unique_constraints } }; |
1092 | my $dup_uc = grep { $_ > 1 } values %uc_cols; |
1093 | |
1094 | is($dup_uc, 0, "duplicate unique constraints ($class)") |
1095 | or diag "uc_cols: @{[ %uc_cols ]}"; |
1096 | } |
1097 | |
a78e3fed |
1098 | sub dbconnect { |
1099 | my ($self, $complain) = @_; |
1100 | |
c0084472 |
1101 | require DBIx::Class::Storage::DBI; |
1102 | my $storage = DBIx::Class::Storage::DBI->new; |
1103 | |
1104 | $complain = defined $complain ? $complain : 1; |
1105 | |
1106 | $storage->connect_info([ |
1107 | @{ $self }{qw/dsn user password/}, |
1108 | { |
1109 | unsafe => 1, |
1110 | RaiseError => $complain, |
1111 | ShowErrorStatement => $complain, |
1112 | PrintError => 0, |
3a89a69f |
1113 | %{ $self->{connect_info_opts} || {} }, |
c0084472 |
1114 | }, |
1115 | ]); |
1116 | |
1117 | my $dbh = eval { $storage->dbh }; |
1118 | die "Failed to connect to database: $@" if !$dbh; |
af96f52e |
1119 | |
c0084472 |
1120 | $self->{storage} = $storage; # storage DESTROY disconnects |
af96f52e |
1121 | |
1122 | return $dbh; |
a78e3fed |
1123 | } |
1124 | |
b511f36e |
1125 | sub get_oqt_cqt { |
1126 | my $self = shift; |
1127 | my %opts = @_; |
1128 | |
1129 | if ((not $opts{always}) && $self->{preserve_case_mode_is_exclusive}) { |
1130 | return ('', ''); |
1131 | } |
1132 | |
1133 | # XXX should get quote_char from the storage of an initialized loader. |
1134 | my ($oqt, $cqt); # open quote, close quote |
1135 | if (ref $self->{quote_char}) { |
1136 | ($oqt, $cqt) = @{ $self->{quote_char} }; |
1137 | } |
1138 | else { |
1139 | $oqt = $cqt = $self->{quote_char} || ''; |
1140 | } |
1141 | |
1142 | return ($oqt, $cqt); |
1143 | } |
1144 | |
a78e3fed |
1145 | sub create { |
1146 | my $self = shift; |
1147 | |
996be9ee |
1148 | $self->{_created} = 1; |
1149 | |
c4a49993 |
1150 | $self->drop_tables; |
1151 | |
c1ac681d |
1152 | my $make_auto_inc = $self->{auto_inc_cb} || sub {}; |
fdd8ff16 |
1153 | @statements = ( |
a78e3fed |
1154 | qq{ |
22270947 |
1155 | CREATE TABLE loader_test1s ( |
a78e3fed |
1156 | id $self->{auto_inc_pk}, |
996be9ee |
1157 | dat VARCHAR(32) NOT NULL UNIQUE |
66742793 |
1158 | ) $self->{innodb} |
a78e3fed |
1159 | }, |
22270947 |
1160 | $make_auto_inc->(qw/loader_test1s id/), |
a78e3fed |
1161 | |
22270947 |
1162 | q{ INSERT INTO loader_test1s (dat) VALUES('foo') }, |
1163 | q{ INSERT INTO loader_test1s (dat) VALUES('bar') }, |
1164 | q{ INSERT INTO loader_test1s (dat) VALUES('baz') }, |
a78e3fed |
1165 | |
9fdf3d5b |
1166 | # also test method collision |
a78e3fed |
1167 | qq{ |
1168 | CREATE TABLE loader_test2 ( |
1169 | id $self->{auto_inc_pk}, |
996be9ee |
1170 | dat VARCHAR(32) NOT NULL, |
e35e5a64 |
1171 | dat2 VARCHAR(32) NOT NULL, |
c9486c78 |
1172 | set_primary_key INTEGER $self->{null}, |
6947b5d6 |
1173 | can INTEGER $self->{null}, |
c9486c78 |
1174 | dbix_class_testcomponent INTEGER $self->{null}, |
2b74a06b |
1175 | meta INTEGER $self->{null}, |
5223f24a |
1176 | UNIQUE (dat2, dat) |
996be9ee |
1177 | ) $self->{innodb} |
1178 | }, |
c1ac681d |
1179 | $make_auto_inc->(qw/loader_test2 id/), |
996be9ee |
1180 | |
1181 | q{ INSERT INTO loader_test2 (dat, dat2) VALUES('aaa', 'zzz') }, |
1182 | q{ INSERT INTO loader_test2 (dat, dat2) VALUES('bbb', 'yyy') }, |
1183 | q{ INSERT INTO loader_test2 (dat, dat2) VALUES('ccc', 'xxx') }, |
1184 | q{ INSERT INTO loader_test2 (dat, dat2) VALUES('ddd', 'www') }, |
1185 | |
1186 | qq{ |
a1cec524 |
1187 | CREATE TABLE LOADER_test23 ( |
996be9ee |
1188 | ID INTEGER NOT NULL PRIMARY KEY, |
1189 | DAT VARCHAR(32) NOT NULL UNIQUE |
66742793 |
1190 | ) $self->{innodb} |
a78e3fed |
1191 | }, |
1192 | |
996be9ee |
1193 | qq{ |
1194 | CREATE TABLE LoAdEr_test24 ( |
1195 | iD INTEGER NOT NULL PRIMARY KEY, |
1196 | DaT VARCHAR(32) NOT NULL UNIQUE |
1197 | ) $self->{innodb} |
1198 | }, |
e35e5a64 |
1199 | |
1200 | qq{ |
1201 | CREATE TABLE loader_test35 ( |
41968729 |
1202 | id INTEGER NOT NULL PRIMARY KEY, |
e35e5a64 |
1203 | a_varchar VARCHAR(100) DEFAULT 'foo', |
1204 | an_int INTEGER DEFAULT 42, |
1b3e8f7a |
1205 | a_negative_int INTEGER DEFAULT -42, |
e35e5a64 |
1206 | a_double DOUBLE PRECISION DEFAULT 10.555, |
1b3e8f7a |
1207 | a_negative_double DOUBLE PRECISION DEFAULT -10.555, |
e35e5a64 |
1208 | a_function $self->{default_function_def} |
1209 | ) $self->{innodb} |
1210 | }, |
42e785fa |
1211 | |
1212 | qq{ |
1213 | CREATE TABLE loader_test36 ( |
1214 | id INTEGER NOT NULL PRIMARY KEY, |
8dcf4292 |
1215 | a_date $self->{basic_date_datatype}, |
4f18b8b2 |
1216 | b_char_as_data VARCHAR(100), |
1217 | c_char_as_data VARCHAR(100) |
42e785fa |
1218 | ) $self->{innodb} |
1219 | }, |
a78e3fed |
1220 | ); |
1221 | |
c930f78b |
1222 | # some DBs require mixed case identifiers to be quoted |
b511f36e |
1223 | my ($oqt, $cqt) = $self->get_oqt_cqt; |
c930f78b |
1224 | |
fdd8ff16 |
1225 | @statements_reltests = ( |
a78e3fed |
1226 | qq{ |
1227 | CREATE TABLE loader_test3 ( |
1228 | id INTEGER NOT NULL PRIMARY KEY, |
1229 | dat VARCHAR(32) |
66742793 |
1230 | ) $self->{innodb} |
a78e3fed |
1231 | }, |
1232 | |
66742793 |
1233 | q{ INSERT INTO loader_test3 (id,dat) VALUES(1,'aaa') }, |
1234 | q{ INSERT INTO loader_test3 (id,dat) VALUES(2,'bbb') }, |
1235 | q{ INSERT INTO loader_test3 (id,dat) VALUES(3,'ccc') }, |
1236 | q{ INSERT INTO loader_test3 (id,dat) VALUES(4,'ddd') }, |
a78e3fed |
1237 | |
1238 | qq{ |
1239 | CREATE TABLE loader_test4 ( |
1240 | id INTEGER NOT NULL PRIMARY KEY, |
1241 | fkid INTEGER NOT NULL, |
1242 | dat VARCHAR(32), |
6eecda8a |
1243 | FOREIGN KEY( fkid ) REFERENCES loader_test3 (id) |
66742793 |
1244 | ) $self->{innodb} |
a78e3fed |
1245 | }, |
1246 | |
66742793 |
1247 | q{ INSERT INTO loader_test4 (id,fkid,dat) VALUES(123,1,'aaa') }, |
1248 | q{ INSERT INTO loader_test4 (id,fkid,dat) VALUES(124,2,'bbb') }, |
1249 | q{ INSERT INTO loader_test4 (id,fkid,dat) VALUES(125,3,'ccc') }, |
1250 | q{ INSERT INTO loader_test4 (id,fkid,dat) VALUES(126,4,'ddd') }, |
a78e3fed |
1251 | |
c930f78b |
1252 | qq| |
a78e3fed |
1253 | CREATE TABLE loader_test5 ( |
9b046d97 |
1254 | id1 INTEGER NOT NULL, |
c930f78b |
1255 | ${oqt}iD2${cqt} INTEGER NOT NULL, |
9b046d97 |
1256 | dat VARCHAR(8), |
3a89a69f |
1257 | from_id INTEGER $self->{null}, |
1258 | to_id INTEGER $self->{null}, |
c930f78b |
1259 | PRIMARY KEY (id1,${oqt}iD2${cqt}), |
c496748b |
1260 | FOREIGN KEY (from_id) REFERENCES loader_test4 (id), |
1261 | FOREIGN KEY (to_id) REFERENCES loader_test4 (id) |
66742793 |
1262 | ) $self->{innodb} |
c930f78b |
1263 | |, |
a78e3fed |
1264 | |
c930f78b |
1265 | qq| INSERT INTO loader_test5 (id1,${oqt}iD2${cqt},dat) VALUES (1,1,'aaa') |, |
a78e3fed |
1266 | |
c930f78b |
1267 | qq| |
a78e3fed |
1268 | CREATE TABLE loader_test6 ( |
708c0939 |
1269 | id INTEGER NOT NULL PRIMARY KEY, |
c930f78b |
1270 | ${oqt}Id2${cqt} INTEGER, |
e7886624 |
1271 | loader_test2_id INTEGER, |
9b046d97 |
1272 | dat VARCHAR(8), |
e7886624 |
1273 | FOREIGN KEY (loader_test2_id) REFERENCES loader_test2 (id), |
c930f78b |
1274 | FOREIGN KEY(id,${oqt}Id2${cqt}) REFERENCES loader_test5 (id1,${oqt}iD2${cqt}) |
66742793 |
1275 | ) $self->{innodb} |
c930f78b |
1276 | |, |
a78e3fed |
1277 | |
c930f78b |
1278 | (qq| INSERT INTO loader_test6 (id, ${oqt}Id2${cqt},loader_test2_id,dat) | . |
66742793 |
1279 | q{ VALUES (1, 1,1,'aaa') }), |
a78e3fed |
1280 | |
1281 | qq{ |
1282 | CREATE TABLE loader_test7 ( |
1283 | id INTEGER NOT NULL PRIMARY KEY, |
1284 | id2 VARCHAR(8) NOT NULL UNIQUE, |
9b046d97 |
1285 | dat VARCHAR(8) |
66742793 |
1286 | ) $self->{innodb} |
a78e3fed |
1287 | }, |
1288 | |
66742793 |
1289 | q{ INSERT INTO loader_test7 (id,id2,dat) VALUES (1,'aaa','bbb') }, |
a78e3fed |
1290 | |
1291 | qq{ |
1292 | CREATE TABLE loader_test8 ( |
1293 | id INTEGER NOT NULL PRIMARY KEY, |
1294 | loader_test7 VARCHAR(8) NOT NULL, |
9b046d97 |
1295 | dat VARCHAR(8), |
a78e3fed |
1296 | FOREIGN KEY (loader_test7) REFERENCES loader_test7 (id2) |
66742793 |
1297 | ) $self->{innodb} |
a78e3fed |
1298 | }, |
1299 | |
1300 | (q{ INSERT INTO loader_test8 (id,loader_test7,dat) } . |
66742793 |
1301 | q{ VALUES (1,'aaa','bbb') }), |
a78e3fed |
1302 | |
1303 | qq{ |
1304 | CREATE TABLE loader_test9 ( |
9b046d97 |
1305 | loader_test9 VARCHAR(8) NOT NULL |
66742793 |
1306 | ) $self->{innodb} |
a78e3fed |
1307 | }, |
996be9ee |
1308 | |
1309 | qq{ |
1310 | CREATE TABLE loader_test16 ( |
1311 | id INTEGER NOT NULL PRIMARY KEY, |
1312 | dat VARCHAR(8) |
1313 | ) $self->{innodb} |
1314 | }, |
1315 | |
1316 | qq{ INSERT INTO loader_test16 (id,dat) VALUES (2,'x16') }, |
1317 | qq{ INSERT INTO loader_test16 (id,dat) VALUES (4,'y16') }, |
1318 | qq{ INSERT INTO loader_test16 (id,dat) VALUES (6,'z16') }, |
1319 | |
1320 | qq{ |
1321 | CREATE TABLE loader_test17 ( |
1322 | id INTEGER NOT NULL PRIMARY KEY, |
1323 | loader16_one INTEGER, |
1324 | loader16_two INTEGER, |
1325 | FOREIGN KEY (loader16_one) REFERENCES loader_test16 (id), |
1326 | FOREIGN KEY (loader16_two) REFERENCES loader_test16 (id) |
1327 | ) $self->{innodb} |
1328 | }, |
1329 | |
1330 | qq{ INSERT INTO loader_test17 (id, loader16_one, loader16_two) VALUES (3, 2, 4) }, |
1331 | qq{ INSERT INTO loader_test17 (id, loader16_one, loader16_two) VALUES (33, 4, 6) }, |
1332 | |
1333 | qq{ |
1334 | CREATE TABLE loader_test18 ( |
1335 | id INTEGER NOT NULL PRIMARY KEY, |
1336 | dat VARCHAR(8) |
1337 | ) $self->{innodb} |
1338 | }, |
1339 | |
1340 | qq{ INSERT INTO loader_test18 (id,dat) VALUES (1,'x18') }, |
1341 | qq{ INSERT INTO loader_test18 (id,dat) VALUES (2,'y18') }, |
1342 | qq{ INSERT INTO loader_test18 (id,dat) VALUES (3,'z18') }, |
1343 | |
1344 | qq{ |
1345 | CREATE TABLE loader_test19 ( |
1346 | id INTEGER NOT NULL PRIMARY KEY, |
1347 | dat VARCHAR(8) |
1348 | ) $self->{innodb} |
1349 | }, |
1350 | |
1351 | qq{ INSERT INTO loader_test19 (id,dat) VALUES (4,'x19') }, |
1352 | qq{ INSERT INTO loader_test19 (id,dat) VALUES (5,'y19') }, |
1353 | qq{ INSERT INTO loader_test19 (id,dat) VALUES (6,'z19') }, |
1354 | |
1355 | qq{ |
1356 | CREATE TABLE loader_test20 ( |
1357 | parent INTEGER NOT NULL, |
1358 | child INTEGER NOT NULL, |
1359 | PRIMARY KEY (parent, child), |
1360 | FOREIGN KEY (parent) REFERENCES loader_test18 (id), |
1361 | FOREIGN KEY (child) REFERENCES loader_test19 (id) |
1362 | ) $self->{innodb} |
1363 | }, |
1364 | |
1365 | q{ INSERT INTO loader_test20 (parent, child) VALUES (1,4) }, |
1366 | q{ INSERT INTO loader_test20 (parent, child) VALUES (2,5) }, |
1367 | q{ INSERT INTO loader_test20 (parent, child) VALUES (3,6) }, |
1368 | |
1369 | qq{ |
1370 | CREATE TABLE loader_test21 ( |
1371 | id INTEGER NOT NULL PRIMARY KEY, |
1372 | dat VARCHAR(8) |
1373 | ) $self->{innodb} |
1374 | }, |
1375 | |
1376 | q{ INSERT INTO loader_test21 (id,dat) VALUES (7,'a21')}, |
1377 | q{ INSERT INTO loader_test21 (id,dat) VALUES (11,'b21')}, |
1378 | q{ INSERT INTO loader_test21 (id,dat) VALUES (13,'c21')}, |
1379 | q{ INSERT INTO loader_test21 (id,dat) VALUES (17,'d21')}, |
1380 | |
1381 | qq{ |
1382 | CREATE TABLE loader_test22 ( |
1383 | parent INTEGER NOT NULL, |
1384 | child INTEGER NOT NULL, |
1385 | PRIMARY KEY (parent, child), |
1386 | FOREIGN KEY (parent) REFERENCES loader_test21 (id), |
1387 | FOREIGN KEY (child) REFERENCES loader_test21 (id) |
1388 | ) $self->{innodb} |
1389 | }, |
1390 | |
1391 | q{ INSERT INTO loader_test22 (parent, child) VALUES (7,11)}, |
1392 | q{ INSERT INTO loader_test22 (parent, child) VALUES (11,13)}, |
1393 | q{ INSERT INTO loader_test22 (parent, child) VALUES (13,17)}, |
25e3e91d |
1394 | |
1395 | qq{ |
1396 | CREATE TABLE loader_test25 ( |
1397 | id1 INTEGER NOT NULL, |
1398 | id2 INTEGER NOT NULL, |
1399 | dat VARCHAR(8), |
1400 | PRIMARY KEY (id1,id2) |
1401 | ) $self->{innodb} |
1402 | }, |
1403 | |
1404 | q{ INSERT INTO loader_test25 (id1,id2,dat) VALUES (33,5,'x25') }, |
1405 | q{ INSERT INTO loader_test25 (id1,id2,dat) VALUES (33,7,'y25') }, |
1406 | q{ INSERT INTO loader_test25 (id1,id2,dat) VALUES (3,42,'z25') }, |
1407 | |
1408 | qq{ |
1409 | CREATE TABLE loader_test26 ( |
1410 | id INTEGER NOT NULL PRIMARY KEY, |
1411 | rel1 INTEGER NOT NULL, |
1412 | rel2 INTEGER NOT NULL, |
1413 | FOREIGN KEY (id, rel1) REFERENCES loader_test25 (id1, id2), |
1414 | FOREIGN KEY (id, rel2) REFERENCES loader_test25 (id1, id2) |
1415 | ) $self->{innodb} |
1416 | }, |
1417 | |
1418 | q{ INSERT INTO loader_test26 (id,rel1,rel2) VALUES (33,5,7) }, |
1419 | q{ INSERT INTO loader_test26 (id,rel1,rel2) VALUES (3,42,42) }, |
26f1c8c9 |
1420 | |
1421 | qq{ |
1422 | CREATE TABLE loader_test27 ( |
1423 | id INTEGER NOT NULL PRIMARY KEY |
1424 | ) $self->{innodb} |
1425 | }, |
1426 | |
1427 | q{ INSERT INTO loader_test27 (id) VALUES (1) }, |
1428 | q{ INSERT INTO loader_test27 (id) VALUES (2) }, |
1429 | |
1430 | qq{ |
1431 | CREATE TABLE loader_test28 ( |
1432 | id INTEGER NOT NULL PRIMARY KEY, |
1433 | FOREIGN KEY (id) REFERENCES loader_test27 (id) |
1434 | ) $self->{innodb} |
1435 | }, |
1436 | |
1437 | q{ INSERT INTO loader_test28 (id) VALUES (1) }, |
1438 | |
1439 | qq{ |
1440 | CREATE TABLE loader_test29 ( |
1441 | id INTEGER NOT NULL PRIMARY KEY, |
92267f92 |
1442 | fk INTEGER NOT NULL UNIQUE, |
26f1c8c9 |
1443 | FOREIGN KEY (fk) REFERENCES loader_test27 (id) |
1444 | ) $self->{innodb} |
1445 | }, |
1446 | |
1447 | q{ INSERT INTO loader_test29 (id,fk) VALUES (1,1) }, |
7dba7c70 |
1448 | |
1449 | qq{ |
1450 | CREATE TABLE loader_test31 ( |
1451 | id INTEGER NOT NULL PRIMARY KEY |
1452 | ) $self->{innodb} |
1453 | }, |
1454 | q{ INSERT INTO loader_test31 (id) VALUES (1) }, |
1455 | |
1456 | qq{ |
1457 | CREATE TABLE loader_test32 ( |
1458 | id INTEGER NOT NULL PRIMARY KEY, |
1459 | rel1 INTEGER NOT NULL, |
0906d55b |
1460 | rel2 INTEGER $self->{null}, |
7dba7c70 |
1461 | FOREIGN KEY (rel1) REFERENCES loader_test31(id), |
1462 | FOREIGN KEY (rel2) REFERENCES loader_test31(id) |
1463 | ) $self->{innodb} |
1464 | }, |
1465 | q{ INSERT INTO loader_test32 (id,rel1) VALUES (1,1) }, |
1466 | |
1467 | qq{ |
1468 | CREATE TABLE loader_test33 ( |
1469 | id1 INTEGER NOT NULL, |
1470 | id2 INTEGER NOT NULL, |
1471 | PRIMARY KEY (id1,id2) |
1472 | ) $self->{innodb} |
1473 | }, |
1474 | q{ INSERT INTO loader_test33 (id1,id2) VALUES (1,2) }, |
1475 | |
1476 | qq{ |
1477 | CREATE TABLE loader_test34 ( |
1478 | id INTEGER NOT NULL PRIMARY KEY, |
1479 | rel1 INTEGER NOT NULL, |
0906d55b |
1480 | rel2 INTEGER $self->{null}, |
7dba7c70 |
1481 | FOREIGN KEY (id,rel1) REFERENCES loader_test33(id1,id2), |
1482 | FOREIGN KEY (id,rel2) REFERENCES loader_test33(id1,id2) |
1483 | ) $self->{innodb} |
1484 | }, |
1485 | q{ INSERT INTO loader_test34 (id,rel1) VALUES (1,2) }, |
a78e3fed |
1486 | ); |
1487 | |
fdd8ff16 |
1488 | @statements_advanced = ( |
a78e3fed |
1489 | qq{ |
1490 | CREATE TABLE loader_test10 ( |
1491 | id10 $self->{auto_inc_pk}, |
1492 | subject VARCHAR(8), |
0906d55b |
1493 | loader_test11 INTEGER $self->{null} |
66742793 |
1494 | ) $self->{innodb} |
a78e3fed |
1495 | }, |
c1ac681d |
1496 | $make_auto_inc->(qw/loader_test10 id10/), |
a78e3fed |
1497 | |
1498 | qq{ |
1499 | CREATE TABLE loader_test11 ( |
1500 | id11 $self->{auto_inc_pk}, |
8793567f |
1501 | a_message VARCHAR(8) DEFAULT 'foo', |
0906d55b |
1502 | loader_test10 INTEGER $self->{null}, |
a78e3fed |
1503 | FOREIGN KEY (loader_test10) REFERENCES loader_test10 (id10) |
66742793 |
1504 | ) $self->{innodb} |
a78e3fed |
1505 | }, |
c1ac681d |
1506 | $make_auto_inc->(qw/loader_test11 id11/), |
a78e3fed |
1507 | |
bc5afe55 |
1508 | (lc($self->{vendor}) ne 'informix' ? |
1509 | (q{ ALTER TABLE loader_test10 ADD CONSTRAINT loader_test11_fk } . |
1510 | q{ FOREIGN KEY (loader_test11) } . |
1511 | q{ REFERENCES loader_test11 (id11) }) |
1512 | : |
1513 | (q{ ALTER TABLE loader_test10 ADD CONSTRAINT } . |
1514 | q{ FOREIGN KEY (loader_test11) } . |
1515 | q{ REFERENCES loader_test11 (id11) } . |
1516 | q{ CONSTRAINT loader_test11_fk }) |
1517 | ), |
a78e3fed |
1518 | ); |
1519 | |
fdd8ff16 |
1520 | @statements_advanced_sqlite = ( |
7fc16540 |
1521 | qq{ |
1522 | CREATE TABLE loader_test10 ( |
1523 | id10 $self->{auto_inc_pk}, |
1524 | subject VARCHAR(8) |
1525 | ) $self->{innodb} |
1526 | }, |
1527 | $make_auto_inc->(qw/loader_test10 id10/), |
1528 | |
1529 | qq{ |
1530 | CREATE TABLE loader_test11 ( |
1531 | id11 $self->{auto_inc_pk}, |
8793567f |
1532 | a_message VARCHAR(8) DEFAULT 'foo', |
7fc16540 |
1533 | loader_test10 INTEGER $self->{null}, |
1534 | FOREIGN KEY (loader_test10) REFERENCES loader_test10 (id10) |
1535 | ) $self->{innodb} |
1536 | }, |
1537 | $make_auto_inc->(qw/loader_test11 id11/), |
1538 | |
1539 | (q{ ALTER TABLE loader_test10 ADD COLUMN } . |
1540 | q{ loader_test11 INTEGER REFERENCES loader_test11 (id11) }), |
1541 | ); |
1542 | |
fdd8ff16 |
1543 | @statements_inline_rels = ( |
4ce22656 |
1544 | qq{ |
1545 | CREATE TABLE loader_test12 ( |
1546 | id INTEGER NOT NULL PRIMARY KEY, |
1547 | id2 VARCHAR(8) NOT NULL UNIQUE, |
cfb6add1 |
1548 | dat VARCHAR(8) NOT NULL UNIQUE |
4ce22656 |
1549 | ) $self->{innodb} |
1550 | }, |
1551 | |
1552 | q{ INSERT INTO loader_test12 (id,id2,dat) VALUES (1,'aaa','bbb') }, |
1553 | |
1554 | qq{ |
1555 | CREATE TABLE loader_test13 ( |
1556 | id INTEGER NOT NULL PRIMARY KEY REFERENCES loader_test12, |
1557 | loader_test12 VARCHAR(8) NOT NULL REFERENCES loader_test12 (id2), |
1558 | dat VARCHAR(8) REFERENCES loader_test12 (dat) |
1559 | ) $self->{innodb} |
1560 | }, |
1561 | |
1562 | (q{ INSERT INTO loader_test13 (id,loader_test12,dat) } . |
1563 | q{ VALUES (1,'aaa','bbb') }), |
1564 | ); |
1565 | |
1566 | |
fdd8ff16 |
1567 | @statements_implicit_rels = ( |
52bf3f26 |
1568 | qq{ |
1569 | CREATE TABLE loader_test14 ( |
1570 | id INTEGER NOT NULL PRIMARY KEY, |
1571 | dat VARCHAR(8) |
1572 | ) $self->{innodb} |
1573 | }, |
1574 | |
1575 | q{ INSERT INTO loader_test14 (id,dat) VALUES (123,'aaa') }, |
1576 | |
1577 | qq{ |
1578 | CREATE TABLE loader_test15 ( |
1579 | id INTEGER NOT NULL PRIMARY KEY, |
1580 | loader_test14 INTEGER NOT NULL, |
1581 | FOREIGN KEY (loader_test14) REFERENCES loader_test14 |
1582 | ) $self->{innodb} |
1583 | }, |
1584 | |
1585 | q{ INSERT INTO loader_test15 (id,loader_test14) VALUES (1,123) }, |
a60b5b8d |
1586 | ); |
52bf3f26 |
1587 | |
af96f52e |
1588 | $self->drop_tables; |
1589 | |
a78e3fed |
1590 | my $dbh = $self->dbconnect(1); |
ddf91322 |
1591 | |
12333562 |
1592 | $dbh->do($_) for @{ $self->{pre_create} || [] }; |
1593 | |
3b29d0ac |
1594 | $dbh->do($_) foreach (@statements); |
9065de34 |
1595 | |
b511f36e |
1596 | $dbh->do($_) foreach (@{ $self->{data_type_tests}{ddl} || [] }); |
9065de34 |
1597 | |
a78e3fed |
1598 | unless($self->{skip_rels}) { |
af96f52e |
1599 | # hack for now, since DB2 doesn't like inline comments, and we need |
1600 | # to test one for mysql, which works on everyone else... |
1601 | # this all needs to be refactored anyways. |
a78e3fed |
1602 | $dbh->do($_) for (@statements_reltests); |
7fc16540 |
1603 | if($self->{vendor} =~ /sqlite/i) { |
1604 | $dbh->do($_) for (@statements_advanced_sqlite); |
1605 | } |
1606 | else { |
a78e3fed |
1607 | $dbh->do($_) for (@statements_advanced); |
1608 | } |
4ce22656 |
1609 | unless($self->{no_inline_rels}) { |
1610 | $dbh->do($_) for (@statements_inline_rels); |
1611 | } |
52bf3f26 |
1612 | unless($self->{no_implicit_rels}) { |
1613 | $dbh->do($_) for (@statements_implicit_rels); |
1614 | } |
a78e3fed |
1615 | } |
df637f3d |
1616 | |
1617 | $dbh->do($_) for @{ $self->{extra}->{create} || [] }; |
a78e3fed |
1618 | $dbh->disconnect(); |
1619 | } |
1620 | |
af96f52e |
1621 | sub drop_tables { |
a78e3fed |
1622 | my $self = shift; |
1623 | |
a78e3fed |
1624 | my @tables = qw/ |
c4a49993 |
1625 | loader_test1 |
22270947 |
1626 | loader_test1s |
a78e3fed |
1627 | loader_test2 |
a1cec524 |
1628 | LOADER_test23 |
996be9ee |
1629 | LoAdEr_test24 |
e35e5a64 |
1630 | loader_test35 |
42e785fa |
1631 | loader_test36 |
a78e3fed |
1632 | /; |
c1ac681d |
1633 | |
1634 | my @tables_auto_inc = ( |
22270947 |
1635 | [ qw/loader_test1s id/ ], |
c1ac681d |
1636 | [ qw/loader_test2 id/ ], |
1637 | ); |
a78e3fed |
1638 | |
1639 | my @tables_reltests = qw/ |
1640 | loader_test4 |
1641 | loader_test3 |
1642 | loader_test6 |
1643 | loader_test5 |
1644 | loader_test8 |
1645 | loader_test7 |
1646 | loader_test9 |
996be9ee |
1647 | loader_test17 |
1648 | loader_test16 |
1649 | loader_test20 |
1650 | loader_test19 |
1651 | loader_test18 |
1652 | loader_test22 |
1653 | loader_test21 |
25e3e91d |
1654 | loader_test26 |
1655 | loader_test25 |
26f1c8c9 |
1656 | loader_test28 |
1657 | loader_test29 |
1658 | loader_test27 |
7dba7c70 |
1659 | loader_test32 |
1660 | loader_test31 |
1661 | loader_test34 |
1662 | loader_test33 |
a78e3fed |
1663 | /; |
1664 | |
1665 | my @tables_advanced = qw/ |
1666 | loader_test11 |
1667 | loader_test10 |
1668 | /; |
c1ac681d |
1669 | |
1670 | my @tables_advanced_auto_inc = ( |
1671 | [ qw/loader_test10 id10/ ], |
1672 | [ qw/loader_test11 id11/ ], |
1673 | ); |
a78e3fed |
1674 | |
4ce22656 |
1675 | my @tables_inline_rels = qw/ |
1676 | loader_test13 |
1677 | loader_test12 |
1678 | /; |
1679 | |
52bf3f26 |
1680 | my @tables_implicit_rels = qw/ |
1681 | loader_test15 |
1682 | loader_test14 |
1683 | /; |
1684 | |
657d4b58 |
1685 | my @tables_rescan = qw/ loader_test30 /; |
a60b5b8d |
1686 | |
b511f36e |
1687 | my @tables_preserve_case_tests = qw/ LoaderTest41 LoaderTest40 /; |
1688 | |
a78e3fed |
1689 | my $drop_fk_mysql = |
c1ac681d |
1690 | q{ALTER TABLE loader_test10 DROP FOREIGN KEY loader_test11_fk}; |
a78e3fed |
1691 | |
1692 | my $drop_fk = |
c1ac681d |
1693 | q{ALTER TABLE loader_test10 DROP CONSTRAINT loader_test11_fk}; |
a78e3fed |
1694 | |
c4a49993 |
1695 | # For some reason some tests do this twice (I guess dependency issues?) |
1696 | # do it twice for all drops |
1697 | for (1,2) { |
1698 | my $dbh = $self->dbconnect(0); |
a78e3fed |
1699 | |
c38ec663 |
1700 | $dbh->do($_) for @{ $self->{extra}{pre_drop_ddl} || [] }; |
7b868481 |
1701 | |
c4a49993 |
1702 | $dbh->do("DROP TABLE $_") for @{ $self->{extra}{drop} || [] }; |
df637f3d |
1703 | |
c4a49993 |
1704 | my $drop_auto_inc = $self->{auto_inc_drop_cb} || sub {}; |
c1ac681d |
1705 | |
c4a49993 |
1706 | unless($self->{skip_rels}) { |
a78e3fed |
1707 | $dbh->do("DROP TABLE $_") for (@tables_reltests); |
b511f36e |
1708 | $dbh->do("DROP TABLE $_") for (@tables_reltests); |
7fc16540 |
1709 | if($self->{vendor} =~ /mysql/i) { |
1710 | $dbh->do($drop_fk_mysql); |
1711 | } |
1712 | else { |
1713 | $dbh->do($drop_fk); |
a78e3fed |
1714 | } |
7fc16540 |
1715 | $dbh->do($_) for map { $drop_auto_inc->(@$_) } @tables_advanced_auto_inc; |
ffb03c96 |
1716 | $dbh->do("DROP TABLE $_") for (@tables_advanced); |
7fc16540 |
1717 | |
4ce22656 |
1718 | unless($self->{no_inline_rels}) { |
1719 | $dbh->do("DROP TABLE $_") for (@tables_inline_rels); |
1720 | } |
52bf3f26 |
1721 | unless($self->{no_implicit_rels}) { |
1722 | $dbh->do("DROP TABLE $_") for (@tables_implicit_rels); |
1723 | } |
c4a49993 |
1724 | } |
1725 | $dbh->do($_) for map { $drop_auto_inc->(@$_) } @tables_auto_inc; |
1726 | $dbh->do("DROP TABLE $_") for (@tables, @tables_rescan); |
9065de34 |
1727 | |
c4a49993 |
1728 | foreach my $data_type_table (@{ $self->{data_type_tests}{table_names} || [] }) { |
26da4cc3 |
1729 | $dbh->do("DROP TABLE $data_type_table"); |
c4a49993 |
1730 | } |
b511f36e |
1731 | |
c4a49993 |
1732 | my ($oqt, $cqt) = $self->get_oqt_cqt(always => 1); |
b511f36e |
1733 | |
c4a49993 |
1734 | $dbh->do("DROP TABLE ${oqt}${_}${cqt}") for @tables_preserve_case_tests; |
ffb03c96 |
1735 | |
c4a49993 |
1736 | $dbh->disconnect; |
1737 | } |
a78e3fed |
1738 | } |
1739 | |
53ef681d |
1740 | sub _custom_column_info { |
1741 | my ( $table_name, $column_name, $column_info ) = @_; |
1742 | |
1743 | $table_name = lc ( $table_name ); |
1744 | $column_name = lc ( $column_name ); |
1745 | |
1746 | if ( $table_name eq 'loader_test35' |
1747 | and $column_name eq 'an_int' |
1748 | ){ |
1749 | return { is_numeric => 1 } |
1750 | } |
1751 | # Set inflate_datetime or inflate_date to check |
1752 | # datetime_timezone and datetime_locale |
1753 | if ( $table_name eq 'loader_test36' ){ |
1754 | return { inflate_datetime => 1 } if |
1755 | ( $column_name eq 'b_char_as_data' ); |
1756 | return { inflate_date => 1 } if |
1757 | ( $column_name eq 'c_char_as_data' ); |
1758 | } |
1759 | |
1760 | return; |
1761 | } |
1762 | |
3b29d0ac |
1763 | my %DATA_TYPE_MULTI_TABLE_OVERRIDES = ( |
7640ef4b |
1764 | oracle => qr/\blong\b/i, |
1765 | mssql => qr/\b(?:timestamp|rowversion)\b/i, |
c7e6dc1f |
1766 | informix => qr/\b(?:bigserial|serial8)\b/i, |
3b29d0ac |
1767 | ); |
1768 | |
9065de34 |
1769 | sub setup_data_type_tests { |
1770 | my $self = shift; |
1771 | |
1772 | return unless my $types = $self->{data_types}; |
1773 | |
1774 | my $tests = $self->{data_type_tests} = {}; |
9065de34 |
1775 | |
3b29d0ac |
1776 | # split types into tables based on overrides |
dd87d4c4 |
1777 | my (@types, @split_off_types, @first_table_types); |
1778 | { |
7640ef4b |
1779 | my $split_off_re = $DATA_TYPE_MULTI_TABLE_OVERRIDES{lc($self->{vendor})} || qr/(?!)/; |
dd87d4c4 |
1780 | |
1781 | @types = keys %$types; |
7640ef4b |
1782 | @split_off_types = grep /$split_off_re/, @types; |
1783 | @first_table_types = grep !/$split_off_re/, @types; |
dd87d4c4 |
1784 | } |
9065de34 |
1785 | |
3b29d0ac |
1786 | @types = +{ map +($_, $types->{$_}), @first_table_types }, |
1787 | map +{ $_, $types->{$_} }, @split_off_types; |
9065de34 |
1788 | |
1789 | my $test_count = 0; |
3b29d0ac |
1790 | my $table_num = 10000; |
9065de34 |
1791 | |
3b29d0ac |
1792 | foreach my $types (@types) { |
1793 | my $table_name = "loader_test$table_num"; |
1794 | push @{ $tests->{table_names} }, $table_name; |
f30ddf15 |
1795 | |
3b29d0ac |
1796 | my $table_moniker = "LoaderTest$table_num"; |
1797 | push @{ $tests->{table_monikers} }, $table_moniker; |
26334ec1 |
1798 | |
3b29d0ac |
1799 | $table_num++; |
9065de34 |
1800 | |
3b29d0ac |
1801 | my $cols = $tests->{columns}{$table_moniker} = {}; |
9dc968df |
1802 | |
3b29d0ac |
1803 | my $ddl = "CREATE TABLE $table_name (\n id INTEGER NOT NULL PRIMARY KEY,\n"; |
9dc968df |
1804 | |
3b29d0ac |
1805 | my %seen_col_names; |
cf0ba25b |
1806 | |
3b29d0ac |
1807 | while (my ($col_def, $expected_info) = each %$types) { |
1808 | (my $type_alias = $col_def) =~ s/\( ([^)]+) \)//xg; |
9065de34 |
1809 | |
3b29d0ac |
1810 | my $size = $1; |
1811 | $size = '' unless defined $size; |
1812 | $size =~ s/\s+//g; |
1813 | my @size = split /,/, $size; |
9065de34 |
1814 | |
3b29d0ac |
1815 | # some DBs don't like very long column names |
ebed3e6e |
1816 | if ($self->{vendor} =~ /^(?:firebird|sqlanywhere|oracle|db2)\z/i) { |
3b29d0ac |
1817 | my ($col_def, $default) = $type_alias =~ /^(.*)(default.*)?\z/i; |
5163dc4a |
1818 | |
3b29d0ac |
1819 | $type_alias = substr $col_def, 0, 15; |
bc1cb85e |
1820 | |
3b29d0ac |
1821 | $type_alias .= '_with_dflt' if $default; |
1822 | } |
f30ddf15 |
1823 | |
3b29d0ac |
1824 | $type_alias =~ s/\s/_/g; |
1825 | $type_alias =~ s/\W//g; |
9065de34 |
1826 | |
3b29d0ac |
1827 | my $col_name = 'col_' . $type_alias; |
1828 | |
1829 | if (@size) { |
1830 | my $size_name = join '_', apply { s/\W//g } @size; |
9065de34 |
1831 | |
3b29d0ac |
1832 | $col_name .= "_sz_$size_name"; |
1833 | } |
1834 | |
1835 | # XXX would be better to check _loader->preserve_case |
1836 | $col_name = lc $col_name; |
1837 | |
1838 | $col_name .= '_' . $seen_col_names{$col_name} if $seen_col_names{$col_name}++; |
1839 | |
1840 | $ddl .= " $col_name $col_def,\n"; |
1841 | |
1842 | $cols->{$col_name} = $expected_info; |
9065de34 |
1843 | |
3b29d0ac |
1844 | $test_count++; |
1845 | } |
1846 | |
1847 | $ddl =~ s/,\n\z/\n)/; |
1848 | |
1849 | push @{ $tests->{ddl} }, $ddl; |
1850 | } |
9065de34 |
1851 | |
9065de34 |
1852 | $tests->{test_count} = $test_count; |
1853 | |
1854 | return $test_count; |
1855 | } |
1856 | |
996be9ee |
1857 | sub DESTROY { |
1858 | my $self = shift; |
565335e6 |
1859 | unless ($ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}) { |
c4a49993 |
1860 | $self->drop_tables if $self->{_created}; |
1861 | rmtree $DUMP_DIR |
565335e6 |
1862 | } |
996be9ee |
1863 | } |
af96f52e |
1864 | |
a78e3fed |
1865 | 1; |
df956aad |
1866 | # vim:et sts=4 sw=4 tw=0: |