no DEFAULT for mysql, patch from trs@bestpractical, CPAN#58505
[DBIx-DBSchema.git] / DBSchema / DBD / mysql.pm
index 0bda38d..07d341a 100644 (file)
@@ -4,7 +4,7 @@ use strict;
 use vars qw($VERSION @ISA %typemap);
 use DBIx::DBSchema::DBD;
 
-$VERSION = '0.06';
+$VERSION = '0.09';
 @ISA = qw(DBIx::DBSchema::DBD);
 
 %typemap = (
@@ -13,6 +13,7 @@ $VERSION = '0.06';
   'BIGSERIAL'      => 'BIGINT',
   'BOOL'           => 'TINYINT',
   'LONG VARBINARY' => 'LONGBLOB',
+  'TEXT'           => 'LONGTEXT',
 );
 
 =head1 NAME
@@ -45,12 +46,22 @@ sub columns {
     $_->{'Type'} =~ /^(\w+)\(?([^)]+)?\)?( \d+)?$/
       or die "Illegal type: ". $_->{'Type'}. "\n";
     my($type, $length) = ($1, $2);
+
+    my $default = $_->{'Default'};
+    if ( defined($default) ) {
+      $default = \"''"    if $default eq '';
+      $default = \0       if $default eq '0';
+      $default = \'NOW()' if uc($default) eq 'CURRENT_TIMESTAMP';
+    } else {
+      $default = '';
+    }
+
     [
       $_->{'Field'},
       $type,
       ( $_->{'Null'} =~ /^YES$/i ? 'NULL' : '' ),
       $length,
-      $_->{'Default'},
+      $default,
       $_->{'Extra'}
     ]
   } @{ $sth->fetchall_arrayref( {} ) };
@@ -120,7 +131,7 @@ sub column_callback {
   $hashref->{'effective_local'} = 'AUTO_INCREMENT'
     if $column_obj->type =~ /^(\w*)SERIAL$/i;
 
-  if ( $column_obj->default =~ /^(NOW)\(\)$/i
+  if ( $column_obj->quoted_default =~ /^(NOW)\(\)$/i
        && $column_obj->type =~ /^(TIMESTAMP|DATETIME)$/i ) {
 
     $hashref->{'effective_default'} = 'CURRENT_TIMESTAMP';
@@ -128,6 +139,15 @@ sub column_callback {
 
   }
 
+  # MySQL no longer supports defaults for text/blob columns
+  if ( $column_obj->type =~ /(TEXT|BLOB)/i
+       and defined $column_obj->default ) {
+
+    # There's no way to unset the default cleanly.
+    # An empty string isn't quite right.
+    $column_obj->{'default'} = undef;
+  }
+
   $hashref;
 
 }
@@ -137,21 +157,45 @@ sub alter_column_callback {
   my $old_name = $old_column->name;
   my $new_def = $new_column->line($dbh);
 
-# this would have been nice, but it appears to be doing too much...
-
-#  return {} if $old_column->line($dbh) eq $new_column->line($dbh);
-#
-#  #{ 'sql_alter' => 
-#  { 'sql_alter_null' => 
-#      "ALTER TABLE $table CHANGE $old_name $new_def",
-#  };
+  my $hashref = {};
+
+  my %canonical = (
+    'INTEGER'          => 'INT',
+    'SERIAL'           => 'INT',
+    'BIGSERIAL'        => 'BIGINT',
+    'REAL'             => 'DOUBLE', #'FLOAT',
+    'DOUBLE PRECISION' => 'DOUBLE',
+  );
+  foreach ($old_column, $new_column) {
+    $_->type($canonical{uc($_->type)}) if $canonical{uc($_->type)};
+  }
 
-  return {} if $old_column->null eq $new_column->null;
-  { 'sql_alter_null' => 
-      "ALTER TABLE $table MODIFY $new_def",
-  };
+  my %canonical_length = (
+    'INT'      => 11,
+    'BIGINT'   => 20,
+    'DECIMAL' => '10,0',
+  );
+  $new_column->length( $canonical_length{uc($new_column->type)} )
+    if $canonical_length{uc($new_column->type)}
+    && ($new_column->length||'') eq '';
+
+  #change type/length
+  if ( uc($old_column->type) ne uc($new_column->type)
+       || ($old_column->length||'') ne ($new_column->length||'')
+     )
+  {
+    my $old_def = $old_column->line($dbh);
+    $hashref->{'sql_alter_type'} =
+      "CHANGE $old_name $new_def";
+  }
 
+  #change nullability
+  if ( $old_column->null ne $new_column->null ) {
+    $hashref->{'sql_alter_null'} =
+      "ALTER TABLE $table MODIFY $new_def";
+  }
 
+  $hashref;
 }
 
 =head1 AUTHOR
@@ -162,7 +206,7 @@ Ivan Kohler <ivan-dbix-dbschema@420.am>
 
 Copyright (c) 2000 Ivan Kohler
 Copyright (c) 2000 Mail Abuse Prevention System LLC
-Copyright (c) 2007 Freeside Internet Services, Inc.
+Copyright (c) 2007-2013 Freeside Internet Services, Inc.
 All rights reserved.
 This program is free software; you can redistribute it and/or modify it under
 the same terms as Perl itself.