diff options
author | Diego Algorta <diego@oboxodo.com> | 2008-06-21 20:18:30 -0300 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-06-22 15:21:11 -0700 |
commit | 2e1b56c93745bf0513e449e95830edd390abfaf2 (patch) | |
tree | 52ca66c284130235b34b8728d30ef5563fe7cbc5 | |
parent | a02d672cd7aead8a24e3b10a6b8e12dd91ee0a49 (diff) | |
download | rails-2e1b56c93745bf0513e449e95830edd390abfaf2.tar.gz rails-2e1b56c93745bf0513e449e95830edd390abfaf2.tar.bz2 rails-2e1b56c93745bf0513e449e95830edd390abfaf2.zip |
MySQL: rename_column preserves default values. [#466 state:resolved]
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rwxr-xr-x | activerecord/lib/active_record/connection_adapters/mysql_adapter.rb | 10 | ||||
-rw-r--r-- | activerecord/test/cases/migration_test.rb | 26 |
3 files changed, 37 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index a1d82fb45d..e2d67c7ff5 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* MySQL: rename_column preserves column defaults. #466 [Diego Algorta] + * Add :from option to calculations. #397 [Ben Munat] * Add :validate option to associations to enable/disable the automatic validation of associated models. Resolves #301. [Jan De Poorter] diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 93aafaaad1..8805c79c5b 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -450,8 +450,16 @@ module ActiveRecord end def rename_column(table_name, column_name, new_column_name) #:nodoc: + options = {} + if column = columns(table_name).find { |c| c.name == column_name.to_s } + options[:default] = column.default + else + raise ActiveRecordError, "No such column: #{table_name}.#{column_name}" + end current_type = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'")["Type"] - execute "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(new_column_name)} #{current_type}" + rename_column_sql = "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(new_column_name)} #{current_type}" + add_column_options!(rename_column_sql, options) + execute(rename_column_sql) end # Maps logical Rails types to MySQL-specific data types. diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index f36255e209..2cac7f04ab 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -483,6 +483,32 @@ if ActiveRecord::Base.connection.supports_migrations? end end + def test_rename_column_preserves_default_value_not_null + begin + default_before = Developer.connection.columns("developers").find { |c| c.name == "salary" }.default + assert_equal 70000, default_before + Developer.connection.rename_column "developers", "salary", "anual_salary" + Developer.reset_column_information + assert Developer.column_names.include?("anual_salary") + default_after = Developer.connection.columns("developers").find { |c| c.name == "anual_salary" }.default + assert_equal 70000, default_after + ensure + Developer.connection.rename_column "developers", "anual_salary", "salary" + Developer.reset_column_information + end + end + + def test_rename_nonexistent_column + ActiveRecord::Base.connection.create_table(:hats) do |table| + table.column :hat_name, :string, :default => nil + end + assert_raises(ActiveRecord::ActiveRecordError) do + Person.connection.rename_column "hats", "nonexistent", "should_fail" + end + ensure + ActiveRecord::Base.connection.drop_table(:hats) + end + def test_rename_column_with_sql_reserved_word begin assert_nothing_raised { Person.connection.rename_column "people", "first_name", "group" } |