From: Johannes Plunien Date: Wed, 27 May 2009 22:24:13 +0000 (+0000) Subject: MySQL producer skips length attribute for columns which do not support that attribute... X-Git-Tag: v0.11008~170 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=de176728b951547a60cdfb63ff7a364f2ebf53da;p=dbsrgits%2FSQL-Translator.git MySQL producer skips length attribute for columns which do not support that attribute. Currently following column types are added to that list: date time timestamp datetime year --- diff --git a/Changes b/Changes index db15df8..a52440e 100644 --- a/Changes +++ b/Changes @@ -9,6 +9,9 @@ * SQLite producer support for multi-event triggers * XML parser switched from XML::XPath to XML::LibXML * Pg producer ALTER TABLE/COLUMN and DROP DEFAULT support +* MySQL producer skips length attribute for columns which do not support that + attribute. Currently following column types are added to that list: + date time timestamp datetime year # ---------------------------------------------------------- # 0.09004 2009-02-13 diff --git a/lib/SQL/Translator/Producer/MySQL.pm b/lib/SQL/Translator/Producer/MySQL.pm index 40c59f2..56bcd6f 100644 --- a/lib/SQL/Translator/Producer/MySQL.pm +++ b/lib/SQL/Translator/Producer/MySQL.pm @@ -145,6 +145,13 @@ my %translate = ( bytea => 'BLOB', ); +# +# Column types that do not support lenth attribute +# +my @no_length_attr = qw/ + date time timestamp datetime year + /; + sub preprocess_schema { my ($schema) = @_; @@ -547,7 +554,7 @@ sub create_field if ( lc($data_type) eq 'enum' || lc($data_type) eq 'set') { $field_def .= '(' . $commalist . ')'; } - elsif ( defined $size[0] && $size[0] > 0 ) { + elsif ( defined $size[0] && $size[0] > 0 && ! grep $data_type eq $_, @no_length_attr ) { $field_def .= '(' . join( ', ', @size ) . ')'; } diff --git a/t/38-mysql-producer.t b/t/38-mysql-producer.t index 16151f5..59e26fb 100644 --- a/t/38-mysql-producer.t +++ b/t/38-mysql-producer.t @@ -19,7 +19,7 @@ use FindBin qw/$Bin/; #============================================================================= BEGIN { - maybe_plan(35, + maybe_plan(40, 'YAML', 'SQL::Translator::Producer::MySQL', 'Test::Differences', @@ -423,3 +423,24 @@ is ( is_deeply \%extra, {}, 'Extra attributes completely removed'; } } + +{ + + # certain types do not support a size, see also: + # http://dev.mysql.com/doc/refman/5.1/de/create-table.html + for my $type (qw/date time timestamp datetime year/) { + my $field = SQL::Translator::Schema::Field->new( + name => "my$type", + table => $table, + data_type => $type, + size => 10, + default_value => undef, + is_auto_increment => 0, + is_nullable => 1, + is_foreign_key => 0, + is_unique => 0 + ); + my $sql = SQL::Translator::Producer::MySQL::create_field($field); + is($sql, "my$type $type", "Skip length param for type $type"); + } +}