jruby / activerecord-jdbc-adapter

JRuby's ActiveRecord adapter using JDBC.
BSD 2-Clause "Simplified" License
462 stars 385 forks source link

Datetime columns wrong cast to Time with MySQL adapter #1064

Closed felipefava closed 4 years ago

felipefava commented 4 years ago

I have a non Rails app where we recently upgraded from activerecord 4 to 5, and we start having this issue:

jruby-9.2.7.0 :018 > ActiveRecord::Base.default_timezone
 => :utc

jruby-9.2.7.0 :013 > date = Time.now
 => 2020-05-08 16:37:01 -0300

jruby-9.2.7.0 :016 > Term.create(version: 'some-value', created_at: date)

D, [2020-05-08T16:37:31.440094 #62090] DEBUG -- :    (79.4ms)  BEGIN TRANSACTION
D, [2020-05-08T16:37:31.561324 #62090] DEBUG -- :   Term Create (81.9ms)  INSERT INTO `terms` (`version`, `created_at`) VALUES ('some-value', '2020-05-08 19:37:01')
D, [2020-05-08T16:37:31.725987 #62090] DEBUG -- :    (162.3ms)  COMMIT TRANSACTION

=> #<Term id: 14, version: "some-value", created_at: "2020-05-08 19:37:01">

jruby-9.2.7.0 :017 > Term.last.created_at
 => 2020-05-08 16:37:01 UTC

The expected created_at value should be 2020-05-08 19:37:01 UTC or at least 2020-05-08 16:37:01 -0300.

I started debugging inside activerecord and activemodel gems before I finally reached here. I saw that ArJdbc::Abstract::DatabaseStatements.exec_query method already returns the column created_at as a Time in UTC but with the wrong time.

FYI: I tested the same example with activerecord 4 and activerecord-jdbcmysql-adapter 1.3.25 and the result was the expected.

Versions jruby: 9.2.7.0 activerecord-jdbcmysql-adapter (52.4-java) activerecord-jdbc-adapter (= 52.4) jdbc-mysql (~> 5.1.36, < 9) -> jdbc-mysql (5.1.47)

felipefava commented 4 years ago

I'm an idiot, I just read the README and this fix my problem

  properties:
    serverTimezone: <%= java.util.TimeZone.getDefault.getID %>