From: Ash Berlin Date: Thu, 10 Jan 2008 13:55:19 +0000 (+0000) Subject: Correct constraint names in preprocess for MySQL producer X-Git-Tag: v0.11008~348^2~8 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=1c680eb9aac43c469260f89be06027951722402e;p=dbsrgits%2FSQL-Translator.git Correct constraint names in preprocess for MySQL producer --- diff --git a/META.yml b/META.yml index 0286bed..32fa024 100644 --- a/META.yml +++ b/META.yml @@ -237,7 +237,7 @@ provides: Test::SQL::Translator: file: lib/Test/SQL/Translator.pm version: 1.08 -generated_by: Module::Build version 0.2806 +generated_by: Module::Build version 0.2808 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.2.html version: 1.2 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' ); diff --git a/t/16xml-parser.t b/t/16xml-parser.t index ed4a93d..b7fcb78 100644 --- a/t/16xml-parser.t +++ b/t/16xml-parser.t @@ -154,6 +154,7 @@ schema_ok( $scma, { fields => ["another_id"], reference_table => "Another", reference_fields => ["id"], + name => 'Basic_fk' }, ], indices => [ diff --git a/t/30sqlt-new-diff-mysql.t b/t/30sqlt-new-diff-mysql.t index eca0a2d..0927d7d 100644 --- a/t/30sqlt-new-diff-mysql.t +++ b/t/30sqlt-new-diff-mysql.t @@ -155,7 +155,7 @@ SET foreign_key_checks=1; ALTER TABLE employee DROP FOREIGN KEY FK5302D47D93FE702E, DROP COLUMN job_title, - ADD CONSTRAINT FK5302D47D93FE702E_diff_1 FOREIGN KEY (employee_id) REFERENCES person (person_id); + ADD CONSTRAINT FK5302D47D93FE702E_diff FOREIGN KEY (employee_id) REFERENCES person (person_id); ALTER TABLE person DROP UNIQUE UC_age_name, DROP INDEX u_name, ADD COLUMN is_rock_star tinyint(4) DEFAULT '1',