1 use DBIx::Class::Optional::Dependencies -skip_all_without => 'deploy';
13 use DBIx::Class::_Util 'sigwarn_silencer';
15 # Test for SQLT-related leaks
17 my $s = DBICTest::Schema->clone;
20 create_schema ({ schema => $s }),
21 create_schema ({ args => { parser_args => { dbic_schema => $s } } }),
24 for my $parser_args_key (qw(
30 push @schemas, create_schema({
31 args => { parser_args => { $parser_args_key => $s } }
33 } qr/\Qparser_args => {\E.+?is deprecated.+\Q@{[__FILE__]}/,
34 "deprecated crazy parser_arg '$parser_args_key' warned";
37 Scalar::Util::weaken ($s);
39 ok (!$s, 'Schema not leaked');
41 isa_ok ($_, 'SQL::Translator::Schema', "SQLT schema object $_ produced")
45 # make sure classname-style works
46 lives_ok { isa_ok (create_schema ({ schema => 'DBICTest::Schema' }), 'SQL::Translator::Schema', 'SQLT schema object produced') };
48 # make sure a connected instance passed via $args does not get the $dbh improperly serialized
51 # YAML is a build_requires dep of SQLT - it may or may not be here
52 eval { require YAML } or skip "Test requires YAML.pm", 1;
56 my $s = DBICTest->init_schema(no_populate => 1);
57 ok ($s->storage->connected, '$schema instance connected');
59 # roundtrip through YAML
60 my $yaml_rt_schema = SQL::Translator->new(
61 parser => 'SQL::Translator::Parser::YAML'
63 data => SQL::Translator->new(
64 parser_args => { dbic_schema => $s },
65 parser => 'SQL::Translator::Parser::DBIx::Class',
66 producer => 'SQL::Translator::Producer::YAML',
70 isa_ok ( $yaml_rt_schema, 'SQL::Translator::Schema', 'SQLT schema object produced after YAML roundtrip');
72 ok ($s->storage->connected, '$schema instance still connected');
75 eval <<'EOE' or die $@;
77 # we are in END - everything remains global
79 $^W = 1; # important, otherwise DBI won't trip the next fail()
80 $SIG{__WARN__} = sub {
81 fail "Unexpected global destruction warning"
82 if $_[0] =~ /is not a DBI/;
90 my $schema = DBICTest->init_schema( no_deploy => 1 );
92 # Dummy was yanked out by the sqlt hook test
93 # CustomSql tests the horrific/deprecated ->name(\$sql) hack
94 # YearXXXXCDs are views
97 { $_ !~ /^ (?: Dummy | CustomSql | Year\d{4}CDs ) $/x }
101 my $idx_exceptions = {
103 'ForceForeign' => -1,
105 'TwoKeys' => -1, # TwoKeys has the index turned off on the rel def
109 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
111 foreach my $source_name (@sources) {
112 my $table = get_table($sqlt_schema, $schema, $source_name);
114 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
115 $fk_count += $idx_exceptions->{$source_name} || 0;
116 my @indices = $table->get_indices;
118 my $index_count = scalar(@indices);
119 is($index_count, $fk_count, "correct number of indices for $source_name with no args");
121 for my $index (@indices) {
122 my $source = $schema->source($source_name);
123 my $pk_test = join("\x00", $source->primary_columns);
124 my $idx_test = join("\x00", $index->fields);
125 isnt ( $pk_test, $idx_test, "no additional index for the primary columns exists in $source_name");
131 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } });
133 foreach my $source_name (@sources) {
134 my $table = get_table($sqlt_schema, $schema, $source_name);
136 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
137 $fk_count += $idx_exceptions->{$source_name} || 0;
138 my @indices = $table->get_indices;
139 my $index_count = scalar(@indices);
140 is($index_count, $fk_count, "correct number of indices for $source_name with add_fk_index => 1");
145 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
147 foreach my $source (@sources) {
148 my $table = get_table($sqlt_schema, $schema, $source);
150 my @indices = $table->get_indices;
151 my $index_count = scalar(@indices);
152 is($index_count, 0, "correct number of indices for $source with add_fk_index => 0");
158 package # hide from PAUSE
159 DBICTest::Schema::NoViewDefinition;
161 use base qw/DBICTest::BaseResult/;
163 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
164 __PACKAGE__->table('noviewdefinition');
169 my $schema_invalid_view = $schema->clone;
170 $schema_invalid_view->register_class('NoViewDefinition', 'DBICTest::Schema::NoViewDefinition');
172 throws_ok { create_schema({ schema => $schema_invalid_view }) }
173 qr/view noviewdefinition is missing a view_definition/,
174 'parser detects views with a view_definition';
178 my $sqlt_schema = create_schema ({
188 [$sqlt_schema->get_tables ],
190 'sources limitng with relationships works',
196 package DBICTest::PartialSchema;
198 use base qw/DBIx::Class::Schema/;
200 __PACKAGE__->load_classes(
201 { 'DBICTest::Schema' => [qw/
212 my $partial_schema = DBICTest::PartialSchema->connect(DBICTest->_database);
215 my $sqlt_schema = do {
217 local $SIG{__WARN__} = sigwarn_silencer(
218 qr/Ignoring relationship .+ related resultsource .+ is not registered with this schema/
221 create_schema({ schema => $partial_schema });
224 my @tables = $sqlt_schema->get_tables;
227 [sort map { $_->name } @tables],
228 [qw/cd cd_to_producer producer tags track/],
229 'partial dbic schema parsing ok',
232 # the primary key is currently unnamed in sqlt - adding below
233 my %constraints_for_table = (
234 producer => [qw/prod_name /],
235 tags => [qw/tagid_cd tagid_cd_tag tags_fk_cd tags_tagid_tag tags_tagid_tag_cd /],
236 track => [qw/track_cd_position track_cd_title track_fk_cd /],
237 cd => [qw/cd_artist_title cd_fk_single_track /],
238 cd_to_producer => [qw/cd_to_producer_fk_cd cd_to_producer_fk_producer /],
241 for my $table (@tables) {
242 my $tablename = $table->name;
243 my @constraints = $table->get_constraints;
245 [ sort map { $_->name } @constraints ],
247 # the primary key (present on all loaded tables) is currently named '' in sqlt
248 # subject to future changes
249 [ '', @{$constraints_for_table{$tablename}} ],
251 "constraints of table '$tablename' ok",
254 }, 'partial schema tests successful');
258 my $cd_rsrc = $schema->source('CD');
259 $cd_rsrc->name(\'main.cd');
261 my $sqlt_schema = create_schema(
262 { schema => $schema },
263 args => { ignore_constraint_names => 0, ignore_index_names => 0 }
266 foreach my $source_name (qw(CD)) {
267 my $table = get_table($sqlt_schema, $schema, $source_name);
269 !(grep {$_->name =~ m/main\./} $table->get_indices),
270 'indices have periods stripped out'
273 !(grep {$_->name =~ m/main\./} $table->get_constraints),
274 'constraints have periods stripped out'
284 my $additional_sqltargs = $args->{args} || {};
288 ignore_constraint_names => 1,
289 ignore_index_names => 1,
290 %{$additional_sqltargs}
293 my $sqlt = SQL::Translator->new( $sqltargs );
295 $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
296 return $sqlt->translate(
297 $args->{schema} ? ( data => $args->{schema} ) : ()
298 ) || die $sqlt->error;
302 my ($sqlt_schema, $schema, $source) = @_;
304 my $table_name = $schema->source($source)->from;
305 $table_name = $$table_name if ref $table_name;
307 return $sqlt_schema->get_table($table_name);