From: Jess Robinson Date: Mon, 27 Nov 2006 19:28:04 +0000 (+0000) Subject: Patched mysql producer to name constraints sanely X-Git-Tag: v0.11008~388 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=fb149f8109a6d4cfea3e04a68d40977c55e3e124;p=dbsrgits%2FSQL-Translator.git Patched mysql producer to name constraints sanely --- diff --git a/Changes b/Changes index d7e927d..cf1e5db 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,7 @@ # 0.08_04 2006-11-10 # ----------------------------------------------------------- +* Patched MySQL producer to name constraints sanely, thanks Ash * Added patch to Producer::DB2 to avoid dependency issues with foreign keys * Added patch to remove single quotes for numeric default values in Producer::DB2 * Fixed Parser::SQLite to require a semicolon after a create trigger statement diff --git a/lib/SQL/Translator/Producer/MySQL.pm b/lib/SQL/Translator/Producer/MySQL.pm index 77e05d5..52adf89 100644 --- a/lib/SQL/Translator/Producer/MySQL.pm +++ b/lib/SQL/Translator/Producer/MySQL.pm @@ -1,7 +1,7 @@ package SQL::Translator::Producer::MySQL; # ------------------------------------------------------------------- -# $Id: MySQL.pm,v 1.51 2006-11-09 18:19:05 schiffbruechige Exp $ +# $Id: MySQL.pm,v 1.52 2006-11-27 19:28:04 schiffbruechige Exp $ # ------------------------------------------------------------------- # Copyright (C) 2002-4 SQLFairy Authors # @@ -93,7 +93,7 @@ Set the fields charater set and collation order. use strict; use warnings; use vars qw[ $VERSION $DEBUG ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.51 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.52 $ =~ /(\d+)\.(\d+)/; $DEBUG = 0 unless defined $DEBUG; use Data::Dumper; @@ -394,8 +394,9 @@ sub create_constraint { my ($c, $options) = @_; - my $qf = $options->{quote_field_names} || ''; - my $qt = $options->{quote_table_names} || ''; + my $qf = $options->{quote_field_names} || ''; + my $qt = $options->{quote_table_names} || ''; + my $counter = ($options->{fk_name_counter} ||= {}); my @fields = $c->fields or next; @@ -413,9 +414,16 @@ sub create_constraint # Make sure FK field is indexed or MySQL complains. # + $counter->{$c->table} ||= {}; my $def = join(' ', - map { $_ || () } 'CONSTRAINT', $qt . $c->table . '_' . $c->name . $qt, 'FOREIGN KEY' - ); + map { $_ || () } + 'CONSTRAINT', + $qt . join('_', $c->table, + $c->name, + ($counter->{$c->table}{$c->name}++ || ()) + ) . $qt, + 'FOREIGN KEY' + ); $def .= ' ('.$qf . join( "$qf, $qf", @fields ) . $qf . ')'; diff --git a/t/38-mysql-producer.t b/t/38-mysql-producer.t index 798701e..03d9c60 100644 --- a/t/38-mysql-producer.t +++ b/t/38-mysql-producer.t @@ -88,6 +88,11 @@ schema: data_type: int order: 1 is_not_null: 1 + foo2: + name: foo2 + data_type: int + order: 2 + is_not_null: 1 constraints: - type: PRIMARY_KEY fields: @@ -97,6 +102,10 @@ schema: type: FOREIGN_KEY fields: foo name: fk_thing + - reference_table: thing + type: FOREIGN_KEY + fields: foo2 + name: fk_thing EOSCHEMA @@ -119,10 +128,13 @@ my @stmts = ( "CREATE TABLE `thing2` ( `id` integer, `foo` integer, + `foo2` integer, INDEX (`id`), INDEX (`foo`), + INDEX (`foo2`), PRIMARY KEY (`id`, `foo`), - CONSTRAINT `thing2_fk_thing` FOREIGN KEY (`foo`) REFERENCES `thing` (`id`) + CONSTRAINT `thing2_fk_thing` FOREIGN KEY (`foo`) REFERENCES `thing` (`id`), + CONSTRAINT `thing2_fk_thing_1` FOREIGN KEY (`foo2`) REFERENCES `thing` (`id`) ) Type=InnoDB;\n\n", "SET foreign_key_checks=1;\n\n"