Skip to content

Commit 5b75424

Browse files
committed
Add optional SQL logging spec helper
Set PLSQL_DEBUG_LOG=debug.log when running rspec to capture every SQL statement issued by PLSQL::OCIConnection / PLSQL::JDBCConnection (exec, cursor_from_query, parse) plus any SQL emitted via ActiveRecord::Base when specs use plsql.activerecord_class. Logger arguments mirror those used in the activerecord-oracle_enhanced-adapter spec helper. logger is added to the Gemfile because it is a bundled gem on Ruby 3.5+ and would otherwise be unavailable when NO_ACTIVERECORD=1 (which skips activerecord, the gem that pulls logger in transitively).
1 parent 076bc16 commit 5b75424

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ end
1414
group :test, :development do
1515
gem "rake", ">= 10.0"
1616
gem "rspec", "~> 3.1"
17+
gem "logger"
1718

1819
unless ENV["NO_ACTIVERECORD"]
1920
gem "activerecord", github: "rails/rails", branch: "main"

spec/support/sql_logger.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Enable with: PLSQL_DEBUG_LOG=debug.log bundle exec rspec
2+
# Logger args mirror activerecord-oracle_enhanced-adapter's spec logger
3+
# (shift_age, shift_size) so debug.log behaves the same way.
4+
if (log_path = ENV["PLSQL_DEBUG_LOG"]) && !log_path.empty?
5+
require "logger"
6+
7+
PLSQL_DEBUG_LOGGER = Logger.new(log_path, 0, 100 * 1024 * 1024)
8+
PLSQL_DEBUG_LOGGER.formatter = ->(_sev, time, _prog, msg) {
9+
"#{time.iso8601(6)} #{msg}\n"
10+
}
11+
12+
module PLSQLSQLLogger
13+
def exec(sql, *bindvars)
14+
PLSQL_DEBUG_LOGGER.info("EXEC #{sql.strip}#{bindvars.empty? ? '' : " BINDS=#{bindvars.inspect}"}")
15+
super
16+
end
17+
18+
def cursor_from_query(sql, bindvars = [], options = {})
19+
PLSQL_DEBUG_LOGGER.info("QUERY #{sql.strip}#{bindvars.empty? ? '' : " BINDS=#{bindvars.inspect}"}")
20+
super
21+
end
22+
23+
def parse(sql)
24+
PLSQL_DEBUG_LOGGER.info("PARSE #{sql.strip}")
25+
super
26+
end
27+
end
28+
29+
PLSQL::OCIConnection.prepend(PLSQLSQLLogger) if defined?(PLSQL::OCIConnection)
30+
PLSQL::JDBCConnection.prepend(PLSQLSQLLogger) if defined?(PLSQL::JDBCConnection)
31+
32+
if defined?(ActiveRecord::Base)
33+
ActiveRecord::Base.logger = PLSQL_DEBUG_LOGGER
34+
end
35+
end

0 commit comments

Comments
 (0)