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( no_deploy => 1 );
35 # Dummy was yanked out by the sqlt hook test
36 # CustomSql tests the horrific/deprecated ->name(\$sql) hack
37 # YearXXXXCDs are views
40 { $_ !~ /^ (?: Dummy | CustomSql | Year\d{4}CDs ) $/x }
44 my $idx_exceptions = {
48 'TwoKeys' => -1, # TwoKeys has the index turned off on the rel def
52 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
54 foreach my $source_name (@sources) {
55 my $table = get_table($sqlt_schema, $schema, $source_name);
57 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
58 $fk_count += $idx_exceptions->{$source_name} || 0;
59 my @indices = $table->get_indices;
61 my $index_count = scalar(@indices);
62 is($index_count, $fk_count, "correct number of indices for $source_name with no args");
64 for my $index (@indices) {
65 my $source = $schema->source($source_name);
66 my $pk_test = join("\x00", $source->primary_columns);
67 my $idx_test = join("\x00", $index->fields);
68 isnt ( $pk_test, $idx_test, "no additional index for the primary columns exists in $source_name");
74 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } });
76 foreach my $source_name (@sources) {
77 my $table = get_table($sqlt_schema, $schema, $source_name);
79 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
80 $fk_count += $idx_exceptions->{$source_name} || 0;
81 my @indices = $table->get_indices;
82 my $index_count = scalar(@indices);
83 is($index_count, $fk_count, "correct number of indices for $source_name with add_fk_index => 1");
88 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
90 foreach my $source (@sources) {
91 my $table = get_table($sqlt_schema, $schema, $source);
93 my @indices = $table->get_indices;
94 my $index_count = scalar(@indices);
95 is($index_count, 0, "correct number of indices for $source with add_fk_index => 0");
101 package # hide from PAUSE
102 DBICTest::Schema::NoViewDefinition;
104 use base qw/DBICTest::BaseResult/;
106 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
107 __PACKAGE__->table('noviewdefinition');
112 my $schema_invalid_view = $schema->clone;
113 $schema_invalid_view->register_class('NoViewDefinition', 'DBICTest::Schema::NoViewDefinition');
115 throws_ok { create_schema({ schema => $schema_invalid_view }) }
116 qr/view noviewdefinition is missing a view_definition/,
117 'parser detects views with a view_definition';
121 my $sqlt_schema = create_schema ({
131 [$sqlt_schema->get_tables ],
133 'sources limitng with relationships works',
139 package DBICTest::PartialSchema;
141 use base qw/DBIx::Class::Schema/;
143 __PACKAGE__->load_classes(
144 { 'DBICTest::Schema' => [qw/
155 my $partial_schema = DBICTest::PartialSchema->connect(DBICTest->_database);
158 my $sqlt_schema = do {
160 local $SIG{__WARN__} = sub {
162 unless $_[0] =~ /Ignoring relationship .+ related resultsource .+ is not registered with this schema/
165 create_schema({ schema => $partial_schema });
168 my @tables = $sqlt_schema->get_tables;
171 [sort map { $_->name } @tables],
172 [qw/cd cd_to_producer producer tags track/],
173 'partial dbic schema parsing ok',
176 # the primary key is currently unnamed in sqlt - adding below
177 my %constraints_for_table = (
178 producer => [qw/prod_name /],
179 tags => [qw/tagid_cd tagid_cd_tag tags_fk_cd tags_tagid_tag tags_tagid_tag_cd /],
180 track => [qw/track_cd_position track_cd_title track_fk_cd /],
181 cd => [qw/cd_artist_title cd_fk_single_track /],
182 cd_to_producer => [qw/cd_to_producer_fk_cd cd_to_producer_fk_producer /],
185 for my $table (@tables) {
186 my $tablename = $table->name;
187 my @constraints = $table->get_constraints;
189 [ sort map { $_->name } @constraints ],
191 # the primary key (present on all loaded tables) is currently named '' in sqlt
192 # subject to future changes
193 [ '', @{$constraints_for_table{$tablename}} ],
195 "constraints of table '$tablename' ok",
198 }, 'partial schema tests successful');
206 my $schema = $args->{schema};
207 my $additional_sqltargs = $args->{args} || {};
211 ignore_constraint_names => 1,
212 ignore_index_names => 1,
213 %{$additional_sqltargs}
216 my $sqlt = SQL::Translator->new( $sqltargs );
218 $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
219 return $sqlt->translate({ data => $schema }) || die $sqlt->error;
223 my ($sqlt_schema, $schema, $source) = @_;
225 my $table_name = $schema->source($source)->from;
226 $table_name = $$table_name if ref $table_name;
228 return $sqlt_schema->get_table($table_name);