Skip to content

Commit c9a6a87

Browse files
authored
Fix date columns serialization for range values (#938)
1 parent dec6836 commit c9a6a87

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- [#935](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/935) Fix schema cache generation
77
(**breaking change**)
88
- [#936](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/936) Fix deteministic fetch when table has a composite primary key
9+
- [#938](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/938) Fix date columns serialization for range values
910

1011
#### Changed
1112

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ def sqlserver_type
1010
end
1111

1212
def serialize(value)
13-
return unless value.present?
13+
value = super
14+
return value unless value.acts_like?(:date)
1415

1516
date = super(value).to_s(:_sqlserver_dateformat)
1617
Data.new date, self

test/cases/column_test_sqlserver.rb

+4
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ def assert_obj_set_and_save(attribute, value)
299299
_(obj.date).must_equal Date.civil(0001, 4, 1)
300300
obj.reload
301301
_(obj.date).must_equal Date.civil(0001, 4, 1)
302+
# Can filter by date range
303+
_(obj).must_equal obj.class.where(date: obj.date..Date::Infinity.new).first
302304
# Can keep and return assigned date.
303305
assert_obj_set_and_save :date, Date.civil(1972, 04, 14)
304306
# Can accept and cast time objects.
@@ -333,6 +335,8 @@ def assert_obj_set_and_save(attribute, value)
333335
obj.reload
334336
_(obj.datetime).must_equal time, "Microseconds were <#{obj.datetime.usec}> vs <3000>"
335337
_(obj).must_equal obj.class.where(datetime: time).first
338+
# Can filter by datetime range
339+
_(obj).must_equal obj.class.where(datetime: time..DateTime::Infinity.new).first
336340
# Will cast to true DB value on attribute write, save and return again.
337341
time = Time.utc 2010, 04, 01, 12, 34, 56, 234567
338342
time2 = Time.utc 2010, 04, 01, 12, 34, 56, 233000

0 commit comments

Comments
 (0)