提交 1b21d951 编写于 作者: R Ryuta Kamizono 提交者: GitHub

Prevent extra `SET time zone` in `configure_connection` (#28413)

`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.
上级 97d3f547
......@@ -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
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册