5 use File::Path qw/rmtree make_path/;
7 use File::Temp qw/tempfile tempdir/;
9 use DBIx::Class::Schema::Loader ();
11 use make_dbictest_db2;
13 my $DUMP_DIR = './t/_common_dump';
15 my $SCHEMA_CLASS = 'DBIXCSL_Test::Schema';
17 # test dynamic schema in 0.04006 mode
19 my $res = run_loader();
20 my $warning = $res->{warnings}[0];
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';
32 # setting naming accessor on dynamic schema should disable warning (even when
33 # we're setting it to 'v4' .)
35 my $res = run_loader(naming => 'v4');
37 is_deeply $res->{warnings}, [], 'no warnings with naming attribute set';
42 # test upgraded dynamic schema
44 my $res = run_loader(naming => 'current');
46 # to dump a schema for debugging...
49 # $schema->_loader->{dump_directory} = '/tmp/HLAGH';
50 # $schema->_loader->_dump_to_dir(values %{ $res->{classes} });
53 is_deeply $res->{warnings}, [], 'no warnings with naming attribute set';
58 # test upgraded dynamic schema with external content loaded
60 my $temp_dir = tempdir;
63 my $external_result_dir = join '/', $temp_dir, split /::/, $SCHEMA_CLASS;
64 make_path $external_result_dir;
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' }
71 __PACKAGE__->has_one('bazrel', 'DBIXCSL_Test::Schema::Bazs',
72 { 'foreign.baz_num' => 'self.baz_id' });
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;
81 __PACKAGE__->has_one('foorel', 'DBIXCSL_Test::Schema::Foos',
82 { 'foreign.fooid' => 'self.foo_id' });
87 my $res = run_loader(naming => 'current');
88 my $schema = $res->{schema};
90 is scalar @{ $res->{warnings} }, 1,
91 'correct nummber of warnings for upgraded dynamic schema with external ' .
92 'content for unsingularized Result.';
94 my $warning = $res->{warnings}[0];
95 like $warning, qr/Detected external content/i,
96 'detected external content warning';
98 lives_and { is $schema->resultset('Quux')->find(1)->a_method, 'hlagh' }
99 'external custom content for unsingularized Result was loaded by upgraded ' .
102 lives_and { isa_ok $schema->resultset('Quux')->find(1)->bazrel,
103 $res->{classes}{bazs} }
104 'unsingularized class names in external content are translated';
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';
117 # test upgraded static schema with external content loaded
119 my $temp_dir = tempdir;
120 push @INC, $temp_dir;
122 my $external_result_dir = join '/', $temp_dir, split /::/, $SCHEMA_CLASS;
123 make_path $external_result_dir;
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' }
130 __PACKAGE__->has_one('bazrel2', 'DBIXCSL_Test::Schema::Bazs',
131 { 'foreign.baz_num' => 'self.baz_id' });
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;
140 __PACKAGE__->has_one('foorel2', 'DBIXCSL_Test::Schema::Foos',
141 { 'foreign.fooid' => 'self.foo_id' });
146 write_v4_schema_pm();
148 my $res = run_loader(dump_directory => $DUMP_DIR, naming => 'current');
149 my $schema = $res->{schema};
153 lives_and { is $schema->resultset('Quux')->find(1)->a_method, 'dongs' }
154 'external custom content for unsingularized Result was loaded by upgraded ' .
157 lives_and { isa_ok $schema->resultset('Quux')->find(1)->bazrel2,
158 $res->{classes}{bazs} }
159 'unsingularized class names in external content are translated';
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';
166 my $file = $schema->_loader->_get_dump_filename($res->{classes}{quuxs});
167 my $code = do { local ($/, @ARGV) = (undef, $file); <> };
169 like $code, qr/package ${SCHEMA_CLASS}::Quux;/,
170 'package line translated correctly from external custom content in static dump';
172 like $code, qr/sub a_method { 'dongs' }/,
173 'external custom content loaded into static dump correctly';
179 # test running against v4 schema without upgrade, twice, then upgrade
181 write_v4_schema_pm();
182 my $res = run_loader(dump_directory => $DUMP_DIR);
183 my $warning = $res->{warnings}[0];
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';
192 is scalar @{ $res->{warnings} }, 3,
193 'correct number of warnings for static schema in backcompat mode';
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});
202 local ($^I, @ARGV) = ('', $quuxs_pm);
204 if (/DO NOT MODIFY THIS OR ANYTHING ABOVE/) {
207 sub a_method { 'mtfnpy' }
209 __PACKAGE__->has_one('bazrel3', 'DBIXCSL_Test::Schema::Bazs',
210 { 'foreign.baz_num' => 'self.baz_id' });
219 # Rerun the loader in backcompat mode to make sure it's still in backcompat
221 $res = run_loader(dump_directory => $DUMP_DIR);
224 # now upgrade the schema
225 $res = run_loader(dump_directory => $DUMP_DIR, naming => 'current');
226 $schema = $res->{schema};
228 like $res->{warnings}[0], qr/Dumping manual schema/i,
229 'correct warnings on upgrading static schema (with "naming" set)';
231 like $res->{warnings}[1], qr/dump completed/i,
232 'correct warnings on upgrading static schema (with "naming" set)';
234 is scalar @{ $res->{warnings} }, 2,
235 'correct number of warnings on upgrading static schema (with "naming" set)'
236 or diag @{ $res->{warnings} };
240 (my $result_dir = "$DUMP_DIR/$SCHEMA_CLASS") =~ s{::}{/}g;
241 my $result_count =()= glob "$result_dir/*";
244 'un-singularized results were replaced during upgrade';
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';
250 lives_and { isa_ok $schema->resultset('Quux')->find(1)->bazrel3,
251 $res->{classes}{bazs} }
252 'unsingularized class names in custom content are translated';
254 my $file = $schema->_loader->_get_dump_filename($res->{classes}{quuxs});
255 my $code = do { local ($/, @ARGV) = (undef, $file); <> };
257 like $code, qr/sub a_method { 'mtfnpy' }/,
258 'custom content from unsingularized Result loaded into static dump correctly';
261 # test upgrading a v4 schema, the check that the version string is correct
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};
268 my $file = $schema->_loader->_get_dump_filename($SCHEMA_CLASS);
269 my $code = do { local ($/, @ARGV) = (undef, $file); <> };
272 $code =~ /^# Created by DBIx::Class::Schema::Loader v(\S+)/m;
274 is $dumped_ver, $DBIx::Class::Schema::Loader::VERSION,
275 'correct version dumped after upgrade of v4 static schema';
278 # Test upgrading an already singular result with custom content that refers to
281 write_v4_schema_pm();
282 my $res = run_loader(dump_directory => $DUMP_DIR);
283 my $schema = $res->{schema};
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});
290 local ($^I, @ARGV) = ('', $bar_pm);
292 if (/DO NOT MODIFY THIS OR ANYTHING ABOVE/) {
295 sub a_method { 'lalala' }
297 __PACKAGE__->has_one('foorel3', 'DBIXCSL_Test::Schema::Foos',
298 { 'foreign.fooid' => 'self.foo_id' });
307 # now upgrade the schema
308 $res = run_loader(dump_directory => $DUMP_DIR, naming => 'current');
309 $schema = $res->{schema};
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';
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';
321 my $file = $schema->_loader->_get_dump_filename($res->{classes}{bar});
322 my $code = do { local ($/, @ARGV) = (undef, $file); <> };
324 like $code, qr/sub a_method { 'lalala' }/,
325 'custom content from Result with unchanged name loaded into static dump ' .
332 rmtree $DUMP_DIR unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP};
336 my %loader_opts = @_;
338 $loader_opts{use_namespaces} = 0
339 unless exists $loader_opts{use_namespaces};
342 foreach my $source_name ($SCHEMA_CLASS->clone->sources) {
343 Class::Unload->unload("${SCHEMA_CLASS}::${source_name}");
346 Class::Unload->unload($SCHEMA_CLASS);
350 my @connect_info = $make_dbictest_db2::dsn;
352 local $SIG{__WARN__} = sub { push(@loader_warnings, $_[0]); };
354 package $SCHEMA_CLASS;
355 use base qw/DBIx::Class::Schema::Loader/;
357 __PACKAGE__->loader_options(\%loader_opts);
358 __PACKAGE__->connection(\@connect_info);
361 ok(!$@, "Loader initialization") or diag $@;
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}";
373 warnings => \@loader_warnings,
374 monikers => \%monikers,
375 classes => \%classes,
379 sub write_v4_schema_pm {
380 (my $schema_dir = "$DUMP_DIR/$SCHEMA_CLASS") =~ s/::[^:]+\z//;
382 make_path $schema_dir;
383 my $schema_pm = "$schema_dir/Schema.pm";
384 open my $fh, '>', $schema_pm or die $!;
386 package DBIXCSL_Test::Schema;
391 use base 'DBIx::Class::Schema';
393 __PACKAGE__->load_classes;
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
400 # You can replace this text with custom content, and it will be preserved on regeneration
407 my $schema = $res->{schema};
409 is_deeply [ @{ $res->{monikers} }{qw/foos bar bazs quuxs/} ],
410 [qw/Foos Bar Bazs Quuxs/],
411 'correct monikers in 0.04006 mode';
413 isa_ok ((my $bar = eval { $schema->resultset('Bar')->find(1) }),
414 $res->{classes}{bar},
417 isa_ok eval { $bar->foo_id }, $res->{classes}{foos},
418 'correct rel name in 0.04006 mode';
420 ok my $baz = eval { $schema->resultset('Bazs')->find(1) };
422 isa_ok eval { $baz->quux }, 'DBIx::Class::ResultSet',
423 'correct rel type and name for UNIQUE FK in 0.04006 mode';
428 my $schema = $res->{schema};
430 is_deeply [ @{ $res->{monikers} }{qw/foos bar bazs quuxs/} ],
431 [qw/Foo Bar Baz Quux/],
432 'correct monikers in current mode';
434 ok my $bar = eval { $schema->resultset('Bar')->find(1) };
436 isa_ok eval { $bar->foo }, $res->{classes}{foos},
437 'correct rel name in current mode';
439 ok my $baz = eval { $schema->resultset('Baz')->find(1) };
441 isa_ok eval { $baz->quux }, $res->{classes}{quuxs},
442 'correct rel type and name for UNIQUE FK in current mode';