diff options
4 files changed, 18 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 313e9daf10..4b7fc376df 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* MySQL, PostgreSQL: change_column_default quotes the default value and doesn't lose column type information. #3987, #6664 [Jonathan Viney, manfred, altano@bigfoot.com] + * Oracle: create_table takes a :sequence_name option to override the 'tablename_seq' default. #7000 [Michael Schoen] * MySQL: retain SSL settings on reconnect. #6976 [randyv2] diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 9bc4c94373..de3cd06224 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -360,7 +360,7 @@ module ActiveRecord def change_column_default(table_name, column_name, default) #:nodoc: current_type = select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Type"] - change_column(table_name, column_name, current_type, { :default => default }) + execute("ALTER TABLE #{table_name} CHANGE #{column_name} #{column_name} #{current_type} DEFAULT #{quote(default)}") end def change_column(table_name, column_name, type, options = {}) #:nodoc: diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 5ef117a749..db946478f9 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -349,7 +349,7 @@ module ActiveRecord end def change_column_default(table_name, column_name, default) #:nodoc: - execute "ALTER TABLE #{table_name} ALTER COLUMN #{quote_column_name(column_name)} SET DEFAULT '#{default}'" + execute "ALTER TABLE #{table_name} ALTER COLUMN #{quote_column_name(column_name)} SET DEFAULT #{quote(default)}" end def rename_column(table_name, column_name, new_column_name) #:nodoc: diff --git a/activerecord/test/migration_test.rb b/activerecord/test/migration_test.rb index 355c459818..10cab682df 100644 --- a/activerecord/test/migration_test.rb +++ b/activerecord/test/migration_test.rb @@ -425,14 +425,26 @@ if ActiveRecord::Base.connection.supports_migrations? end def test_change_column_with_new_default - Person.connection.add_column "people", "administrator", :boolean, :default => 1 + Person.connection.add_column "people", "administrator", :boolean, :default => true Person.reset_column_information assert Person.new.administrator? - assert_nothing_raised { Person.connection.change_column "people", "administrator", :boolean, :default => 0 } + assert_nothing_raised { Person.connection.change_column "people", "administrator", :boolean, :default => false } Person.reset_column_information assert !Person.new.administrator? end + + def test_change_column_default + Person.connection.change_column_default "people", "first_name", "Tester" + Person.reset_column_information + assert_equal "Tester", Person.new.first_name + end + + def test_change_column_default_to_null + Person.connection.change_column_default "people", "first_name", nil + Person.reset_column_information + assert_nil Person.new.first_name + end def test_add_table assert !Reminder.table_exists? |