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;
21 my $sqlt_schema = create_schema ({ schema => $s });
22 Scalar::Util::weaken ($s);
24 ok (!$s, 'Schema not leaked');
26 isa_ok ($sqlt_schema, 'SQL::Translator::Schema', 'SQLT schema object produced');
29 # make sure classname-style works
30 lives_ok { isa_ok (create_schema ({ schema => 'DBICTest::Schema' }), 'SQL::Translator::Schema', 'SQLT schema object produced') };
33 my $schema = DBICTest->init_schema();
34 # Dummy was yanked out by the sqlt hook test
35 # CustomSql tests the horrific/deprecated ->name(\$sql) hack
36 # YearXXXXCDs are views
39 { $_ !~ /^ (?: Dummy | CustomSql | Year\d{4}CDs ) $/x }
43 my $idx_exceptions = {
47 'TwoKeys' => -1, # TwoKeys has the index turned off on the rel def
51 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
53 foreach my $source_name (@sources) {
54 my $table = get_table($sqlt_schema, $schema, $source_name);
56 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
57 $fk_count += $idx_exceptions->{$source_name} || 0;
58 my @indices = $table->get_indices;
60 my $index_count = scalar(@indices);
61 is($index_count, $fk_count, "correct number of indices for $source_name with no args");
63 for my $index (@indices) {
64 my $source = $schema->source($source_name);
65 my $pk_test = join("\x00", $source->primary_columns);
66 my $idx_test = join("\x00", $index->fields);
67 isnt ( $pk_test, $idx_test, "no additional index for the primary columns exists in $source_name");
73 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } });
75 foreach my $source_name (@sources) {
76 my $table = get_table($sqlt_schema, $schema, $source_name);
78 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
79 $fk_count += $idx_exceptions->{$source_name} || 0;
80 my @indices = $table->get_indices;
81 my $index_count = scalar(@indices);
82 is($index_count, $fk_count, "correct number of indices for $source_name with add_fk_index => 1");
87 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
89 foreach my $source (@sources) {
90 my $table = get_table($sqlt_schema, $schema, $source);
92 my @indices = $table->get_indices;
93 my $index_count = scalar(@indices);
94 is($index_count, 0, "correct number of indices for $source with add_fk_index => 0");
100 package # hide from PAUSE
101 DBICTest::Schema::NoViewDefinition;
103 use base qw/DBICTest::BaseResult/;
105 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
106 __PACKAGE__->table('noviewdefinition');
111 my $schema_invalid_view = $schema->clone;
112 $schema_invalid_view->register_class('NoViewDefinition', 'DBICTest::Schema::NoViewDefinition');
114 throws_ok { create_schema({ schema => $schema_invalid_view }) }
115 qr/view noviewdefinition is missing a view_definition/,
116 'parser detects views with a view_definition';
120 my $sqlt_schema = create_schema ({
130 [$sqlt_schema->get_tables ],
132 'sources limitng with relationships works',
138 package DBICTest::PartialSchema;
140 use base qw/DBIx::Class::Schema/;
142 __PACKAGE__->load_classes(
143 { 'DBICTest::Schema' => [qw/
154 my $partial_schema = DBICTest::PartialSchema->connect(DBICTest->_database);
157 my $sqlt_schema = do {
159 local $SIG{__WARN__} = sub {
161 unless $_[0] =~ /Ignoring relationship .+ related resultsource .+ is not registered with this schema/
164 create_schema({ schema => $partial_schema });
167 my @tables = $sqlt_schema->get_tables;
170 [sort map { $_->name } @tables],
171 [qw/cd cd_to_producer producer tags track/],
172 'partial dbic schema parsing ok',
175 # the primary key is currently unnamed in sqlt - adding below
176 my %constraints_for_table = (
177 producer => [qw/prod_name /],
178 tags => [qw/tagid_cd tagid_cd_tag tags_fk_cd tags_tagid_tag tags_tagid_tag_cd /],
179 track => [qw/track_cd_position track_cd_title track_fk_cd /],
180 cd => [qw/cd_artist_title cd_fk_single_track /],
181 cd_to_producer => [qw/cd_to_producer_fk_cd cd_to_producer_fk_producer /],
184 for my $table (@tables) {
185 my $tablename = $table->name;
186 my @constraints = $table->get_constraints;
188 [ sort map { $_->name } @constraints ],
190 # the primary key (present on all loaded tables) is currently named '' in sqlt
191 # subject to future changes
192 [ '', @{$constraints_for_table{$tablename}} ],
194 "constraints of table '$tablename' ok",
197 }, 'partial schema tests successful');
205 my $schema = $args->{schema};
206 my $additional_sqltargs = $args->{args} || {};
210 ignore_constraint_names => 1,
211 ignore_index_names => 1,
212 %{$additional_sqltargs}
215 my $sqlt = SQL::Translator->new( $sqltargs );
217 $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
218 return $sqlt->translate({ data => $schema }) || die $sqlt->error;
222 my ($sqlt_schema, $schema, $source) = @_;
224 my $table_name = $schema->source($source)->from;
225 $table_name = $$table_name if ref $table_name;
227 return $sqlt_schema->get_table($table_name);