抑制 Rake 任务 db:schema:load 中的输出 [英] Suppress Output in Rake Task db:schema:load
问题描述
如何抑制 db:load:schema 的输出?运行
How can you suppress the output of db:load:schema? Running
bundle exec rake db:schema:load
使用 -s
、-q
甚至 VERBOSE=false
选项对输出没有影响;出现了我不想看到的相同create_table ... add_index ..."垃圾.我从自定义 Rake 任务中调用它,我不希望用户每次都看到所有这些.
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.
更新:
我使用@Deefour 的一些指导解决了这个问题:
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
适用于 Windows 机器,基于 Unix 的可以使用 >/dev/null
.
>NUL
is for Windows machines, Unix-based can use > /dev/null
.
而不是
Rake::Task['db:schema:load'].invoke
就像我在自定义任务中所做的那样.请注意,此解决方案特定于 Windows 机器.对于基于 Unix 的机器,我想您应该能够使用下面接受的解决方案.
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.
推荐答案
这里有一个更简洁的跨系统解决方案:
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
还有
quietly do
# anything written to STDOUT or STDERR here will be silenced
Rake::Task["db:schema:load"].invoke
end
我更喜欢 silence_stream(STDOUT)
而不是 quietly
因为它仍然允许显示写入 STDERR
的错误消息,这会很有帮助当 rake 命令开始起作用时.
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.
参考:silence_stream、silence_warnings, &安静
References: silence_stream, silence_warnings, & quietly
这篇关于抑制 Rake 任务 db:schema:load 中的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!