From 5b75424a7b16f4e2cdbf60afaa907ac7bdb2e566 Mon Sep 17 00:00:00 2001 From: Yasuo Honda Date: Thu, 7 May 2026 17:46:23 +0900 Subject: [PATCH 1/2] 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). --- Gemfile | 1 + spec/support/sql_logger.rb | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 spec/support/sql_logger.rb diff --git a/Gemfile b/Gemfile index 85d1363..58fe003 100644 --- a/Gemfile +++ b/Gemfile @@ -14,6 +14,7 @@ end group :test, :development do gem "rake", ">= 10.0" gem "rspec", "~> 3.1" + gem "logger" unless ENV["NO_ACTIVERECORD"] gem "activerecord", github: "rails/rails", branch: "main" diff --git a/spec/support/sql_logger.rb b/spec/support/sql_logger.rb new file mode 100644 index 0000000..51b2521 --- /dev/null +++ b/spec/support/sql_logger.rb @@ -0,0 +1,35 @@ +# Enable with: PLSQL_DEBUG_LOG=debug.log bundle exec rspec +# Logger args mirror activerecord-oracle_enhanced-adapter's spec logger +# (shift_age, shift_size) so debug.log behaves the same way. +if (log_path = ENV["PLSQL_DEBUG_LOG"]) && !log_path.empty? + require "logger" + + PLSQL_DEBUG_LOGGER = Logger.new(log_path, 0, 100 * 1024 * 1024) + PLSQL_DEBUG_LOGGER.formatter = ->(_sev, time, _prog, msg) { + "#{time.iso8601(6)} #{msg}\n" + } + + module PLSQLSQLLogger + def exec(sql, *bindvars) + PLSQL_DEBUG_LOGGER.info("EXEC #{sql.strip}#{bindvars.empty? ? '' : " BINDS=#{bindvars.inspect}"}") + super + end + + def cursor_from_query(sql, bindvars = [], options = {}) + PLSQL_DEBUG_LOGGER.info("QUERY #{sql.strip}#{bindvars.empty? ? '' : " BINDS=#{bindvars.inspect}"}") + super + end + + def parse(sql) + PLSQL_DEBUG_LOGGER.info("PARSE #{sql.strip}") + super + end + end + + PLSQL::OCIConnection.prepend(PLSQLSQLLogger) if defined?(PLSQL::OCIConnection) + PLSQL::JDBCConnection.prepend(PLSQLSQLLogger) if defined?(PLSQL::JDBCConnection) + + if defined?(ActiveRecord::Base) + ActiveRecord::Base.logger = PLSQL_DEBUG_LOGGER + end +end From 48cf4b421c1df0920de8a86682234fe45b7b9923 Mon Sep 17 00:00:00 2001 From: Yasuo Honda Date: Thu, 7 May 2026 17:49:18 +0900 Subject: [PATCH 2/2] Always write debug.log during spec runs Match the activerecord-oracle_enhanced-adapter convention of writing spec SQL to debug.log unconditionally. The previous PLSQL_DEBUG_LOG env var gate is removed so debug.log is always available when tests fail, and debug.log is added to .gitignore. --- .gitignore | 1 + spec/support/sql_logger.rb | 48 ++++++++++++++++++-------------------- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index 1d6ff5e..8aeed57 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ pkg log tmp sqlnet.log +debug.log Gemfile.lock *.zip .idea diff --git a/spec/support/sql_logger.rb b/spec/support/sql_logger.rb index 51b2521..59bbbaf 100644 --- a/spec/support/sql_logger.rb +++ b/spec/support/sql_logger.rb @@ -1,35 +1,33 @@ -# Enable with: PLSQL_DEBUG_LOG=debug.log bundle exec rspec +# Always write SQL executed during the spec run to debug.log. # Logger args mirror activerecord-oracle_enhanced-adapter's spec logger # (shift_age, shift_size) so debug.log behaves the same way. -if (log_path = ENV["PLSQL_DEBUG_LOG"]) && !log_path.empty? - require "logger" +require "logger" - PLSQL_DEBUG_LOGGER = Logger.new(log_path, 0, 100 * 1024 * 1024) - PLSQL_DEBUG_LOGGER.formatter = ->(_sev, time, _prog, msg) { - "#{time.iso8601(6)} #{msg}\n" - } +PLSQL_DEBUG_LOGGER = Logger.new("debug.log", 0, 100 * 1024 * 1024) +PLSQL_DEBUG_LOGGER.formatter = ->(_sev, time, _prog, msg) { + "#{time.iso8601(6)} #{msg}\n" +} - module PLSQLSQLLogger - def exec(sql, *bindvars) - PLSQL_DEBUG_LOGGER.info("EXEC #{sql.strip}#{bindvars.empty? ? '' : " BINDS=#{bindvars.inspect}"}") - super - end +module PLSQLSQLLogger + def exec(sql, *bindvars) + PLSQL_DEBUG_LOGGER.info("EXEC #{sql.strip}#{bindvars.empty? ? '' : " BINDS=#{bindvars.inspect}"}") + super + end - def cursor_from_query(sql, bindvars = [], options = {}) - PLSQL_DEBUG_LOGGER.info("QUERY #{sql.strip}#{bindvars.empty? ? '' : " BINDS=#{bindvars.inspect}"}") - super - end + def cursor_from_query(sql, bindvars = [], options = {}) + PLSQL_DEBUG_LOGGER.info("QUERY #{sql.strip}#{bindvars.empty? ? '' : " BINDS=#{bindvars.inspect}"}") + super + end - def parse(sql) - PLSQL_DEBUG_LOGGER.info("PARSE #{sql.strip}") - super - end + def parse(sql) + PLSQL_DEBUG_LOGGER.info("PARSE #{sql.strip}") + super end +end - PLSQL::OCIConnection.prepend(PLSQLSQLLogger) if defined?(PLSQL::OCIConnection) - PLSQL::JDBCConnection.prepend(PLSQLSQLLogger) if defined?(PLSQL::JDBCConnection) +PLSQL::OCIConnection.prepend(PLSQLSQLLogger) if defined?(PLSQL::OCIConnection) +PLSQL::JDBCConnection.prepend(PLSQLSQLLogger) if defined?(PLSQL::JDBCConnection) - if defined?(ActiveRecord::Base) - ActiveRecord::Base.logger = PLSQL_DEBUG_LOGGER - end +if defined?(ActiveRecord::Base) + ActiveRecord::Base.logger = PLSQL_DEBUG_LOGGER end