fix mysql NULL reverse-engineering and updating
[DBIx-DBSchema.git] / DBSchema / DBD / mysql.pm
index a99dcf4..0bda38d 100644 (file)
@@ -32,6 +32,7 @@ $schema = new_native DBIx::DBSchema $dbh;
 This module implements a MySQL-native driver for DBIx::DBSchema.
 
 =cut
+    use Data::Dumper;
 
 sub columns {
   my($proto, $dbh, $table ) = @_;
@@ -40,13 +41,14 @@ sub columns {
   my $sth = $dbh->prepare("SHOW COLUMNS FROM $table") or die $dbh->errstr;
   $sth->execute or die $sth->errstr;
   my @r = map {
+    #warn Dumper($_);
     $_->{'Type'} =~ /^(\w+)\(?([^)]+)?\)?( \d+)?$/
       or die "Illegal type: ". $_->{'Type'}. "\n";
     my($type, $length) = ($1, $2);
     [
       $_->{'Field'},
       $type,
-      $_->{'Null'},
+      ( $_->{'Null'} =~ /^YES$/i ? 'NULL' : '' ),
       $length,
       $_->{'Default'},
       $_->{'Extra'}
@@ -130,6 +132,28 @@ sub column_callback {
 
 }
 
+sub alter_column_callback {
+  my( $proto, $dbh, $table, $old_column, $new_column ) = @_;
+  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",
+#  };
+
+  return {} if $old_column->null eq $new_column->null;
+  { 'sql_alter_null' => 
+      "ALTER TABLE $table MODIFY $new_def",
+  };
+
+
+}
+
 =head1 AUTHOR
 
 Ivan Kohler <ivan-dbix-dbschema@420.am>