Skip to content
Browse files

Refactor mysql rename column statement generation

* Simplify code
* Fix to not lose the default value for NOT NULL columns (previous code had a
  bug here)
* Change base property_schema_statement method to only add a default to the
  statement if it is non-NULL
  • Loading branch information...
1 parent 2acacf0 commit 9eb1f5b44fae874d546d6878096a3c51f603aa3a @dkubb dkubb committed Apr 7, 2012
Showing with 15 additions and 18 deletions.
  1. +5 −1 lib/dm-migrations/adapters/dm-do-adapter.rb
  2. +8 −15 lib/dm-migrations/sql/mysql.rb
  3. +2 −2 spec/integration/sql_spec.rb
View
6 lib/dm-migrations/adapters/dm-do-adapter.rb
@@ -239,7 +239,11 @@ def property_schema_statement(connection, schema)
statement << "(#{connection.quote_value(length)})"
end
- statement << " DEFAULT #{connection.quote_value(schema[:default])}" if schema.key?(:default)
+ default = schema[:default]
+ if default
+ statement << " DEFAULT #{connection.quote_value(default)}"
+ end
+
statement << ' NOT NULL' unless schema[:allow_nil]
statement
View
23 lib/dm-migrations/sql/mysql.rb
@@ -42,31 +42,24 @@ def change_column_type_statement(name, column)
end
def rename_column_type_statement(table_name, old_col, new_col)
- column_info = select("SHOW COLUMNS FROM #{quote_name(table_name)} LIKE ?", old_col).first
+ table = quote_name(table_name)
+ column_info = select("SHOW COLUMNS FROM #{table} LIKE ?", old_col).first
+
column_options = {
:name => column_info.field,
:primitive => column_info.type,
+ :serial => column_info.extra == 'auto_increment',
+ :allow_nil => column_info.null == 'YES',
+ :default => column_info.default,
}
- 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)
+ column_definition = column.split(' ', 2).last
- "ALTER TABLE #{quote_name(table_name)} CHANGE #{quote_name(old_col)} #{quote_name(new_col)} #{column_definition}"
+ "ALTER TABLE #{table} CHANGE #{quote_name(old_col)} #{quote_name(new_col)} #{column_definition}"
end
class Table
View
4 spec/integration/sql_spec.rb
@@ -165,7 +165,7 @@
when :mysql
before do
# create the table so the existing column schema can be instrospected
- @adapter.execute('CREATE TABLE `people` (name VARCHAR(50) NOT NULL)')
+ @adapter.execute("CREATE TABLE `people` (name VARCHAR(50) DEFAULT 'John' NOT NULL)")
@modifier = DataMapper::Migration::TableModifier.new(@adapter, :people) do
rename_column :name, :first_name
@@ -177,7 +177,7 @@
end
it "should change the column" do
- @modifier.to_sql.should == %q{ALTER TABLE `people` CHANGE `name` `first_name` varchar(50) NOT NULL}
+ @modifier.to_sql.should == %q{ALTER TABLE `people` CHANGE `name` `first_name` varchar(50) DEFAULT 'John' NOT NULL}
end
end
end

0 comments on commit 9eb1f5b

Please sign in to comment.
Something went wrong with that request. Please try again.