consolidate multiple ALTER TABLE statements for efficiency
[DBIx-DBSchema.git] / DBSchema / Table.pm
index 8d047de..ab5f83f 100644 (file)
@@ -634,17 +634,22 @@ sub sql_alter_table {
   foreach my $column ( $new->columns ) {
 
     if ( $self->column($column) )  {
-
       warn "  $table.$column exists\n" if $DEBUG > 1;
-      push @r, $self->column($column)->sql_alter_column( $new->column($column),
-                                                         $dbh,
-                                                         $opt,
-                                                       );
+
+      my ($alter_table, $sql) = 
+        $self->column($column)->sql_alter_column( $new->column($column),
+                                                  $dbh,
+                                                  $opt,
+                                                );
+      push @at, @$alter_table;
+      push @r, @$sql;
 
     } else {
-  
       warn "column $table.$column does not exist.\n" if $DEBUG > 1;
-      push @r, $new->column($column)->sql_add_column( $dbh );
+
+      my ($alter_table, $sql) = $new->column($column)->sql_add_column( $dbh );
+      push @at, @$alter_table;
+      push @r, @$sql;
   
     }
   
@@ -715,9 +720,11 @@ sub sql_alter_table {
 
     warn "column $table.$column should be dropped.\n" if $DEBUG;
 
-    push @r, $self->column($column)->sql_drop_column( $dbh );
+    push @at, $self->column($column)->sql_drop_column( $dbh );
 
   }
+
+  unshift @r, "ALTER TABLE $table ", join(', ', @at) if @at;
   
   ###
   # return the statements