Skip to content
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ If you do not want to rewrite default `#to_sql` method you may specify
You can also disable log formatting by specifying `PpSql.add_rails_logger_formatting=false`
in initializers.

Formatting the sql messages can add significant overhead to the schema dump portion of the database migration process. This overhead can be avoided by setting `PpSql.disable_for_db_tasks = true`. Alternatively, run time opt out can be done by setting `PPSQL_DISABLE=1`, such as `PPSQL_DISABLE=1 rails db:migrate`.

### Add to Application record

I found usefull this trick:
Expand Down
20 changes: 16 additions & 4 deletions lib/pp_sql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ module PpSql
# if you do not want to rewrite AR native method #to_sql
# you may switch this setting to false in initializer
class << self
attr_accessor :rewrite_to_sql_method, :add_rails_logger_formatting
attr_accessor :rewrite_to_sql_method, :add_rails_logger_formatting, :disable_for_db_tasks

def enabled_for?(action)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a bit odd, since with this lately u'r wrapping the boolean settings with another one. Seems to be excessive

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I was originally using a disabled_ accessor the logic was feeling hard to read (if enabled && !ENV[disabled]) so pulling it into a single method felt cleaner. However, based on your other suggestions the disabled ENV can go away so I'll remove this method.

return false if ENV['PPSQL_DISABLED']

send(action)
end
end
self.rewrite_to_sql_method = true
self.add_rails_logger_formatting = true
self.disable_for_db_tasks = true
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  self.enable_for_rails_rake_db_tasks = ENV['PPSQL_ENABLE_FOR_RAILS_RAKE_DB_TASKS']

Copy link
Copy Markdown
Author

@bschrag620 bschrag620 Aug 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From testing, something I didn't quite realize is that this file and the rake tasks are loaded before any of the local project files/initializers are read. This means that setting enable_for_rails_rake_db_tasks within the project won't have any effect because the rake task loading will have already been executed (or skipped). In other words, with the current changes, the only way to opt into formatting is by usage of the ENV var. Is that ok?


module Formatter
private
Expand All @@ -28,7 +35,7 @@ def _sql_formatter

module ToSqlBeautify
def to_sql
if ::PpSql.rewrite_to_sql_method
if ::PpSql.enabled_for?(:rewrite_to_sql_method)
extend Formatter
_sql_formatter.format(defined?(super) ? super.dup : dup)
else
Expand All @@ -37,7 +44,7 @@ def to_sql
end

def pp_sql
if ::PpSql.rewrite_to_sql_method
if ::PpSql.enabled_for?(:rewrite_to_sql_method)
puts to_sql
else
extend Formatter
Expand All @@ -50,7 +57,7 @@ module LogSubscriberPrettyPrint
include Formatter

def sql(event)
return super unless ::PpSql.add_rails_logger_formatting
return super unless ::PpSql.enabled_for?(:add_rails_logger_formatting)

e = event.dup
e.payload[:sql] = _sql_formatter.format(e.payload[:sql].dup)
Expand All @@ -66,6 +73,11 @@ class Railtie < Rails::Railtie
ActiveRecord::LogSubscriber.prepend LogSubscriberPrettyPrint
end
end

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

      rake_tasks do
        path = File.expand_path(__dir__)
        load("#{path}/pp_sql/tasks/db_hooks.rake") unless PpSql.enable_for_rails_rake_db_tasks
     end

rake_tasks do
path = File.expand_path(__dir__)
Dir.glob("#{path}/pp_sql/tasks/**/*.rake").each { |f| load f }
end
end
end
end
15 changes: 15 additions & 0 deletions lib/pp_sql/tasks/db_hooks.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

namespace :pp_sql do
desc 'Hook that is ran before rails `db:*` tasks that can disable pp_sql'

task :disable_during_db_tasks do
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  task :disable_during_db_tasks do
    PpSql.add_rails_logger_formatting = false
    PpSql.rewrite_to_sql_method = false
  end
end

if PpSql.disable_for_db_tasks
PpSql.add_rails_logger_formatting = false
PpSql.rewrite_to_sql_method = false
end
end
end

db_tasks = Rake::Task.tasks.select { |task| task.name.starts_with?('db:') }
db_tasks.each { |task| task.enhance(['pp_sql:disable_during_db_tasks']) }
10 changes: 10 additions & 0 deletions test/pp_sql_rails_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ class User < ActiveRecord::Base; end
assert_equal LOGGER.string.lines.count, 6
end

it 'can be disabled with an env var' do
old_env = ENV.to_hash
ENV.update('PPSQL_DISABLED' => '1')
User.create
clear_logs!
User.first
assert_equal 1, LOGGER.string.lines.count
ENV.replace old_env
end

it 'Rails & ActiveRecord with default output' do
PpSql.add_rails_logger_formatting = false
User.create
Expand Down
7 changes: 7 additions & 0 deletions test/pp_sql_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ class SubString < String
assert_equal str.to_sql.lines.count, 1
end

it 'can be disabled with env var' do
old_env = ENV.to_hash
ENV.update('PPSQL_DISABLED' => '1')
assert_equal 1, str.to_sql.lines.count
ENV.replace old_env
end

it 'formats and prints with pp_sql' do
out, = capture_io do
str.pp_sql
Expand Down