diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2018-07-09 04:06:06 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2018-07-09 04:06:45 +0900 |
commit | 0d54a51065950a3d7f496ff5422bb78fa24de854 (patch) | |
tree | 8ea651adfa139552946537f841d351ec87f8fd78 /activerecord | |
parent | 50a9ca665d51cd3298af8b3c1dabf20ad5ce77da (diff) | |
parent | 64078e088650124b6c37ce6a3c352ad2dc4f072c (diff) | |
download | rails-0d54a51065950a3d7f496ff5422bb78fa24de854.tar.gz rails-0d54a51065950a3d7f496ff5422bb78fa24de854.tar.bz2 rails-0d54a51065950a3d7f496ff5422bb78fa24de854.zip |
Merge pull request #33280 from nkondratyev/fix/mysql-time-default-values
Fix default value for mysql time types with specified precision
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb | 4 | ||||
-rw-r--r-- | activerecord/test/cases/defaults_test.rb | 14 | ||||
-rw-r--r-- | activerecord/test/schema/mysql2_specific_schema.rb | 2 |
4 files changed, 20 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index fad09182c9..b0027cd2fb 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,7 @@ +* Fix default value for mysql time types with specified precision. + + *Nikolay Kondratyev* + * Fix `touch` option to behave consistently with `Persistence#touch` method. *Ryuta Kamizono* diff --git a/activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb index ce50590651..2087938d7c 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb @@ -80,8 +80,8 @@ module ActiveRecord 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 diff --git a/activerecord/test/cases/defaults_test.rb b/activerecord/test/cases/defaults_test.rb index 1c96aaabe2..f34501b204 100644 --- a/activerecord/test/cases/defaults_test.rb +++ b/activerecord/test/cases/defaults_test.rb @@ -109,13 +109,23 @@ if current_adapter?(:Mysql2Adapter) if ActiveRecord::Base.connection.version >= "5.6.0" 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 + + 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 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 + 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 diff --git a/activerecord/test/schema/mysql2_specific_schema.rb b/activerecord/test/schema/mysql2_specific_schema.rb index e634e9e6b1..1ac3b60c92 100644 --- a/activerecord/test/schema/mysql2_specific_schema.rb +++ b/activerecord/test/schema/mysql2_specific_schema.rb @@ -5,12 +5,14 @@ ActiveRecord::Schema.define do if ActiveRecord::Base.connection.version >= "5.6.0" 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" } + t.timestamp :precise_timestamp, precision: 6, default: -> { "CURRENT_TIMESTAMP(6)" } end create_table :binary_fields, force: true do |t| |