Skip to content

Commit 7050b97

Browse files
committed
Fix change_column to drop default with null: false
Currently `change_column` cannot drop default if `null: false` is specified at the same time. This change fixes the issue. ```ruby # cannot drop default change_column "tests", "contributor", :boolean, default: nil, null: false # we need the following workaround currently change_column "tests", "contributor", :boolean, null: false change_column "tests", "contributor", :boolean, default: nil ```
1 parent f5d66cb commit 7050b97

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,11 +777,11 @@ def add_column_sql(table_name, column_name, type, options = {})
777777
def change_column_sql(table_name, column_name, type, options = {})
778778
column = column_for(table_name, column_name)
779779

780-
unless options_include_default?(options)
780+
unless options.key?(:default)
781781
options[:default] = column.default
782782
end
783783

784-
unless options.has_key?(:null)
784+
unless options.key?(:null)
785785
options[:null] = column.null
786786
end
787787

activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ def change_column(table_name, column_name, type, options = {}) #:nodoc:
499499
end
500500
execute sql
501501

502-
change_column_default(table_name, column_name, options[:default]) if options_include_default?(options)
502+
change_column_default(table_name, column_name, options[:default]) if options.key?(:default)
503503
change_column_null(table_name, column_name, options[:null], options[:default]) if options.key?(:null)
504504
change_column_comment(table_name, column_name, options[:comment]) if options.key?(:comment)
505505
end

activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,11 +405,10 @@ def change_column_null(table_name, column_name, null, default = nil) #:nodoc:
405405

406406
def change_column(table_name, column_name, type, options = {}) #:nodoc:
407407
alter_table(table_name) do |definition|
408-
include_default = options_include_default?(options)
409408
definition[column_name].instance_eval do
410409
self.type = type
411410
self.limit = options[:limit] if options.include?(:limit)
412-
self.default = options[:default] if include_default
411+
self.default = options[:default] if options.include?(:default)
413412
self.null = options[:null] if options.include?(:null)
414413
self.precision = options[:precision] if options.include?(:precision)
415414
self.scale = options[:scale] if options.include?(:scale)

activerecord/test/cases/migration/columns_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,16 @@ def test_change_column_with_nil_default
225225
assert_nil TestModel.new.contributor
226226
end
227227

228+
def test_change_column_to_drop_default_with_null_false
229+
add_column "test_models", "contributor", :boolean, default: true, null: false
230+
assert TestModel.new.contributor?
231+
232+
change_column "test_models", "contributor", :boolean, default: nil, null: false
233+
TestModel.reset_column_information
234+
assert_not TestModel.new.contributor?
235+
assert_nil TestModel.new.contributor
236+
end
237+
228238
def test_change_column_with_new_default
229239
add_column "test_models", "administrator", :boolean, default: true
230240
assert TestModel.new.administrator?

0 commit comments

Comments
 (0)