Skip to content

Commit

Permalink
Ensure that microsecond precision is only used for version of mysql t…
Browse files Browse the repository at this point in the history
…hat support it. Fixes rails#19711
  • Loading branch information
jorihardman committed Jul 20, 2015
1 parent a2bb266 commit e975d7c
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ def initialize(connection, logger = nil, pool = nil) #:nodoc:
@prepared_statements = false
end

class Version
include Comparable

def initialize(version_string)
@version = version_string.split('.').map(&:to_i)
end

def <=>(version_string)
@version <=> version_string.split('.').map(&:to_i)
end
end

class BindCollector < Arel::Collectors::Bind
def compile(bvs, conn)
casted_binds = conn.prepare_binds_for_database(bvs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def supports_index_sort_order?
#
# http://bugs.mysql.com/bug.php?id=39170
def supports_transaction_isolation?
version[0] >= 5
version >= '5.0.0'
end

def supports_indexes_in_create?
Expand All @@ -319,11 +319,11 @@ def supports_foreign_keys?
end

def supports_views?
version[0] >= 5
version >= '5.0.0'
end

def supports_datetime_with_precision?
(version[0] == 5 && version[1] >= 6) || version[0] >= 6
version >= '5.6.4'
end

def native_database_types
Expand Down Expand Up @@ -386,6 +386,14 @@ def unquoted_false
0
end

def quoted_date(value)
if supports_datetime_with_precision?
super
else
super.sub(/\.\d{6}\z/, '')
end
end

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

def disable_referential_integrity #:nodoc:
Expand Down Expand Up @@ -938,15 +946,15 @@ def subquery_for(key, select)
end

def version
@version ||= full_version.scan(/^(\d+)\.(\d+)\.(\d+)/).flatten.map(&:to_i)
@version ||= Version.new(full_version.match(/^\d+\.\d+\.\d+/)[0])
end

def mariadb?
full_version =~ /mariadb/i
end

def supports_rename_index?
mariadb? ? false : (version[0] == 5 && version[1] >= 7) || version[0] >= 6
mariadb? ? false : version >= '5.7.6'
end

def configure_connection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,6 @@ class SQLite3Adapter < AbstractAdapter
boolean: { name: "boolean" }
}

class Version
include Comparable

def initialize(version_string)
@version = version_string.split('.').map(&:to_i)
end

def <=>(version_string)
@version <=> version_string.split('.').map(&:to_i)
end
end

class StatementPool < ConnectionAdapters::StatementPool
private

Expand Down
14 changes: 14 additions & 0 deletions activerecord/test/cases/adapters/mysql/quoting_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,18 @@ def test_type_cast_true
def test_type_cast_false
assert_equal 0, @conn.type_cast(false)
end

def test_quoted_date_precision_for_gte_564
@conn.stubs(:full_version).returns('5.6.4')
@conn.remove_instance_variable(:@version)
t = Time.now.change(usec: 1)
assert_match(/\.000001\z/, @conn.quoted_date(t))
end

def test_quoted_date_precision_for_lt_564
@conn.stubs(:full_version).returns('5.6.3')
@conn.remove_instance_variable(:@version)
t = Time.now.change(usec: 1)
refute_match(/\.000001\z/, @conn.quoted_date(t))
end
end
21 changes: 21 additions & 0 deletions activerecord/test/cases/adapters/mysql2/quoting_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require "cases/helper"

class Mysql2QuotingTest < ActiveRecord::Mysql2TestCase
setup do
@connection = ActiveRecord::Base.connection
end

test 'quoted date precision for gte 5.6.4' do
@connection.stubs(:full_version).returns('5.6.4')
@connection.remove_instance_variable(:@version)
t = Time.now.change(usec: 1)
assert_match(/\.000001\z/, @connection.quoted_date(t))
end

test 'quoted date precision for lt 5.6.4' do
@connection.stubs(:full_version).returns('5.6.3')
@connection.remove_instance_variable(:@version)
t = Time.now.change(usec: 1)
refute_match(/\.000001\z/, @connection.quoted_date(t))
end
end
3 changes: 2 additions & 1 deletion activerecord/test/cases/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def in_memory_db?

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

def mysql_enforcing_gtid_consistency?
Expand Down

0 comments on commit e975d7c

Please sign in to comment.