Skip to content

Commit

Permalink
Prevent extra SET time zone in configure_connection (rails#28413)
Browse files Browse the repository at this point in the history
`SET time zone 'value'` is an alias for `SET timezone TO 'value'`.

https://www.postgresql.org/docs/current/static/sql-set.html

So if `variables["timezone"]` is specified, it is enough to
`SET timezone` once.
  • Loading branch information
kamipo authored Aug 21, 2017
1 parent 97d3f54 commit 1b21d95
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -688,18 +688,20 @@ def configure_connection
# Use standard-conforming strings so we don't have to do the E'...' dance.
set_standard_conforming_strings

variables = @config.fetch(:variables, {}).stringify_keys

# If using Active Record's time zone support configure the connection to return
# TIMESTAMP WITH ZONE types in UTC.
# (SET TIME ZONE does not use an equals sign like other SET variables)
if ActiveRecord::Base.default_timezone == :utc
execute("SET time zone 'UTC'", "SCHEMA")
elsif @local_tz
execute("SET time zone '#{@local_tz}'", "SCHEMA")
unless variables["timezone"]
if ActiveRecord::Base.default_timezone == :utc
variables["timezone"] = "UTC"
elsif @local_tz
variables["timezone"] = @local_tz
end
end

# SET statements from :variables config hash
# https://www.postgresql.org/docs/current/static/sql-set.html
variables = @config[:variables] || {}
variables.map do |k, v|
if v == ":default" || v == :default
# Sets the value to the global or compile default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,13 @@ def test_set_session_variable_default
end
end

def test_set_session_timezone
run_without_connection do |orig_connection|
ActiveRecord::Base.establish_connection(orig_connection.deep_merge(variables: { timezone: "America/New_York" }))
assert_equal "America/New_York", ActiveRecord::Base.connection.query_value("SHOW TIME ZONE")
end
end

def test_get_and_release_advisory_lock
lock_id = 5295901941911233559
list_advisory_locks = <<-SQL
Expand Down

0 comments on commit 1b21d95

Please sign in to comment.