Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTime is not set to UTC on seed's create command #111

Open
andrewchen5678 opened this issue Aug 19, 2016 · 3 comments
Open

DateTime is not set to UTC on seed's create command #111

andrewchen5678 opened this issue Aug 19, 2016 · 3 comments

Comments

@andrewchen5678
Copy link

Hi, when I export data with DateTime in postgresql, and import again, the resulting timestamp gets shifted by the timezone offset.
For example, I have an object in the database with last_accessed set to "2016-08-12 02:20:20" in the database, which is UTC timezone, when I run puts SeedDump.dump(SessionUser.order(id: :desc)), it becomes this:

SessionUser.create!([
  {last_accessed: "2016-08-12 02:20:20", visitor_tag: "57aa76e706715e3bc6f3d2c3", using_promo_code: nil}
])

If I run this to seed data, it becomes last_accessed "2016-08-12 09:20:20" instead in the database when I run it on a machine with pacific timezone.

@kofronpi
Copy link
Collaborator

kofronpi commented May 4, 2017

Hi @andrewchen5678 . If your datetime does not store timezone, should seed_dump know about it ?

@silva96
Copy link

silva96 commented Jul 11, 2018

How did you solve this bug? When I do the SeedDump.dump it writes the file with the UTC time, without timezone!

So when loading it back to the database my app believes they are in app's timezone instead of utc

Example:

my timezone is America/Santiago (UTC-4)

if it is 1 PM in santiago, it is 5 pm in UTC

When I Dump it, the date will become 5pm on the file. (which is UTC, but no timezone explicitly set on the file)

When importing it, will be inserted as 5pm America santiago which will be 9pm UTC

I think the export to the file should include the timezone

Like this:

2018-07-11T13:00:00.000-04:00

@dimitrypo
Copy link

I've experienced the same issue and managed to fix it:

You dump records, load seeds, dump again - ALL dates shifted by time zone.
Here is the monkey-patch you can use if you have the same issue:

    class SeedDump
      module DumpMethods
        def value_to_s(value)
          value = case value
          when BigDecimal, IPAddr
            value.to_s
          when Date, Time, DateTime
            value.to_s(:iso8601)
          when Range
            range_to_string(value)
          when ->(v) { v.class.ancestors.map(&:to_s).include?('RGeo::Feature::Instance') }
            value.to_s
          else
            value
          end

          value.inspect
        end
      end
    end

Note key part is: value.to_s(:iso8601)
This way it includes time zone information in a dump

Just place it into config/initializers/seeds_dump.rb

I would've submitted PR but it looks like the project is abandoned (but still works), so no use in doing that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants