Additionally, the DBIx::Class parser accepts a C<sources> parameter as a hash
ref or an array ref, containing a list of source to deploy. If present, then
-only the sources listed will get deployed.
+only the sources listed will get deployed. Furthermore, you can use the
+C<add_fk_index> parser parameter to prevent the parser from creating an index for each
+FK.
=cut
name format. For the ALTER file, the same format is used, replacing
$version in the name with "$preversion-$version".
+See L<DBIx::Class::Schema/deploy> for details of $sqlt_args.
+
If no arguments are passed, then the following default values are used:
=over 4
# -------------------------------------------------------------------
# parse($tr, $data)
#
+# setting parser_args => { add_fk_index => 0 } will prevent
+# the auto-generation of an index for each FK.
+#
# Note that $data, in the case of this parser, is not useful.
# We're working with DBIx::Class Schemas, not data streams.
# -------------------------------------------------------------------
(defined $is_deferrable ? ( deferrable => $is_deferrable ) : ()),
);
- my $index = $table->add_index(
- name => join('_', $table->name, 'idx', @keys),
- fields => \@keys,
- type => 'NORMAL',
- );
- }
+ unless (exists $args->{add_fk_index} && ($args->{add_fk_index} == 0)) {
+ my $index = $table->add_index(
+ name => join('_', $table->name, 'idx', @keys),
+ fields => \@keys,
+ type => 'NORMAL',
+ );
+ }
+ }
}
}
-
+
if ($source->result_class->can('sqlt_deploy_hook')) {
$source->result_class->sqlt_deploy_hook($table);
}
--- /dev/null
+#!/usr/bin/perl
+use strict;
+use warnings;
+use Test::More;
+use lib qw(t/lib);
+use DBICTest;
+
+BEGIN {
+ eval "use DBD::mysql; use SQL::Translator 0.09;";
+ plan $@
+ ? ( skip_all => 'needs SQL::Translator 0.09 for testing' )
+ : ( tests => 99 );
+}
+
+my $schema = DBICTest->init_schema();
+
+{
+ my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
+
+ foreach my $source ($schema->sources) {
+ my $table = $sqlt_schema->get_table($schema->source($source)->from);
+
+ my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
+ my @indices = $table->get_indices;
+ my $index_count = scalar(@indices);
+ is($index_count, $fk_count, "correct number of indices for $source with no args");
+ }
+}
+
+{
+ my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } });
+
+ foreach my $source ($schema->sources) {
+ my $table = $sqlt_schema->get_table($schema->source($source)->from);
+
+ my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
+ my @indices = $table->get_indices;
+ my $index_count = scalar(@indices);
+ is($index_count, $fk_count, "correct number of indices for $source with add_fk_index => 1");
+ }
+}
+
+{
+ my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
+
+ foreach my $source ($schema->sources) {
+ my $table = $sqlt_schema->get_table($schema->source($source)->from);
+
+ my @indices = $table->get_indices;
+ my $index_count = scalar(@indices);
+ is($index_count, 0, "correct number of indices for $source with add_fk_index => 0");
+ }
+}
+
+sub create_schema {
+ my $args = shift;
+
+ my $schema = $args->{schema};
+ my $additional_sqltargs = $args->{args} || {};
+
+ my $sqltargs = {
+ add_drop_table => 1,
+ ignore_constraint_names => 1,
+ ignore_index_names => 1,
+ %{$additional_sqltargs}
+ };
+
+ my $sqlt = SQL::Translator->new( $sqltargs );
+
+ $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
+ return $sqlt->translate({ data => $schema }) or die $sqlt->error;
+}