12 require DBIx::Class::Storage::DBI;
14 'Test needs SQL::Translator ' . DBIx::Class::Storage::DBI->_sqlt_minimum_version
15 if not DBIx::Class::Storage::DBI->_sqlt_version_ok;
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 }
39 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
41 foreach my $source (@sources) {
42 my $table = get_table($sqlt_schema, $schema, $source);
44 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
45 my @indices = $table->get_indices;
46 my $index_count = scalar(@indices);
47 $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def
48 is($index_count, $fk_count, "correct number of indices for $source with no args");
53 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } });
55 foreach my $source (@sources) {
56 my $table = get_table($sqlt_schema, $schema, $source);
58 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
59 my @indices = $table->get_indices;
60 my $index_count = scalar(@indices);
61 $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def
62 is($index_count, $fk_count, "correct number of indices for $source with add_fk_index => 1");
67 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
69 foreach my $source (@sources) {
70 my $table = get_table($sqlt_schema, $schema, $source);
72 my @indices = $table->get_indices;
73 my $index_count = scalar(@indices);
74 is($index_count, 0, "correct number of indices for $source with add_fk_index => 0");
80 package # hide from PAUSE
81 DBICTest::Schema::NoViewDefinition;
83 use base qw/DBICTest::BaseResult/;
85 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
86 __PACKAGE__->table('noviewdefinition');
91 my $schema_invalid_view = $schema->clone;
92 $schema_invalid_view->register_class('NoViewDefinition', 'DBICTest::Schema::NoViewDefinition');
94 throws_ok { create_schema({ schema => $schema_invalid_view }) }
95 qr/view noviewdefinition is missing a view_definition/,
96 'parser detects views with a view_definition';
100 my $sqlt_schema = create_schema ({
110 [$sqlt_schema->get_tables ],
112 'sources limitng with relationships works',
122 my $schema = $args->{schema};
123 my $additional_sqltargs = $args->{args} || {};
127 ignore_constraint_names => 1,
128 ignore_index_names => 1,
129 %{$additional_sqltargs}
132 my $sqlt = SQL::Translator->new( $sqltargs );
134 $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
135 return $sqlt->translate({ data => $schema }) || die $sqlt->error;
139 my ($sqlt_schema, $schema, $source) = @_;
141 my $table_name = $schema->source($source)->from;
142 $table_name = $$table_name if ref $table_name;
144 return $sqlt_schema->get_table($table_name);