Skip to content

Commit

Permalink
Merge pull request #44040 from kmasuda-aiming/extract-on-update-curre…
Browse files Browse the repository at this point in the history
…nt-timestamp

Extract `on update CURRENT_TIMESTAMP` for mysql2 adapter
  • Loading branch information
kamipo committed Jan 3, 2022
2 parents 7749451 + ada7568 commit 9a0d951
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
Expand Up @@ -163,6 +163,7 @@ def new_column_from_field(table_name, field)
default, default_function = field[:Default], nil

if type_metadata.type == :datetime && /\ACURRENT_TIMESTAMP(?:\([0-6]?\))?\z/i.match?(default)
default = "#{default} ON UPDATE #{default}" if /on update CURRENT_TIMESTAMP/i.match?(field[:Extra])
default, default_function = nil, default
elsif type_metadata.extra == "DEFAULT_GENERATED"
default = +"(#{default})" unless default.start_with?("(")
Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/defaults_test.rb
Expand Up @@ -151,6 +151,11 @@ class MysqlDefaultExpressionTest < ActiveRecord::TestCase
assert_match %r/t\.datetime\s+"precise_datetime",.+default: -> { "CURRENT_TIMESTAMP\(6\)" }/i, output
end

test "schema dump datetime includes precise default expression with on update" do
output = dump_table_schema("datetime_defaults")
assert_match %r/t\.datetime\s+"updated_datetime",.+default: -> { "CURRENT_TIMESTAMP\(6\) ON UPDATE CURRENT_TIMESTAMP\(6\)" }/i, output
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(?:\(\))?" }/i, output
Expand All @@ -161,6 +166,11 @@ class MysqlDefaultExpressionTest < ActiveRecord::TestCase
assert_match %r/t\.timestamp\s+"precise_timestamp",.+default: -> { "CURRENT_TIMESTAMP\(6\)" }/i, output
end

test "schema dump timestamp includes precise default expression with on update" do
output = dump_table_schema("timestamp_defaults")
assert_match %r/t\.timestamp\s+"updated_timestamp",.+default: -> { "CURRENT_TIMESTAMP\(6\) ON UPDATE 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
Expand Down
2 changes: 2 additions & 0 deletions activerecord/test/schema/mysql2_specific_schema.rb
Expand Up @@ -5,12 +5,14 @@
create_table :datetime_defaults, force: true do |t|
t.datetime :modified_datetime, precision: nil, default: -> { "CURRENT_TIMESTAMP" }
t.datetime :precise_datetime, default: -> { "CURRENT_TIMESTAMP(6)" }
t.datetime :updated_datetime, default: -> { "CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)" }
end

create_table :timestamp_defaults, force: true do |t|
t.timestamp :nullable_timestamp
t.timestamp :modified_timestamp, precision: nil, default: -> { "CURRENT_TIMESTAMP" }
t.timestamp :precise_timestamp, precision: 6, default: -> { "CURRENT_TIMESTAMP(6)" }
t.timestamp :updated_timestamp, precision: 6, default: -> { "CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)" }
end
end

Expand Down

0 comments on commit 9a0d951

Please sign in to comment.