Permalink
Browse files

PostgreSQLAdapter: set time_zone to UTC when Base.default_timezone ==…

… :utc so that Postgres doesn't incorrectly offset-adjust values inserted into TIMESTAMP WITH TIME ZONE columns [#3777 state:resolved]
  • Loading branch information...
1 parent 64f8c87 commit c5b652f3d25ef92ae0f67551464fb03bda6aeb4f @jackc jackc committed with gbuesing Jan 24, 2010
View
@@ -1,5 +1,7 @@
*Edge*
+* PostgreSQLAdapter: set time_zone to UTC when Base.default_timezone == :utc so that Postgres doesn't incorrectly offset-adjust values inserted into TIMESTAMP WITH TIME ZONE columns. #3777 [Jack Christensen]
+
* Allow relations to be used as scope.
class Item
@@ -998,7 +998,7 @@ def extract_precision(sql_type) # def extract_precision(sql_type)
configure_connection
end
- # Configures the encoding, verbosity, and schema search path of the connection.
+ # Configures the encoding, verbosity, schema search path, and time zone of the connection.
# This is called by #connect and should not be called manually.
def configure_connection
if @config[:encoding]
@@ -1010,6 +1010,10 @@ def configure_connection
end
self.client_min_messages = @config[:min_messages] if @config[:min_messages]
self.schema_search_path = @config[:schema_search_path] || @config[:schema_order]
+
+ # If using ActiveRecord's time zone support configure the connection to return
+ # TIMESTAMP WITH ZONE types in UTC.
+ execute("SET time zone 'UTC'") if ActiveRecord::Base.default_timezone == :utc
end
# Returns the current ID of a table's sequence.
@@ -21,6 +21,9 @@ class PostgresqlBitString < ActiveRecord::Base
class PostgresqlOid < ActiveRecord::Base
end
+class PostgresqlTimestampWithZone < ActiveRecord::Base
+end
+
class PostgresqlDataTypeTest < ActiveRecord::TestCase
self.use_transactional_fixtures = false
@@ -50,6 +53,8 @@ def setup
@connection.execute("INSERT INTO postgresql_oids (obj_id) VALUES (1234)")
@first_oid = PostgresqlOid.find(1)
+
+ @connection.execute("INSERT INTO postgresql_timestamp_with_zones (time) VALUES ('2010-01-01 10:00:00-1')")
end
def test_data_type_of_array_types
@@ -201,4 +206,38 @@ def test_update_oid
assert @first_oid.reload
assert_equal @first_oid.obj_id, new_value
end
+
+ def test_timestamp_with_zone_values_with_rails_time_zone_support
+ old_tz = ActiveRecord::Base.time_zone_aware_attributes
+ old_default_tz = ActiveRecord::Base.default_timezone
+
+ ActiveRecord::Base.time_zone_aware_attributes = true
+ ActiveRecord::Base.default_timezone = :utc
+
+ @connection.reconnect!
+
+ @first_timestamp_with_zone = PostgresqlTimestampWithZone.find(1)
+ assert_equal Time.utc(2010,1,1, 11,0,0), @first_timestamp_with_zone.time
+ ensure
+ ActiveRecord::Base.default_timezone = old_default_tz
+ ActiveRecord::Base.time_zone_aware_attributes = old_tz
+ @connection.reconnect!
+ end
+
+ def test_timestamp_with_zone_values_without_rails_time_zone_support
+ old_tz = ActiveRecord::Base.time_zone_aware_attributes
+ old_default_tz = ActiveRecord::Base.default_timezone
+
+ ActiveRecord::Base.time_zone_aware_attributes = false
+ ActiveRecord::Base.default_timezone = :local
+
+ @connection.reconnect!
+
+ @first_timestamp_with_zone = PostgresqlTimestampWithZone.find(1)
+ assert_equal Time.utc(2010,1,1, 11,0,0), @first_timestamp_with_zone.time
+ ensure
+ ActiveRecord::Base.default_timezone = old_default_tz
+ ActiveRecord::Base.time_zone_aware_attributes = old_tz
+ @connection.reconnect!
+ end
end
@@ -1,7 +1,7 @@
ActiveRecord::Schema.define do
%w(postgresql_arrays postgresql_moneys postgresql_numbers postgresql_times postgresql_network_addresses postgresql_bit_strings
- postgresql_oids postgresql_xml_data_type defaults geometrics).each do |table_name|
+ postgresql_oids postgresql_xml_data_type defaults geometrics postgresql_timestamp_with_zones).each do |table_name|
execute "DROP TABLE IF EXISTS #{quote_table_name table_name}"
end
@@ -100,6 +100,13 @@
obj_id OID
);
_SQL
+
+ execute <<_SQL
+ CREATE TABLE postgresql_timestamp_with_zones (
+ id SERIAL PRIMARY KEY,
+ time TIMESTAMP WITH TIME ZONE
+ );
+_SQL
begin
execute <<_SQL

0 comments on commit c5b652f

Please sign in to comment.