Preserve existing column default functions when altering table in SQLite

This commit is contained in:
fatkodima 2023-05-26 14:09:52 +03:00
parent 54ec908a2d
commit 4e0079f09a
2 changed files with 15 additions and 0 deletions

@ -534,6 +534,7 @@ def copy_table(from, to, options = {})
if column.has_default?
type = lookup_cast_type_from_column(column)
default = type.deserialize(column.default)
default = -> { column.default_function } if default.nil?
end
column_options = {

@ -297,6 +297,20 @@ def test_change_column_default_with_from_and_to
assert_equal "Tester", TestModel.new.first_name
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
add_column "test_models", "first_name", :string
connection.change_column_null "test_models", "first_name", false