Permalink
Browse files

Include all column attributes when generating an ALTER TABLE / CHANGE…

… statement.
  • Loading branch information...
1 parent bcc90b3 commit b40c9f182b133e909d527e39ca5b01a254a7ddd3 @postmodern postmodern committed with dkubb Feb 9, 2012
Showing with 25 additions and 2 deletions.
  1. +25 −2 lib/dm-migrations/sql/mysql.rb
View
27 lib/dm-migrations/sql/mysql.rb
@@ -42,8 +42,31 @@ def change_column_type_statement(name, column)
end
def rename_column_type_statement(table_name, old_col, new_col)
- table_info = select("SHOW COLUMNS FROM #{quote_name(table_name)} LIKE ?", old_col).first
- "ALTER TABLE #{quote_name(table_name)} CHANGE #{quote_name(old_col)} #{quote_name(new_col)} #{table_info.type}"
+ column_info = select("SHOW COLUMNS FROM #{quote_name(table_name)} LIKE ?", old_col).first
+ column_options = {
+ :name => column_info.field,
+ :primitive => column_info.type
+ }
+
+ if column_info.null == 'YES'
+ column_options[:allow_nil] = true
+ column_options[:default] = column_info.default
+ else
+ column_options[:allow_nil] = false
+ end
+
+ case column_info.extra
+ when 'auto_increment'
+ column_options[:serial] = true
+ end
+
+ column = with_connection do |connection|
+ property_schema_statement(connection,column_options)
+ end
+
+ column_name, column_definition = column.split(' ',2)
+
+ "ALTER TABLE #{quote_name(table_name)} CHANGE #{quote_name(old_col)} #{quote_name(new_col)} #{column_definition}"
end
class Table

0 comments on commit b40c9f1

Please sign in to comment.