Skip to content

Commit 0a0ee51

Browse files
authored
Merge pull request rails-sqlserver#737 from aidanharan/time-with-default-precision
Use default precision for 'time' column type
2 parents 825e8f2 + 5cbc494 commit 0a0ee51

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ module SQLServer
44
module Type
55
class Time < ActiveRecord::Type::Time
66

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

912
def serialize(value)
@@ -42,7 +45,7 @@ def cast_value(value)
4245
end
4346

4447
def fractional_scale
45-
precision
48+
precision || DEFAULT_FRACTIONAL_SCALE
4649
end
4750

4851
end

test/cases/column_test_sqlserver.rb

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ def assert_obj_set_and_save(attribute, value)
349349
end
350350

351351
it 'datetime2' do
352-
skip 'datetime2 not supported in this protocal version' unless connection_dblib_73?
352+
skip 'datetime2 not supported in this protocol version' unless connection_dblib_73?
353353
col = column('datetime2_7')
354354
_(col.sql_type).must_equal 'datetime2(7)'
355355
_(col.type).must_equal :datetime
@@ -414,7 +414,7 @@ def assert_obj_set_and_save(attribute, value)
414414
end
415415

416416
it 'datetimeoffset' do
417-
skip 'datetimeoffset not supported in this protocal version' unless connection_dblib_73?
417+
skip 'datetimeoffset not supported in this protocol version' unless connection_dblib_73?
418418
col = column('datetimeoffset_7')
419419
_(col.sql_type).must_equal 'datetimeoffset(7)'
420420
_(col.type).must_equal :datetimeoffset
@@ -480,7 +480,7 @@ def assert_obj_set_and_save(attribute, value)
480480
end
481481

482482
it 'time(7)' do
483-
skip 'time() not supported in this protocal version' unless connection_dblib_73?
483+
skip 'time() not supported in this protocol version' unless connection_dblib_73?
484484
col = column('time_7')
485485
_(col.sql_type).must_equal 'time(7)'
486486
_(col.type).must_equal :time
@@ -512,7 +512,7 @@ def assert_obj_set_and_save(attribute, value)
512512
end
513513

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

544+
it 'time using default precision' do
545+
skip 'time() not supported in this protocol version' unless connection_dblib_73?
546+
col = column('time_default')
547+
_(col.sql_type).must_equal 'time(7)'
548+
_(col.type).must_equal :time
549+
_(col.null).must_equal true
550+
_(col.default).must_equal Time.utc(1900, 01, 01, 15, 03, 42, Rational(62197800, 1000)), "Nanoseconds were <#{col.default.nsec}> vs <62197800>"
551+
_(col.default_function).must_be_nil
552+
type = connection.lookup_cast_type_from_column(col)
553+
_(type).must_be_instance_of Type::Time
554+
_(type.limit).must_be_nil
555+
_(type.precision).must_equal 7
556+
_(type.scale).must_be_nil
557+
# Time's #usec precision (low micro)
558+
obj.time_default = Time.utc(2000, 01, 01, 15, 45, 00, 300)
559+
_(obj.time_default).must_equal Time.utc(2000, 01, 01, 15, 45, 00, 300), "Microseconds were <#{obj.time_default.usec}> vs <0>"
560+
_(obj.time_default).must_equal Time.utc(2000, 01, 01, 15, 45, 00, 300), "Nanoseconds were <#{obj.time_default.nsec}> vs <300>"
561+
obj.save! ; obj.reload
562+
_(obj.time_default).must_equal Time.utc(2000, 01, 01, 15, 45, 00, 300), "Microseconds were <#{obj.time_default.usec}> vs <0>"
563+
_(obj.time_default).must_equal Time.utc(2000, 01, 01, 15, 45, 00, 300), "Nanoseconds were <#{obj.time_default.nsec}> vs <300>"
564+
# Time's #usec precision (high micro)
565+
obj.time_default = Time.utc(2000, 01, 01, 15, 45, 00, 234567)
566+
_(obj.time_default).must_equal Time.utc(2000, 01, 01, 15, 45, 00, 234567), "Microseconds were <#{obj.time_default.usec}> vs <234567>"
567+
obj.save! ; obj.reload
568+
_(obj.time_default).must_equal Time.utc(2000, 01, 01, 15, 45, 00, 234567), "Microseconds were <#{obj.time_default.usec}> vs <234567>"
569+
# Time's #usec precision (high nano rounded)
570+
obj.time_default = Time.utc(2000, 01, 01, 15, 45, 00, Rational(288321545, 1000))
571+
_(obj.time_default).must_equal Time.utc(2000, 01, 01, 15, 45, 00, Rational(288321500, 1000)), "Nanoseconds were <#{obj.time_default.nsec}> vs <288321500>"
572+
obj.save! ; obj.reload
573+
_(obj.time_default).must_equal Time.utc(2000, 01, 01, 15, 45, 00, Rational(288321500, 1000)), "Nanoseconds were <#{obj.time_default.nsec}> vs <288321500>"
574+
end
575+
544576
# Character Strings
545577

546578
it 'char(10)' do

test/cases/schema_dumper_test_sqlserver.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class SchemaDumperTestSQLServer < ActiveRecord::TestCase
3434
if connection_dblib_73?
3535
assert_line :time_7, type: 'time', limit: nil, precision: 7, scale: nil, default: "04:20:00.2883215"
3636
assert_line :time_2, type: 'time', limit: nil, precision: 2, scale: nil, default: nil
37+
assert_line :time_default, type: 'time', limit: nil, precision: 7, scale: nil, default: "15:03:42.0621978"
3738
end
3839
# Character Strings
3940
assert_line :char_10, type: 'char', limit: 10, precision: nil, scale: nil, default: "1234567890", collation: nil

test/schema/datatypes/2012.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ CREATE TABLE [sst_datatypes] (
3535
[smalldatetime] [smalldatetime] NULL DEFAULT '1901-01-01T15:45:00.000Z',
3636
[time_7] [time](7) NULL DEFAULT '04:20:00.2883215',
3737
[time_2] [time](2) NULL,
38+
[time_default] [time] NULL DEFAULT '15:03:42.0621978',
3839
-- Character Strings
3940
[char_10] [char](10) NULL DEFAULT '1234567890',
4041
[varchar_50] [varchar](50) NULL DEFAULT 'test varchar_50',

0 commit comments

Comments
 (0)