Skip to content

Commit

Permalink
Fix schema dumper behavior about datetime precision value
Browse files Browse the repository at this point in the history
By rails#42297, the default value of datetime precision in rails is now 6.
With this change, if the value of datetime precision is 0, it should be
dumped into schema, but it is not.
So I modified it to dump to schema when the value of datetime precision
is 0, and not to dump when the value is 6, which is the default value of
rails.
  • Loading branch information
yota authored and public-rant committed Feb 17, 2022
1 parent 8e942ba commit a4b8b74
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 7 deletions.
Expand Up @@ -53,7 +53,9 @@ def schema_limit(column)
end

def schema_precision(column)
super unless /\A(?:date)?time(?:stamp)?\b/.match?(column.sql_type) && column.precision == 0
return if /\Adatetime\b/.match(column.sql_type) && column.precision == 6
return if /\Atime(?:stamp)?\b/.match?(column.sql_type) && column.precision == 0
super
end

def schema_collation(column)
Expand Down
8 changes: 5 additions & 3 deletions activerecord/test/cases/date_time_precision_test.rb
Expand Up @@ -193,13 +193,15 @@ def test_writing_a_blank_attribute_timestamptz
end
end

def test_schema_dump_includes_datetime_precision
def test_schema_dump_includes_non_default_datetime_precision
@connection.create_table(:foos, force: true) do |t|
t.datetime :datetime_zero, precision: 0
t.timestamps precision: 6
end
output = dump_table_schema("foos")
assert_match %r{t\.datetime\s+"created_at",\s+precision: 6,\s+null: false$}, output
assert_match %r{t\.datetime\s+"updated_at",\s+precision: 6,\s+null: false$}, output
assert_match %r{t\.datetime\s+"datetime_zero",\s+precision: 0$}, output
assert_match %r{t\.datetime\s+"created_at",\s+null: false$}, output
assert_match %r{t\.datetime\s+"updated_at",\s+null: false$}, output
end

if current_adapter?(:PostgreSQLAdapter, :SQLServerAdapter)
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
2 changes: 1 addition & 1 deletion activerecord/test/cases/primary_keys_test.rb
Expand Up @@ -321,7 +321,7 @@ def test_any_type_primary_key
test "schema typed primary key column" do
@connection.create_table(:scheduled_logs, id: :timestamp, precision: 6, force: true)
schema = dump_table_schema("scheduled_logs")
assert_match %r/create_table "scheduled_logs", id: { type: :timestamp, precision: 6.* }/, schema
assert_match %r/create_table "scheduled_logs", id: { type: :timestamp.* }/, schema
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/schema_dumper_test.rb
Expand Up @@ -840,7 +840,7 @@ def test_schema_dump_defaults_with_universally_supported_types
assert_match %r{t\.date\s+"date_with_default",\s+default: "2014-06-05"}, output

if supports_datetime_with_precision?
assert_match %r{t\.datetime\s+"datetime_with_default",\s+precision: 6,\s+default: "2014-06-05 07:17:04"}, output
assert_match %r{t\.datetime\s+"datetime_with_default",\s+default: "2014-06-05 07:17:04"}, output
else
assert_match %r{t\.datetime\s+"datetime_with_default",\s+default: "2014-06-05 07:17:04"}, output
end
Expand Down

0 comments on commit a4b8b74

Please sign in to comment.