Skip to content

Commit

Permalink
Fix precision handling in time migration (#1144)
Browse files Browse the repository at this point in the history
  • Loading branch information
kiyot authored Jan 8, 2024
1 parent 31c5fb9 commit 1bf0e4c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#### Fixed

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

## v7.1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,16 @@ def type_to_sql(type, limit: nil, precision: nil, scale: nil, **)
when 5..8 then "bigint"
else raise(ActiveRecordError, "No integer type has byte size #{limit}. Use a numeric with precision 0 instead.")
end
when "time" # https://learn.microsoft.com/en-us/sql/t-sql/data-types/time-transact-sql
column_type_sql = type.to_s
if precision
if (0..7) === precision
column_type_sql << "(#{precision})"
else
raise(ActiveRecordError, "The time type has precision of #{precision}. The allowed range of precision is from 0 to 7")
end
end
column_type_sql
when "datetime2"
column_type_sql = super
if precision
Expand All @@ -319,7 +329,7 @@ def type_to_sql(type, limit: nil, precision: nil, scale: nil, **)
if (0..7) === precision
column_type_sql << "(#{precision})"
else
raise(ActiveRecordError, "The datetimeoffset type has precision of #{precision}. The allowed range of precision is from 0 to 7.")
raise(ActiveRecordError, "The datetimeoffset type has precision of #{precision}. The allowed range of precision is from 0 to 7")
end
end
column_type_sql
Expand Down
30 changes: 30 additions & 0 deletions test/cases/active_schema_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,34 @@ class ActiveSchemaTestSQLServer < ActiveRecord::TestCase
connection.drop_table :datetimeoffset_precisions rescue nil
end
end

describe 'time precision' do
it 'valid precisions are correct' do
assert_nothing_raised do
connection.create_table :time_precisions do |t|
t.time :precision_default
t.time :precision_5, precision: 5
t.time :precision_7, precision: 7
end
end

columns = connection.columns("time_precisions")

assert_equal columns.find { |column| column.name == "precision_default" }.precision, 7
assert_equal columns.find { |column| column.name == "precision_5" }.precision, 5
assert_equal columns.find { |column| column.name == "precision_7" }.precision, 7
ensure
connection.drop_table :time_precisions rescue nil
end

it 'invalid precision raises exception' do
assert_raise(ActiveRecord::ActiveRecordError) do
connection.create_table :time_precisions do |t|
t.time :precision_8, precision: 8
end
end
ensure
connection.drop_table :time_precisions rescue nil
end
end
end
3 changes: 3 additions & 0 deletions test/cases/coerced_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1832,6 +1832,9 @@ def test_time_precision_is_truncated_on_assignment_coerced

# SQL Server uses default precision for time.
coerce_tests! :test_no_time_precision_isnt_truncated_on_assignment

# SQL Server accepts precision of 7 for time.
coerce_tests! :test_invalid_time_precision_raises_error
end

class DefaultNumbersTest < ActiveRecord::TestCase
Expand Down

0 comments on commit 1bf0e4c

Please sign in to comment.