From 9c5591da2dd9404ac189690ad59670ec7d364b16 Mon Sep 17 00:00:00 2001 From: Marcel Molina Date: Sun, 14 May 2006 18:37:22 +0000 Subject: Preserve MySQL boolean column defaults when changing a column in a migration. Closes #5015. [pdcawley@bofh.org.uk] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4340 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ .../lib/active_record/connection_adapters/mysql_adapter.rb | 4 +++- .../lib/active_record/connection_adapters/sqlite_adapter.rb | 2 +- activerecord/test/migration_test.rb | 8 ++++++++ 4 files changed, 14 insertions(+), 2 deletions(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 17cb061d46..d4e2e05dbc 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Preserve MySQL boolean column defaults when changing a column in a migration. Closes #5015. [pdcawley@bofh.org.uk] + * PostgreSQL: migrations support :limit with :integer columns by mapping limit < 4 to smallint, > 4 to bigint, and anything else to integer. #2900 [keegan@thebasement.org] * Dates and times interpret empty strings as nil rather than 2000-01-01. #4830 [kajism@yahoo.com] diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 7a697c2335..417eca180e 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -308,7 +308,9 @@ module ActiveRecord end def change_column(table_name, column_name, type, options = {}) #:nodoc: - options[:default] ||= select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Default"] + if options[:default].nil? + options[:default] = select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Default"] + end change_column_sql = "ALTER TABLE #{table_name} CHANGE #{column_name} #{column_name} #{type_to_sql(type, options[:limit])}" add_column_options!(change_column_sql, options) diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index ba219b9799..cf4114aacc 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -243,7 +243,7 @@ module ActiveRecord definition[column_name].instance_eval do self.type = type self.limit = options[:limit] if options[:limit] - self.default = options[:default] if options[:default] + self.default = options[:default] unless options[:default].nil? end end end diff --git a/activerecord/test/migration_test.rb b/activerecord/test/migration_test.rb index 5714f539ca..e71790ed1f 100644 --- a/activerecord/test/migration_test.rb +++ b/activerecord/test/migration_test.rb @@ -1,5 +1,6 @@ require 'abstract_unit' require 'fixtures/person' +require 'fixtures/topic' require File.dirname(__FILE__) + '/fixtures/migrations/1_people_have_last_names' require File.dirname(__FILE__) + '/fixtures/migrations/2_we_need_reminders' @@ -320,6 +321,13 @@ if ActiveRecord::Base.connection.supports_migrations? new_columns = Person.connection.columns(Person.table_name, "#{name} Columns") assert_nil new_columns.find { |c| c.name == 'age' and c.type == :integer } assert new_columns.find { |c| c.name == 'age' and c.type == :string } + + old_columns = Topic.connection.columns(Topic.table_name, "#{name} Columns") + assert old_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == true } + assert_nothing_raised { Topic.connection.change_column :topics, :approved, :boolean, :default => false } + new_columns = Topic.connection.columns(Topic.table_name, "#{name} Columns") + assert_nil new_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == true } + assert new_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == false } end def test_change_column_with_new_default -- cgit v1.2.3