Skip to content

Commit

Permalink
Merge pull request rails-sqlserver#737 from aidanharan/time-with-defa…
Browse files Browse the repository at this point in the history
…ult-precision

Use default precision for 'time' column type
  • Loading branch information
wpolicarpo authored Feb 28, 2020
2 parents 825e8f2 + 5cbc494 commit 0a0ee51
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/active_record/connection_adapters/sqlserver/type/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ module SQLServer
module Type
class Time < ActiveRecord::Type::Time

# Default fractional scale for 'time' (See https://docs.microsoft.com/en-us/sql/t-sql/data-types/time-transact-sql)
DEFAULT_FRACTIONAL_SCALE = 7

include TimeValueFractional2

def serialize(value)
Expand Down Expand Up @@ -42,7 +45,7 @@ def cast_value(value)
end

def fractional_scale
precision
precision || DEFAULT_FRACTIONAL_SCALE
end

end
Expand Down
40 changes: 36 additions & 4 deletions test/cases/column_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def assert_obj_set_and_save(attribute, value)
end

it 'datetime2' do
skip 'datetime2 not supported in this protocal version' unless connection_dblib_73?
skip 'datetime2 not supported in this protocol version' unless connection_dblib_73?
col = column('datetime2_7')
_(col.sql_type).must_equal 'datetime2(7)'
_(col.type).must_equal :datetime
Expand Down Expand Up @@ -414,7 +414,7 @@ def assert_obj_set_and_save(attribute, value)
end

it 'datetimeoffset' do
skip 'datetimeoffset not supported in this protocal version' unless connection_dblib_73?
skip 'datetimeoffset not supported in this protocol version' unless connection_dblib_73?
col = column('datetimeoffset_7')
_(col.sql_type).must_equal 'datetimeoffset(7)'
_(col.type).must_equal :datetimeoffset
Expand Down Expand Up @@ -480,7 +480,7 @@ def assert_obj_set_and_save(attribute, value)
end

it 'time(7)' do
skip 'time() not supported in this protocal version' unless connection_dblib_73?
skip 'time() not supported in this protocol version' unless connection_dblib_73?
col = column('time_7')
_(col.sql_type).must_equal 'time(7)'
_(col.type).must_equal :time
Expand Down Expand Up @@ -512,7 +512,7 @@ def assert_obj_set_and_save(attribute, value)
end

it 'time(2)' do
skip 'time() not supported in this protocal version' unless connection_dblib_73?
skip 'time() not supported in this protocol version' unless connection_dblib_73?
col = column('time_2')
_(col.sql_type).must_equal 'time(2)'
_(col.type).must_equal :time
Expand Down Expand Up @@ -541,6 +541,38 @@ def assert_obj_set_and_save(attribute, value)
_(obj.time_2).must_equal Time.utc(2000, 01, 01, 15, 45, 00, 0), "Microseconds were <#{obj.time_2.usec}> vs <0>"
end

it 'time using default precision' do
skip 'time() not supported in this protocol version' unless connection_dblib_73?
col = column('time_default')
_(col.sql_type).must_equal 'time(7)'
_(col.type).must_equal :time
_(col.null).must_equal true
_(col.default).must_equal Time.utc(1900, 01, 01, 15, 03, 42, Rational(62197800, 1000)), "Nanoseconds were <#{col.default.nsec}> vs <62197800>"
_(col.default_function).must_be_nil
type = connection.lookup_cast_type_from_column(col)
_(type).must_be_instance_of Type::Time
_(type.limit).must_be_nil
_(type.precision).must_equal 7
_(type.scale).must_be_nil
# Time's #usec precision (low micro)
obj.time_default = Time.utc(2000, 01, 01, 15, 45, 00, 300)
_(obj.time_default).must_equal Time.utc(2000, 01, 01, 15, 45, 00, 300), "Microseconds were <#{obj.time_default.usec}> vs <0>"
_(obj.time_default).must_equal Time.utc(2000, 01, 01, 15, 45, 00, 300), "Nanoseconds were <#{obj.time_default.nsec}> vs <300>"
obj.save! ; obj.reload
_(obj.time_default).must_equal Time.utc(2000, 01, 01, 15, 45, 00, 300), "Microseconds were <#{obj.time_default.usec}> vs <0>"
_(obj.time_default).must_equal Time.utc(2000, 01, 01, 15, 45, 00, 300), "Nanoseconds were <#{obj.time_default.nsec}> vs <300>"
# Time's #usec precision (high micro)
obj.time_default = Time.utc(2000, 01, 01, 15, 45, 00, 234567)
_(obj.time_default).must_equal Time.utc(2000, 01, 01, 15, 45, 00, 234567), "Microseconds were <#{obj.time_default.usec}> vs <234567>"
obj.save! ; obj.reload
_(obj.time_default).must_equal Time.utc(2000, 01, 01, 15, 45, 00, 234567), "Microseconds were <#{obj.time_default.usec}> vs <234567>"
# Time's #usec precision (high nano rounded)
obj.time_default = Time.utc(2000, 01, 01, 15, 45, 00, Rational(288321545, 1000))
_(obj.time_default).must_equal Time.utc(2000, 01, 01, 15, 45, 00, Rational(288321500, 1000)), "Nanoseconds were <#{obj.time_default.nsec}> vs <288321500>"
obj.save! ; obj.reload
_(obj.time_default).must_equal Time.utc(2000, 01, 01, 15, 45, 00, Rational(288321500, 1000)), "Nanoseconds were <#{obj.time_default.nsec}> vs <288321500>"
end

# Character Strings

it 'char(10)' do
Expand Down
1 change: 1 addition & 0 deletions test/cases/schema_dumper_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class SchemaDumperTestSQLServer < ActiveRecord::TestCase
if connection_dblib_73?
assert_line :time_7, type: 'time', limit: nil, precision: 7, scale: nil, default: "04:20:00.2883215"
assert_line :time_2, type: 'time', limit: nil, precision: 2, scale: nil, default: nil
assert_line :time_default, type: 'time', limit: nil, precision: 7, scale: nil, default: "15:03:42.0621978"
end
# Character Strings
assert_line :char_10, type: 'char', limit: 10, precision: nil, scale: nil, default: "1234567890", collation: nil
Expand Down
1 change: 1 addition & 0 deletions test/schema/datatypes/2012.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ CREATE TABLE [sst_datatypes] (
[smalldatetime] [smalldatetime] NULL DEFAULT '1901-01-01T15:45:00.000Z',
[time_7] [time](7) NULL DEFAULT '04:20:00.2883215',
[time_2] [time](2) NULL,
[time_default] [time] NULL DEFAULT '15:03:42.0621978',
-- Character Strings
[char_10] [char](10) NULL DEFAULT '1234567890',
[varchar_50] [varchar](50) NULL DEFAULT 'test varchar_50',
Expand Down

0 comments on commit 0a0ee51

Please sign in to comment.