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';
15 # test dynamic schema in 0.04006 mode
17 my $res = run_loader();
18 my $warning = $res->{warnings}[0];
20 like $warning, qr/dynamic schema/i,
21 'dynamic schema in backcompat mode detected';
22 like $warning, qr/run in 0\.04006 mode/i,
23 'dynamic schema in 0.04006 mode warning';
24 like $warning, qr/DBIx::Class::Schema::Loader::Manual::UpgradingFromV4/,
25 'warning refers to upgrading doc';
30 # setting naming accessor on dynamic schema should disable warning (even when
31 # we're setting it to 'v4' .)
33 my $res = run_loader(naming => 'v4');
35 is_deeply $res->{warnings}, [], 'no warnings with naming attribute set';
40 # test upgraded dynamic schema
42 my $res = run_loader(naming => 'current');
44 # to dump a schema for debugging...
47 # $schema->_loader->{dump_directory} = '/tmp/HLAGH';
48 # $schema->_loader->_dump_to_dir(values %{ $res->{classes} });
51 is_deeply $res->{warnings}, [], 'no warnings with naming attribute set';
56 # test upgraded dynamic schema with external content loaded
58 my $temp_dir = tempdir;
61 my $external_result_dir = join '/', $temp_dir, split /::/, $SCHEMA_CLASS;
62 make_path $external_result_dir;
64 IO::File->new(">$external_result_dir/Quuxs.pm")->print(<<"EOF");
65 package ${SCHEMA_CLASS}::Quuxs;
66 sub a_method { 'hlagh' }
70 my $res = run_loader(naming => 'current');
71 my $schema = $res->{schema};
73 is scalar @{ $res->{warnings} }, 1,
74 'correct nummber of warnings for upgraded dynamic schema with external ' .
75 'content for unsingularized Result.';
77 my $warning = $res->{warnings}[0];
78 like $warning, qr/Detected external content/i,
79 'detected external content warning';
81 is eval { $schema->resultset('Quux')->find(1)->a_method }, 'hlagh',
82 'external custom content for unsingularized Result was loaded by upgraded ' .
91 # test running against v4 schema without upgrade
93 # write out the 0.04006 Schema.pm we have in __DATA__
94 (my $schema_dir = "$DUMP_DIR/$SCHEMA_CLASS") =~ s/::[^:]+\z//;
95 make_path $schema_dir;
96 my $schema_pm = "$schema_dir/Schema.pm";
97 open my $fh, '>', $schema_pm or die $!;
104 my $res = run_loader(dump_directory => $DUMP_DIR);
105 my $warning = $res->{warnings}[0];
107 like $warning, qr/static schema/i,
108 'static schema in backcompat mode detected';
109 like $warning, qr/0.04006/,
110 'correct version detected';
111 like $warning, qr/DBIx::Class::Schema::Loader::Manual::UpgradingFromV4/,
112 'refers to upgrading doc';
114 is scalar @{ $res->{warnings} }, 3,
115 'correct number of warnings for static schema in backcompat mode';
119 # add some custom content to a Result that will be replaced
120 my $schema = $res->{schema};
121 my $quuxs_pm = $schema->_loader
122 ->_get_dump_filename($res->{classes}{quuxs});
124 local ($^I, @ARGV) = ('', $quuxs_pm);
126 if (/DO NOT MODIFY THIS OR ANYTHING ABOVE/) {
128 print "sub a_method { 'mtfnpy' }\n";
136 # now upgrade the schema
137 $res = run_loader(dump_directory => $DUMP_DIR, naming => 'current');
138 $schema = $res->{schema};
140 like $res->{warnings}[0], qr/Dumping manual schema/i,
141 'correct warnings on upgrading static schema (with "naming" set)';
143 like $res->{warnings}[1], qr/dump completed/i,
144 'correct warnings on upgrading static schema (with "naming" set)';
146 is scalar @{ $res->{warnings} }, 2,
147 'correct number of warnings on upgrading static schema (with "naming" set)'
148 or diag @{ $res->{warnings} };
152 (my $result_dir = "$DUMP_DIR/$SCHEMA_CLASS") =~ s{::}{/}g;
153 my $result_count =()= glob "$result_dir/*";
156 'un-singularized results were replaced during upgrade';
158 # check that custom content was preserved
159 is eval { $schema->resultset('Quux')->find(1)->a_method }, 'mtfnpy',
160 'custom content was carried over from un-singularized Result';
166 rmtree $DUMP_DIR unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP};
170 my %loader_opts = @_;
173 foreach my $source_name ($SCHEMA_CLASS->clone->sources) {
174 Class::Unload->unload("${SCHEMA_CLASS}::${source_name}");
177 Class::Unload->unload($SCHEMA_CLASS);
181 my @connect_info = $make_dbictest_db2::dsn;
183 local $SIG{__WARN__} = sub { push(@loader_warnings, $_[0]); };
185 package $SCHEMA_CLASS;
186 use base qw/DBIx::Class::Schema::Loader/;
188 __PACKAGE__->loader_options(\%loader_opts);
189 __PACKAGE__->connection(\@connect_info);
192 ok(!$@, "Loader initialization") or diag $@;
194 my $schema = $SCHEMA_CLASS->clone;
195 my (%monikers, %classes);
196 foreach my $source_name ($schema->sources) {
197 my $table_name = $schema->source($source_name)->from;
198 $monikers{$table_name} = $source_name;
199 $classes{$table_name} = "${SCHEMA_CLASS}::${source_name}";
204 warnings => \@loader_warnings,
205 monikers => \%monikers,
206 classes => \%classes,
212 my $schema = $res->{schema};
214 is_deeply [ @{ $res->{monikers} }{qw/foos bar bazs quuxs/} ],
215 [qw/Foos Bar Bazs Quuxs/],
216 'correct monikers in 0.04006 mode';
218 isa_ok ((my $bar = eval { $schema->resultset('Bar')->find(1) }),
219 $res->{classes}{bar},
222 isa_ok eval { $bar->foo_id }, $res->{classes}{foos},
223 'correct rel name in 0.04006 mode';
225 ok my $baz = eval { $schema->resultset('Bazs')->find(1) };
227 isa_ok eval { $baz->quux }, 'DBIx::Class::ResultSet',
228 'correct rel type and name for UNIQUE FK in 0.04006 mode';
233 my $schema = $res->{schema};
235 is_deeply [ @{ $res->{monikers} }{qw/foos bar bazs quuxs/} ],
236 [qw/Foo Bar Baz Quux/],
237 'correct monikers in current mode';
239 ok my $bar = eval { $schema->resultset('Bar')->find(1) };
241 isa_ok eval { $bar->foo }, $res->{classes}{foos},
242 'correct rel name in current mode';
244 ok my $baz = eval { $schema->resultset('Baz')->find(1) };
246 isa_ok eval { $baz->quux }, $res->{classes}{quuxs},
247 'correct rel type and name for UNIQUE FK in current mode';
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