mysql: implement altering column types & lengths
authorivan <ivan>
Sun, 18 Dec 2011 01:04:40 +0000 (01:04 +0000)
committerivan <ivan>
Sun, 18 Dec 2011 01:04:40 +0000 (01:04 +0000)
Changes
DBSchema.pm
DBSchema/DBD/mysql.pm
README
debian/changelog

diff --git a/Changes b/Changes
index a775843..54aa28e 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,8 +1,9 @@
 Revision history for Perl extension DBIx::DBSchema.
 
-0.40 unreleased
+0.40 Sat Dec 17 17:03:51 PST 2011
         - doc: sql_update_schema link to sql_add_column misspelled
-        - fix Pg mapping for DOUBLE PRECISION type to avoid needless alter cols
+        - Pg: fix mapping for DOUBLE PRECISION type to avoid needless alter cols
+        - mysql: implement altering column types & lengths
 
 0.39 Fri Mar 26 20:24:58 PDT 2010
         - mysql: TEXT->LONGTEXT
index e34582f..12a240e 100644 (file)
@@ -10,7 +10,7 @@ use DBIx::DBSchema::Column;
 use DBIx::DBSchema::ColGroup::Unique;
 use DBIx::DBSchema::ColGroup::Index;
 
-$VERSION = "0.40_02";
+$VERSION = "0.40";
 $VERSION = eval $VERSION; # modperlstyle: convert the string into a number
 
 $DEBUG = 0;
index 7f3582f..798ea43 100644 (file)
@@ -4,7 +4,7 @@ use strict;
 use vars qw($VERSION @ISA %typemap);
 use DBIx::DBSchema::DBD;
 
-$VERSION = '0.08';
+$VERSION = '0.09';
 @ISA = qw(DBIx::DBSchema::DBD);
 
 %typemap = (
@@ -148,21 +148,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'} =
+      "ALTER TABLE $table 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
@@ -173,7 +197,7 @@ Ivan Kohler <ivan-dbix-dbschema@420.am>
 
 Copyright (c) 2000 Ivan Kohler
 Copyright (c) 2000 Mail Abuse Prevention System LLC
-Copyright (c) 2007-2010 Freeside Internet Services, Inc.
+Copyright (c) 2007-2011 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.
diff --git a/README b/README
index 6f5bb20..c380d53 100644 (file)
--- a/README
+++ b/README
@@ -2,7 +2,7 @@ DBIx::DBSchema
 
 Copyright (c) 2000-2007 Ivan Kohler
 Copyright (c) 2000 Mail Abuse Prevention System LLC
-Copyright (c) 2007-2010 Freeside Internet Services, Inc.
+Copyright (c) 2007-2011 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.
index 303b74a..1a078a1 100644 (file)
@@ -1,3 +1,9 @@
+libdbix-dbschema-perl (0.40~03-1) UNRELEASED; urgency=low
+
+  * new upstream (test) release
+
+ -- Ivan Kohler <ivan-debian@420.am>  Fri, 16 Dec 2011 16:57:26 -0800
+
 libdbix-dbschema-perl (0.40~02-1) UNRELEASED; urgency=low
 
   * new upstream (test) release