Skip to content

Commit

Permalink
Changing column null does not change default function
Browse files Browse the repository at this point in the history
  • Loading branch information
coding-chimp committed Dec 10, 2024
1 parent 1201cb1 commit ffa9d6d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Fix MySQL default functions getting dropped when changing a column's nullability.

*Bastian Bartmann*

* SQLite extensions can be configured in `config/database.yml`.

The database configuration option `extensions:` allows an application to load SQLite extensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,11 @@ def build_change_column_definition(table_name, column_name, type, **options) # :
type ||= column.sql_type

unless options.key?(:default)
options[:default] = column.default
options[:default] = if column.default_function
-> { column.default_function }
else
column.default
end
end

unless options.key?(:null)
Expand Down
14 changes: 14 additions & 0 deletions activerecord/test/cases/migration/columns_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,20 @@ def test_change_column_null_with_non_boolean_arguments_raises
assert_equal "change_column_null expects a boolean value (true for NULL, false for NOT NULL). Got: #{{ from: true, to: false }}", e.message
end

def test_change_column_null_does_not_change_default_functions
skip unless current_adapter?(:Mysql2Adapter, :TrilogyAdapter) && supports_default_expression?

function = connection.mariadb? ? "current_timestamp(6)" : "(now())"

connection.change_column_default "test_models", "created_at", -> { function }
TestModel.reset_column_information
assert_equal function, TestModel.columns_hash["created_at"].default_function

connection.change_column_null "test_models", "created_at", true
TestModel.reset_column_information
assert_equal function, TestModel.columns_hash["created_at"].default_function
end

def test_remove_column_no_second_parameter_raises_exception
assert_raise(ArgumentError) { connection.remove_column("funny") }
end
Expand Down

0 comments on commit ffa9d6d

Please sign in to comment.