activerecord-import icon indicating copy to clipboard operation
activerecord-import copied to clipboard

Datetime field in "returning" sends back string and not casted ActiveSupport::TimeWithZone value.

Open skunkworker opened this issue 3 years ago • 1 comments

I'm using 1.0.5 with Postgres 12.3.

user_hashes = {name: "John", verified_at: Time.now }
outcome = User.import( user_hashes, validate: false, returning: [:name, :verified_at] )
puts outcome.results =>
  [ 1, "John", "2020-07-29 11:19:57.111111" ]

When returning a datetime field in the :returning block, the field will come back as a string and not as a casted date.

I've got an additional method that calls ::User.type_for_attribute(:verified_at).cast(value) which returns an ActiveSupport::TimeWithZone object but I am unsure if this is intentional behavior or just a bug, the rest of the columns appear to be properly cast though.

skunkworker avatar Jul 29 '20 17:07 skunkworker

No typecasting is done on the returning values within ActiveRecord-Import. If you would like to investigate adding support for that, I think that would be a nice feature...but I also don't think it's completely unreasonable require casting/parsing the returning values either.

Have you done any benchmarks to see how parsing compares to casting in your example?

DateTime.parse(value).in_time_zone

vs.

User.type_for_attribute(:verified_at).cast(value)

Here is where I would start looking: https://github.com/zdennis/activerecord-import/blob/900aace6fb983cf80b14cbad2b44c64848d24822/lib/activerecord-import/adapters/postgresql_adapter.rb#L55

jkowens avatar Sep 24 '20 18:09 jkowens