Fix for mysql producer drop primary key, refs #62250
Sebatian B. Knapp [Sat, 7 Jan 2012 13:50:42 +0000 (14:50 +0100)]
Changes
lib/SQL/Translator/Producer/MySQL.pm
t/38-mysql-producer.t

diff --git a/Changes b/Changes
index a630ee3..a9d37ea 100644 (file)
--- 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
index bd53796..8ec6041 100644 (file)
@@ -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
index 6d1c681..76126be 100644 (file)
@@ -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');
+}