From: Sebatian B. Knapp Date: Sat, 7 Jan 2012 13:50:42 +0000 (+0100) Subject: Fix for mysql producer drop primary key, refs #62250 X-Git-Tag: v0.11011~40 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b62fa4925be9fc3f0ef0fd32a31ea1ced60696cd;hp=a389997035f775ca60aa3fee2f5abb5390e38219;p=dbsrgits%2FSQL-Translator.git Fix for mysql producer drop primary key, refs #62250 --- diff --git a/Changes b/Changes index a630ee3..a9d37ea 100644 --- a/Changes +++ b/Changes @@ -20,6 +20,7 @@ * Check in MySQL parser to avoid trying to parse a table defined twice in the same file as indices (and probably other things) get messed up * Workaround for some MySQL quirks on primary key definitions +* Fix dropping primary keys in MySQL diffs (RT#62250, patch from Nick Bertrand) * MySQL producer does not attempt to write out non-existent unique constraint names * MySQL parser correctly differentiates between signed and unsigned integer column display sizes diff --git a/lib/SQL/Translator/Producer/MySQL.pm b/lib/SQL/Translator/Producer/MySQL.pm index bd53796..8ec6041 100644 --- a/lib/SQL/Translator/Producer/MySQL.pm +++ b/lib/SQL/Translator/Producer/MySQL.pm @@ -694,12 +694,15 @@ sub alter_drop_constraint my $qc = $options->{quote_field_names} || ''; my $table_name = quote_table_name($c->table->name, $qt); - my $out = sprintf('ALTER TABLE %s DROP %s %s', - $table_name, - $c->type eq FOREIGN_KEY ? $c->type : "INDEX", - $qc . $c->name . $qc ); - - return $out; + my @out = ('ALTER','TABLE',$table_name,'DROP'); + if($c->type eq PRIMARY_KEY) { + push @out, $c->type; + } + else { + push @out, ($c->type eq FOREIGN_KEY ? $c->type : "INDEX"), + $qc . $c->name . $qc; + } + return join(' ',@out); } sub alter_create_constraint diff --git a/t/38-mysql-producer.t b/t/38-mysql-producer.t index 6d1c681..76126be 100644 --- a/t/38-mysql-producer.t +++ b/t/38-mysql-producer.t @@ -19,7 +19,7 @@ use FindBin qw/$Bin/; #============================================================================= BEGIN { - maybe_plan(72, + maybe_plan(73, 'YAML', 'SQL::Translator::Producer::MySQL', 'Test::Differences', @@ -764,3 +764,22 @@ EOV } } } + +{ # test for rt62250 + my $table = SQL::Translator::Schema::Table->new(name => 'table'); + $table->add_field( + SQL::Translator::Schema::Field->new( name => 'mypk', + table => $table, + data_type => 'INT', + size => 10, + default_value => undef, + is_auto_increment => 1, + is_nullable => 0, + is_foreign_key => 0, + is_unique => 1 )); + + my $constraint = $table->add_constraint(fields => ['mypk'], type => 'PRIMARY_KEY'); + my $options = {quote_table_names => '`'}; + is(SQL::Translator::Producer::MySQL::alter_drop_constraint($constraint,$options), + 'ALTER TABLE `table` DROP PRIMARY KEY','valid drop primary key'); +}