X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FProducer%2FMySQL.pm;h=6c20e566cf2acca05f098c57bd35aff7d33721bd;hb=1c680eb9aac43c469260f89be06027951722402e;hp=f3f53a487c5a25fc3f6e2c58f4d4cfab87dbbdf8;hpb=ace08c3da3d5186a4014a946fd70cf335e051b70;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator/Producer/MySQL.pm b/lib/SQL/Translator/Producer/MySQL.pm index f3f53a4..6c20e56 100644 --- a/lib/SQL/Translator/Producer/MySQL.pm +++ b/lib/SQL/Translator/Producer/MySQL.pm @@ -167,6 +167,9 @@ sub preprocess_schema { }; + # Names are only specific to a given schema + local %used_names = (); + # # Work out which tables need to be InnoDB to support foreign key # constraints. We do this first as we need InnoDB at both ends. @@ -175,10 +178,19 @@ sub preprocess_schema { $mysql_table_type_to_options->($table); - foreach ( $table->get_constraints ) { - next unless $_->type eq FOREIGN_KEY; + foreach my $c( $table->get_constraints ) { + next unless $c->type eq FOREIGN_KEY; + + # Normalize constraint names here. + my $c_name = $c->name; + # Give the constraint a name if it doesn't have one, so it doens't feel + # left out + $c_name = $table->name . '_fk' unless length $c_name; + + $c->name( next_unused_name($c_name) ); + for my $meth (qw/table reference_table/) { - my $table = $schema->get_table($_->$meth) || next; + my $table = $schema->get_table($c->$meth) || next; next if $mysql_table_type_to_options->($table); $table->options( { 'ENGINE' => 'InnoDB' } ); } @@ -510,7 +522,6 @@ sub create_constraint my $qf = $options->{quote_field_names} || ''; my $qt = $options->{quote_table_names} || ''; my $leave_name = $options->{leave_name} || undef; - my $counter = ($options->{fk_name_counter} ||= {}); my @fields = $c->fields or next; @@ -531,18 +542,10 @@ sub create_constraint my $table = $c->table; my $c_name = $c->name; - # Give the constraint a name if it doesn't have one, so it doens't feel - # left out - unless ( $c_name ){ - $c_name = $table->name . '_fk'; - } - - $counter->{$table} ||= {}; my $def = join(' ', map { $_ || () } 'CONSTRAINT', - $qt . join('_', next_unused_name($c_name) - ) . $qt, + $qt . $c_name . $qt, 'FOREIGN KEY' );