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 create_schema ({ schema => $s });
22 Scalar::Util::weaken ($s);
24 ok (!$s, 'Schema not leaked');
28 my $schema = DBICTest->init_schema();
29 # Dummy was yanked out by the sqlt hook test
30 # CustomSql tests the horrific/deprecated ->name(\$sql) hack
31 # YearXXXXCDs are views
34 { $_ !~ /^ (?: Dummy | CustomSql | Year\d{4}CDs ) $/x }
38 my $idx_exceptions = {
42 'TwoKeys' => -1, # TwoKeys has the index turned off on the rel def
46 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
48 foreach my $source_name (@sources) {
49 my $table = get_table($sqlt_schema, $schema, $source_name);
51 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
52 $fk_count += $idx_exceptions->{$source_name} || 0;
53 my @indices = $table->get_indices;
55 my $index_count = scalar(@indices);
56 is($index_count, $fk_count, "correct number of indices for $source_name with no args");
58 for my $index (@indices) {
59 my $source = $schema->source($source_name);
60 my $pk_test = join("\x00", $source->primary_columns);
61 my $idx_test = join("\x00", $index->fields);
62 isnt ( $pk_test, $idx_test, "no additional index for the primary columns exists in $source_name");
68 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } });
70 foreach my $source_name (@sources) {
71 my $table = get_table($sqlt_schema, $schema, $source_name);
73 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
74 $fk_count += $idx_exceptions->{$source_name} || 0;
75 my @indices = $table->get_indices;
76 my $index_count = scalar(@indices);
77 is($index_count, $fk_count, "correct number of indices for $source_name with add_fk_index => 1");
82 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
84 foreach my $source (@sources) {
85 my $table = get_table($sqlt_schema, $schema, $source);
87 my @indices = $table->get_indices;
88 my $index_count = scalar(@indices);
89 is($index_count, 0, "correct number of indices for $source with add_fk_index => 0");
95 package # hide from PAUSE
96 DBICTest::Schema::NoViewDefinition;
98 use base qw/DBICTest::BaseResult/;
100 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
101 __PACKAGE__->table('noviewdefinition');
106 my $schema_invalid_view = $schema->clone;
107 $schema_invalid_view->register_class('NoViewDefinition', 'DBICTest::Schema::NoViewDefinition');
109 throws_ok { create_schema({ schema => $schema_invalid_view }) }
110 qr/view noviewdefinition is missing a view_definition/,
111 'parser detects views with a view_definition';
115 my $sqlt_schema = create_schema ({
125 [$sqlt_schema->get_tables ],
127 'sources limitng with relationships works',
137 my $schema = $args->{schema};
138 my $additional_sqltargs = $args->{args} || {};
142 ignore_constraint_names => 1,
143 ignore_index_names => 1,
144 %{$additional_sqltargs}
147 my $sqlt = SQL::Translator->new( $sqltargs );
149 $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
150 return $sqlt->translate({ data => $schema }) || die $sqlt->error;
154 my ($sqlt_schema, $schema, $source) = @_;
156 my $table_name = $schema->source($source)->from;
157 $table_name = $$table_name if ref $table_name;
159 return $sqlt_schema->get_table($table_name);