11 use DBIx::Class::_Util 'sigwarn_silencer';
16 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('deploy')
17 unless DBIx::Class::Optional::Dependencies->req_ok_for ('deploy')
20 # Test for SQLT-related leaks
22 my $s = DBICTest::Schema->clone;
25 create_schema ({ schema => $s }),
26 create_schema ({ args => { parser_args => { dbic_schema => $s } } }),
29 for my $parser_args_key (qw(
35 push @schemas, create_schema({
36 args => { parser_args => { $parser_args_key => $s } }
38 } qr/\Qparser_args => {\E.+?is deprecated.+\Q@{[__FILE__]}/,
39 "deprecated crazy parser_arg '$parser_args_key' warned";
42 Scalar::Util::weaken ($s);
44 ok (!$s, 'Schema not leaked');
46 isa_ok ($_, 'SQL::Translator::Schema', "SQLT schema object $_ produced")
50 # make sure classname-style works
51 lives_ok { isa_ok (create_schema ({ schema => 'DBICTest::Schema' }), 'SQL::Translator::Schema', 'SQLT schema object produced') };
53 # make sure a connected instance passed via $args does not get the $dbh improperly serialized
56 # YAML is a build_requires dep of SQLT - it may or may not be here
57 eval { require YAML } or skip "Test requires YAML.pm", 1;
61 my $s = DBICTest->init_schema(no_populate => 1);
62 ok ($s->storage->connected, '$schema instance connected');
64 # roundtrip through YAML
65 my $yaml_rt_schema = SQL::Translator->new(
66 parser => 'SQL::Translator::Parser::YAML'
68 data => SQL::Translator->new(
69 parser_args => { dbic_schema => $s },
70 parser => 'SQL::Translator::Parser::DBIx::Class',
71 producer => 'SQL::Translator::Producer::YAML',
75 isa_ok ( $yaml_rt_schema, 'SQL::Translator::Schema', 'SQLT schema object produced after YAML roundtrip');
77 ok ($s->storage->connected, '$schema instance still connected');
80 eval <<'EOE' or die $@;
82 # we are in END - everything remains global
84 $^W = 1; # important, otherwise DBI won't trip the next fail()
85 $SIG{__WARN__} = sub {
86 fail "Unexpected global destruction warning"
87 if $_[0] =~ /is not a DBI/;
95 my $schema = DBICTest->init_schema( no_deploy => 1 );
97 # Dummy was yanked out by the sqlt hook test
98 # CustomSql tests the horrific/deprecated ->name(\$sql) hack
99 # YearXXXXCDs are views
102 { $_ !~ /^ (?: Dummy | CustomSql | Year\d{4}CDs ) $/x }
106 my $idx_exceptions = {
108 'ForceForeign' => -1,
110 'TwoKeys' => -1, # TwoKeys has the index turned off on the rel def
114 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
116 foreach my $source_name (@sources) {
117 my $table = get_table($sqlt_schema, $schema, $source_name);
119 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
120 $fk_count += $idx_exceptions->{$source_name} || 0;
121 my @indices = $table->get_indices;
123 my $index_count = scalar(@indices);
124 is($index_count, $fk_count, "correct number of indices for $source_name with no args");
126 for my $index (@indices) {
127 my $source = $schema->source($source_name);
128 my $pk_test = join("\x00", $source->primary_columns);
129 my $idx_test = join("\x00", $index->fields);
130 isnt ( $pk_test, $idx_test, "no additional index for the primary columns exists in $source_name");
136 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } });
138 foreach my $source_name (@sources) {
139 my $table = get_table($sqlt_schema, $schema, $source_name);
141 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
142 $fk_count += $idx_exceptions->{$source_name} || 0;
143 my @indices = $table->get_indices;
144 my $index_count = scalar(@indices);
145 is($index_count, $fk_count, "correct number of indices for $source_name with add_fk_index => 1");
150 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
152 foreach my $source (@sources) {
153 my $table = get_table($sqlt_schema, $schema, $source);
155 my @indices = $table->get_indices;
156 my $index_count = scalar(@indices);
157 is($index_count, 0, "correct number of indices for $source with add_fk_index => 0");
163 package # hide from PAUSE
164 DBICTest::Schema::NoViewDefinition;
166 use base qw/DBICTest::BaseResult/;
168 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
169 __PACKAGE__->table('noviewdefinition');
174 my $schema_invalid_view = $schema->clone;
175 $schema_invalid_view->register_class('NoViewDefinition', 'DBICTest::Schema::NoViewDefinition');
177 throws_ok { create_schema({ schema => $schema_invalid_view }) }
178 qr/view noviewdefinition is missing a view_definition/,
179 'parser detects views with a view_definition';
183 my $sqlt_schema = create_schema ({
193 [$sqlt_schema->get_tables ],
195 'sources limitng with relationships works',
201 package DBICTest::PartialSchema;
203 use base qw/DBIx::Class::Schema/;
205 __PACKAGE__->load_classes(
206 { 'DBICTest::Schema' => [qw/
217 my $partial_schema = DBICTest::PartialSchema->connect(DBICTest->_database);
220 my $sqlt_schema = do {
222 local $SIG{__WARN__} = sigwarn_silencer(
223 qr/Ignoring relationship .+ related resultsource .+ is not registered with this schema/
226 create_schema({ schema => $partial_schema });
229 my @tables = $sqlt_schema->get_tables;
232 [sort map { $_->name } @tables],
233 [qw/cd cd_to_producer producer tags track/],
234 'partial dbic schema parsing ok',
237 # the primary key is currently unnamed in sqlt - adding below
238 my %constraints_for_table = (
239 producer => [qw/prod_name /],
240 tags => [qw/tagid_cd tagid_cd_tag tags_fk_cd tags_tagid_tag tags_tagid_tag_cd /],
241 track => [qw/track_cd_position track_cd_title track_fk_cd /],
242 cd => [qw/cd_artist_title cd_fk_single_track /],
243 cd_to_producer => [qw/cd_to_producer_fk_cd cd_to_producer_fk_producer /],
246 for my $table (@tables) {
247 my $tablename = $table->name;
248 my @constraints = $table->get_constraints;
250 [ sort map { $_->name } @constraints ],
252 # the primary key (present on all loaded tables) is currently named '' in sqlt
253 # subject to future changes
254 [ '', @{$constraints_for_table{$tablename}} ],
256 "constraints of table '$tablename' ok",
259 }, 'partial schema tests successful');
263 my $cd_rsrc = $schema->source('CD');
264 $cd_rsrc->name(\'main.cd');
266 my $sqlt_schema = create_schema(
267 { schema => $schema },
268 args => { ignore_constraint_names => 0, ignore_index_names => 0 }
271 foreach my $source_name (qw(CD)) {
272 my $table = get_table($sqlt_schema, $schema, $source_name);
274 !(grep {$_->name =~ m/main\./} $table->get_indices),
275 'indices have periods stripped out'
278 !(grep {$_->name =~ m/main\./} $table->get_constraints),
279 'constraints have periods stripped out'
289 my $additional_sqltargs = $args->{args} || {};
293 ignore_constraint_names => 1,
294 ignore_index_names => 1,
295 %{$additional_sqltargs}
298 my $sqlt = SQL::Translator->new( $sqltargs );
300 $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
301 return $sqlt->translate(
302 $args->{schema} ? ( data => $args->{schema} ) : ()
303 ) || die $sqlt->error;
307 my ($sqlt_schema, $schema, $source) = @_;
309 my $table_name = $schema->source($source)->from;
310 $table_name = $$table_name if ref $table_name;
312 return $sqlt_schema->get_table($table_name);