diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-10-04 16:51:35 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-10-04 16:51:35 -0700 |
commit | 2ae9166d8fef242e3b0d52af0c8c79d86a80268e (patch) | |
tree | be5adbbf0ba449a7d99ed2913a640f2ff4da1d40 | |
parent | 98e001641ff0f859349cd60d270fec972edc16de (diff) | |
download | rails-2ae9166d8fef242e3b0d52af0c8c79d86a80268e.tar.gz rails-2ae9166d8fef242e3b0d52af0c8c79d86a80268e.tar.bz2 rails-2ae9166d8fef242e3b0d52af0c8c79d86a80268e.zip |
log the statement name along with the SQL
4 files changed, 20 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 1e1a7323ee..cbe563676b 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -424,13 +424,14 @@ module ActiveRecord 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 diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 71a70e5c48..0c4d005e63 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -774,7 +774,7 @@ module ActiveRecord 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) }) diff --git a/activerecord/test/cases/adapters/postgresql/connection_test.rb b/activerecord/test/cases/adapters/postgresql/connection_test.rb index 59d95d1293..81aa977c59 100644 --- a/activerecord/test/cases/adapters/postgresql/connection_test.rb +++ b/activerecord/test/cases/adapters/postgresql/connection_test.rb @@ -81,6 +81,16 @@ module ActiveRecord 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 diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index 6bcbf9c3cc..739e2b2f19 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -121,12 +121,15 @@ end 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 |