use_namespaces is now default, still needs the upgrade code
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 25backcompat_v4.t
CommitLineData
66afce69 1use strict;
2use warnings;
3use Test::More;
b24cb177 4use Test::Exception;
a0e0a56a 5use File::Path qw/rmtree make_path/;
66afce69 6use Class::Unload;
ffc705f3 7use File::Temp qw/tempfile tempdir/;
8use IO::File;
68d49e50 9use DBIx::Class::Schema::Loader ();
66afce69 10use lib qw(t/lib);
11use make_dbictest_db2;
12
13my $DUMP_DIR = './t/_common_dump';
14rmtree $DUMP_DIR;
a0e0a56a 15my $SCHEMA_CLASS = 'DBIXCSL_Test::Schema';
66afce69 16
66afce69 17# test dynamic schema in 0.04006 mode
18{
19 my $res = run_loader();
a0e0a56a 20 my $warning = $res->{warnings}[0];
66afce69 21
a0e0a56a 22 like $warning, qr/dynamic schema/i,
66afce69 23 'dynamic schema in backcompat mode detected';
a0e0a56a 24 like $warning, qr/run in 0\.04006 mode/i,
66afce69 25 'dynamic schema in 0.04006 mode warning';
a0e0a56a 26 like $warning, qr/DBIx::Class::Schema::Loader::Manual::UpgradingFromV4/,
27 'warning refers to upgrading doc';
28
29 run_v4_tests($res);
30}
66afce69 31
a0e0a56a 32# setting naming accessor on dynamic schema should disable warning (even when
33# we're setting it to 'v4' .)
34{
35 my $res = run_loader(naming => 'v4');
66afce69 36
a0e0a56a 37 is_deeply $res->{warnings}, [], 'no warnings with naming attribute set';
f53dcdf0 38
39 run_v4_tests($res);
a0e0a56a 40}
41
42# test upgraded dynamic schema
43{
44 my $res = run_loader(naming => 'current');
66afce69 45
a0e0a56a 46# to dump a schema for debugging...
47# {
48# mkdir '/tmp/HLAGH';
49# $schema->_loader->{dump_directory} = '/tmp/HLAGH';
50# $schema->_loader->_dump_to_dir(values %{ $res->{classes} });
51# }
66afce69 52
a0e0a56a 53 is_deeply $res->{warnings}, [], 'no warnings with naming attribute set';
66afce69 54
a0e0a56a 55 run_v5_tests($res);
56}
57
ffc705f3 58# test upgraded dynamic schema with external content loaded
59{
60 my $temp_dir = tempdir;
61 push @INC, $temp_dir;
62
63 my $external_result_dir = join '/', $temp_dir, split /::/, $SCHEMA_CLASS;
64 make_path $external_result_dir;
65
b24cb177 66 # make external content for Result that will be singularized
ffc705f3 67 IO::File->new(">$external_result_dir/Quuxs.pm")->print(<<"EOF");
68package ${SCHEMA_CLASS}::Quuxs;
69sub a_method { 'hlagh' }
b24cb177 70
71__PACKAGE__->has_one('bazrel', 'DBIXCSL_Test::Schema::Bazs',
72 { 'foreign.baz_num' => 'self.baz_id' });
73
741;
75EOF
76
77 # make external content for Result that will NOT be singularized
78 IO::File->new(">$external_result_dir/Bar.pm")->print(<<"EOF");
79package ${SCHEMA_CLASS}::Bar;
80
81__PACKAGE__->has_one('foorel', 'DBIXCSL_Test::Schema::Foos',
82 { 'foreign.fooid' => 'self.foo_id' });
83
ffc705f3 841;
85EOF
86
87 my $res = run_loader(naming => 'current');
88 my $schema = $res->{schema};
89
90 is scalar @{ $res->{warnings} }, 1,
91'correct nummber of warnings for upgraded dynamic schema with external ' .
92'content for unsingularized Result.';
93
94 my $warning = $res->{warnings}[0];
95 like $warning, qr/Detected external content/i,
96 'detected external content warning';
97
b24cb177 98 lives_and { is $schema->resultset('Quux')->find(1)->a_method, 'hlagh' }
ffc705f3 99'external custom content for unsingularized Result was loaded by upgraded ' .
100'dynamic Schema';
101
b24cb177 102 lives_and { isa_ok $schema->resultset('Quux')->find(1)->bazrel,
103 $res->{classes}{bazs} }
104 'unsingularized class names in external content are translated';
105
106 lives_and { isa_ok $schema->resultset('Bar')->find(1)->foorel,
107 $res->{classes}{foos} }
108'unsingularized class names in external content from unchanged Result class ' .
109'names are translated';
110
ffc705f3 111 run_v5_tests($res);
112
113 rmtree $temp_dir;
114 pop @INC;
115}
116
30a4c064 117# test upgraded static schema with external content loaded
118{
119 my $temp_dir = tempdir;
120 push @INC, $temp_dir;
121
122 my $external_result_dir = join '/', $temp_dir, split /::/, $SCHEMA_CLASS;
123 make_path $external_result_dir;
124
b24cb177 125 # make external content for Result that will be singularized
30a4c064 126 IO::File->new(">$external_result_dir/Quuxs.pm")->print(<<"EOF");
127package ${SCHEMA_CLASS}::Quuxs;
128sub a_method { 'dongs' }
b24cb177 129
130__PACKAGE__->has_one('bazrel2', 'DBIXCSL_Test::Schema::Bazs',
131 { 'foreign.baz_num' => 'self.baz_id' });
132
1331;
134EOF
135
136 # make external content for Result that will NOT be singularized
137 IO::File->new(">$external_result_dir/Bar.pm")->print(<<"EOF");
138package ${SCHEMA_CLASS}::Bar;
139
140__PACKAGE__->has_one('foorel2', 'DBIXCSL_Test::Schema::Foos',
141 { 'foreign.fooid' => 'self.foo_id' });
142
30a4c064 1431;
144EOF
145
146 write_v4_schema_pm();
147
148 my $res = run_loader(dump_directory => $DUMP_DIR, naming => 'current');
149 my $schema = $res->{schema};
150
151 run_v5_tests($res);
152
b24cb177 153 lives_and { is $schema->resultset('Quux')->find(1)->a_method, 'dongs' }
30a4c064 154'external custom content for unsingularized Result was loaded by upgraded ' .
155'static Schema';
156
b24cb177 157 lives_and { isa_ok $schema->resultset('Quux')->find(1)->bazrel2,
158 $res->{classes}{bazs} }
159 'unsingularized class names in external content are translated';
160
161 lives_and { isa_ok $schema->resultset('Bar')->find(1)->foorel2,
162 $res->{classes}{foos} }
163'unsingularized class names in external content from unchanged Result class ' .
164'names are translated in static schema';
165
30a4c064 166 my $file = $schema->_loader->_get_dump_filename($res->{classes}{quuxs});
167 my $code = do { local ($/, @ARGV) = (undef, $file); <> };
168
169 like $code, qr/package ${SCHEMA_CLASS}::Quux;/,
170'package line translated correctly from external custom content in static dump';
171
172 like $code, qr/sub a_method { 'dongs' }/,
173'external custom content loaded into static dump correctly';
174
175 rmtree $temp_dir;
176 pop @INC;
177}
178
b24cb177 179# test running against v4 schema without upgrade, twice, then upgrade
a0e0a56a 180{
30a4c064 181 write_v4_schema_pm();
a0e0a56a 182 my $res = run_loader(dump_directory => $DUMP_DIR);
183 my $warning = $res->{warnings}[0];
184
185 like $warning, qr/static schema/i,
186 'static schema in backcompat mode detected';
187 like $warning, qr/0.04006/,
188 'correct version detected';
189 like $warning, qr/DBIx::Class::Schema::Loader::Manual::UpgradingFromV4/,
190 'refers to upgrading doc';
191
ffc705f3 192 is scalar @{ $res->{warnings} }, 3,
193 'correct number of warnings for static schema in backcompat mode';
194
a0e0a56a 195 run_v4_tests($res);
196
197 # add some custom content to a Result that will be replaced
198 my $schema = $res->{schema};
199 my $quuxs_pm = $schema->_loader
200 ->_get_dump_filename($res->{classes}{quuxs});
201 {
202 local ($^I, @ARGV) = ('', $quuxs_pm);
203 while (<>) {
204 if (/DO NOT MODIFY THIS OR ANYTHING ABOVE/) {
205 print;
b24cb177 206 print <<EOF;
207sub a_method { 'mtfnpy' }
208
209__PACKAGE__->has_one('bazrel3', 'DBIXCSL_Test::Schema::Bazs',
210 { 'foreign.baz_num' => 'self.baz_id' });
211EOF
a0e0a56a 212 }
213 else {
214 print;
215 }
216 }
217 }
218
b24cb177 219 # Rerun the loader in backcompat mode to make sure it's still in backcompat
220 # mode.
221 $res = run_loader(dump_directory => $DUMP_DIR);
222 run_v4_tests($res);
223
a0e0a56a 224 # now upgrade the schema
225 $res = run_loader(dump_directory => $DUMP_DIR, naming => 'current');
226 $schema = $res->{schema};
227
228 like $res->{warnings}[0], qr/Dumping manual schema/i,
229 'correct warnings on upgrading static schema (with "naming" set)';
230
231 like $res->{warnings}[1], qr/dump completed/i,
232 'correct warnings on upgrading static schema (with "naming" set)';
233
234 is scalar @{ $res->{warnings} }, 2,
f53dcdf0 235'correct number of warnings on upgrading static schema (with "naming" set)'
236 or diag @{ $res->{warnings} };
a0e0a56a 237
238 run_v5_tests($res);
239
240 (my $result_dir = "$DUMP_DIR/$SCHEMA_CLASS") =~ s{::}{/}g;
241 my $result_count =()= glob "$result_dir/*";
242
243 is $result_count, 4,
244 'un-singularized results were replaced during upgrade';
245
246 # check that custom content was preserved
b24cb177 247 lives_and { is $schema->resultset('Quux')->find(1)->a_method, 'mtfnpy' }
a0e0a56a 248 'custom content was carried over from un-singularized Result';
b24cb177 249
250 lives_and { isa_ok $schema->resultset('Quux')->find(1)->bazrel3,
251 $res->{classes}{bazs} }
252 'unsingularized class names in custom content are translated';
253
254 my $file = $schema->_loader->_get_dump_filename($res->{classes}{quuxs});
255 my $code = do { local ($/, @ARGV) = (undef, $file); <> };
256
257 like $code, qr/sub a_method { 'mtfnpy' }/,
258'custom content from unsingularized Result loaded into static dump correctly';
259}
260
68d49e50 261# test upgrading a v4 schema, the check that the version string is correct
262{
263 write_v4_schema_pm();
264 run_loader(dump_directory => $DUMP_DIR);
265 my $res = run_loader(dump_directory => $DUMP_DIR, naming => 'current');
266 my $schema = $res->{schema};
267
268 my $file = $schema->_loader->_get_dump_filename($SCHEMA_CLASS);
269 my $code = do { local ($/, @ARGV) = (undef, $file); <> };
270
271 my ($dumped_ver) =
272 $code =~ /^# Created by DBIx::Class::Schema::Loader v(\S+)/m;
273
274 is $dumped_ver, $DBIx::Class::Schema::Loader::VERSION,
275 'correct version dumped after upgrade of v4 static schema';
276}
277
b24cb177 278# Test upgrading an already singular result with custom content that refers to
279# old class names.
280{
281 write_v4_schema_pm();
282 my $res = run_loader(dump_directory => $DUMP_DIR);
283 my $schema = $res->{schema};
284 run_v4_tests($res);
285
286 # add some custom content to a Result that will be replaced
287 my $bar_pm = $schema->_loader
288 ->_get_dump_filename($res->{classes}{bar});
289 {
290 local ($^I, @ARGV) = ('', $bar_pm);
291 while (<>) {
292 if (/DO NOT MODIFY THIS OR ANYTHING ABOVE/) {
293 print;
294 print <<EOF;
295sub a_method { 'lalala' }
296
297__PACKAGE__->has_one('foorel3', 'DBIXCSL_Test::Schema::Foos',
298 { 'foreign.fooid' => 'self.foo_id' });
299EOF
300 }
301 else {
302 print;
303 }
304 }
305 }
306
307 # now upgrade the schema
308 $res = run_loader(dump_directory => $DUMP_DIR, naming => 'current');
309 $schema = $res->{schema};
310 run_v5_tests($res);
311
312 # check that custom content was preserved
313 lives_and { is $schema->resultset('Bar')->find(1)->a_method, 'lalala' }
314 'custom content was preserved from Result pre-upgrade';
315
316 lives_and { isa_ok $schema->resultset('Bar')->find(1)->foorel3,
317 $res->{classes}{foos} }
318'unsingularized class names in custom content from Result with unchanged ' .
319'name are translated';
320
321 my $file = $schema->_loader->_get_dump_filename($res->{classes}{bar});
322 my $code = do { local ($/, @ARGV) = (undef, $file); <> };
323
324 like $code, qr/sub a_method { 'lalala' }/,
325'custom content from Result with unchanged name loaded into static dump ' .
326'correctly';
66afce69 327}
328
329done_testing;
330
ffc705f3 331END {
332 rmtree $DUMP_DIR unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP};
333}
a0e0a56a 334
dbe9e0f7 335sub run_loader {
336 my %loader_opts = @_;
337
f22644d7 338 $loader_opts{use_namespaces} = 0
339 unless exists $loader_opts{use_namespaces};
340
dbe9e0f7 341 eval {
342 foreach my $source_name ($SCHEMA_CLASS->clone->sources) {
343 Class::Unload->unload("${SCHEMA_CLASS}::${source_name}");
344 }
345
346 Class::Unload->unload($SCHEMA_CLASS);
347 };
348 undef $@;
349
350 my @connect_info = $make_dbictest_db2::dsn;
351 my @loader_warnings;
352 local $SIG{__WARN__} = sub { push(@loader_warnings, $_[0]); };
353 eval qq{
354 package $SCHEMA_CLASS;
355 use base qw/DBIx::Class::Schema::Loader/;
356
357 __PACKAGE__->loader_options(\%loader_opts);
358 __PACKAGE__->connection(\@connect_info);
359 };
360
361 ok(!$@, "Loader initialization") or diag $@;
362
363 my $schema = $SCHEMA_CLASS->clone;
364 my (%monikers, %classes);
365 foreach my $source_name ($schema->sources) {
366 my $table_name = $schema->source($source_name)->from;
367 $monikers{$table_name} = $source_name;
368 $classes{$table_name} = "${SCHEMA_CLASS}::${source_name}";
369 }
370
371 return {
372 schema => $schema,
373 warnings => \@loader_warnings,
374 monikers => \%monikers,
375 classes => \%classes,
376 };
377}
378
30a4c064 379sub write_v4_schema_pm {
380 (my $schema_dir = "$DUMP_DIR/$SCHEMA_CLASS") =~ s/::[^:]+\z//;
381 rmtree $schema_dir;
382 make_path $schema_dir;
383 my $schema_pm = "$schema_dir/Schema.pm";
384 open my $fh, '>', $schema_pm or die $!;
385 print $fh <<'EOF';
386package DBIXCSL_Test::Schema;
387
388use strict;
389use warnings;
390
391use base 'DBIx::Class::Schema';
392
393__PACKAGE__->load_classes;
394
395
396# Created by DBIx::Class::Schema::Loader v0.04006 @ 2009-12-25 01:49:25
397# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ibIJTbfM1ji4pyD/lgSEog
398
399
400# You can replace this text with custom content, and it will be preserved on regeneration
4011;
402EOF
403}
404
dbe9e0f7 405sub run_v4_tests {
406 my $res = shift;
407 my $schema = $res->{schema};
408
409 is_deeply [ @{ $res->{monikers} }{qw/foos bar bazs quuxs/} ],
410 [qw/Foos Bar Bazs Quuxs/],
411 'correct monikers in 0.04006 mode';
412
413 isa_ok ((my $bar = eval { $schema->resultset('Bar')->find(1) }),
414 $res->{classes}{bar},
415 'found a bar');
416
417 isa_ok eval { $bar->foo_id }, $res->{classes}{foos},
418 'correct rel name in 0.04006 mode';
419
420 ok my $baz = eval { $schema->resultset('Bazs')->find(1) };
421
422 isa_ok eval { $baz->quux }, 'DBIx::Class::ResultSet',
423 'correct rel type and name for UNIQUE FK in 0.04006 mode';
424}
425
426sub run_v5_tests {
427 my $res = shift;
428 my $schema = $res->{schema};
429
430 is_deeply [ @{ $res->{monikers} }{qw/foos bar bazs quuxs/} ],
431 [qw/Foo Bar Baz Quux/],
432 'correct monikers in current mode';
433
434 ok my $bar = eval { $schema->resultset('Bar')->find(1) };
435
436 isa_ok eval { $bar->foo }, $res->{classes}{foos},
437 'correct rel name in current mode';
438
439 ok my $baz = eval { $schema->resultset('Baz')->find(1) };
440
441 isa_ok eval { $baz->quux }, $res->{classes}{quuxs},
442 'correct rel type and name for UNIQUE FK in current mode';
443}