diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2014-12-17 16:01:28 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2015-01-02 22:30:07 +0900 |
commit | 4157f5d172830df07b44a66d4096d152e068dc4a (patch) | |
tree | f62df286d1f9ca7137fa4149358370f0fa6ef315 /activerecord/lib/active_record/connection_adapters | |
parent | ae419af6667125662a4324e8809e2a15aad686d2 (diff) | |
download | rails-4157f5d172830df07b44a66d4096d152e068dc4a.tar.gz rails-4157f5d172830df07b44a66d4096d152e068dc4a.tar.bz2 rails-4157f5d172830df07b44a66d4096d152e068dc4a.zip |
Format the datetime string according to the precision of the datetime field.
Incompatible to rounding behavior between MySQL 5.6 and earlier.
In 5.5, when you insert `2014-08-17 12:30:00.999999` the fractional part
is ignored. In 5.6, it's rounded to `2014-08-17 12:30:01`:
http://bugs.mysql.com/bug.php?id=68760
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index ac62fb9825..d8431372ee 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -688,7 +688,7 @@ module ActiveRecord m.register_type(%r(datetime)i) do |sql_type| precision = extract_precision(sql_type) - Type::DateTime.new(precision: precision) + MysqlDateTime.new(precision: precision) end m.register_type(%r(enum)i) do |sql_type| @@ -884,6 +884,22 @@ module ActiveRecord TableDefinition.new(native_database_types, name, temporary, options, as) end + class MysqlDateTime < Type::DateTime # :nodoc: + def type_cast_for_database(value) + if value.acts_like?(:time) && value.respond_to?(:usec) + result = super.to_s(:db) + case precision + when 1..6 + "#{result}.#{sprintf("%0#{precision}d", value.usec / 10**(6 - precision))}" + else + result + end + else + super + end + end + end + class MysqlString < Type::String # :nodoc: def type_cast_for_database(value) case value |