Skip to content

Commit 1bf0e4c

Browse files
authored
Fix precision handling in time migration (#1144)
1 parent 31c5fb9 commit 1bf0e4c

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#### Fixed
44

55
- [#1145](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1145) Ensure correct order of COLLATE and NOT NULL in CREATE TABLE statements
6+
- [#1144](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1144) Fix precision handling in time migration
67
- [#1143](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1143) Fix precision handling for datetimeoffset migration
78

89
## v7.1.0

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,16 @@ def type_to_sql(type, limit: nil, precision: nil, scale: nil, **)
303303
when 5..8 then "bigint"
304304
else raise(ActiveRecordError, "No integer type has byte size #{limit}. Use a numeric with precision 0 instead.")
305305
end
306+
when "time" # https://learn.microsoft.com/en-us/sql/t-sql/data-types/time-transact-sql
307+
column_type_sql = type.to_s
308+
if precision
309+
if (0..7) === precision
310+
column_type_sql << "(#{precision})"
311+
else
312+
raise(ActiveRecordError, "The time type has precision of #{precision}. The allowed range of precision is from 0 to 7")
313+
end
314+
end
315+
column_type_sql
306316
when "datetime2"
307317
column_type_sql = super
308318
if precision
@@ -319,7 +329,7 @@ def type_to_sql(type, limit: nil, precision: nil, scale: nil, **)
319329
if (0..7) === precision
320330
column_type_sql << "(#{precision})"
321331
else
322-
raise(ActiveRecordError, "The datetimeoffset type has precision of #{precision}. The allowed range of precision is from 0 to 7.")
332+
raise(ActiveRecordError, "The datetimeoffset type has precision of #{precision}. The allowed range of precision is from 0 to 7")
323333
end
324334
end
325335
column_type_sql

test/cases/active_schema_test_sqlserver.rb

+30
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,34 @@ class ActiveSchemaTestSQLServer < ActiveRecord::TestCase
9494
connection.drop_table :datetimeoffset_precisions rescue nil
9595
end
9696
end
97+
98+
describe 'time precision' do
99+
it 'valid precisions are correct' do
100+
assert_nothing_raised do
101+
connection.create_table :time_precisions do |t|
102+
t.time :precision_default
103+
t.time :precision_5, precision: 5
104+
t.time :precision_7, precision: 7
105+
end
106+
end
107+
108+
columns = connection.columns("time_precisions")
109+
110+
assert_equal columns.find { |column| column.name == "precision_default" }.precision, 7
111+
assert_equal columns.find { |column| column.name == "precision_5" }.precision, 5
112+
assert_equal columns.find { |column| column.name == "precision_7" }.precision, 7
113+
ensure
114+
connection.drop_table :time_precisions rescue nil
115+
end
116+
117+
it 'invalid precision raises exception' do
118+
assert_raise(ActiveRecord::ActiveRecordError) do
119+
connection.create_table :time_precisions do |t|
120+
t.time :precision_8, precision: 8
121+
end
122+
end
123+
ensure
124+
connection.drop_table :time_precisions rescue nil
125+
end
126+
end
97127
end

test/cases/coerced_tests.rb

+3
Original file line numberDiff line numberDiff line change
@@ -1832,6 +1832,9 @@ def test_time_precision_is_truncated_on_assignment_coerced
18321832

18331833
# SQL Server uses default precision for time.
18341834
coerce_tests! :test_no_time_precision_isnt_truncated_on_assignment
1835+
1836+
# SQL Server accepts precision of 7 for time.
1837+
coerce_tests! :test_invalid_time_precision_raises_error
18351838
end
18361839

18371840
class DefaultNumbersTest < ActiveRecord::TestCase

0 commit comments

Comments
 (0)