8

How can you suppress the output of db:load:schema? Running

bundle exec rake db:schema:load

with the -s, -q, or even VERBOSE=false options makes no difference in the output; the same "create_table... add_index..." garbage that I don't want to see appears. I'm invoking this from inside a custom Rake task and I don't want the user to see all of this every time.

UPDATE:

I solved the problem with some guidance from @Deefour by using:

system "bundle exec rake db:schema:load -s RAILS_ENV=#{Rails.env} >NUL"

>NUL is for Windows machines, Unix-based can use > /dev/null.

rather than

Rake::Task['db:schema:load'].invoke

as I had been doing in my custom task. Note that this solution is specific to Windows machines. For Unix-based machines I imagine you should be able to use the accepted solution below.

Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
jake
  • 1,661
  • 1
  • 21
  • 30

2 Answers2

27

Here is a cleaner solution that works cross-system:

silence_stream(STDOUT) do
  # anything written to STDOUT here will be silenced
  Rake::Task["db:schema:load"].invoke
end

also

quietly do
  # anything written to STDOUT or STDERR here will be silenced
  Rake::Task["db:schema:load"].invoke
end

I prefer silence_stream(STDOUT) toquietly because it will still allow error messages written to STDERR to be shown, which will be helpful when the rake command starts to act up.

References: silence_stream, silence_warnings, & quietly

lightswitch05
  • 9,058
  • 7
  • 52
  • 75
4

Instead of calling the task with Rake::Task['...'].invoke, you can run the command in a subshell, redirecting output to /dev/null.

system "bundle exec rake db:schema:load > /dev/null 2>&1"
deefour
  • 34,974
  • 7
  • 97
  • 90
  • When I did that, instead of the output of db:schema:load, it displayed "The process cannot access the file because it is being used by another process", and `db:schema:load` did not run. – jake Aug 22 '12 at 20:02
  • Can you add to your Question the rest of the rake task that the `db:schema:load` is executing within? – deefour Aug 22 '12 at 20:06
  • Reading up on `/dev/null`, I'm realizing it might be helpful for you to know I'm on a Windows machine, not Unix-based. :P – jake Aug 22 '12 at 20:42
  • I solved my problem by using `system "bundle exec rake db:schema:load -s RAILS_ENV=#{Rails.env} >NUL`, but your answer led me in the right direction, so I marked it correct. – jake Aug 22 '12 at 20:49