Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't create a superfluous index for FK that is PK #56

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 13 additions & 4 deletions lib/SQL/Translator/Producer/MySQL.pm
Expand Up @@ -449,21 +449,30 @@ sub create_table
my %indexed_fields;
for my $index ( $table->get_indices ) {
push @index_defs, create_index($index, $options);
$indexed_fields{ $_ } = 1 for $index->fields;
$indexed_fields{ join "\0", $index->fields } = 1;
$indexed_fields{ ($index->fields)[0] } = 1;
}

#
# Constraints -- need to handle more than just FK. -ky
#
my @constraint_defs;
my @constraints = $table->get_constraints;

for my $pk ( grep $_->type eq PRIMARY_KEY, @constraints ) {
$indexed_fields{ join "\0", $pk->fields } = 1;
$indexed_fields{ ($pk->fields)[0] } = 1;
}


for my $c ( @constraints ) {
my $constr = create_constraint($c, $options);
push @constraint_defs, $constr if($constr);

unless ( $indexed_fields{ ($c->fields())[0] } || $c->type ne FOREIGN_KEY ) {
push @index_defs, "INDEX (" . $generator->quote(($c->fields())[0]) . ")";
$indexed_fields{ ($c->fields())[0] } = 1;
unless ( $indexed_fields{ join("\0", $c->fields) } || $c->type ne FOREIGN_KEY ) {
push @index_defs, "INDEX (" . join(', ' => map $generator->quote($_), $c->fields) . ")";
$indexed_fields{ join("\0", $c->fields) } = 1;
$indexed_fields{ ($c->fields)[0] } = 1;
}
}

Expand Down
75 changes: 74 additions & 1 deletion t/38-mysql-producer.t
Expand Up @@ -19,7 +19,7 @@ use FindBin qw/$Bin/;
#=============================================================================

BEGIN {
maybe_plan(73,
maybe_plan(76,
'YAML',
'SQL::Translator::Producer::MySQL',
'Test::Differences',
Expand Down Expand Up @@ -800,3 +800,76 @@ EOV
is(SQL::Translator::Producer::MySQL::alter_drop_constraint($constraint,$options),
'ALTER TABLE `table` DROP PRIMARY KEY','valid drop primary key');
}

{ # do not create an extra index for a FK that is also the PK
my $sqlt;
$sqlt = SQL::Translator->new(
show_warnings => 1,
no_comments => 1,
from => "YAML",
to => "MySQL",
quote_table_names => 1,
quote_field_names => 1
);

my $out = $sqlt->translate(\<<'EOT') or die "Translate error:".$sqlt->error;
---
schema:
tables:
thing:
name: thing
extra:
order: 1
fields:
id1:
name: id1
data_type: int
is_primary_key: 1
order: 1
is_foreign_key: 0
id2:
name: id2
data_type: int
is_primary_key: 1
order: 2
is_foreign_key: 0
constraints:
- type: PRIMARY_KEY
fields:
- id1
- id2
thing2:
name: thing2
extra:
order: 2
fields:
thing2_id1:
name: thing2_id1
data_type: int
is_primary_key: 1
order: 1
is_foreign_key: 1
thing2_id2:
name: thing2_id2
data_type: int
is_primary_key: 1
order: 2
is_foreign_key: 1
constraints:
- type: PRIMARY_KEY
fields:
- thing2_id1
- thing2_id2
- reference_table: thing
type: FOREIGN_KEY
fields:
- thing2_id1
- thing2_id2
name: fk_thing
EOT

note $out;
like $out, qr/PRIMARY KEY \(`thing2_id1`, `thing2_id2`\)/, 'created primary key';
unlike $out, qr/INDEX \(`thing2_id1`, `thing2_id2`\)/, 'no extra index';
unlike $out, qr/INDEX \(`thing2_id1`\)/, 'no single column index';
}