Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4-2-stable: Fix datetime with precision for mysql adapter #30651

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@ def unquoted_false
0
end

def quoted_date(value)
if supports_datetime_with_precision? && value.acts_like?(:time) && value.respond_to?(:usec)
"#{super}.#{sprintf("%06d", value.usec)}"
else
super
end
end

# REFERENTIAL INTEGRITY ====================================

def disable_referential_integrity #:nodoc:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,6 @@ def quote_string(string)
@connection.escape(string)
end

def quoted_date(value)
if supports_datetime_with_precision? && value.acts_like?(:time) && value.respond_to?(:usec)
"#{super}.#{sprintf("%06d", value.usec)}"
else
super
end
end

#--
# CONNECTION MANAGEMENT ====================================
#++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@ def cast_value(value)
super
end
end

def has_precision?
precision || 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I wrong, or this will return 0 if precision is not present?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If precision is not present, the behavior differs between MySQL and PostgreSQL.
In MySQL, it means no precision, truncates microseconds.
In PostgreSQL, it means no limit, never truncates microseconds.

end
end

class Time < Type::Time # :nodoc:
Expand Down Expand Up @@ -328,8 +332,11 @@ def find_type(field)

def initialize_type_map(m) # :nodoc:
super
m.register_type %r(datetime)i, Fields::DateTime.new
m.register_type %r(time)i, Fields::Time.new
m.register_type(%r(datetime)i) do |sql_type|
precision = extract_precision(sql_type)
Fields::DateTime.new(precision: precision)
end
end

def exec_without_stmt(sql, name = 'SQL') # :nodoc:
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/adapters/mysql/datetime_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def mysql_datetime_precision(table_name, column_name)
result = results.find do |result_hash|
result_hash["column_name"] == column_name
end
result && result["datetime_precision"]
result && result["datetime_precision"].to_i
end

def activerecord_column_option(tablename, column_name, option)
Expand Down
5 changes: 2 additions & 3 deletions activerecord/test/cases/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ def in_memory_db?
end

def mysql_56?
current_adapter?(:MysqlAdapter, :Mysql2Adapter) &&
ActiveRecord::Base.connection.send(:version) >= '5.6.0' &&
ActiveRecord::Base.connection.send(:version) < '5.7.0'
current_adapter?(:Mysql2Adapter) &&
ActiveRecord::Base.connection.send(:version) >= '5.6.0'
end

def mysql_enforcing_gtid_consistency?
Expand Down