10 require DBIx::Class::Storage::DBI;
12 'Test needs SQL::Translator ' . DBIx::Class::Storage::DBI->_sqlt_minimum_version
13 if not DBIx::Class::Storage::DBI->_sqlt_version_ok;
16 my $schema = DBICTest->init_schema();
17 # Dummy was yanked out by the sqlt hook test
18 # CustomSql tests the horrific/deprecated ->name(\$sql) hack
19 # YearXXXXCDs and NoViewDefinition are views
22 { $_ !~ /^ (?: Dummy | CustomSql | Year\d{4}CDs | NoViewDefinition ) $/x }
27 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
29 foreach my $source (@sources) {
30 my $table = get_table($sqlt_schema, $schema, $source);
32 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
33 my @indices = $table->get_indices;
34 my $index_count = scalar(@indices);
35 $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def
36 is($index_count, $fk_count, "correct number of indices for $source with no args");
41 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } });
43 foreach my $source (@sources) {
44 my $table = get_table($sqlt_schema, $schema, $source);
46 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
47 my @indices = $table->get_indices;
48 my $index_count = scalar(@indices);
49 $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def
50 is($index_count, $fk_count, "correct number of indices for $source with add_fk_index => 1");
55 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
57 foreach my $source (@sources) {
58 my $table = get_table($sqlt_schema, $schema, $source);
60 my @indices = $table->get_indices;
61 my $index_count = scalar(@indices);
62 is($index_count, 0, "correct number of indices for $source with add_fk_index => 0");
68 package # hide from PAUSE
69 DBICTest::Schema::NoViewDefinition;
71 use base qw/DBICTest::BaseResult/;
73 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
74 __PACKAGE__->table('noviewdefinition');
79 my $schema_invalid_view = $schema->clone;
80 $schema_invalid_view->register_class('NoViewDefinition', 'DBICTest::Schema::NoViewDefinition');
83 my $sqlt_schema = create_schema({ schema => $schema_invalid_view });
85 #like($@, qr/view noviewdefinition is missing a view_definition/, "parser detects views with a view_definition");
86 throws_ok { create_schema({ schema => $schema_invalid_view }) }
87 qr/view noviewdefinition is missing a view_definition/,
88 'parser detects views with a view_definition';
90 # my @views = $sqlt_schema->get_views;
92 # # the following views are skipped:
93 # # Year1999CDs is virtual
94 # # NoViewDefinition has no view_definition
95 # is(scalar @views, 1, "number of views ok");
97 # foreach my $view (@views) {
98 # ok($view->is_valid, "view " . $view->name . " is valid");
101 # my @expected_view_names = (qw/ year2000cds /);
102 # my @view_names = sort map { $_->name } @views;
104 # is_deeply( @view_names, @expected_view_names, "all expected views included int SQL::Translator schema" );
112 my $schema = $args->{schema};
113 my $additional_sqltargs = $args->{args} || {};
117 ignore_constraint_names => 1,
118 ignore_index_names => 1,
119 %{$additional_sqltargs}
122 my $sqlt = SQL::Translator->new( $sqltargs );
124 $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
125 return $sqlt->translate({ data => $schema }) || die $sqlt->error;
129 my ($sqlt_schema, $schema, $source) = @_;
131 my $table_name = $schema->source($source)->from;
132 $table_name = $$table_name if ref $table_name;
134 return $sqlt_schema->get_table($table_name);