Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MySQL::SchemaDumper behavior about datetime precision value #44171

Merged
merged 2 commits into from Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -53,7 +53,7 @@ def schema_limit(column)
end

def schema_precision(column)
super unless /\A(?:date)?time(?:stamp)?\b/.match?(column.sql_type) && column.precision == 0
super unless /\Atime(?:stamp)?\b/.match?(column.sql_type) && column.precision == 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this change to the following?

Suggested change
super unless /\Atime(?:stamp)?\b/.match?(column.sql_type) && column.precision == 0
super unless /\A(?:date)?time(?:stamp)?\b/.match?(column.sql_type) && column.precision == 6

Ie. keep the same behavior towards datetime and timestamp columns, but do not dump the precision if it is the default. Ideally I'd have liked to refer to a constant (eg. DEFAULT_TIMESTAMP_PRECISION somewhere, but unfortunately, the code is littered with the magic literal 6...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In PostgreSQL, it always dumps the value of precision, and I thought it would be simpler to use the same specification for MySQL.
I think it would be better to always dump the precision value for time and timestamp types.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any reason not to dump the default value of precision?

end

def schema_collation(column)
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/defaults_test.rb
Expand Up @@ -143,7 +143,7 @@ class MysqlDefaultExpressionTest < ActiveRecord::TestCase
if supports_datetime_with_precision?
test "schema dump datetime includes default expression" do
output = dump_table_schema("datetime_defaults")
assert_match %r/t\.datetime\s+"modified_datetime",\s+default: -> { "CURRENT_TIMESTAMP(?:\(\))?" }/i, output
assert_match %r/t\.datetime\s+"modified_datetime",\s+precision: 0,\s+default: -> { "CURRENT_TIMESTAMP(?:\(\))?" }/i, output
end

test "schema dump datetime includes precise default expression" do
Expand Down