Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Refactor mysql rename column statement generation
Browse files Browse the repository at this point in the history
* 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
dkubb committed Apr 7, 2012
1 parent 2acacf0 commit 9eb1f5b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
6 changes: 5 additions & 1 deletion lib/dm-migrations/adapters/dm-do-adapter.rb
Expand Up @@ -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
Expand Down
23 changes: 8 additions & 15 deletions lib/dm-migrations/sql/mysql.rb
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/integration/sql_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 9eb1f5b

Please sign in to comment.