Skip to content

Commit ab3b87e

Browse files
authored
Merge pull request rails#53838 from Shopify/bb/default-function-bug
Changing column nullability does not change default function
2 parents 1201cb1 + ffa9d6d commit ab3b87e

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-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+
* Fix MySQL default functions getting dropped when changing a column's nullability.
2+
3+
*Bastian Bartmann*
4+
15
* SQLite extensions can be configured in `config/database.yml`.
26

37
The database configuration option `extensions:` allows an application to load SQLite extensions

activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,11 @@ def build_change_column_definition(table_name, column_name, type, **options) # :
403403
type ||= column.sql_type
404404

405405
unless options.key?(:default)
406-
options[:default] = column.default
406+
options[:default] = if column.default_function
407+
-> { column.default_function }
408+
else
409+
column.default
410+
end
407411
end
408412

409413
unless options.key?(:null)

activerecord/test/cases/migration/columns_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,20 @@ def test_change_column_null_with_non_boolean_arguments_raises
346346
assert_equal "change_column_null expects a boolean value (true for NULL, false for NOT NULL). Got: #{{ from: true, to: false }}", e.message
347347
end
348348

349+
def test_change_column_null_does_not_change_default_functions
350+
skip unless current_adapter?(:Mysql2Adapter, :TrilogyAdapter) && supports_default_expression?
351+
352+
function = connection.mariadb? ? "current_timestamp(6)" : "(now())"
353+
354+
connection.change_column_default "test_models", "created_at", -> { function }
355+
TestModel.reset_column_information
356+
assert_equal function, TestModel.columns_hash["created_at"].default_function
357+
358+
connection.change_column_null "test_models", "created_at", true
359+
TestModel.reset_column_information
360+
assert_equal function, TestModel.columns_hash["created_at"].default_function
361+
end
362+
349363
def test_remove_column_no_second_parameter_raises_exception
350364
assert_raise(ArgumentError) { connection.remove_column("funny") }
351365
end

0 commit comments

Comments
 (0)