Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ 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.

## Disable during `db:migrate`

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_during_db_migrate = true`.

### Add to Application record

I found usefull this trick:
Expand Down
8 changes: 7 additions & 1 deletion lib/pp_sql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ 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_during_db_migrate
end
self.rewrite_to_sql_method = true
self.add_rails_logger_formatting = true
self.disable_during_db_migrate = false

module Formatter
private
Expand Down Expand Up @@ -66,6 +67,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
14 changes: 14 additions & 0 deletions lib/pp_sql/tasks/db_hooks.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

namespace :pp_sql do
desc 'Hook that is ran before rails db:migrate that will disable pp_sql when disable_during_db_migrate is set to true'

task :disable_during_db_migrate do
if PpSql.disable_during_db_migrate
PpSql.add_rails_logger_formatting = false
PpSql.rewrite_to_sql_method = false
end
end
end

Rake::Task['db:migrate'].enhance(['pp_sql:disable_during_db_migrate'])