提交 2ae9166d 编写于 作者: A Aaron Patterson

log the statement name along with the SQL

上级 98e00164
......@@ -424,13 +424,14 @@ def close
protected
def log(sql, name = "SQL", binds = [])
def log(sql, name = "SQL", binds = [], statement_name = nil)
@instrumenter.instrument(
"sql.active_record",
:sql => sql,
:name => name,
:connection_id => object_id,
:binds => binds) { yield }
:sql => sql,
:name => name,
:connection_id => object_id,
:statement_name => statement_name,
:binds => binds) { yield }
rescue => e
message = "#{e.class.name}: #{e.message}: #{sql}"
@logger.error message if @logger
......
......@@ -774,7 +774,7 @@ def exec_no_cache(sql, name, binds)
def exec_cache(sql, name, binds)
stmt_key = prepare_statement(sql)
log(sql, name, binds) do
log(sql, name, binds, stmt_key) do
@connection.send_query_prepared(stmt_key, binds.map { |col, val|
type_cast(val, col)
})
......
......@@ -81,6 +81,16 @@ def test_schema_names_logs_name
assert_equal 'SCHEMA', @subscriber.logged[0][1]
end
def test_statement_key_is_logged
bindval = 1
@connection.exec_query('SELECT $1::integer', 'SQL', [[nil, bindval]])
name = @subscriber.payloads.last[:statement_name]
assert name
res = @connection.exec_query("EXPLAIN (FORMAT JSON) EXECUTE #{name}(#{bindval})")
plan = res.column_types['QUERY PLAN'].type_cast res.rows.first.first
assert_operator plan.length, :>, 0
end
# Must have with_manual_interventions set to true for this
# test to run.
# When prompted, restart the PostgreSQL server with the
......
......@@ -121,12 +121,15 @@ def travel_to(time, &block)
class SQLSubscriber
attr_reader :logged
attr_reader :payloads
def initialize
@logged = []
@payloads = []
end
def start(name, id, payload)
@payloads << payload
@logged << [payload[:sql], payload[:name], payload[:binds]]
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册