fix mysql NULL reverse-engineering and updating
authorivan <ivan>
Sun, 28 Oct 2007 12:53:17 +0000 (12:53 +0000)
committerivan <ivan>
Sun, 28 Oct 2007 12:53:17 +0000 (12:53 +0000)
Changes
DBSchema/DBD/mysql.pm

diff --git a/Changes b/Changes
index f0c68ef..4ce418a 100644 (file)
--- a/Changes
+++ b/Changes
@@ -6,6 +6,7 @@ Revision history for Perl extension DBIx::DBSchema.
        - Update Column.pm, move all mysql and Pg-specific code to DBD driver
          callbacks
        - Update Table.pm, add local_options
+       - Fix mysql NULL reverse-engineering and updating code
 
 0.34 Sun Aug 19 10:08:51 PDT 2007
         - More work on update schema from Slaven Rezic <srezic@cpan.org>,
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>