Skip to content

Commit 2224d06

Browse files
committed
Support microsecond datetime precision on MariaDB 5.3+.
We support microsecond datetime precision for MySQL 5.6.4+. MariaDB has supported it since 5.3.0, but even 10.x versions return a compatible version string like `5.5.5-10.1.8-MariaDB-log` which we parse as 5.5.5, before MySQL supported microsecond precision. Specialize our version check to account for MariaDB to fix.
1 parent 2080ff2 commit 2224d06

File tree

6 files changed

+56
-24
lines changed

6 files changed

+56
-24
lines changed

activerecord/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* MariaDB 5.3+ supports microsecond datetime precision.
2+
3+
*Jeremy Daer*
4+
15
* Delegate `empty?`, `none?` and `one?`. Now they can be invoked as model class methods.
26

37
Example:

activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ def supports_views?
121121
end
122122

123123
def supports_datetime_with_precision?
124-
version >= '5.6.4'
124+
if mariadb?
125+
version >= '5.3.0'
126+
else
127+
version >= '5.6.4'
128+
end
125129
end
126130

127131
def supports_advisory_locks?
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require "cases/helper"
2+
3+
class Mysql2DatetimePrecisionQuotingTest < ActiveRecord::Mysql2TestCase
4+
setup do
5+
@connection = ActiveRecord::Base.connection
6+
end
7+
8+
test 'microsecond precision for MySQL gte 5.6.4' do
9+
stub_version '5.6.4'
10+
assert_microsecond_precision
11+
end
12+
13+
test 'no microsecond precision for MySQL lt 5.6.4' do
14+
stub_version '5.6.3'
15+
assert_no_microsecond_precision
16+
end
17+
18+
test 'microsecond precision for MariaDB gte 5.3.0' do
19+
stub_version '5.5.5-10.1.8-MariaDB-log'
20+
assert_microsecond_precision
21+
end
22+
23+
test 'no microsecond precision for MariaDB lt 5.3.0' do
24+
stub_version '5.2.9-MariaDB'
25+
assert_no_microsecond_precision
26+
end
27+
28+
private
29+
def assert_microsecond_precision
30+
assert_match_quoted_microsecond_datetime(/\.000001\z/)
31+
end
32+
33+
def assert_no_microsecond_precision
34+
assert_match_quoted_microsecond_datetime(/\d\z/)
35+
end
36+
37+
def assert_match_quoted_microsecond_datetime(match)
38+
assert_match match, @connection.quoted_date(Time.now.change(usec: 1))
39+
end
40+
41+
def stub_version(full_version_string)
42+
@connection.stubs(:full_version).returns(full_version_string)
43+
@connection.remove_instance_variable(:@version) if @connection.instance_variable_defined?(:@version)
44+
end
45+
end

activerecord/test/cases/adapters/mysql2/quoting_test.rb

Lines changed: 0 additions & 21 deletions
This file was deleted.

activerecord/test/cases/date_time_precision_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'cases/helper'
22
require 'support/schema_dumping_helper'
33

4-
if ActiveRecord::Base.connection.supports_datetime_with_precision?
4+
if subsecond_precision_supported?
55
class DateTimePrecisionTest < ActiveRecord::TestCase
66
include SchemaDumpingHelper
77
self.use_transactional_tests = false

activerecord/test/cases/time_precision_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'cases/helper'
22
require 'support/schema_dumping_helper'
33

4-
if ActiveRecord::Base.connection.supports_datetime_with_precision?
4+
if subsecond_precision_supported?
55
class TimePrecisionTest < ActiveRecord::TestCase
66
include SchemaDumpingHelper
77
self.use_transactional_tests = false

0 commit comments

Comments
 (0)