Preserve existing column default functions when altering table in SQLite
This commit is contained in:
parent
54ec908a2d
commit
4e0079f09a
@ -534,6 +534,7 @@ def copy_table(from, to, options = {})
|
|||||||
if column.has_default?
|
if column.has_default?
|
||||||
type = lookup_cast_type_from_column(column)
|
type = lookup_cast_type_from_column(column)
|
||||||
default = type.deserialize(column.default)
|
default = type.deserialize(column.default)
|
||||||
|
default = -> { column.default_function } if default.nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
column_options = {
|
column_options = {
|
||||||
|
@ -297,6 +297,20 @@ def test_change_column_default_with_from_and_to
|
|||||||
assert_equal "Tester", TestModel.new.first_name
|
assert_equal "Tester", TestModel.new.first_name
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_change_column_default_preserves_existing_column_default_function
|
||||||
|
skip unless current_adapter?(:SQLite3Adapter)
|
||||||
|
|
||||||
|
connection.change_column_default "test_models", "created_at", -> { "CURRENT_TIMESTAMP" }
|
||||||
|
TestModel.reset_column_information
|
||||||
|
assert_equal "CURRENT_TIMESTAMP", TestModel.columns_hash["created_at"].default_function
|
||||||
|
|
||||||
|
add_column "test_models", "edited_at", :datetime
|
||||||
|
connection.change_column_default "test_models", "edited_at", -> { "CURRENT_TIMESTAMP" }
|
||||||
|
TestModel.reset_column_information
|
||||||
|
assert_equal "CURRENT_TIMESTAMP", TestModel.columns_hash["created_at"].default_function
|
||||||
|
assert_equal "CURRENT_TIMESTAMP", TestModel.columns_hash["edited_at"].default_function
|
||||||
|
end
|
||||||
|
|
||||||
def test_change_column_null_false
|
def test_change_column_null_false
|
||||||
add_column "test_models", "first_name", :string
|
add_column "test_models", "first_name", :string
|
||||||
connection.change_column_null "test_models", "first_name", false
|
connection.change_column_null "test_models", "first_name", false
|
||||||
|
Loading…
Reference in New Issue
Block a user