4 use File::Path qw/rmtree make_path/;
6 use File::Temp qw/tempfile tempdir/;
11 my $DUMP_DIR = './t/_common_dump';
13 my $SCHEMA_CLASS = 'DBIXCSL_Test::Schema';
19 foreach my $source_name ($SCHEMA_CLASS->clone->sources) {
20 Class::Unload->unload("${SCHEMA_CLASS}::${source_name}");
23 Class::Unload->unload($SCHEMA_CLASS);
27 my @connect_info = $make_dbictest_db2::dsn;
29 local $SIG{__WARN__} = sub { push(@loader_warnings, $_[0]); };
31 package $SCHEMA_CLASS;
32 use base qw/DBIx::Class::Schema::Loader/;
34 __PACKAGE__->loader_options(\%loader_opts);
35 __PACKAGE__->connection(\@connect_info);
38 ok(!$@, "Loader initialization") or diag $@;
40 my $schema = $SCHEMA_CLASS->clone;
41 my (%monikers, %classes);
42 foreach my $source_name ($schema->sources) {
43 my $table_name = $schema->source($source_name)->from;
44 $monikers{$table_name} = $source_name;
45 $classes{$table_name} = "${SCHEMA_CLASS}::${source_name}";
50 warnings => \@loader_warnings,
51 monikers => \%monikers,
58 my $schema = $res->{schema};
60 is_deeply [ @{ $res->{monikers} }{qw/foos bar bazs quuxs/} ],
61 [qw/Foos Bar Bazs Quuxs/],
62 'correct monikers in 0.04006 mode';
64 isa_ok ((my $bar = eval { $schema->resultset('Bar')->find(1) }),
68 isa_ok eval { $bar->foo_id }, $res->{classes}{foos},
69 'correct rel name in 0.04006 mode';
71 ok my $baz = eval { $schema->resultset('Bazs')->find(1) };
73 isa_ok eval { $baz->quux }, 'DBIx::Class::ResultSet',
74 'correct rel type and name for UNIQUE FK in 0.04006 mode';
79 my $schema = $res->{schema};
81 is_deeply [ @{ $res->{monikers} }{qw/foos bar bazs quuxs/} ],
82 [qw/Foo Bar Baz Quux/],
83 'correct monikers in current mode';
85 ok my $bar = eval { $schema->resultset('Bar')->find(1) };
87 isa_ok eval { $bar->foo }, $res->{classes}{foos},
88 'correct rel name in current mode';
90 ok my $baz = eval { $schema->resultset('Baz')->find(1) };
92 isa_ok eval { $baz->quux }, $res->{classes}{quuxs},
93 'correct rel type and name for UNIQUE FK in current mode';
96 # test dynamic schema in 0.04006 mode
98 my $res = run_loader();
99 my $warning = $res->{warnings}[0];
101 like $warning, qr/dynamic schema/i,
102 'dynamic schema in backcompat mode detected';
103 like $warning, qr/run in 0\.04006 mode/i,
104 'dynamic schema in 0.04006 mode warning';
105 like $warning, qr/DBIx::Class::Schema::Loader::Manual::UpgradingFromV4/,
106 'warning refers to upgrading doc';
111 # setting naming accessor on dynamic schema should disable warning (even when
112 # we're setting it to 'v4' .)
114 my $res = run_loader(naming => 'v4');
116 is_deeply $res->{warnings}, [], 'no warnings with naming attribute set';
121 # test upgraded dynamic schema
123 my $res = run_loader(naming => 'current');
125 # to dump a schema for debugging...
127 # mkdir '/tmp/HLAGH';
128 # $schema->_loader->{dump_directory} = '/tmp/HLAGH';
129 # $schema->_loader->_dump_to_dir(values %{ $res->{classes} });
132 is_deeply $res->{warnings}, [], 'no warnings with naming attribute set';
137 # test upgraded dynamic schema with external content loaded
139 my $temp_dir = tempdir;
140 push @INC, $temp_dir;
142 my $external_result_dir = join '/', $temp_dir, split /::/, $SCHEMA_CLASS;
143 make_path $external_result_dir;
145 IO::File->new(">$external_result_dir/Quuxs.pm")->print(<<"EOF");
146 package ${SCHEMA_CLASS}::Quuxs;
147 sub a_method { 'hlagh' }
151 my $res = run_loader(naming => 'current');
152 my $schema = $res->{schema};
154 is scalar @{ $res->{warnings} }, 1,
155 'correct nummber of warnings for upgraded dynamic schema with external ' .
156 'content for unsingularized Result.';
158 my $warning = $res->{warnings}[0];
159 like $warning, qr/Detected external content/i,
160 'detected external content warning';
162 is eval { $schema->resultset('Quux')->find(1)->a_method }, 'hlagh',
163 'external custom content for unsingularized Result was loaded by upgraded ' .
172 # test running against v4 schema without upgrade
174 # write out the 0.04006 Schema.pm we have in __DATA__
175 (my $schema_dir = "$DUMP_DIR/$SCHEMA_CLASS") =~ s/::[^:]+\z//;
176 make_path $schema_dir;
177 my $schema_pm = "$schema_dir/Schema.pm";
178 open my $fh, '>', $schema_pm or die $!;
185 my $res = run_loader(dump_directory => $DUMP_DIR);
186 my $warning = $res->{warnings}[0];
188 like $warning, qr/static schema/i,
189 'static schema in backcompat mode detected';
190 like $warning, qr/0.04006/,
191 'correct version detected';
192 like $warning, qr/DBIx::Class::Schema::Loader::Manual::UpgradingFromV4/,
193 'refers to upgrading doc';
195 is scalar @{ $res->{warnings} }, 3,
196 'correct number of warnings for static schema in backcompat mode';
200 # add some custom content to a Result that will be replaced
201 my $schema = $res->{schema};
202 my $quuxs_pm = $schema->_loader
203 ->_get_dump_filename($res->{classes}{quuxs});
205 local ($^I, @ARGV) = ('', $quuxs_pm);
207 if (/DO NOT MODIFY THIS OR ANYTHING ABOVE/) {
209 print "sub a_method { 'mtfnpy' }\n";
217 # now upgrade the schema
218 $res = run_loader(dump_directory => $DUMP_DIR, naming => 'current');
219 $schema = $res->{schema};
221 like $res->{warnings}[0], qr/Dumping manual schema/i,
222 'correct warnings on upgrading static schema (with "naming" set)';
224 like $res->{warnings}[1], qr/dump completed/i,
225 'correct warnings on upgrading static schema (with "naming" set)';
227 is scalar @{ $res->{warnings} }, 2,
228 'correct number of warnings on upgrading static schema (with "naming" set)'
229 or diag @{ $res->{warnings} };
233 (my $result_dir = "$DUMP_DIR/$SCHEMA_CLASS") =~ s{::}{/}g;
234 my $result_count =()= glob "$result_dir/*";
237 'un-singularized results were replaced during upgrade';
239 # check that custom content was preserved
240 is eval { $schema->resultset('Quux')->find(1)->a_method }, 'mtfnpy',
241 'custom content was carried over from un-singularized Result';
247 rmtree $DUMP_DIR unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP};
250 # a Schema.pm made with 0.04006
253 package DBIXCSL_Test::Schema;
258 use base 'DBIx::Class::Schema';
260 __PACKAGE__->load_classes;
263 # Created by DBIx::Class::Schema::Loader v0.04006 @ 2009-12-25 01:49:25
264 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ibIJTbfM1ji4pyD/lgSEog
267 # You can replace this text with custom content, and it will be preserved on regeneration