1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add change_null for change_table

To change a NOT NULL constraint `reversible`.

When changing a NOT NULL constraint, we use `ActiveRecord::ConnectionAdapters::SchemaStatements#change` method that is not reversible, so `up` and `down` methods were required. Actually, we can use `change_column_null` method if only one constraint changed, but if we want to change multiple constarints with ALTER QUERY, `up` and `down` methods were required.
This commit is contained in:
hotatekaoru 2020-05-05 11:32:38 +09:00
parent 92d03850f3
commit 972d18eab5
2 changed files with 18 additions and 0 deletions

View file

@ -496,6 +496,7 @@ module ActiveRecord
# t.timestamps
# t.change
# t.change_default
# t.change_null
# t.rename
# t.references
# t.belongs_to
@ -615,6 +616,16 @@ module ActiveRecord
@base.change_column_default(name, column_name, default_or_changes)
end
# Sets or removes a NOT NULL constraint on a column.
#
# t.change_null(:qualification, true)
# t.change_null(:qualification, false, 0)
#
# See {connection.change_column_null}[rdoc-ref:SchemaStatements#change_column_null]
def change_null(column_name, null, default = nil)
@base.change_column_null(name, column_name, null, default)
end
# Removes the column(s) from the table definition.
#
# t.remove(:qualification)

View file

@ -281,6 +281,13 @@ module ActiveRecord
end
end
def test_change_null_changes_column
with_change_table do |t|
@connection.expect :change_column_null, nil, [:delete_me, :bar, true, nil]
t.change_null :bar, true
end
end
def test_remove_drops_single_column
with_change_table do |t|
if RUBY_VERSION < "2.7"