use_namespaces is now default, still needs the upgrade code
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 25backcompat_v4.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use Test::Exception;
5 use File::Path qw/rmtree make_path/;
6 use Class::Unload;
7 use File::Temp qw/tempfile tempdir/;
8 use IO::File;
9 use DBIx::Class::Schema::Loader ();
10 use lib qw(t/lib);
11 use make_dbictest_db2;
12
13 my $DUMP_DIR = './t/_common_dump';
14 rmtree $DUMP_DIR;
15 my $SCHEMA_CLASS = 'DBIXCSL_Test::Schema';
16
17 # test dynamic schema in 0.04006 mode
18 {
19     my $res = run_loader();
20     my $warning = $res->{warnings}[0];
21
22     like $warning, qr/dynamic schema/i,
23         'dynamic schema in backcompat mode detected';
24     like $warning, qr/run in 0\.04006 mode/i,
25         'dynamic schema in 0.04006 mode warning';
26     like $warning, qr/DBIx::Class::Schema::Loader::Manual::UpgradingFromV4/,
27         'warning refers to upgrading doc';
28     
29     run_v4_tests($res);
30 }
31
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');
36
37     is_deeply $res->{warnings}, [], 'no warnings with naming attribute set';
38
39     run_v4_tests($res);
40 }
41
42 # test upgraded dynamic schema
43 {
44     my $res = run_loader(naming => 'current');
45
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 #    }
52
53     is_deeply $res->{warnings}, [], 'no warnings with naming attribute set';
54
55     run_v5_tests($res);
56 }
57
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
66     # make external content for Result that will be singularized
67     IO::File->new(">$external_result_dir/Quuxs.pm")->print(<<"EOF");
68 package ${SCHEMA_CLASS}::Quuxs;
69 sub a_method { 'hlagh' }
70
71 __PACKAGE__->has_one('bazrel', 'DBIXCSL_Test::Schema::Bazs',
72     { 'foreign.baz_num' => 'self.baz_id' });
73
74 1;
75 EOF
76
77     # make external content for Result that will NOT be singularized
78     IO::File->new(">$external_result_dir/Bar.pm")->print(<<"EOF");
79 package ${SCHEMA_CLASS}::Bar;
80
81 __PACKAGE__->has_one('foorel', 'DBIXCSL_Test::Schema::Foos',
82     { 'foreign.fooid' => 'self.foo_id' });
83
84 1;
85 EOF
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
98     lives_and { is $schema->resultset('Quux')->find(1)->a_method, 'hlagh' }
99 'external custom content for unsingularized Result was loaded by upgraded ' .
100 'dynamic Schema';
101
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
111     run_v5_tests($res);
112
113     rmtree $temp_dir;
114     pop @INC;
115 }
116
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
125     # make external content for Result that will be singularized
126     IO::File->new(">$external_result_dir/Quuxs.pm")->print(<<"EOF");
127 package ${SCHEMA_CLASS}::Quuxs;
128 sub a_method { 'dongs' }
129
130 __PACKAGE__->has_one('bazrel2', 'DBIXCSL_Test::Schema::Bazs',
131     { 'foreign.baz_num' => 'self.baz_id' });
132
133 1;
134 EOF
135
136     # make external content for Result that will NOT be singularized
137     IO::File->new(">$external_result_dir/Bar.pm")->print(<<"EOF");
138 package ${SCHEMA_CLASS}::Bar;
139
140 __PACKAGE__->has_one('foorel2', 'DBIXCSL_Test::Schema::Foos',
141     { 'foreign.fooid' => 'self.foo_id' });
142
143 1;
144 EOF
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
153     lives_and { is $schema->resultset('Quux')->find(1)->a_method, 'dongs' }
154 'external custom content for unsingularized Result was loaded by upgraded ' .
155 'static Schema';
156
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
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
179 # test running against v4 schema without upgrade, twice, then upgrade
180 {
181     write_v4_schema_pm();
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
192     is scalar @{ $res->{warnings} }, 3,
193         'correct number of warnings for static schema in backcompat mode';
194
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;
206                 print <<EOF;
207 sub a_method { 'mtfnpy' }
208
209 __PACKAGE__->has_one('bazrel3', 'DBIXCSL_Test::Schema::Bazs',
210     { 'foreign.baz_num' => 'self.baz_id' });
211 EOF
212             }
213             else {
214                 print;
215             }
216         }
217     }
218
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
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,
235 'correct number of warnings on upgrading static schema (with "naming" set)'
236         or diag @{ $res->{warnings} };
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
247     lives_and { is $schema->resultset('Quux')->find(1)->a_method, 'mtfnpy' }
248         'custom content was carried over from un-singularized Result';
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
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
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;
295 sub a_method { 'lalala' }
296
297 __PACKAGE__->has_one('foorel3', 'DBIXCSL_Test::Schema::Foos',
298     { 'foreign.fooid' => 'self.foo_id' });
299 EOF
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';
327 }
328
329 done_testing;
330
331 END {
332     rmtree $DUMP_DIR unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP};
333 }
334
335 sub run_loader {
336     my %loader_opts = @_;
337
338     $loader_opts{use_namespaces} = 0
339         unless exists $loader_opts{use_namespaces};
340
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
379 sub 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';
386 package DBIXCSL_Test::Schema;
387
388 use strict;
389 use warnings;
390
391 use 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
401 1;
402 EOF
403 }
404
405 sub 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
426 sub 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 }