livehelpnow / tds_ecto

TDS Adapter for Ecto
57 stars 34 forks source link

Support for datetime2? #65

Open kevinkoltz opened 6 years ago

kevinkoltz commented 6 years ago

MSSQL's datetime2 has a default microsecond precision of 7. However, Elixir's Calendar only supports a precision of 6: @type microsecond :: {0..999_999, 0..6}

Using the following in migrations works when inserting rows from Ecto: timestamps(inserted_at: :created_at, type: :datetime2)

Schema uses type :utc_datetime: timestamps(inserted_at: :created_at, type: :utc_datetime)

Once a row is inserted outside of Ecto using getutcdate() or in the format '2007-05-02T19:58:47.1234567' with the default precision of 7, Ecto fails to print the date:

iex(13)> record.updated_at
%Inspect.Error{
  message: "got ArgumentError with message \"argument error\" while inspecting %{__struct__: DateTime, calendar: Calendar.ISO, day: 2, hour: 19, microsecond: {1234567, 6}, minute: 58, month: 5, second: 47, std_offset: 0, time_zone: \"Etc/UTC\", utc_offset: 0, year: 2007, zone_abbr: \"UTC\"}"
}
iex(14)> record.updated_at.microsecond
{1234567, 6}

Update:

It looks like Ecto 3.0 will be removing microseconds with :utc_datetime and other types mentioned in their changelog and adding :utc_datetime_usec which will keep them.

mjaric commented 6 years ago

I will see if I can push this while working on v3 updates.