14 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('deploy')
15 unless DBIx::Class::Optional::Dependencies->req_ok_for ('deploy')
18 # Test for SQLT-related leaks
20 my $s = DBICTest::Schema->clone;
23 create_schema ({ schema => $s }),
24 create_schema ({ args => { parser_args => { 'DBIx::Class::Schema' => $s } } }),
25 create_schema ({ args => { parser_args => { 'DBIx::Schema' => $s } } }),
26 create_schema ({ args => { parser_args => { package => $s } } }),
29 Scalar::Util::weaken ($s);
31 ok (!$s, 'Schema not leaked');
33 isa_ok ($_, 'SQL::Translator::Schema', "SQLT schema object $_ produced")
37 # make sure classname-style works
38 lives_ok { isa_ok (create_schema ({ schema => 'DBICTest::Schema' }), 'SQL::Translator::Schema', 'SQLT schema object produced') };
41 my $schema = DBICTest->init_schema( no_deploy => 1 );
43 # Dummy was yanked out by the sqlt hook test
44 # CustomSql tests the horrific/deprecated ->name(\$sql) hack
45 # YearXXXXCDs are views
48 { $_ !~ /^ (?: Dummy | CustomSql | Year\d{4}CDs ) $/x }
52 my $idx_exceptions = {
56 'TwoKeys' => -1, # TwoKeys has the index turned off on the rel def
60 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
62 foreach my $source_name (@sources) {
63 my $table = get_table($sqlt_schema, $schema, $source_name);
65 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
66 $fk_count += $idx_exceptions->{$source_name} || 0;
67 my @indices = $table->get_indices;
69 my $index_count = scalar(@indices);
70 is($index_count, $fk_count, "correct number of indices for $source_name with no args");
72 for my $index (@indices) {
73 my $source = $schema->source($source_name);
74 my $pk_test = join("\x00", $source->primary_columns);
75 my $idx_test = join("\x00", $index->fields);
76 isnt ( $pk_test, $idx_test, "no additional index for the primary columns exists in $source_name");
82 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } });
84 foreach my $source_name (@sources) {
85 my $table = get_table($sqlt_schema, $schema, $source_name);
87 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
88 $fk_count += $idx_exceptions->{$source_name} || 0;
89 my @indices = $table->get_indices;
90 my $index_count = scalar(@indices);
91 is($index_count, $fk_count, "correct number of indices for $source_name with add_fk_index => 1");
96 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
98 foreach my $source (@sources) {
99 my $table = get_table($sqlt_schema, $schema, $source);
101 my @indices = $table->get_indices;
102 my $index_count = scalar(@indices);
103 is($index_count, 0, "correct number of indices for $source with add_fk_index => 0");
109 package # hide from PAUSE
110 DBICTest::Schema::NoViewDefinition;
112 use base qw/DBICTest::BaseResult/;
114 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
115 __PACKAGE__->table('noviewdefinition');
120 my $schema_invalid_view = $schema->clone;
121 $schema_invalid_view->register_class('NoViewDefinition', 'DBICTest::Schema::NoViewDefinition');
123 throws_ok { create_schema({ schema => $schema_invalid_view }) }
124 qr/view noviewdefinition is missing a view_definition/,
125 'parser detects views with a view_definition';
129 my $sqlt_schema = create_schema ({
139 [$sqlt_schema->get_tables ],
141 'sources limitng with relationships works',
147 package DBICTest::PartialSchema;
149 use base qw/DBIx::Class::Schema/;
151 __PACKAGE__->load_classes(
152 { 'DBICTest::Schema' => [qw/
163 my $partial_schema = DBICTest::PartialSchema->connect(DBICTest->_database);
166 my $sqlt_schema = do {
168 local $SIG{__WARN__} = sub {
170 unless $_[0] =~ /Ignoring relationship .+ related resultsource .+ is not registered with this schema/
173 create_schema({ schema => $partial_schema });
176 my @tables = $sqlt_schema->get_tables;
179 [sort map { $_->name } @tables],
180 [qw/cd cd_to_producer producer tags track/],
181 'partial dbic schema parsing ok',
184 # the primary key is currently unnamed in sqlt - adding below
185 my %constraints_for_table = (
186 producer => [qw/prod_name /],
187 tags => [qw/tagid_cd tagid_cd_tag tags_fk_cd tags_tagid_tag tags_tagid_tag_cd /],
188 track => [qw/track_cd_position track_cd_title track_fk_cd /],
189 cd => [qw/cd_artist_title cd_fk_single_track /],
190 cd_to_producer => [qw/cd_to_producer_fk_cd cd_to_producer_fk_producer /],
193 for my $table (@tables) {
194 my $tablename = $table->name;
195 my @constraints = $table->get_constraints;
197 [ sort map { $_->name } @constraints ],
199 # the primary key (present on all loaded tables) is currently named '' in sqlt
200 # subject to future changes
201 [ '', @{$constraints_for_table{$tablename}} ],
203 "constraints of table '$tablename' ok",
206 }, 'partial schema tests successful');
214 my $schema = $args->{schema};
215 my $additional_sqltargs = $args->{args} || {};
219 ignore_constraint_names => 1,
220 ignore_index_names => 1,
221 %{$additional_sqltargs}
224 my $sqlt = SQL::Translator->new( $sqltargs );
226 $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
227 return $sqlt->translate({ data => $schema }) || die $sqlt->error;
231 my ($sqlt_schema, $schema, $source) = @_;
233 my $table_name = $schema->source($source)->from;
234 $table_name = $$table_name if ref $table_name;
236 return $sqlt_schema->get_table($table_name);