Skip to content

Commit 724509a

Browse files
committed
make change_column_null reversible. Closes #13576.
Closes #13623.
1 parent b502e3d commit 724509a

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

activerecord/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Make `change_column_null` revertable. Fixes #13576.
2+
3+
*Yves Senn*, *Nishant Modak*, *Prathamesh Sonpatki*
4+
15
* Don't create/drop the test database if RAILS_ENV is specified explicitely.
26

37
Previously, when the environment was development, we would always

activerecord/lib/active_record/migration/command_recorder.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def respond_to?(*args) # :nodoc:
7474
:rename_index, :rename_column, :add_index, :remove_index, :add_timestamps, :remove_timestamps,
7575
:change_column_default, :add_reference, :remove_reference, :transaction,
7676
:drop_join_table, :drop_table, :execute_block, :enable_extension,
77-
:change_column, :execute, :remove_columns, # irreversible methods need to be here too
77+
:change_column, :execute, :remove_columns, :change_column_null # irreversible methods need to be here too
7878
].each do |method|
7979
class_eval <<-EOV, __FILE__, __LINE__ + 1
8080
def #{method}(*args, &block) # def create_table(*args, &block)
@@ -157,6 +157,11 @@ def invert_remove_index(args)
157157
alias :invert_add_belongs_to :invert_add_reference
158158
alias :invert_remove_belongs_to :invert_remove_reference
159159

160+
def invert_change_column_null(args)
161+
args[2] = !args[2]
162+
[:change_column_null, args]
163+
end
164+
160165
# Forwards any missing method call to the \target.
161166
def method_missing(method, *args, &block)
162167
if @delegate.respond_to?(method)

activerecord/test/cases/migration/change_schema_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,22 @@ def test_keeping_default_and_notnull_constraints_on_change
308308
assert_equal 2000, connection.select_values("SELECT money FROM testings").first.to_i
309309
end
310310

311+
def test_change_column_null
312+
testing_table_with_only_foo_attribute do
313+
notnull_migration = Class.new(ActiveRecord::Migration) do
314+
def change
315+
change_column_null :testings, :foo, false
316+
end
317+
end
318+
notnull_migration.new.suppress_messages do
319+
notnull_migration.migrate(:up)
320+
assert_equal false, connection.columns(:testings).find{ |c| c.name == "foo"}.null
321+
notnull_migration.migrate(:down)
322+
assert connection.columns(:testings).find{ |c| c.name == "foo"}.null
323+
end
324+
end
325+
end
326+
311327
def test_column_exists
312328
connection.create_table :testings do |t|
313329
t.column :foo, :string

0 commit comments

Comments
 (0)