Skip to content

Error with datetime2 with precision of 0, and UTC Time object with fractional seconds >= 0.5 #465

@alawton

Description

@alawton

I ran into this issue while migrating over a mysql database over to Azure SQL using SSMA. The migration set a datatype of datetime2(0) for created_at and updated_at columns (and other datetime values as well).

When trying to update or insert new data, I would sometimes get an ArgumentError: argument out of range error. I was able to track it down to how cast_fractional was working with precision for Time objects.

In time_value_fractional.rb, the TimeValueFractional2 fractional_scale parameter is set to "precision". in cases where we have datetime2(0), the precision is set to 0.

In cast_fractional(value), when calculating the second:

seconds = ((seconds * (1 / fractional_precision)).round / (1 / fractional_precision)).round(fractional_scale)

when fractional_scale = 0, and the fractional seconds are >= 0.5, that will round up to 1, which, ends up trying to change the :nsec property to 1000000000, which is invalid.

To test, set a column in the sql database to datetime2(0), and create a new activerecordobject with a fractional second that will round up to 1. example:

        value = Time.utc(2016,4,19,16,45,40,771036)
        user = User.new({
            created_at: value
        })

this will cause an ArgumentInvalid exception to be thrown

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions