[PATCH/puppet 1/1] fixes #2518 - This defaults all datetime fields in ActiveRecord to store in UTC vs the puppetmaster localtime

18 views
Skip to first unread message

Ohad Levy

unread,
Sep 16, 2009, 9:13:51 AM9/16/09
to puppe...@googlegroups.com
From 1368df476535169785812c2f6400f84a4f5064bc Mon Sep 17 00:00:00 2001
From: Ohad Levy <ohad...@gmail.com>
Date: Wed, 16 Sep 2009 21:08:23 +0800
Subject: [PATCH/puppet 1/1] fixes #2518 - This defaults all datetime fields in ActiveRecord to store in UTC vs the puppetmaster localtime
Signed-off-by: Ohad Levy <ohad...@gmail.com>

---
 lib/puppet/rails.rb |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

diff --git a/lib/puppet/rails.rb b/lib/puppet/rails.rb
index fc8eacd..c8e9947 100644
--- a/lib/puppet/rails.rb
+++ b/lib/puppet/rails.rb
@@ -22,6 +22,12 @@ module Puppet::Rails
             ActiveRecord::Base.logger.level = Logger::DEBUG
         end
 
+        # Ensure that Active Record uses UTC to store datetime
+        # fixes #2518
+        Time.zone = "UTC"
+        ActiveRecord::Base.time_zone_aware_attributes = true
+        ActiveRecord::Base.default_timezone = "UTC"
+
         ActiveRecord::Base.verify_active_connections!
 
         begin
--
1.6.0.4

Luke Kanies

unread,
Sep 16, 2009, 2:12:57 PM9/16/09
to puppe...@googlegroups.com
How does this affect the rest of the system? It sets the TZ to UTC
for everything, right?

I know we should be doing this anyway, but if so we need to do so as
part of a more obvious effort, rather than just as a Rails fix.
--
One of the Ten Commandments for Technicians:
(7) Work thou not on energized equipment, for if thou dost, thy
fellow workers will surely buy beers for thy widow and
console her in other ways.
---------------------------------------------------------------------
Luke Kanies | http://reductivelabs.com | http://madstop.com

Ohad Levy

unread,
Sep 16, 2009, 9:24:05 PM9/16/09
to puppe...@googlegroups.com
The way I understand the problem is that all time object when saved into a database lose their timezone properties.
this patch, make AR aware of timezones, and ensures that it does the right conversions when storing time objects for example:
if you save last_compile in local time, AR will store it as utc, but when you read it AR will return a time object with local time zone offset.

this is important when you share your database with other applications, where they expect all datetime objects to be in UTC.
This does not change any other behavior of writing time in other places - e.g. your yaml files will still contain the timezone information as they are a dump of Time objects.

Ohad

Luke Kanies

unread,
Sep 16, 2009, 9:25:15 PM9/16/09
to puppe...@googlegroups.com
But you've got 'Time.zone = "UTC"', which is global behaviour, right?
--
At my lemonade stand I used to give the first glass away free and charge
five dollars for the second glass. The refill contained the antidote.
-- Emo Phillips

Ohad Levy

unread,
Sep 16, 2009, 9:46:29 PM9/16/09
to puppe...@googlegroups.com
as far as I can understand from the rails code, that would only be valid to AR, as Time.zone will return ActiveSupport::TimeZone object, but Time.now still returns local time.

Luke Kanies

unread,
Sep 17, 2009, 12:22:52 PM9/17/09
to puppe...@googlegroups.com
Hmm. I just tried this in irb, and I don't even have a 'zone=' method
on Time, even after loading active_record.

Do you know where this method comes from?
--
Barondes' First Law:
Science abhors contradictions; scientists' minds are replete with
them.

Ohad Levy

unread,
Sep 17, 2009, 9:58:07 PM9/17/09
to puppe...@googlegroups.com
As far as I understand, it was introduced in Rails 2.1 (seems to be working on my 2.1 version).

maybe add ::ActiveRecord::VERSION::MINOR >= 1 before the whole statement?

Ohad

Luke Kanies

unread,
Sep 18, 2009, 11:57:08 AM9/18/09
to puppe...@googlegroups.com
We already require 2.1 or better, and I'm running 2.3.something.
--
The secret of being a bore is to tell everything. -- Voltaire

Ohad Levy

unread,
Sep 22, 2009, 5:12:48 AM9/22/09
to puppe...@googlegroups.com
thats strange:

irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'active_record'
=> true
irb(main):003:0> require 'active_record/version'
=> []
irb(main):004:0> ::ActiveRecord::VERSION::MAJOR
=> 2
irb(main):005:0> ::ActiveRecord::VERSION::MINOR
=> 3
irb(main):006:0> Time.zone = "UTC"
=> "UTC"
irb(main):007:0> ActiveRecord::Base.time_zone_aware_attributes = true
=> true
irb(main):008:0> ActiveRecord::Base.default_timezone = "UTC"
=> "UTC"

Luke Kanies

unread,
Sep 22, 2009, 5:37:52 PM9/22/09
to puppe...@googlegroups.com
Sorry, problem with my system and a recent upgrade; you're right.
--
Some people are afraid of heights. I'm afraid of widths.
-- Stephen Wright
Reply all
Reply to author
Forward
0 new messages