Skip to content

Commit

Permalink
PostgreSQL: Ensure the database time zone matches Ruby's time zone
Browse files Browse the repository at this point in the history
This restores bf6661c (reverts 7a7c608).

If `AR::Base.default_timezone = :local`, incorrect value will be saved
unless the database time zone matches Ruby's time zone.
  • Loading branch information
kamipo committed Aug 21, 2017
1 parent 1b21d95 commit f19295b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,6 @@ def initialize(connection, logger, connection_parameters, config)
super(connection, logger, config)

@connection_parameters = connection_parameters

# @local_tz is initialized as nil to avoid warnings when connect tries to use it
@local_tz = nil
@max_identifier_length = nil

connect
Expand All @@ -219,7 +216,6 @@ def initialize(connection, logger, connection_parameters, config)

@type_map = Type::HashLookupTypeMap.new
initialize_type_map
@local_tz = execute("SHOW TIME ZONE", "SCHEMA").first["TimeZone"]
@use_insert_returning = @config.key?(:insert_returning) ? self.class.type_cast_config_to_boolean(@config[:insert_returning]) : true
end

Expand Down Expand Up @@ -695,8 +691,9 @@ def configure_connection
unless variables["timezone"]
if ActiveRecord::Base.default_timezone == :utc
variables["timezone"] = "UTC"
elsif @local_tz
variables["timezone"] = @local_tz
else
offset = Time.now.utc_offset / 3600.0
variables["timezone"] = offset
end
end

Expand Down
22 changes: 22 additions & 0 deletions activerecord/test/cases/adapters/postgresql/timestamp_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,28 @@ def test_timestamp_with_zone_values_without_rails_time_zone_support
ensure
@connection.reconnect!
end

def test_timestamp_with_zone_values_with_local_timezone
time = Time.now.utc.change(usec: 0)
timestamp = nil

with_timezone_config default: :local do
with_env_tz "America/New_York" do
@connection.reconnect!

timestamp = PostgresqlTimestampWithZone.create!(id: 2, time: time)
timestamp.reload
assert_equal time, timestamp.time
end
end

@connection.reconnect!

timestamp.reload
assert_equal time, timestamp.time
ensure
@connection.reconnect!
end
end

class PostgresqlTimestampFixtureTest < ActiveRecord::PostgreSQLTestCase
Expand Down

0 comments on commit f19295b

Please sign in to comment.