Skip to content

Commit 5390b49

Browse files
committed
Ensure small datetime/datetime2 fractionals are properly quoted. Fixes #457.
1 parent dfc29b5 commit 5390b49

File tree

7 files changed

+32
-2
lines changed

7 files changed

+32
-2
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## v4.2.10
2+
3+
#### Fixed
4+
5+
* Ensure small datetime/datetime2 fractionals are properly quoted. Fixes #457.
6+
7+
18
## v4.2.9
29

310
#### Fixed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.2.9
1+
4.2.10

lib/active_record/connection_adapters/sqlserver/type/time_value_fractional.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def cast_fractional(value)
1717

1818
def quote_fractional(value)
1919
seconds = (value.send(fractional_property).to_f / fractional_operator.to_f).round(fractional_scale)
20-
seconds.to_s.split('.').last.to(fractional_scale-1)
20+
seconds.to_d.to_s.split('.').last.to(fractional_scale-1)
2121
end
2222

2323
def fractional_property

test/cases/column_test_sqlserver.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,11 @@ def assert_obj_set_and_save(attribute, value)
354354
obj.datetime2_7.must_equal Time.utc(9999, 12, 31, 23, 59, 59, Rational(123456800, 1000)), "Nanoseconds were <#{obj.datetime2_7.nsec}> vs <123456800>"
355355
obj.save!
356356
obj.reload.datetime2_7.must_equal Time.utc(9999, 12, 31, 23, 59, 59, Rational(123456800, 1000)), "Nanoseconds were <#{obj.datetime2_7.nsec}> vs <123456800>"
357+
# Can save small fraction nanosecond precisoins and return again.
358+
obj.datetime2_7 = Time.utc(2008, 6, 21, 13, 30, 0, Rational(15020, 1000))
359+
obj.datetime2_7.must_equal Time.utc(2008, 6, 21, 13, 30, 0, Rational(15000, 1000)), "Nanoseconds were <#{obj.datetime2_7.nsec}> vs <15000>"
360+
obj.save!
361+
obj.reload.datetime2_7.must_equal Time.utc(2008, 6, 21, 13, 30, 0, Rational(15000, 1000)), "Nanoseconds were <#{obj.datetime2_7.nsec}> vs <15000>"
357362
# With other precisions.
358363
time = Time.utc 9999, 12, 31, 23, 59, 59, Rational(123456789, 1000)
359364
col = column('datetime2_3')

test/cases/specific_schema_test_sqlserver.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ class SpecificSchemaTestSQLServer < ActiveRecord::TestCase
6868
obj.reload.date.must_be_instance_of Date
6969
end
7070

71+
it 'allows datetime2 as timestamps' do
72+
SSTestBooking.columns_hash['created_at'].sql_type.must_equal 'datetime2(7)'
73+
SSTestBooking.columns_hash['updated_at'].sql_type.must_equal 'datetime2(7)'
74+
obj1 = SSTestBooking.new name: 'test1'
75+
obj1.save!
76+
obj1.created_at.must_be_instance_of Time
77+
obj1.updated_at.must_be_instance_of Time
78+
end
79+
7180
# Natural primary keys.
7281

7382
it 'work with identity inserts' do

test/models/sqlserver/booking.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class SSTestBooking < ActiveRecord::Base
2+
self.table_name = 'sst_bookings'
3+
end

test/schema/sqlserver_specific_schema.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737

3838
# Edge Cases
3939

40+
create_table 'sst_bookings', force: true do |t|
41+
t.string :name
42+
t.datetime2 :created_at, null: false
43+
t.datetime2 :updated_at, null: false
44+
end
45+
4046
create_table 'sst_uuids', force: true, id: :uuid do |t|
4147
t.string :name
4248
t.uuid :other_uuid, default: 'NEWID()'

0 commit comments

Comments
 (0)