提交 b67f57d1 编写于 作者: R Ryuta Kamizono

Merge pull request #33280 from nkondratyev/fix/mysql-time-default-values

Fix default value for mysql time types with specified precision
上级 c46a33bf
* Fix default value for mysql time types with specified precision.
*Nikolay Kondratyev*
* Fix `touch` option to behave consistently with `Persistence#touch` method.
*Ryuta Kamizono*
......
......@@ -80,8 +80,8 @@ def create_table_definition(*args)
def new_column_from_field(table_name, field)
type_metadata = fetch_type_metadata(field[:Type], field[:Extra])
if type_metadata.type == :datetime && /\ACURRENT_TIMESTAMP(?:\(\))?\z/i.match?(field[:Default])
default, default_function = nil, "CURRENT_TIMESTAMP"
if type_metadata.type == :datetime && /\ACURRENT_TIMESTAMP(?:\([0-6]?\))?\z/i.match?(field[:Default])
default, default_function = nil, field[:Default]
else
default, default_function = field[:Default], nil
end
......
......@@ -106,21 +106,31 @@ class PostgresqlDefaultExpressionTest < ActiveRecord::TestCase
class MysqlDefaultExpressionTest < ActiveRecord::TestCase
include SchemaDumpingHelper
if ActiveRecord::Base.connection.version >= "5.6.0"
if subsecond_precision_supported?
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" }/, output
assert_match %r/t\.datetime\s+"modified_datetime",\s+default: -> { "CURRENT_TIMESTAMP(?:\(\))?" }/i, output
end
end
test "schema dump timestamp includes default expression" do
output = dump_table_schema("timestamp_defaults")
assert_match %r/t\.timestamp\s+"modified_timestamp",\s+default: -> { "CURRENT_TIMESTAMP" }/, output
end
test "schema dump datetime includes precise default expression" do
output = dump_table_schema("datetime_defaults")
assert_match %r/t\.datetime\s+"precise_datetime",.+default: -> { "CURRENT_TIMESTAMP\(6\)" }/i, output
end
test "schema dump timestamp without default expression" do
output = dump_table_schema("timestamp_defaults")
assert_match %r/t\.timestamp\s+"nullable_timestamp"$/, output
test "schema dump timestamp includes default expression" do
output = dump_table_schema("timestamp_defaults")
assert_match %r/t\.timestamp\s+"modified_timestamp",\s+default: -> { "CURRENT_TIMESTAMP(?:\(\))?" }/i, output
end
test "schema dump timestamp includes precise default expression" do
output = dump_table_schema("timestamp_defaults")
assert_match %r/t\.timestamp\s+"precise_timestamp",.+default: -> { "CURRENT_TIMESTAMP\(6\)" }/i, output
end
test "schema dump timestamp without default expression" do
output = dump_table_schema("timestamp_defaults")
assert_match %r/t\.timestamp\s+"nullable_timestamp"$/, output
end
end
end
......
......@@ -2,15 +2,17 @@
ActiveRecord::Schema.define do
if ActiveRecord::Base.connection.version >= "5.6.0"
if subsecond_precision_supported?
create_table :datetime_defaults, force: true do |t|
t.datetime :modified_datetime, default: -> { "CURRENT_TIMESTAMP" }
t.datetime :precise_datetime, precision: 6, default: -> { "CURRENT_TIMESTAMP(6)" }
end
end
create_table :timestamp_defaults, force: true do |t|
t.timestamp :nullable_timestamp
t.timestamp :modified_timestamp, default: -> { "CURRENT_TIMESTAMP" }
create_table :timestamp_defaults, force: true do |t|
t.timestamp :nullable_timestamp
t.timestamp :modified_timestamp, default: -> { "CURRENT_TIMESTAMP" }
t.timestamp :precise_timestamp, precision: 6, default: -> { "CURRENT_TIMESTAMP(6)" }
end
end
create_table :binary_fields, force: true do |t|
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册